From 2822ef0580a472b40f3788362ebde95302288e77 Mon Sep 17 00:00:00 2001 From: Ben Guidarelli Date: Thu, 12 Oct 2023 14:06:46 -0400 Subject: [PATCH] Fix sei receiving issue --- connect/src/protocols/tokenTransfer.ts | 8 +-- examples/src/cosmos.ts | 67 ++++++++++--------- examples/src/helpers/helpers.ts | 64 +++++++++--------- examples/src/helpers/signers.ts | 13 ++-- examples/src/tokenBridge.ts | 11 ++- platforms/cosmwasm/src/platformUtils.ts | 4 ++ platforms/cosmwasm/src/protocols/ibc.ts | 1 - .../cosmwasm/src/protocols/tokenBridge.ts | 33 ++++----- platforms/cosmwasm/src/unsignedTransaction.ts | 3 +- 9 files changed, 99 insertions(+), 105 deletions(-) diff --git a/connect/src/protocols/tokenTransfer.ts b/connect/src/protocols/tokenTransfer.ts index b8634664a..b441140b9 100644 --- a/connect/src/protocols/tokenTransfer.ts +++ b/connect/src/protocols/tokenTransfer.ts @@ -312,9 +312,7 @@ export class TokenTransfer implements WormholeTransfer { const toChain = this.wh.getChain(this.transfer.to.chain); - const toAddress = toNative(this.transfer.to.chain, signer.address()) - //@ts-ignore - .toUniversalAddress(); + const rcvAddress = toNative(this.transfer.to.chain, signer.address()); let unsigned: UnsignedTransaction[] = []; const txHashes: TxHash[] = []; @@ -331,10 +329,10 @@ export class TokenTransfer implements WormholeTransfer { ); const tb = await toChain.getAutomaticTokenBridge(); - xfer = tb.redeem(toAddress, vaa); + xfer = tb.redeem(rcvAddress, vaa); } else { const tb = await toChain.getTokenBridge(); - xfer = tb.redeem(toAddress, vaa); + xfer = tb.redeem(rcvAddress, vaa); } // TODO: better error diff --git a/examples/src/cosmos.ts b/examples/src/cosmos.ts index 4740cccaf..cc70f8408 100644 --- a/examples/src/cosmos.ts +++ b/examples/src/cosmos.ts @@ -49,57 +49,58 @@ import { TransferStuff, getStuff } from "./helpers"; const token = "native"; const amount = await wh.normalizeAmount(external.chain, token, 0.01); - const wc = wh.getChain("Wormchain"); - const ibcBridge = (await wc.getIbcBridge()) as CosmwasmIbcBridge; - console.log(await ibcBridge.fetchChannel("Osmosis")); - console.log(await ibcBridge.fetchChannel("Cosmoshub")); - return; - // Transfer native token from source chain, through gateway, to a cosmos chain // const route1 = await transferIntoCosmos(wh, token, amount, leg1, leg2); const route1 = await GatewayTransfer.from(wh, { chain: external.chain, - txid: "0x152b9686dc5ad8deef3329d2898609f8810b23d05760889e9a82952ead1a5ec6", + txid: "0x8d7ed0176d24a0b84ea1bcbd6af63d4a539404b568bb5072bdc9f6f89deb4042", }); console.log("Route 1 (!Cosmos => Cosmos)", route1); + const wormchainToCosmos = route1.ibcTransfers![0]; + console.log("Wormchain To Cosmos IBC Transfer: ", wormchainToCosmos); // Lookup the Gateway representation of the wrappd token const cosmosTokenAddress = toNative( "Wormchain", - route1.ibcTransfers![0].data.denom, + wormchainToCosmos.data.denom, ); console.log("Wrapped Token: ", cosmosTokenAddress.toString()); + const rcvIbcBridge = await cosmos1.getIbcBridge(); + console.log( + await rcvIbcBridge.lookupTransferFromIbcMsgId(wormchainToCosmos.id), + ); + return; + // Wait for the delivery from wormchain to be confirmed + // // Transfer Gateway factory tokens over IBC through gateway to another Cosmos chain - const route2 = await GatewayTransfer.from(wh, { - chain: cosmos1.chain, - txid: "C6729DBCF6902BEB9EB4E61FE0C7F4B939465F24D4CB91287E04FF71293416D6", - }); - // const route2 = await transferBetweenCosmos( - // wh, - // { chain: cosmos1.chain, address: cosmosTokenAddress }, - // 1000n, - // leg2, - // leg3 - // ); + //const route2 = await GatewayTransfer.from(wh, { + // chain: cosmos1.chain, + // txid: "C6729DBCF6902BEB9EB4E61FE0C7F4B939465F24D4CB91287E04FF71293416D6", + //}); + const route2 = await transferBetweenCosmos( + wh, + { chain: cosmos1.chain, address: cosmosTokenAddress }, + 1000n, + leg2, + leg3, + ); console.log("Route 2 (Cosmos -> Cosmos): ", route2); // Transfer Gateway factory token through gateway back to source chain - const route3 = await GatewayTransfer.from(wh, { - chain: cosmos2.chain, - txid: "7BE3F55926979C25C3853D7FB761F0C7E67C3439BEA11DA0F63C7D84DE3B7604", - }); - - // const old = - // "ibc/992697726C555A3376B330350423DB6126E6BA1D2EE265C40C3B11E00C85D5FE"; - // const route3 = await transferOutOfCosmos( - // wh, - // { chain: cosmos2.chain, address: toNative("Wormchain", old) }, - // 5998990n, - // leg3, - // leg1, - // ); + // const route3 = await GatewayTransfer.from(wh, { + // chain: cosmos2.chain, + // txid: "7BE3F55926979C25C3853D7FB761F0C7E67C3439BEA11DA0F63C7D84DE3B7604", + // }); + + const route3 = await transferOutOfCosmos( + wh, + { chain: cosmos2.chain, address: cosmosTokenAddress }, + 5998990n, + leg3, + leg1, + ); console.log("Route 3 (Cosmos => !Cosmos): ", route3); })(); diff --git a/examples/src/helpers/helpers.ts b/examples/src/helpers/helpers.ts index 63139ca5f..8858ff1b0 100644 --- a/examples/src/helpers/helpers.ts +++ b/examples/src/helpers/helpers.ts @@ -26,33 +26,6 @@ import { import { CosmosEvmSigner, CosmosSigner, EvmSigner, SolSigner } from "./signers"; import { ChainRestAuthApi } from "@injectivelabs/sdk-ts"; -// import { -// cosmosAminoConverters, -// cosmwasmAminoConverters, -// cosmwasmProtoRegistry, -// ibcAminoConverters, -// seiprotocolProtoRegistry, -// seiprotocolAminoConverters, -// } from "@sei-js/proto"; -// -// export const createSeiRegistry = (): Registry => { -// return new Registry([ -// ...defaultRegistryTypes, -// ...cosmwasmProtoRegistry, -// ...seiprotocolProtoRegistry, -// ]); -// }; -// -// export const createSeiAminoTypes = (): AminoTypes => { -// const types = { -// ...cosmosAminoConverters, -// ...cosmwasmAminoConverters, -// ...ibcAminoConverters, -// ...seiprotocolAminoConverters, -// }; -// return new AminoTypes(types); -// }; - // read in from `.env` require("dotenv").config(); export interface TransferStuff { @@ -127,13 +100,7 @@ export async function getCosmosSigner( return new CosmosEvmSigner(chain.chain, chainId, mnemonic, restRpc); } - let options = undefined; - // if (chain.chain === "Sei") { - // options = { - // registry: createSeiRegistry(), - // aminoTypes: createSeiAminoTypes(), - // }; - // } + let options = getCosmosOptions(chain.chain); // Otherwise use the default signer const signer = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { @@ -149,3 +116,32 @@ export async function getCosmosSigner( return new CosmosSigner(chain.chain, signingClient, acct); } + +function getCosmosOptions(chain: ChainName): object | undefined { + // TODO: I'm not sure if this is needed + // if (chain === "Sei") { + // const { + // cosmosAminoConverters, + // cosmwasmAminoConverters, + // cosmwasmProtoRegistry, + // ibcAminoConverters, + // seiprotocolProtoRegistry, + // seiprotocolAminoConverters, + // } = require("@sei-js/proto"); + // const registry = new Registry([ + // ...defaultRegistryTypes, + // ...cosmwasmProtoRegistry, + // ...seiprotocolProtoRegistry, + // ]); + + // const aminoTypes = new AminoTypes({ + // ...cosmosAminoConverters, + // ...cosmwasmAminoConverters, + // ...ibcAminoConverters, + // ...seiprotocolAminoConverters, + // }); + // return { registry, aminoTypes }; + // } + + return undefined; +} diff --git a/examples/src/helpers/signers.ts b/examples/src/helpers/signers.ts index 7854549d3..875572bae 100644 --- a/examples/src/helpers/signers.ts +++ b/examples/src/helpers/signers.ts @@ -87,7 +87,9 @@ export class SolSigner implements Signer { async sign(tx: UnsignedTransaction[]): Promise { const signed = []; for (const txn of tx) { - const { transaction } = txn; + const { description, transaction } = txn; + console.log(`Signing: ${description} for ${this.address()}`); + transaction.partialSign(this._keypair); signed.push(transaction.serialize()); } @@ -122,12 +124,8 @@ export class CosmosSigner implements Signer { transaction.fee, transaction.memo, ); - const encoded = TxRaw.encode(txRaw).finish(); - // console.log( - // "Encoded: ", - // encodeURIComponent(Buffer.from(encoded).toString("base64")) - // ); + const encoded = TxRaw.encode(txRaw).finish(); signed.push(encoded); } @@ -162,7 +160,8 @@ export class CosmosEvmSigner implements Signer { const signed: SignedTx[] = []; for (const tx of txns) { - const { transaction } = tx as CosmwasmUnsignedTransaction; + const { description, transaction } = tx as CosmwasmUnsignedTransaction; + console.log(`Signing ${description} for ${this.address()}`); // need to set contractAddress and msg const message: Msgs[] = transaction.msgs.map((m) => { diff --git a/examples/src/tokenBridge.ts b/examples/src/tokenBridge.ts index b4cc91e5b..ac61fbccd 100644 --- a/examples/src/tokenBridge.ts +++ b/examples/src/tokenBridge.ts @@ -4,6 +4,7 @@ import { TokenTransfer, ChainName, Signer, + normalizeAmount, } from "@wormhole-foundation/connect-sdk"; // Import the platform specific packages import { EvmPlatform } from "@wormhole-foundation/connect-sdk-evm"; @@ -30,14 +31,10 @@ import { TransferStuff, getStuff, waitLog } from "./helpers"; const source = await getStuff(sendChain); const destination = await getStuff(rcvChain); + const amt = normalizeAmount("0.01", 18n); + // Choose your adventure - await manualTokenTransfer( - wh, - "native", - 1_000_000_000_000n, - source, - destination, - ); + await manualTokenTransfer(wh, "native", amt, source, destination); // await automaticTokenTransfer(wh, "native", 100_000_000n, source, destination); // await automaticTokenTransferWithGasDropoff( diff --git a/platforms/cosmwasm/src/platformUtils.ts b/platforms/cosmwasm/src/platformUtils.ts index 1fa44dbc6..cd961f02e 100644 --- a/platforms/cosmwasm/src/platformUtils.ts +++ b/platforms/cosmwasm/src/platformUtils.ts @@ -111,6 +111,10 @@ export module CosmwasmUtils { const txhashes: TxHash[] = []; for (const stxn of stxns) { const result = await rpc.broadcastTx(stxn); + if (result.code !== 0) + throw new Error( + `Error sending transaction (${result.transactionHash}): ${result.rawLog}`, + ); txhashes.push(result.transactionHash); } return txhashes; diff --git a/platforms/cosmwasm/src/protocols/ibc.ts b/platforms/cosmwasm/src/protocols/ibc.ts index faa65ed1f..f032a8347 100644 --- a/platforms/cosmwasm/src/protocols/ibc.ts +++ b/platforms/cosmwasm/src/protocols/ibc.ts @@ -31,7 +31,6 @@ import { IBC_TIMEOUT_MILLIS, IBC_TRANSFER_PORT, IbcChannel, - networkChainToChannelId, networkToChannelMap, } from "../constants"; import { CosmwasmContracts } from "../contracts"; diff --git a/platforms/cosmwasm/src/protocols/tokenBridge.ts b/platforms/cosmwasm/src/protocols/tokenBridge.ts index 80c67f7e6..1dce6897e 100644 --- a/platforms/cosmwasm/src/protocols/tokenBridge.ts +++ b/platforms/cosmwasm/src/protocols/tokenBridge.ts @@ -27,9 +27,9 @@ import { CosmwasmChainName, UniversalOrCosmwasm, WrappedRegistryResponse, + toCosmwasmAddrString, } from "../types"; import { CosmwasmPlatform } from "../platform"; -import { CosmwasmAddress } from "../address"; export class CosmwasmTokenBridge implements TokenBridge<"Cosmwasm"> { private tokenBridge: string; @@ -185,6 +185,8 @@ export class CosmwasmTokenBridge implements TokenBridge<"Cosmwasm"> { const recipientAddress = Buffer.from( recipient.address.toUniversalAddress().toUint8Array(), ); + // TODO: do we need to use the _native_ address for cosmos chains? + const encodedRecipient = Buffer.from(recipientAddress).toString("base64"); const denom = CosmwasmPlatform.getNativeDenom(this.chain); @@ -201,7 +203,7 @@ export class CosmwasmTokenBridge implements TokenBridge<"Cosmwasm"> { info, }, recipient_chain: recipientChainId, - recipient: Buffer.from(recipientAddress).toString("base64"), + recipient: encodedRecipient, fee: relayerFee, nonce: nonce, }; @@ -278,24 +280,23 @@ export class CosmwasmTokenBridge implements TokenBridge<"Cosmwasm"> { // TODO: unwrapNative const data = Buffer.from(serialize(vaa)).toString("base64"); + const senderAddress = toCosmwasmAddrString(sender); - let msg; - if ( + const toTranslator = this.translator && toNative(this.chain, this.translator) .toUniversalAddress() - .equals(vaa.payload.to.address) - ) { - msg = buildExecuteMsg(sender.toString(), this.translator, { - complete_transfer_and_convert: { - vaa: data, - }, - }); - } else { - msg = buildExecuteMsg(sender.toString(), this.tokenBridge, { - submit_vaa: { data }, - }); - } + .equals(vaa.payload.to.address); + + const msg = toTranslator + ? buildExecuteMsg(senderAddress, this.translator!, { + complete_transfer_and_convert: { + vaa: data, + }, + }) + : buildExecuteMsg(senderAddress, this.tokenBridge, { + submit_vaa: { data }, + }); yield this.createUnsignedTx( { diff --git a/platforms/cosmwasm/src/unsignedTransaction.ts b/platforms/cosmwasm/src/unsignedTransaction.ts index 3800a5488..f47983d01 100644 --- a/platforms/cosmwasm/src/unsignedTransaction.ts +++ b/platforms/cosmwasm/src/unsignedTransaction.ts @@ -27,7 +27,7 @@ export function buildExecuteMsg( msg: Record, funds?: Coin[], ): EncodeObject { - const obj = { + return { typeUrl: MSG_EXECUTE_CONTRACT_TYPE_URL, value: MsgExecuteContract.fromPartial({ sender: sender, @@ -36,7 +36,6 @@ export function buildExecuteMsg( funds, }), }; - return obj; } export class CosmwasmUnsignedTransaction implements UnsignedTransaction {