Skip to content

Commit

Permalink
Working on wrapped token from foreign chain to unwrap back to Algoran…
Browse files Browse the repository at this point in the history
…d ASA (#185)
  • Loading branch information
SilentRhetoric authored and barnjamin committed Dec 29, 2023
1 parent f7621af commit ce62a59
Show file tree
Hide file tree
Showing 15 changed files with 828 additions and 140 deletions.
5 changes: 3 additions & 2 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"docs": "typedoc"
},
"devDependencies": {
"dotenv": "^16.3.1"
"dotenv": "^16.3.1",
"tsx": "^4.7.0"
},
"dependencies": {
"@wormhole-foundation/connect-sdk": "^0.3.0-beta.6",
Expand All @@ -64,4 +65,4 @@
"@wormhole-foundation/connect-sdk-solana-cctp": "^0.3.0-beta.6",
"@wormhole-foundation/connect-sdk-cosmwasm-ibc": "^0.3.0-beta.6"
}
}
}
50 changes: 34 additions & 16 deletions examples/src/algoTokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,50 @@ import {
import { TransferStuff, getStuff } from "./helpers";

// Import the platform specific packages
import { AlgorandPlatform } from "@wormhole-foundation/connect-sdk-algorand";
import { AlgorandAddress, AlgorandPlatform } from "@wormhole-foundation/connect-sdk-algorand";
import { EvmPlatform } from "@wormhole-foundation/connect-sdk-evm";
import { SolanaPlatform } from "@wormhole-foundation/connect-sdk-solana";

// Register the protocols
import "@wormhole-foundation/connect-sdk-algorand-tokenbridge";
import "@wormhole-foundation/connect-sdk-evm-tokenbridge";
import "@wormhole-foundation/connect-sdk-solana-tokenbridge";

/*
1. Algorand native to other chain
2. Return wrapped ALGO from other chain to Algorand native
3. Algorand ASA to other chain
4. Return wrapped ASA from other chain to Algorand ASA
5. Other chain native to Algorand wrapped token
6. Return Algorand wrapped token to other chain native
7. Other chain token to Algorand wrapped token
8. Return Algorand wrapped token to other chain token
*/

(async function () {
// init Wormhole object, passing config for which network
// Init Wormhole object, passing config for which network
// to use (e.g. Mainnet/Testnet) and what Platforms to support
const wh = new Wormhole("Testnet", [AlgorandPlatform, SolanaPlatform, EvmPlatform]);
const wh = new Wormhole("Testnet", [AlgorandPlatform, EvmPlatform]);

// Grab chain Contexts -- these hold a reference to a cached rpc client
const sendChain = wh.getChain("Algorand");
const rcvChain = wh.getChain("Avalanche");

// shortcut to allow transferring native gas token
const token: TokenId | "native" = "native";
// Shortcut to allow transferring native gas token - worked 12-28-23
// const token: TokenId | "native" = "native";

// Test Algorand native ASA outbound with Testnet USDC 10458941 - worked 12-28-23
// const token = Wormhole.chainAddress("Algorand", new AlgorandAddress(BigInt(10458941)).toString());

// Test Algorand wrapped ASA outbound with Testnet wAVAX 86783266 - worked 12-28-23
// const token = Wormhole.chainAddress("Algorand", new AlgorandAddress(BigInt(86783266)).toString());

// Test other chain wrapped token back to Algorand ASA with Avalanche wUSDC 0x12EB0d635FD4C5692d779755Ba82b33F6439fc73 - Failing on redeem 12-28-23
const token = Wormhole.chainAddress("Avalanche", "0x12EB0d635FD4C5692d779755Ba82b33F6439fc73");

// Normalized given token decimals later but can just pass bigints as base units
// Note: The Token bridge will dedust past 8 decimals
// this means any amount specified past that point will be returned
// to the caller
const amount = "0.1";
const amount = "0.0001";

// With automatic set to true, perform an automatic transfer. This will invoke a relayer
// contract intermediary that knows to pick up the transfers
Expand All @@ -58,15 +76,15 @@ import "@wormhole-foundation/connect-sdk-solana-tokenbridge";

// Used to normalize the amount to account for the tokens decimals
const decimals =
// @ts-ignore
token === "native"
? BigInt(sendChain.config.nativeTokenDecimals)
: await wh.getDecimals(sendChain.chain, token);
: await wh.getDecimals(sendChain.chain, token.address);

// Set this to the transfer txid of the initiating transaction to recover a token transfer
// and attempt to fetch details about its progress.
let recoverTxid = undefined;
// recoverTxid =
// "2daoPz9KyVkG8WGztfatMRx3EKbiRSUVGKAoCST9286eGrzXg5xowafBUUKfd3JrHzvd4AwoH57ujWaJ72k6oiCY";
// let recoverTxid = undefined;
let recoverTxid = "0xdab98de823cd9e2ec3975bf366503dcd896a47a7ce3764fb964cc84b54f7159c"; // Avalanche-->Algorand

// Finally create and perform the transfer given the parameters set above
const xfer = !recoverTxid
Expand All @@ -87,11 +105,11 @@ import "@wormhole-foundation/connect-sdk-solana-tokenbridge";
txid: recoverTxid,
});

console.log(xfer);
console.log("xfer: ", xfer);
// Log out the results
// if (xfer.getTransferState() <= TransferState.DestinationInitiated) {
// console.log(await xfer.completeTransfer(destination.signer));
// }
if (xfer.getTransferState() <= TransferState.DestinationInitiated) {
console.log(await xfer.completeTransfer(destination.signer));
}
})();

async function tokenTransfer<N extends Network>(
Expand Down
159 changes: 159 additions & 0 deletions examples/src/log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
* Executing task: npm run algo


> @wormhole-foundation/connect-sdk-examples@0.3.0-beta.5 algo
> tsx src/algoTokenBridge.ts

Native address type for platform Algorand has already registered
Native address type for platform Evm has already registered
(node:18028) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Starting transfer
Signing: TokenBridge.wrapAndTransferETH for 0xBA3B58B8EF2EDf9D8e50a25C4e11138Afa372688
Started transfer: [
'0x97b28abc5906e7d3ff20594b77148fda5d7d52ccc4554e0eba48b50d06d79199'
]
Getting Attestation
Got Attestation: [
{
chain: 'Avalanche',
emitter: UniversalAddress { type: 'Universal', address: [Uint8Array] },
sequence: 15303n
}
]
Completing Transfer
vaa: {
protocolName: 'TokenBridge',
payloadName: 'Transfer',
payloadLiteral: 'TokenBridge:Transfer',
guardianSet: 0,
signatures: [ { guardianIndex: 0, signature: [Signature] } ],
timestamp: 1703790280,
nonce: 0,
emitterChain: 'Avalanche',
emitterAddress: UniversalAddress {
type: 'Universal',
address: Uint8Array(32) [
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 97, 228, 78, 80, 108, 165,
101, 158, 108, 11, 186, 155, 103, 133, 134,
250, 45, 114, 151, 86
]
},
sequence: 15303n,
consistencyLevel: 1,
payload: {
token: { amount: 10000n, address: [UniversalAddress], chain: 'Avalanche' },
to: { address: [UniversalAddress], chain: 'Algorand' },
fee: 0n
},
hash: Uint8Array(32) [
196, 4, 161, 240, 184, 25, 174, 93,
138, 251, 8, 180, 118, 43, 74, 43,
207, 2, 48, 103, 183, 146, 241, 69,
14, 90, 112, 208, 174, 229, 233, 155
]
}
accounts: [
'VMPR6NWIIQCZ4F3F7DA5S64ATYDNJRI4JPT3NQHUW37X4HZCN5D6ZQBAKM',
'Z7AISPXE4YFESMU35JXBEM6JZSKTVGPASEUQXLM7XTEKVUHOYTRVQHVYVQ'
]
txs: [
{
tx: Transaction {
name: 'Transaction',
tag: <Buffer 54 58>,
type: 'appl',
from: [Object],
appIndex: 86525623,
appOnComplete: 0,
appArgs: [Array],
appAccounts: [Array],
note: Uint8Array(0) [],
lease: Uint8Array(0) [],
flatFee: false,
genesisHash: <Buffer 48 63 b5 18 a4 b3 c8 4e c8 10 f2 2d 4f 10 81 cb 0f 71 f0 59 a7 ac 20 de c6 2f 7f 70 e5 09 3a 22>,
fee: 0,
firstRound: 35741742,
lastRound: 35742742,
genesisID: 'testnet-v1.0',
group: undefined
},
signer: {
addr: 'EZATROXX2HISIRZDRGXW4LRQ46Z6IUJYYIHU3PJGP7P5IQDPKVX42N767A',
signTxn: [Function: signTxn]
}
},
{
tx: Transaction {
name: 'Transaction',
tag: <Buffer 54 58>,
type: 'appl',
from: [Object],
appIndex: 86525623,
appOnComplete: 0,
appArgs: [Array],
appAccounts: [Array],
note: Uint8Array(0) [],
lease: Uint8Array(0) [],
flatFee: false,
genesisHash: <Buffer 48 63 b5 18 a4 b3 c8 4e c8 10 f2 2d 4f 10 81 cb 0f 71 f0 59 a7 ac 20 de c6 2f 7f 70 e5 09 3a 22>,
fee: 3000,
firstRound: 35741742,
lastRound: 35742742,
genesisID: 'testnet-v1.0',
group: undefined
},
signer: null
}
]
Signing: TokenBridge.redeem with signer EZATROXX2HISIRZDRGXW4LRQ46Z6IUJYYIHU3PJGP7P5IQDPKVX42N767A for address KKPWL6OFVUFOAVQGGURJ2EGNZYZZDPEQ37CHEFLLIAFYTCVLP7UZPSV3ME
Signing: TokenBridge.redeem without signer for address KKPWL6OFVUFOAVQGGURJ2EGNZYZZDPEQ37CHEFLLIAFYTCVLP7UZPSV3ME
Signing: TokenBridge.redeem without signer for address KKPWL6OFVUFOAVQGGURJ2EGNZYZZDPEQ37CHEFLLIAFYTCVLP7UZPSV3ME
Completed Transfer: [
'3F4EJTW2S53TQ5THXIQWGESIQKW35QTPPKBJHBN44AZ3MKNZO5XQ',
'5DXF3G2HRCMSUFVMCAUKY35F4D77O45UM7VCP43FLLYZJTCCFYLQ',
'PCY7Q2RPLNJYHTFLZUGKGVBTXBC5F3A2F5OMTJVCV3PWIRLXBXCQ'
]
TokenTransfer {
wh: Wormhole {
_network: 'Testnet',
_platforms: Map(2) { 'Algorand' => [AlgorandPlatform], 'Evm' => [EvmPlatform] },
_chains: Map(2) { 'Avalanche' => [EvmChain], 'Algorand' => [AlgorandChain] },
config: {
api: 'https://api.testnet.wormholescan.io',
circleAPI: 'https://iris-api-sandbox.circle.com/v1/attestations',
chains: [Object]
}
},
_state: 4,
transfer: {
token: 'native',
amount: 100000000000000n,
from: { chain: 'Avalanche', address: [EvmAddress] },
to: { chain: 'Algorand', address: [AlgorandAddress] },
automatic: false,
payload: undefined,
nativeGas: undefined
},
txids: [
{
chain: 'Avalanche',
txid: '0x97b28abc5906e7d3ff20594b77148fda5d7d52ccc4554e0eba48b50d06d79199'
},
{
chain: 'Algorand',
txid: '3F4EJTW2S53TQ5THXIQWGESIQKW35QTPPKBJHBN44AZ3MKNZO5XQ'
},
{
chain: 'Algorand',
txid: '5DXF3G2HRCMSUFVMCAUKY35F4D77O45UM7VCP43FLLYZJTCCFYLQ'
},
{
chain: 'Algorand',
txid: 'PCY7Q2RPLNJYHTFLZUGKGVBTXBC5F3A2F5OMTJVCV3PWIRLXBXCQ'
}
],
vaas: [ { id: [Object], vaa: [Object] } ]
}
* Terminal will be reused by tasks, press any key to close it.
4 changes: 2 additions & 2 deletions examples/src/tokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import "@wormhole-foundation/connect-sdk-solana-tokenbridge";
const rcvChain = wh.getChain("Solana");

// shortcut to allow transferring native gas token
const token: TokenId<"Avalanche"> | "native" = "native";
// const token: TokenId<"Avalanche"> | "native" = "native";

// A TokenId is just a `{chain, address}` pair and an alias for ChainAddress
// The `address` field must be a parsed address.
// You can get a TokenId (or ChainAddress) prepared for you
// by calling the static `chainAddress` method on the Wormhole class.
// e.g.
// const token = Wormhole.chainAddress("Avalanche", "0xd00ae08403B9bbb9124bB305C09058E32C39A48c"); // TokenId<"Avalanche">
const token = Wormhole.chainAddress("Avalanche", "0xd00ae08403B9bbb9124bB305C09058E32C39A48c"); // TokenId<"Avalanche">

// Normalized given token decimals later but can just pass bigints as base units
// Note: The Token bridge will dedust past 8 decimals
Expand Down
Loading

0 comments on commit ce62a59

Please sign in to comment.