Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bridge_amino and test #61

Merged
merged 4 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested this against redwood locally, including all negative tests I could think of, but we will need to implement #59 to run it in CI.

https://redwood.regen.aneka.io/txs/C70A43E236BAAB1B2DD2D352A03CBB72D8DF307EB7F03D61905315B26324C738

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';
blushi marked this conversation as resolved.
Show resolved Hide resolved

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: 2 additions & 2 deletions packages/api/src/tx/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { Registry, EncodeObject, GeneratedType } from '@cosmjs/proto-signing';

import { SigningConnectionOptions } from '../api';
import { regenRegistry } from './regen-message-type-registry';
import { regenTypeRegistry } from './regen-message-type-registry';
import { createStargateSigningClient } from './stargate-signing';
import { createEcocreditAminoConverters } from './modules';
import { messageTypeRegistry } from '../generated/typeRegistry';
Expand Down Expand Up @@ -37,7 +37,7 @@ export async function setupTxExtension(
messageTypeRegistry.forEach((value, key) => {
customRegistry.push([`/${key}`, value]);
});
regenRegistry.forEach(([key, value]) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleting this. As @blushi pointed out, the messageTypeRegistry already registers all the custom regen msgs.

regenTypeRegistry.forEach(([key, value]) => {
customRegistry.push([`/${key}`, value]);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to this PR, but why do we need this?
messageTypeRegistry.forEach above is already registering all the types

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleted


Expand Down
8 changes: 4 additions & 4 deletions packages/api/src/tx/regen-message-type-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from '../generated/regen/ecocredit/marketplace/v1/tx';
import { GeneratedType } from '@cosmjs/proto-signing';

const ecocreditRegistry: Array<[string, GeneratedType]> = [
const ecocreditRegistry: ReadonlyArray<[string, GeneratedType]> = [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadonlyArray is more in line with the established convention for type registries.

[MsgCancel.$type, MsgCancel],
[MsgCreateClass.$type, MsgCreateClass],
[MsgSend.$type, MsgSend],
Expand All @@ -40,20 +40,20 @@ const ecocreditRegistry: Array<[string, GeneratedType]> = [
[MsgUpdateProjectAdmin.$type, MsgUpdateProjectAdmin],
];

const basketRegistry: Array<[string, GeneratedType]> = [
const basketRegistry: ReadonlyArray<[string, GeneratedType]> = [
[MsgCreate.$type, MsgCreate],
[MsgPut.$type, MsgPut],
[MsgTake.$type, MsgTake],
];

const marketplaceRegistry: Array<[string, GeneratedType]> = [
const marketplaceRegistry: ReadonlyArray<[string, GeneratedType]> = [
[MsgBuyDirect.$type, MsgBuyDirect],
[MsgSell.$type, MsgSell],
[MsgUpdateSellOrders.$type, MsgUpdateSellOrders],
[MsgCancelSellOrder.$type, MsgCancelSellOrder],
];

export const regenRegistry = [
export const regenTypeRegistry = [
...ecocreditRegistry,
...basketRegistry,
...marketplaceRegistry,
Expand Down