Skip to content

Commit

Permalink
add proto-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
trung2891 committed Mar 18, 2024
1 parent 228957d commit 7487453
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
3 changes: 2 additions & 1 deletion simulate-tests/bridge-contract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
TransferBackMsg,
} from "@oraichain/common-contracts-sdk/build/CwIcs20Latest.types";
import { toDisplay } from "@oraichain/oraidex-common";
import { parseToIbcWasmMemo } from "./proto-gen";

let cosmosChain: CWSimulateApp;
// oraichain support cosmwasm
Expand Down Expand Up @@ -517,7 +518,7 @@ describe.only("IBCModule", () => {

it.each([
[
"",
parseToIbcWasmMemo("", "", ""),
ibcTransferAmount,
"empty-memo-should-fallback-to-transfer-to-receiver",
],
Expand Down
1 change: 1 addition & 0 deletions simulate-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dotenv": "^16.4.5",
"jest": "^29.7.0",
"process": "^0.11.10",
"protobufjs": "^7.2.6",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
},
Expand Down
81 changes: 81 additions & 0 deletions simulate-tests/proto-gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { decode, fromWords } from "bech32";
import protobuf from "protobufjs";

export const parseToIbcWasmMemo = (
destinationReceiver: string,
destinationChannel: string,
destinationDenom: string
): string => {
const protobufRoot = protobuf.Root.fromJSON({
nested: {
IbcBridgeWasmMemo: {
fields: {
destinationReceiver: {
type: "string",
id: 1,
},
destinationChannel: {
type: "string",
id: 2,
},
destinationDenom: {
type: "string",
id: 3,
},
},
},
},
});

const msg = protobufRoot.lookupType("IbcBridgeWasmMemo");
const message = msg.create({
destinationReceiver,
destinationChannel,
destinationDenom,
});
return Buffer.from(msg.encode(message).finish()).toString("base64");
};

export const parseToIbcHookMemo = (
receiver: string,
destinationReceiver: string,
destinationChannel: string,
destinationDenom: string
): string => {
const protobufRoot = protobuf.Root.fromJSON({
nested: {
IbcHooksMemo: {
fields: {
receiver: {
type: "bytes",
id: 1,
},
destinationReceiver: {
type: "string",
id: 2,
},
destinationChannel: {
type: "string",
id: 3,
},
destinationDenom: {
type: "string",
id: 4,
},
},
},
},
});

const receiverCanonical = receiver
? Buffer.from(fromWords(decode(receiver).words))
: "";
const msg = protobufRoot.lookupType("IbcHooksMemo");
const message = msg.create({
receiver: receiverCanonical,
destinationReceiver,
destinationChannel,
destinationDenom,
});
return Buffer.from(msg.encode(message).finish()).toString("base64");
};
2 changes: 1 addition & 1 deletion simulate-tests/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3815,7 +3815,7 @@ protobufjs@^6.8.8, protobufjs@~6.11.2, protobufjs@~6.11.3:
"@types/node" ">=13.7.0"
long "^4.0.0"

protobufjs@^7.0.0, protobufjs@^7.2.3:
protobufjs@^7.0.0, protobufjs@^7.2.3, protobufjs@^7.2.6:
version "7.2.6"
resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-7.2.6.tgz#4a0ccd79eb292717aacf07530a07e0ed20278215"
integrity sha512-dgJaEDDL6x8ASUZ1YqWciTRrdOuYNzoOf27oHNfdyvKqHr5i0FV7FSLU+aIeFjyFgVxrpTOtQUi0BLLBymZaBw==
Expand Down

0 comments on commit 7487453

Please sign in to comment.