From cb0d33904c12b61a06def9aa85a56c96a633743e Mon Sep 17 00:00:00 2001 From: Mark Hagelberg <9438198+mhagel@users.noreply.github.com> Date: Thu, 1 Dec 2022 12:07:32 -0500 Subject: [PATCH] feat: add bridge_amino and test (#61) * feat: add bridge_amino and test * chore: delete unnecessary regen-message-type-registry --- packages/api/e2e/amino.spec.ts | 19 ++++++ .../src/tx/modules/ecocredit/converters.ts | 3 + .../tx/modules/ecocredit/v1/bridge_amino.ts | 67 +++++++++++++++++++ packages/api/src/tx/msg.ts | 4 -- .../api/src/tx/regen-message-type-registry.ts | 60 ----------------- 5 files changed, 89 insertions(+), 64 deletions(-) create mode 100644 packages/api/src/tx/modules/ecocredit/v1/bridge_amino.ts delete mode 100644 packages/api/src/tx/regen-message-type-registry.ts diff --git a/packages/api/e2e/amino.spec.ts b/packages/api/e2e/amino.spec.ts index c374a1c8..2b518ed1 100644 --- a/packages/api/e2e/amino.spec.ts +++ b/packages/api/e2e/amino.spec.ts @@ -7,6 +7,7 @@ import { MsgCreateProject, MsgRetire, MsgSend, + MsgBridge, } from '../src/generated/regen/ecocredit/v1/tx'; import { StdFee } from '@cosmjs/amino/build/signdoc'; import { Secp256k1HdWallet } from '@cosmjs/amino/build/secp256k1hdwallet'; @@ -264,6 +265,24 @@ xdescribe('RegenApi with tendermint connection - Amino Tests', () => { await runAminoTest(msgClient, TEST_MSG_CANCEL); }); + it('should sign and broadcast MsgBridge using legacy amino sign mode', async () => { + const { msgClient } = await connect(); + const TEST_BRIDGE_BATCH_DENOM = 'C02-001-20200101-20210101-001'; + + const TEST_MSG_BRIDGE = MsgBridge.fromPartial({ + owner: TEST_ADDRESS, + recipient: makeEthContract(), + target: 'polygon', + credits: [ + { + batchDenom: TEST_BRIDGE_BATCH_DENOM, + amount: MIN_CREDIT_AMOUNT, + }, + ], + }); + + await runAminoTest(msgClient, TEST_MSG_BRIDGE); + }); }); describe('Signing and broadcasting Basket txs using legacy amino sign mode', () => { it('should sign and broadcast MsgCreate', async () => { diff --git a/packages/api/src/tx/modules/ecocredit/converters.ts b/packages/api/src/tx/modules/ecocredit/converters.ts index 3c75b4f2..b096d270 100644 --- a/packages/api/src/tx/modules/ecocredit/converters.ts +++ b/packages/api/src/tx/modules/ecocredit/converters.ts @@ -15,6 +15,7 @@ import { updateSellOrderConverter, updateSellOrdersTypeUrl, } from './marketplace/v1/update_sell_orders'; +import { bridgeConverter, bridgeTypeUrl } from './v1/bridge_amino'; import { createBatchConverter, createBatchTypeUrl, @@ -67,6 +68,7 @@ import { MsgCreateProject MsgCreateBatch MsgRetire + MsgBridge Marketplace: MsgBuyDirect MsgCancelSellOrder @@ -81,6 +83,7 @@ import { export function createEcocreditAminoConverters(): AminoConverters { return { // core module + [bridgeTypeUrl]: bridgeConverter(), [cancelTypeUrl]: cancelConverter(), [createBatchTypeUrl]: createBatchConverter(), [createClassTypeUrl]: createClassConverter(), diff --git a/packages/api/src/tx/modules/ecocredit/v1/bridge_amino.ts b/packages/api/src/tx/modules/ecocredit/v1/bridge_amino.ts new file mode 100644 index 00000000..3993676d --- /dev/null +++ b/packages/api/src/tx/modules/ecocredit/v1/bridge_amino.ts @@ -0,0 +1,67 @@ +import { AminoMsg } from '@cosmjs/amino'; +import { AminoConverter } from '@cosmjs/stargate'; +import { MsgBridge } from '../../../../generated/regen/ecocredit/v1/tx'; +import { Credits } from '../../../../generated/regen/ecocredit/v1/types'; +import { AminoCredits } from './msg_cancel'; + +const msgBridgeAminoType = 'regen.core/MsgBridge'; + +export const bridgeTypeUrl = '/' + MsgBridge.$type; + +export interface AminoMsgBridge extends AminoMsg { + readonly type: typeof msgBridgeAminoType; + readonly value: { + // readonly $type: string; TODO: we will leave these off until nested types can be supported + readonly owner: string; + readonly target?: string; + readonly recipient?: string; + readonly credits: AminoCredits[]; + }; +} + +export function isAminoMsgBridge(msg: AminoMsg): msg is AminoMsgBridge { + return msg.type === msgBridgeAminoType; +} + +export function bridgeConverter(): AminoConverter { + return { + aminoType: msgBridgeAminoType, + toAmino: ({ + owner, + target, + recipient, + credits, + }: MsgBridge): AminoMsgBridge['value'] => { + return { + owner, + target, + recipient, + credits: credits.map(credit => { + return { + batch_denom: credit.batchDenom, + amount: credit.amount, + }; + }), + }; + }, + fromAmino: ({ + owner, + target, + recipient, + credits, + }: AminoMsgBridge['value']): Partial => { + return { + owner, + target, + recipient, + credits: credits.map(credit => { + return { + $type: Credits.$type, + batchDenom: credit.batch_denom, + amount: credit.amount, + }; + }), + }; + }, + }; +} diff --git a/packages/api/src/tx/msg.ts b/packages/api/src/tx/msg.ts index bd3d13b5..f7fd8615 100644 --- a/packages/api/src/tx/msg.ts +++ b/packages/api/src/tx/msg.ts @@ -9,7 +9,6 @@ import { import { Registry, EncodeObject, GeneratedType } from '@cosmjs/proto-signing'; import { SigningConnectionOptions } from '../api'; -import { regenRegistry } from './regen-message-type-registry'; import { createStargateSigningClient } from './stargate-signing'; import { createEcocreditAminoConverters } from './modules'; import { messageTypeRegistry } from '../generated/typeRegistry'; @@ -37,9 +36,6 @@ export async function setupTxExtension( messageTypeRegistry.forEach((value, key) => { customRegistry.push([`/${key}`, value]); }); - regenRegistry.forEach(([key, value]) => { - customRegistry.push([`/${key}`, value]); - }); const registry = new Registry([...defaultRegistryTypes, ...customRegistry]); const aminoTypes = new AminoTypes(createDefaultTypes()); diff --git a/packages/api/src/tx/regen-message-type-registry.ts b/packages/api/src/tx/regen-message-type-registry.ts deleted file mode 100644 index 1092586a..00000000 --- a/packages/api/src/tx/regen-message-type-registry.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { - MsgCancel, - MsgCreateClass, - MsgSend, - MsgBridge, - MsgCreateBatch, - MsgCreateProject, - MsgMintBatchCredits, - MsgRetire, - MsgUpdateClassAdmin, - MsgUpdateClassIssuers, - MsgUpdateProjectMetadata, - MsgUpdateProjectAdmin, -} from '../generated/regen/ecocredit/v1/tx'; -import { - MsgCreate, - MsgPut, - MsgTake, -} from '../generated/regen/ecocredit/basket/v1/tx'; -import { - MsgBuyDirect, - MsgSell, - MsgUpdateSellOrders, - MsgCancelSellOrder, -} from '../generated/regen/ecocredit/marketplace/v1/tx'; -import { GeneratedType } from '@cosmjs/proto-signing'; - -const ecocreditRegistry: Array<[string, GeneratedType]> = [ - [MsgCancel.$type, MsgCancel], - [MsgCreateClass.$type, MsgCreateClass], - [MsgSend.$type, MsgSend], - [MsgBridge.$type, MsgBridge], - [MsgCreateBatch.$type, MsgCreateBatch], - [MsgCreateProject.$type, MsgCreateProject], - [MsgMintBatchCredits.$type, MsgMintBatchCredits], - [MsgRetire.$type, MsgRetire], - [MsgUpdateClassAdmin.$type, MsgUpdateClassAdmin], - [MsgUpdateClassIssuers.$type, MsgUpdateClassIssuers], - [MsgUpdateProjectMetadata.$type, MsgUpdateProjectMetadata], - [MsgUpdateProjectAdmin.$type, MsgUpdateProjectAdmin], -]; - -const basketRegistry: Array<[string, GeneratedType]> = [ - [MsgCreate.$type, MsgCreate], - [MsgPut.$type, MsgPut], - [MsgTake.$type, MsgTake], -]; - -const marketplaceRegistry: Array<[string, GeneratedType]> = [ - [MsgBuyDirect.$type, MsgBuyDirect], - [MsgSell.$type, MsgSell], - [MsgUpdateSellOrders.$type, MsgUpdateSellOrders], - [MsgCancelSellOrder.$type, MsgCancelSellOrder], -]; - -export const regenRegistry = [ - ...ecocreditRegistry, - ...basketRegistry, - ...marketplaceRegistry, -];