Skip to content

Commit

Permalink
feat: add bridge_amino and test (#61)
Browse files Browse the repository at this point in the history
* feat: add bridge_amino and test
* chore: delete unnecessary regen-message-type-registry
  • Loading branch information
mhagel authored Dec 1, 2022
1 parent 84d6b5a commit cb0d339
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 64 deletions.
19 changes: 19 additions & 0 deletions packages/api/e2e/amino.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/tx/modules/ecocredit/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
updateSellOrderConverter,
updateSellOrdersTypeUrl,
} from './marketplace/v1/update_sell_orders';
import { bridgeConverter, bridgeTypeUrl } from './v1/bridge_amino';
import {
createBatchConverter,
createBatchTypeUrl,
Expand Down Expand Up @@ -67,6 +68,7 @@ import {
MsgCreateProject
MsgCreateBatch
MsgRetire
MsgBridge
Marketplace:
MsgBuyDirect
MsgCancelSellOrder
Expand All @@ -81,6 +83,7 @@ import {
export function createEcocreditAminoConverters(): AminoConverters {
return {
// core module
[bridgeTypeUrl]: bridgeConverter(),
[cancelTypeUrl]: cancelConverter(),
[createBatchTypeUrl]: createBatchConverter(),
[createClassTypeUrl]: createClassConverter(),
Expand Down
67 changes: 67 additions & 0 deletions packages/api/src/tx/modules/ecocredit/v1/bridge_amino.ts
Original file line number Diff line number Diff line change
@@ -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<MsgBridge> => {
return {
owner,
target,
recipient,
credits: credits.map(credit => {
return {
$type: Credits.$type,
batchDenom: credit.batch_denom,
amount: credit.amount,
};
}),
};
},
};
}
4 changes: 0 additions & 4 deletions packages/api/src/tx/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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());
Expand Down
60 changes: 0 additions & 60 deletions packages/api/src/tx/regen-message-type-registry.ts

This file was deleted.

0 comments on commit cb0d339

Please sign in to comment.