Skip to content

Commit

Permalink
Fix sei receiving issue
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Oct 13, 2023
1 parent a69ec0b commit 2822ef0
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 105 deletions.
8 changes: 3 additions & 5 deletions connect/src/protocols/tokenTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand All @@ -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
Expand Down
67 changes: 34 additions & 33 deletions examples/src/cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})();
Expand Down
64 changes: 30 additions & 34 deletions examples/src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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, {
Expand All @@ -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;
}
13 changes: 6 additions & 7 deletions examples/src/helpers/signers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ export class SolSigner implements Signer {
async sign(tx: UnsignedTransaction[]): Promise<any[]> {
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());
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) => {
Expand Down
11 changes: 4 additions & 7 deletions examples/src/tokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions platforms/cosmwasm/src/platformUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion platforms/cosmwasm/src/protocols/ibc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
IBC_TIMEOUT_MILLIS,
IBC_TRANSFER_PORT,
IbcChannel,
networkChainToChannelId,
networkToChannelMap,
} from "../constants";
import { CosmwasmContracts } from "../contracts";
Expand Down
33 changes: 17 additions & 16 deletions platforms/cosmwasm/src/protocols/tokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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,
};
Expand Down Expand Up @@ -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(
{
Expand Down
3 changes: 1 addition & 2 deletions platforms/cosmwasm/src/unsignedTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function buildExecuteMsg(
msg: Record<string, any>,
funds?: Coin[],
): EncodeObject {
const obj = {
return {
typeUrl: MSG_EXECUTE_CONTRACT_TYPE_URL,
value: MsgExecuteContract.fromPartial({
sender: sender,
Expand All @@ -36,7 +36,6 @@ export function buildExecuteMsg(
funds,
}),
};
return obj;
}

export class CosmwasmUnsignedTransaction implements UnsignedTransaction {
Expand Down

0 comments on commit 2822ef0

Please sign in to comment.