From 5c4a8d980f0e1fa9429cf5fb04eabbe791ab392b Mon Sep 17 00:00:00 2001 From: cboh4 Date: Thu, 10 Oct 2024 17:21:17 +0300 Subject: [PATCH 1/2] wip: testing upgrade --- contracts/cw20-ics20/test/docker-compose.yml | 4 +- contracts/cw20-ics20/test/package.json | 8 +- contracts/cw20-ics20/test/test.ts | 96 ++++++++++---------- contracts/cw20-ics20/test/utils.ts | 65 ++++++++----- 4 files changed, 96 insertions(+), 77 deletions(-) diff --git a/contracts/cw20-ics20/test/docker-compose.yml b/contracts/cw20-ics20/test/docker-compose.yml index 0755d3c62..4b8409726 100644 --- a/contracts/cw20-ics20/test/docker-compose.yml +++ b/contracts/cw20-ics20/test/docker-compose.yml @@ -1,7 +1,7 @@ version: "3.9" services: localsecret-1: - image: "ghcr.io/scrtlabs/localsecret:v1.5.0-beta.1" + image: "ghcr.io/scrtlabs/localsecret:v1.14.0" # ports are only here in case interaction # between localhost and container is desired ports: @@ -15,7 +15,7 @@ services: environment: LOG_LEVEL: "TRACE" localsecret-2: - image: "ghcr.io/scrtlabs/localsecret:v1.5.0-beta.1" + image: "ghcr.io/scrtlabs/localsecret:v1.14.0" # ports are only here in case interaction # between localhost and container is desired ports: diff --git a/contracts/cw20-ics20/test/package.json b/contracts/cw20-ics20/test/package.json index 8dd71bbce..c58d87519 100644 --- a/contracts/cw20-ics20/test/package.json +++ b/contracts/cw20-ics20/test/package.json @@ -3,14 +3,14 @@ "version": "0.0.0", "license": "MIT", "devDependencies": { - "@confio/relayer": "0.0.0-main-3b30964", - "@cosmjs/stargate": "0.29.0", + "@confio/relayer": "0.12.0", + "@cosmjs/stargate": "0.32.3", "@noble/hashes": "1.1.2", "@types/jest": "28.1.7", "@types/node": "18.7.9", - "jest": "28.1.3", + "jest": "^28.1.3", "prettier": "2.7.1", - "secretjs": "1.4.3", + "secretjs": "1.12.5", "ts-jest": "28.0.8", "ts-node": "10.9.1", "typescript": "4.7.4" diff --git a/contracts/cw20-ics20/test/test.ts b/contracts/cw20-ics20/test/test.ts index 9a63b51ca..30ade3748 100644 --- a/contracts/cw20-ics20/test/test.ts +++ b/contracts/cw20-ics20/test/test.ts @@ -11,7 +11,8 @@ import { TxResultCode, Wallet, } from "secretjs"; -import { MsgInstantiateContractResponse } from "secretjs/dist/protobuf_stuff/secret/compute/v1beta1/msg"; +// import { MsgInstantiateContractResponse } from "secretjs/dist/protobuf_stuff/secret/compute/v1beta1/msg"; +import { MsgInstantiateContractResponse } from "secretjs/dist/protobuf/secret/compute/v1beta1/msg"; import { AminoWallet } from "secretjs/dist/wallet_amino"; import { ibcDenom, loopRelayer, sleep, createIbcConnection, waitForBlocks, createIbcChannel } from "./utils"; @@ -31,7 +32,7 @@ type Contract = { address: string; codeId: number; ibcPortId: string; - codeHash: string; + code_hash: string; }; const contracts: { snip20: Contract; ics20: Contract } = { @@ -40,14 +41,14 @@ const contracts: { snip20: Contract; ics20: Contract } = { address: "", codeId: -1, ibcPortId: "", - codeHash: "", + code_hash: "", }, ics20: { wasm: new Uint8Array(), address: "", codeId: -1, ibcPortId: "", - codeHash: "", + code_hash: "", }, }; @@ -66,15 +67,16 @@ beforeAll(async () => { for (let i = 0; i < mnemonics.length; i++) { const mnemonic = mnemonics[i]; const walletAmino = new AminoWallet(mnemonic); + const walletProto = new Wallet(mnemonic); accounts1[i] = { address: walletAmino.address, mnemonic: mnemonic, walletAmino, - walletProto: new Wallet(mnemonic), - secretjs: await SecretNetworkClient.create({ - grpcWebUrl: "http://localhost:9091", - wallet: walletAmino, - walletAddress: walletAmino.address, + walletProto, + secretjs: new SecretNetworkClient({ + url: "http://localhost:1317", + wallet: walletProto, + walletAddress: walletProto.address, chainId: "secretdev-1", }), }; @@ -84,28 +86,29 @@ beforeAll(async () => { for (let i = 0; i < mnemonics.length; i++) { const mnemonic = mnemonics[i]; const walletAmino = new AminoWallet(mnemonic); + const walletProto = new Wallet(mnemonic); accounts2[i] = { address: walletAmino.address, mnemonic: mnemonic, walletAmino, - walletProto: new Wallet(mnemonic), - secretjs: await SecretNetworkClient.create({ - grpcWebUrl: "http://localhost:9391", - wallet: walletAmino, - walletAddress: walletAmino.address, + walletProto, + secretjs: new SecretNetworkClient({ + url: "http://localhost:3317", + wallet: walletProto, + walletAddress: walletProto.address, chainId: "secretdev-2", }), }; } - await waitForBlocks("secretdev-1", "http://localhost:9091"); - await waitForBlocks("secretdev-2", "http://localhost:9391"); + await waitForBlocks("secretdev-1", "http://localhost:1317"); + await waitForBlocks("secretdev-2", "http://localhost:3317"); contracts.snip20.wasm = fs.readFileSync(`${__dirname}/snip20.wasm`) as Uint8Array; contracts.ics20.wasm = fs.readFileSync(`${__dirname}/../contract.wasm`) as Uint8Array; - contracts.snip20.codeHash = toHex(sha256(contracts.snip20.wasm)); - contracts.ics20.codeHash = toHex(sha256(contracts.ics20.wasm)); + contracts.snip20.code_hash = toHex(sha256(contracts.snip20.wasm)); + contracts.ics20.code_hash = toHex(sha256(contracts.ics20.wasm)); console.log("Storing contracts on secretdev-1..."); @@ -113,13 +116,13 @@ beforeAll(async () => { [ new MsgStoreCode({ sender: accounts1[0].address, - wasmByteCode: contracts.snip20.wasm, + wasm_byte_code: contracts.snip20.wasm, source: "", builder: "", }), new MsgStoreCode({ sender: accounts1[0].address, - wasmByteCode: contracts.ics20.wasm, + wasm_byte_code: contracts.ics20.wasm, source: "", builder: "", }), @@ -139,9 +142,9 @@ beforeAll(async () => { tx = await accounts1[0].secretjs.tx.compute.instantiateContract( { sender: accounts1[0].address, - codeId: contracts.snip20.codeId, - codeHash: contracts.snip20.codeHash, - initMsg: { + code_id: contracts.snip20.codeId, + code_hash: contracts.snip20.code_hash, + init_msg: { name: "Secret SCRT", admin: accounts1[0].address, symbol: "SSCRT", @@ -170,14 +173,14 @@ beforeAll(async () => { tx = await accounts1[0].secretjs.tx.compute.instantiateContract( { sender: accounts1[0].address, - codeId: contracts.ics20.codeId, - codeHash: contracts.ics20.codeHash, - initMsg: { + code_id: contracts.ics20.codeId, + code_hash: contracts.ics20.code_hash, + init_msg: { admin: accounts1[0].address, allowlist: [ { contract: contracts.snip20.address, - code_hash: contracts.snip20.codeHash, + code_hash: contracts.snip20.code_hash, }, ], }, @@ -216,8 +219,8 @@ test( [ new MsgExecuteContract({ sender: accounts1[0].address, - contractAddress: contracts.snip20.address, - codeHash: contracts.snip20.codeHash, + contract_address: contracts.snip20.address, + code_hash: contracts.snip20.code_hash, msg: { set_viewing_key: { key: "banana", @@ -226,12 +229,12 @@ test( }), new MsgExecuteContract({ sender: accounts1[0].address, - contractAddress: contracts.snip20.address, - codeHash: contracts.snip20.codeHash, + contract_address: contracts.snip20.address, + code_hash: contracts.snip20.code_hash, msg: { send: { recipient: contracts.ics20.address, - recipient_code_hash: contracts.ics20.codeHash, + recipient_code_hash: contracts.ics20.code_hash, amount: "1", msg: toBase64( toUtf8( @@ -256,8 +259,8 @@ test( expect(tx.code).toBe(TxResultCode.Success); let snip20Balance: any = await accounts1[0].secretjs.query.compute.queryContract({ - contractAddress: contracts.snip20.address, - codeHash: contracts.snip20.codeHash, + contract_address: contracts.snip20.address, + code_hash: contracts.snip20.code_hash, query: { balance: { key: "banana", @@ -293,14 +296,15 @@ test( // send tokens back from secretdev-2 tx = await accounts2[1].secretjs.tx.ibc.transfer({ sender: accounts2[1].address, - sourcePort: "transfer", - sourceChannel: channelId2, + source_port: "transfer", + source_channel: channelId2, token: { denom: expectedIbcDenom, amount: "1", }, receiver: accounts1[0].address, - timeoutTimestampSec: String(Math.floor(Date.now() / 1000) + 10 * 60) /* 10 minutes */, + timeout_timestamp: String(Math.floor(Date.now() / 1000) + 10 * 60) /* 10 minutes */, + memo: "memo" }); if (tx.code !== TxResultCode.Success) { @@ -312,8 +316,8 @@ test( while (true) { snip20Balance = await accounts1[0].secretjs.query.compute.queryContract({ - contractAddress: contracts.snip20.address, - codeHash: contracts.snip20.codeHash, + contract_address: contracts.snip20.address, + code_hash: contracts.snip20.code_hash, query: { balance: { key: "banana", address: accounts1[0].address }, }, @@ -332,12 +336,12 @@ test( [ new MsgExecuteContract({ sender: accounts1[0].address, - contractAddress: contracts.snip20.address, - codeHash: contracts.snip20.codeHash, + contract_address: contracts.snip20.address, + code_hash: contracts.snip20.code_hash, msg: { send: { recipient: contracts.ics20.address, - recipient_code_hash: contracts.ics20.codeHash, + recipient_code_hash: contracts.ics20.code_hash, amount: "1", msg: toBase64( toUtf8( @@ -363,8 +367,8 @@ test( // Balance is deducted optimistically so we should see 999 right away snip20Balance = await accounts1[0].secretjs.query.compute.queryContract({ - contractAddress: contracts.snip20.address, - codeHash: contracts.snip20.codeHash, + contract_address: contracts.snip20.address, + code_hash: contracts.snip20.code_hash, query: { balance: { key: "banana", @@ -378,8 +382,8 @@ test( while (true) { snip20Balance = await accounts1[0].secretjs.query.compute.queryContract({ - contractAddress: contracts.snip20.address, - codeHash: contracts.snip20.codeHash, + contract_address: contracts.snip20.address, + code_hash: contracts.snip20.code_hash, query: { balance: { key: "banana", address: accounts1[0].address }, }, diff --git a/contracts/cw20-ics20/test/utils.ts b/contracts/cw20-ics20/test/utils.ts index fc608dcfe..1a5a78c53 100644 --- a/contracts/cw20-ics20/test/utils.ts +++ b/contracts/cw20-ics20/test/utils.ts @@ -3,8 +3,16 @@ import { ChannelPair } from "@confio/relayer/build/lib/link"; import { GasPrice } from "@cosmjs/stargate"; import { sha256 } from "@noble/hashes/sha256"; import { SecretNetworkClient, toHex, toUtf8, Wallet } from "secretjs"; -import { Order, State as ChannelState } from "secretjs/dist/protobuf_stuff/ibc/core/channel/v1/channel"; -import { State as ConnectionState } from "secretjs/dist/protobuf_stuff/ibc/core/connection/v1/connection"; +// import { Order, State as ChannelState } from "secretjs/dist/protobuf_stuff/ibc/core/channel/v1/channel"; +import { Order, State as ChannelState, stateToJSON as stateToJSONChannel } from "secretjs/dist/protobuf/ibc/core/channel/v1/channel"; +// import { State as ConnectionState } from "secretjs/dist/protobuf_stuff/ibc/core/connection/v1/connection"; +import { State as ConnectionState, stateToJSON as stateToJSONConnection } from "secretjs/dist/protobuf/ibc/core/connection/v1/connection"; + +export const chain1LCD = "http://localhost:1317"; +export const chain2LCD = "http://localhost:2317"; + +export const chain1RPC = "http://localhost:26657"; +export const chain2RPC = "http://localhost:36657"; export const ibcDenom = ( paths: { @@ -28,9 +36,9 @@ export async function sleep(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)); } -export async function waitForBlocks(chainId: string, grpcWebUrl: string) { - const secretjs = await SecretNetworkClient.create({ - grpcWebUrl, +export async function waitForBlocks(chainId: string, url: string) { + const secretjs = new SecretNetworkClient({ + url, chainId, }); @@ -38,21 +46,20 @@ export async function waitForBlocks(chainId: string, grpcWebUrl: string) { while (true) { try { const { block } = await secretjs.query.tendermint.getLatestBlock({}); - if (Number(block?.header?.height) >= 1) { console.log(`Current block on ${chainId}: ${block!.header!.height}`); break; } } catch (e) { - // console.error("block error:", e); + console.error("block error:", e); } await sleep(100); } } -export async function waitForIBCConnection(chainId: string, grpcWebUrl: string) { - const secretjs = await SecretNetworkClient.create({ - grpcWebUrl, +export async function waitForIBCConnection(chainId: string, url: string) { + const secretjs = new SecretNetworkClient({ + url, chainId, }); @@ -61,7 +68,7 @@ export async function waitForIBCConnection(chainId: string, grpcWebUrl: string) try { const { connections } = await secretjs.query.ibc_connection.connections({}); - if (connections.length >= 1 && connections[0].state === ConnectionState.STATE_OPEN) { + if (connections.length >= 1 && connections[0].state === stateToJSONConnection(ConnectionState.STATE_OPEN)) { console.log("Found an open connection on", chainId); break; } @@ -72,9 +79,9 @@ export async function waitForIBCConnection(chainId: string, grpcWebUrl: string) } } -export async function waitForIBCChannel(chainId: string, grpcWebUrl: string, channelId: string) { - const secretjs = await SecretNetworkClient.create({ - grpcWebUrl, +export async function waitForIBCChannel(chainId: string, url: string, channelId: string) { + const secretjs = new SecretNetworkClient({ + url, chainId, }); @@ -84,7 +91,7 @@ export async function waitForIBCChannel(chainId: string, grpcWebUrl: string, cha const { channels } = await secretjs.query.ibc_channel.channels({}); for (const c of channels) { - if (c.channelId === channelId && c.state == ChannelState.STATE_OPEN) { + if (c.channel_id === channelId && c.state == stateToJSONChannel(ChannelState.STATE_OPEN)) { console.log(`${channelId} is open on ${chainId}`); break outter; } @@ -105,10 +112,13 @@ export async function createIbcConnection(): Promise { const signerB = signerA; // Create IBC Client for chain A - const clientA = await IbcClient.connectWithSigner("http://localhost:26657", signerA, signerA.address, { - prefix: "secret", + const clientA = await IbcClient.connectWithSigner( + chain1RPC, + signerA, + signerA.address, + { gasPrice: GasPrice.fromString("0.25uscrt"), - estimatedBlockTime: 5750, + estimatedBlockTime: 750, estimatedIndexerTime: 500, }); // console.group("IBC client for chain A"); @@ -116,22 +126,27 @@ export async function createIbcConnection(): Promise { // console.groupEnd(); // Create IBC Client for chain A - const clientB = await IbcClient.connectWithSigner("http://localhost:36657", signerB, signerB.address, { - prefix: "secret", + const clientB = await IbcClient.connectWithSigner( + chain2RPC, + signerB, + signerB.address, + { gasPrice: GasPrice.fromString("0.25uscrt"), - estimatedBlockTime: 5750, + estimatedBlockTime: 750, estimatedIndexerTime: 500, }); - // console.group("IBC client for chain B"); + console.group("IBC client for chain B"); // console.log(JSON.stringify(clientB)); // console.groupEnd(); // Create new connectiosn for the 2 clients + console.log("clientA:", clientA); + console.log("clientB:", clientB); const link = await Link.createWithNewConnections(clientA, clientB); - // console.group("IBC link details"); - // console.log(JSON.stringify(link)); - // console.groupEnd(); + console.group("IBC link details"); + console.log(JSON.stringify(link)); + console.groupEnd(); return link; } From 119591268d08b878fb1c7e20d00cb5032b70c2f9 Mon Sep 17 00:00:00 2001 From: cboh4 Date: Wed, 16 Oct 2024 22:55:06 +0300 Subject: [PATCH 2/2] test sending memo field from secretdev-1 to secretdev-2 --- contracts/cw20-ics20/test/docker-compose.yml | 2 ++ contracts/cw20-ics20/test/test.ts | 16 +++++++---- contracts/cw20-ics20/test/utils.ts | 28 ++++++++++++-------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/contracts/cw20-ics20/test/docker-compose.yml b/contracts/cw20-ics20/test/docker-compose.yml index 4b8409726..b82f27031 100644 --- a/contracts/cw20-ics20/test/docker-compose.yml +++ b/contracts/cw20-ics20/test/docker-compose.yml @@ -13,6 +13,7 @@ services: # faucet - "5000:5000" environment: + FAST_BLOCKS: "true" LOG_LEVEL: "TRACE" localsecret-2: image: "ghcr.io/scrtlabs/localsecret:v1.14.0" @@ -27,5 +28,6 @@ services: # faucet - "5300:5000" environment: + FAST_BLOCKS: "true" CHAINID: "secretdev-2" LOG_LEVEL: "TRACE" diff --git a/contracts/cw20-ics20/test/test.ts b/contracts/cw20-ics20/test/test.ts index 30ade3748..993c807c9 100644 --- a/contracts/cw20-ics20/test/test.ts +++ b/contracts/cw20-ics20/test/test.ts @@ -56,7 +56,8 @@ let channelId1 = ""; let channelId2 = ""; beforeAll(async () => { - const linkPromise = createIbcConnection(); + console.log("Waiting for IBC connection..."); + const link = await createIbcConnection(); const mnemonics = [ "grant rice replace explain federal release fix clever romance raise often wild taxi quarter soccer fiber love must tape steak together observe swap guitar", @@ -134,6 +135,7 @@ beforeAll(async () => { } expect(tx.code).toBe(TxResultCode.Success); + contracts.snip20.codeId = Number(tx.arrayLog.find((x) => x.key === "code_id").value); contracts.ics20.codeId = Number(tx.arrayLog.reverse().find((x) => x.key === "code_id").value); @@ -196,9 +198,6 @@ beforeAll(async () => { contracts.ics20.address = MsgInstantiateContractResponse.decode(tx.data[0]).address; contracts.ics20.ibcPortId = "wasm." + contracts.ics20.address; - console.log("Waiting for IBC connection..."); - const link = await linkPromise; - console.log("Creating IBC channel..."); const channels = await createIbcChannel(link, contracts.ics20.ibcPortId); @@ -215,6 +214,7 @@ test( // register snip20 on ics20, then send tokens from secretdev-1 console.log("Sending tokens from secretdev-1..."); + const memo1 = "test memo from secretdev-1"; let tx = await accounts1[0].secretjs.tx.broadcast( [ new MsgExecuteContract({ @@ -245,7 +245,9 @@ test( }) ) ), + memo: memo1, }, + // memo: "test2 memo from secretdev-1", }, }), ], @@ -258,6 +260,10 @@ test( } expect(tx.code).toBe(TxResultCode.Success); + let res = tx.arrayLog.find(element => element.key === 'memo'); + let receivedMemo = res ? res.value : undefined; + expect(receivedMemo).toBe(memo1); + let snip20Balance: any = await accounts1[0].secretjs.query.compute.queryContract({ contract_address: contracts.snip20.address, code_hash: contracts.snip20.code_hash, @@ -304,7 +310,7 @@ test( }, receiver: accounts1[0].address, timeout_timestamp: String(Math.floor(Date.now() / 1000) + 10 * 60) /* 10 minutes */, - memo: "memo" + memo: "test memo from secretdev-2" }); if (tx.code !== TxResultCode.Success) { diff --git a/contracts/cw20-ics20/test/utils.ts b/contracts/cw20-ics20/test/utils.ts index 1a5a78c53..5f8903e14 100644 --- a/contracts/cw20-ics20/test/utils.ts +++ b/contracts/cw20-ics20/test/utils.ts @@ -1,5 +1,7 @@ import { IbcClient, Link } from "@confio/relayer"; import { ChannelPair } from "@confio/relayer/build/lib/link"; +import { stringToPath } from "@cosmjs/crypto"; +import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"; import { GasPrice } from "@cosmjs/stargate"; import { sha256 } from "@noble/hashes/sha256"; import { SecretNetworkClient, toHex, toUtf8, Wallet } from "secretjs"; @@ -9,7 +11,7 @@ import { Order, State as ChannelState, stateToJSON as stateToJSONChannel } from import { State as ConnectionState, stateToJSON as stateToJSONConnection } from "secretjs/dist/protobuf/ibc/core/connection/v1/connection"; export const chain1LCD = "http://localhost:1317"; -export const chain2LCD = "http://localhost:2317"; +export const chain2LCD = "http://localhost:3317"; export const chain1RPC = "http://localhost:26657"; export const chain2RPC = "http://localhost:36657"; @@ -106,47 +108,51 @@ export async function waitForIBCChannel(chainId: string, url: string, channelId: export async function createIbcConnection(): Promise { // Create signers as LocalSecret account d // (Both sides are localsecret so same account can be used on both sides) - const signerA = new Wallet( - "word twist toast cloth movie predict advance crumble escape whale sail such angry muffin balcony keen move employ cook valve hurt glimpse breeze brick" + const signerA = await DirectSecp256k1HdWallet.fromMnemonic( + "word twist toast cloth movie predict advance crumble escape whale sail such angry muffin balcony keen move employ cook valve hurt glimpse breeze brick", // account d + { hdPaths: [stringToPath("m/44'/529'/0'/0/0")], prefix: "secret" }, ); + const [account] = await signerA.getAccounts(); const signerB = signerA; // Create IBC Client for chain A const clientA = await IbcClient.connectWithSigner( chain1RPC, signerA, - signerA.address, + account.address, { gasPrice: GasPrice.fromString("0.25uscrt"), estimatedBlockTime: 750, estimatedIndexerTime: 500, }); - // console.group("IBC client for chain A"); + console.group("IBC client for chain A"); // console.log(JSON.stringify(clientA)); // console.groupEnd(); + await sleep(500); // Create IBC Client for chain A const clientB = await IbcClient.connectWithSigner( chain2RPC, signerB, - signerB.address, + account.address, { gasPrice: GasPrice.fromString("0.25uscrt"), estimatedBlockTime: 750, estimatedIndexerTime: 500, }); console.group("IBC client for chain B"); + await sleep(500); // console.log(JSON.stringify(clientB)); // console.groupEnd(); // Create new connectiosn for the 2 clients - console.log("clientA:", clientA); - console.log("clientB:", clientB); + // econsole.log("clientA:", clientA); + // console.log("clientB:", clientB); const link = await Link.createWithNewConnections(clientA, clientB); - console.group("IBC link details"); - console.log(JSON.stringify(link)); - console.groupEnd(); + // console.group("IBC link details"); + // console.log(JSON.stringify(link)); + // console.groupEnd(); return link; }