From 3462564afe1708cb9a9d89192c042bda72387d1f Mon Sep 17 00:00:00 2001 From: David Date: Thu, 21 Jul 2022 15:59:09 +1000 Subject: [PATCH] Attempt to add all generated Msgs to txClient dynamically --- src/lib/web3/api.ts | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/lib/web3/api.ts b/src/lib/web3/api.ts index c3ab6469d..567a9a75a 100755 --- a/src/lib/web3/api.ts +++ b/src/lib/web3/api.ts @@ -3,11 +3,8 @@ 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; @@ -15,9 +12,12 @@ 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; @@ -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; + }, + {} + ), }; };