Skip to content

Commit

Permalink
Attempt to add all generated Msgs to txClient dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
dib542 committed Jul 22, 2022
1 parent bae0b6d commit 3462564
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/lib/web3/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import { StdFee } from '@cosmjs/launchpad';
import { defaultRegistryTypes, SigningStargateClient } from '@cosmjs/stargate';
import { Registry, OfflineSigner, EncodeObject } from '@cosmjs/proto-signing';
import { Api } from '../../generated/duality/duality.duality/module/rest';
import {
MsgWithdrawShares,
MsgDepositShares,
} from '../../generated/duality/duality.duality/module/types/duality/tx';
import { Api } from './generated/duality/duality.duality/module/rest';
import * as msgs from './generated/duality/duality.duality/module/types/duality/tx';

const { REACT_APP__RPC_URL = '', REACT_APP__REST_URL = '' } = process.env;

export const MissingWalletError = new Error('wallet is required');

export const registry = new Registry(defaultRegistryTypes);

// register additional Msgs here
registry.register('/duality.duality.MsgDepositShares', MsgDepositShares);
registry.register('/duality.duality.MsgWithdrawShares', MsgWithdrawShares);
Object.entries(msgs).forEach(([key, value]) => {
// register additional Msgs here
if (typeof value === 'object') {
registry.register(`/duality.duality.${key}`, value);
}
});

interface TxClientOptions {
addr?: string;
Expand All @@ -43,14 +43,20 @@ const txClient = async (
msgs: EncodeObject[],
{ fee = 'auto', memo }: SignAndBroadcastOptions = {}
) => client.signAndBroadcast(address, msgs, fee, memo),
msgWithdrawShares: (data: MsgWithdrawShares): EncodeObject => ({
typeUrl: '/duality.duality.MsgWithdrawShares',
value: MsgWithdrawShares.fromPartial(data),
}),
msgDepositShares: (data: MsgDepositShares): EncodeObject => ({
typeUrl: '/duality.duality.MsgDepositShares',
value: MsgDepositShares.fromPartial(data),
}),
...Object.entries(msgs).reduce<{ [key: string]: object }>(
(acc, [key, value]) => {
// register additional Msgs here
if (typeof value === 'object') {
acc[key] = (data: typeof value): EncodeObject => ({
typeUrl: `/duality.duality.${key}`,
value: value.fromPartial(data as object),
});
registry.register(`/duality.duality.${key}`, value);
}
return acc;
},
{}
),
};
};

Expand Down

0 comments on commit 3462564

Please sign in to comment.