|
| 1 | +import { |
| 2 | + Chain, |
| 3 | + GatewayTransfer, |
| 4 | + GatewayTransferDetails, |
| 5 | + Network, |
| 6 | + TokenId, |
| 7 | + Wormhole, |
| 8 | + amount, |
| 9 | + wormhole, |
| 10 | +} from '@wormhole-foundation/sdk'; |
| 11 | + |
| 12 | +// Import the platform specific packages |
| 13 | + |
| 14 | +import cosmwasm from '@wormhole-foundation/sdk/cosmwasm'; |
| 15 | +import evm from '@wormhole-foundation/sdk/evm'; |
| 16 | +import solana from '@wormhole-foundation/sdk/solana'; |
| 17 | +import { SignerStuff, getSigner } from './helpers/index.js'; |
| 18 | + |
| 19 | +// We're going to transfer into, around, and out of the Cosmos ecosystem |
| 20 | +// First on Avalanche, transparently through gateway and over IBC to Cosmoshub |
| 21 | +// Then over IBC, transparently through gateway and over IBC to Osmosis |
| 22 | +// Finally out of Osmosis, transparently through gateway, out to Avalanche |
| 23 | + |
| 24 | +// eg: |
| 25 | +// Avalanche[avax] => {Gateway ->}Osmosis[gateway/wrapped avax] |
| 26 | +// Osmosis[gateway/wrapped avax] -> {Gateway ->} Cosmoshub[gateway/wrapped avax] |
| 27 | +// Cosmoshub[gateway/wrapped avax] -> {Gateway} => Avalanch[avax] |
| 28 | + |
| 29 | +// Key: |
| 30 | +// => : Regular contract call |
| 31 | +// -> : IBC Transfer |
| 32 | +// {*}: Transparently handled by Gateway |
| 33 | + |
| 34 | +(async function () { |
| 35 | + // Init Wormhole object, passing config for which network |
| 36 | + // to use (e.g. Mainnet/Testnet) and what Platforms to support |
| 37 | + const wh = await wormhole('Mainnet', [evm, solana, cosmwasm]); |
| 38 | + // Pick up where you left off by updating the txids as you go |
| 39 | + let fakeIt = false; |
| 40 | + |
| 41 | + // Grab chain Contexts for each leg of our journey |
| 42 | + const external = wh.getChain('Solana'); |
| 43 | + const cosmos1 = wh.getChain('Dymension'); |
| 44 | + const cosmos2 = wh.getChain('Injective'); |
| 45 | + |
| 46 | + // Get signer from local key but anything that implements |
| 47 | + // Signer interface (e.g. wrapper around web wallet) should work |
| 48 | + const leg1 = await getSigner(external); |
| 49 | + const leg2 = await getSigner(cosmos1); |
| 50 | + const leg3 = await getSigner(cosmos2); |
| 51 | + |
| 52 | + // We'll use the native token on the source chain |
| 53 | + const token: TokenId = Wormhole.tokenId(external.chain, 'native'); |
| 54 | + const amt = amount.units( |
| 55 | + amount.parse('0.001', external.config.nativeTokenDecimals) |
| 56 | + ); |
| 57 | + |
| 58 | + // Transfer native token from source chain, through gateway, to a cosmos chain |
| 59 | + let route1 = fakeIt |
| 60 | + ? await GatewayTransfer.from( |
| 61 | + wh, |
| 62 | + { |
| 63 | + chain: external.chain, |
| 64 | + txid: '5y2BnJ1Nwqe4m6KTSrry5Ni88xqVrqo4jdbuNwAPDuXEonQRVLbALf7abViwucKKr8U8cDfJtDmqnuRAAC6i6wtb', |
| 65 | + }, |
| 66 | + 600_000 |
| 67 | + ) |
| 68 | + : await transferIntoCosmos(wh, token, amt, leg1, leg2); |
| 69 | + console.log('Route 1 (External => Cosmos)', route1); |
| 70 | + |
| 71 | + // Lookup the Gateway representation of the wrappd token |
| 72 | + const { denom } = route1.ibcTransfers![0]!.data; |
| 73 | + const cosmosTokenAddress = Wormhole.parseAddress('Wormchain', denom); |
| 74 | + |
| 75 | + // Transfer Gateway factory tokens over IBC through gateway to another Cosmos chain |
| 76 | + let route2 = fakeIt |
| 77 | + ? await GatewayTransfer.from( |
| 78 | + wh, |
| 79 | + { |
| 80 | + chain: cosmos1.chain, |
| 81 | + txid: '3014CABA727C8A1BFCBD282095C771ACBAB3B13CC595B702ABFD3A4502315FBD', |
| 82 | + }, |
| 83 | + 600_000 |
| 84 | + ) |
| 85 | + : await transferBetweenCosmos( |
| 86 | + wh, |
| 87 | + { chain: cosmos1.chain, address: cosmosTokenAddress }, |
| 88 | + 1000n, |
| 89 | + leg2, |
| 90 | + leg3 |
| 91 | + ); |
| 92 | + console.log('Route 2 (Cosmos -> Cosmos): ', route2); |
| 93 | + |
| 94 | + // Transfer Gateway factory token through gateway back to source chain |
| 95 | + let route3 = fakeIt |
| 96 | + ? await GatewayTransfer.from( |
| 97 | + wh, |
| 98 | + { |
| 99 | + chain: cosmos2.chain, |
| 100 | + txid: 'BEDD0CE2FEA8FF5DF81FCA5142E72745E154F87D496CDA147FC4D5D46A7C7D81', |
| 101 | + }, |
| 102 | + 600_000 |
| 103 | + ) |
| 104 | + : await transferOutOfCosmos( |
| 105 | + wh, |
| 106 | + { chain: cosmos2.chain, address: cosmosTokenAddress }, |
| 107 | + 1000n, |
| 108 | + leg3, |
| 109 | + leg1 |
| 110 | + ); |
| 111 | + console.log('Route 3 (Cosmos => External): ', route3); |
| 112 | +})(); |
| 113 | + |
| 114 | +async function transferIntoCosmos( |
| 115 | + wh: Wormhole<Network>, |
| 116 | + token: TokenId, |
| 117 | + amount: bigint, |
| 118 | + src: SignerStuff<Network, Chain>, |
| 119 | + dst: SignerStuff<Network, Chain> |
| 120 | +): Promise<GatewayTransfer<Network>> { |
| 121 | + console.log( |
| 122 | + `Beginning transfer into Cosmos from ${ |
| 123 | + src.chain.chain |
| 124 | + }:${src.address.address.toString()} to ${ |
| 125 | + dst.chain.chain |
| 126 | + }:${dst.address.address.toString()}` |
| 127 | + ); |
| 128 | + |
| 129 | + const xfer = await GatewayTransfer.from(wh, { |
| 130 | + token: token, |
| 131 | + amount: amount, |
| 132 | + from: src.address, |
| 133 | + to: dst.address, |
| 134 | + } as GatewayTransferDetails); |
| 135 | + console.log('Created GatewayTransfer: ', xfer.transfer); |
| 136 | + |
| 137 | + const srcTxIds = await xfer.initiateTransfer(src.signer); |
| 138 | + console.log('Started transfer on source chain', srcTxIds); |
| 139 | + |
| 140 | + const attests = await xfer.fetchAttestation(600_000); |
| 141 | + console.log('Got Attestations', attests); |
| 142 | + return xfer; |
| 143 | +} |
| 144 | + |
| 145 | +async function transferBetweenCosmos<N extends Network>( |
| 146 | + wh: Wormhole<N>, |
| 147 | + token: TokenId, |
| 148 | + amount: bigint, |
| 149 | + src: SignerStuff<N, Chain>, |
| 150 | + dst: SignerStuff<N, Chain> |
| 151 | +): Promise<GatewayTransfer<N>> { |
| 152 | + console.log( |
| 153 | + `Beginning transfer within cosmos from ${ |
| 154 | + src.chain.chain |
| 155 | + }:${src.address.address.toString()} to ${ |
| 156 | + dst.chain.chain |
| 157 | + }:${dst.address.address.toString()}` |
| 158 | + ); |
| 159 | + |
| 160 | + const xfer = await GatewayTransfer.from(wh, { |
| 161 | + token: token, |
| 162 | + amount: amount, |
| 163 | + from: src.address, |
| 164 | + to: dst.address, |
| 165 | + } as GatewayTransferDetails); |
| 166 | + console.log('Created GatewayTransfer: ', xfer.transfer); |
| 167 | + |
| 168 | + const srcTxIds = await xfer.initiateTransfer(src.signer); |
| 169 | + console.log('Started transfer on source chain', srcTxIds); |
| 170 | + |
| 171 | + const attests = await xfer.fetchAttestation(60_000); |
| 172 | + console.log('Got attests: ', attests); |
| 173 | + |
| 174 | + return xfer; |
| 175 | +} |
| 176 | + |
| 177 | +async function transferOutOfCosmos<N extends Network>( |
| 178 | + wh: Wormhole<N>, |
| 179 | + token: TokenId, |
| 180 | + amount: bigint, |
| 181 | + src: SignerStuff<N, Chain>, |
| 182 | + dst: SignerStuff<N, Chain> |
| 183 | +): Promise<GatewayTransfer<N>> { |
| 184 | + console.log( |
| 185 | + `Beginning transfer out of cosmos from ${ |
| 186 | + src.chain.chain |
| 187 | + }:${src.address.address.toString()} to ${ |
| 188 | + dst.chain.chain |
| 189 | + }:${dst.address.address.toString()}` |
| 190 | + ); |
| 191 | + |
| 192 | + const xfer = await GatewayTransfer.from(wh, { |
| 193 | + token: token, |
| 194 | + amount: amount, |
| 195 | + from: src.address, |
| 196 | + to: dst.address, |
| 197 | + } as GatewayTransferDetails); |
| 198 | + console.log('Created GatewayTransfer: ', xfer.transfer); |
| 199 | + const srcTxIds = await xfer.initiateTransfer(src.signer); |
| 200 | + console.log('Started transfer on source chain', srcTxIds); |
| 201 | + |
| 202 | + const attests = await xfer.fetchAttestation(600_000); |
| 203 | + console.log('Got attests', attests); |
| 204 | + |
| 205 | + // Since we're leaving cosmos, this is required to complete the transfer |
| 206 | + const dstTxIds = await xfer.completeTransfer(dst.signer); |
| 207 | + console.log('Completed transfer on destination chain', dstTxIds); |
| 208 | + |
| 209 | + return xfer; |
| 210 | +} |
0 commit comments