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 a224620
Show file tree
Hide file tree
Showing 14 changed files with 669 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
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 a224620

Please sign in to comment.