Skip to content

Commit

Permalink
adding Algorand work from pr
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Dec 27, 2023
1 parent e6c265c commit 7056e86
Show file tree
Hide file tree
Showing 29 changed files with 2,746 additions and 134 deletions.
4 changes: 3 additions & 1 deletion core/base/src/constants/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const rpcConfig = [[
["Wormchain", "https://wormchain-rpc.quickapi.com"],
["Xpla", "https://dimension-rpc.xpla.dev"],
["Sei", "https://sei-rpc.polkachu.com/"],
["Algorand", "https://mainnet-api.algonode.cloud"],
]], [
"Testnet", [
["Ethereum", "https://rpc.ankr.com/eth_goerli"],
Expand All @@ -44,7 +45,8 @@ const rpcConfig = [[
["Evmos", "https://evmos-testnet-rpc.polkachu.com"],
["Wormchain", "https://gateway.testnet.xlabs.xyz/"],
["Xpla", "https://cube-rpc.xpla.dev"],
["Sepolia", "https://ethereum-sepolia.publicnode.com"]
["Sepolia", "https://ethereum-sepolia.publicnode.com"],
["Algorand", "https://testnet-api.algonode.cloud"],
]], [
"Devnet", [
["Ethereum", "http://eth-devnet:8545"],
Expand Down
9 changes: 9 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"sideEffects": false,
"scripts": {
"algo": "tsx src/algoTokenBridge.ts",
"tb": "tsx src/tokenBridge.ts",
"cctp": "tsx src/cctp.ts",
"demo": "tsx src/index.ts",
Expand All @@ -50,17 +51,25 @@
},
"dependencies": {
"@wormhole-foundation/connect-sdk": "^0.3.0-beta.5",

"@wormhole-foundation/connect-sdk-solana": "^0.3.0-beta.5",
"@wormhole-foundation/connect-sdk-evm": "^0.3.0-beta.5",
"@wormhole-foundation/connect-sdk-cosmwasm": "^0.3.0-beta.5",
"@wormhole-foundation/connect-sdk-algorand": "^0.3.0-beta.5",

"@wormhole-foundation/connect-sdk-evm-core": "^0.3.0-beta.5",
"@wormhole-foundation/connect-sdk-solana-core": "^0.3.0-beta.5",
"@wormhole-foundation/connect-sdk-cosmwasm-core": "^0.3.0-beta.5",
"@wormhole-foundation/connect-sdk-algorand-core": "^0.3.0-beta.5",

"@wormhole-foundation/connect-sdk-evm-tokenbridge": "^0.3.0-beta.5",
"@wormhole-foundation/connect-sdk-solana-tokenbridge": "^0.3.0-beta.5",
"@wormhole-foundation/connect-sdk-cosmwasm-tokenbridge": "^0.3.0-beta.5",
"@wormhole-foundation/connect-sdk-algorand-tokenbridge": "^0.3.0-beta.5",

"@wormhole-foundation/connect-sdk-evm-cctp": "^0.3.0-beta.5",
"@wormhole-foundation/connect-sdk-solana-cctp": "^0.3.0-beta.5",

"@wormhole-foundation/connect-sdk-cosmwasm-ibc": "^0.3.0-beta.5"
}
}
178 changes: 178 additions & 0 deletions examples/src/algoTokenBridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import {
Chain,
Network,
Platform,
TokenId,
TokenTransfer,
TransferState,
Wormhole,
normalizeAmount,
} from "@wormhole-foundation/connect-sdk";
import { TransferStuff, getStuff } from "./helpers";

// Import the platform specific packages
import { 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";

(async function () {
// 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]);

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

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

// 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";

// With automatic set to true, perform an automatic transfer. This will invoke a relayer
// contract intermediary that knows to pick up the transfers
// With automatic set to false, perform a manual transfer from source to destination
// of the token
// On the destination side, a wrapped version of the token will be minted
// to the address specified in the transfer VAA
const automatic = false;

// The automatic relayer has the ability to deliver some native gas funds to the destination account
// The amount specified for native gas will be swapped for the native gas token according
// to the swap rate provided by the contract, denominated in native gas tokens
const nativeGas = automatic ? "0.01" : undefined;

// Get signer from local key but anything that implements
// Signer interface (e.g. wrapper around web wallet) should work
const source = await getStuff(sendChain);
const destination = await getStuff(rcvChain);

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

// 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 = "0x191ffc4682aa0713d1010cff9fa5921c7941871cc9bd0ef431b7154d719825fa";
// recoverTxid =
// "2daoPz9KyVkG8WGztfatMRx3EKbiRSUVGKAoCST9286eGrzXg5xowafBUUKfd3JrHzvd4AwoH57ujWaJ72k6oiCY";

// Finally create and perform the transfer given the parameters set above
const xfer = !recoverTxid
? // Perform the token transfer
await tokenTransfer(wh, {
token,
amount: normalizeAmount(amount, decimals),
source,
destination,
delivery: {
automatic,
nativeGas: nativeGas ? normalizeAmount(nativeGas, decimals) : undefined,
},
})
: // Recover the transfer from the originating txid
await TokenTransfer.from(wh, {
chain: source.chain.chain,
txid: recoverTxid,
});

// Log out the results
console.log(xfer);

if (xfer.getTransferState() <= TransferState.DestinationInitiated) {
console.log(await xfer.completeTransfer(destination.signer));
}
})();

async function tokenTransfer<N extends Network>(
wh: Wormhole<N>,
route: {
token: TokenId | "native";
amount: bigint;
source: TransferStuff<N, Platform, Chain>;
destination: TransferStuff<N, Platform, Chain>;
delivery?: {
automatic: boolean;
nativeGas?: bigint;
};
payload?: Uint8Array;
},
roundTrip?: boolean,
): Promise<TokenTransfer<N>> {
// Create a TokenTransfer object to track the state of
// the transfer over time
const xfer = await wh.tokenTransfer(
route.token,
route.amount,
route.source.address,
route.destination.address,
route.delivery?.automatic ?? false,
route.payload,
route.delivery?.nativeGas,
);

if (xfer.transfer.automatic) {
const quote = await TokenTransfer.quoteTransfer(
route.source.chain,
route.destination.chain,
xfer.transfer,
);
console.log(quote);

if (quote.destinationToken.amount < 0)
throw "The amount requested is too low to cover the fee and any native gas requested.";
}

// 1) Submit the transactions to the source chain, passing a signer to sign any txns
console.log("Starting transfer");
const srcTxids = await xfer.initiateTransfer(route.source.signer);
console.log(`Started transfer: `, srcTxids);

// If automatic, we're done
if (route.delivery?.automatic) return xfer;

// 2) wait for the VAA to be signed and ready (not required for auto transfer)
console.log("Getting Attestation");
const attestIds = await xfer.fetchAttestation(60_000);
console.log(`Got Attestation: `, attestIds);

// 3) redeem the VAA on the dest chain
console.log("Completing Transfer");
const destTxids = await xfer.completeTransfer(route.destination.signer);
console.log(`Completed Transfer: `, destTxids);

return xfer;
// // No need to send back, dip
// if (!roundTrip) return xfer;

// // We can look up the destination asset for this transfer given the context of
// // the sending chain and token and destination chain
// const token = await TokenTransfer.lookupDestinationToken(
// route.source.chain,
// route.destination.chain,
// xfer.transfer,
// );
// console.log(token);

// // The wrapped token may have a different number of decimals
// // to make things easy, lets just send the amount from the VAA back
// const amount = xfer.vaas![0]!.vaa!.payload.token.amount;
// return await tokenTransfer(wh, {
// ...route,
// token,
// amount,
// source: route.destination,
// destination: route.source,
// });
}
6 changes: 5 additions & 1 deletion examples/src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
} from "@wormhole-foundation/connect-sdk";

// Importing from src so we dont have to rebuild to see debug stuff in signer
import { getCosmwasmSigner } from "@wormhole-foundation/connect-sdk-cosmwasm/src/testing";
import { getEvmSigner } from "@wormhole-foundation/connect-sdk-evm/src/testing";
import { getSolanaSigner } from "@wormhole-foundation/connect-sdk-solana/src/testing";
import { getCosmwasmSigner } from "@wormhole-foundation/connect-sdk-cosmwasm/src/testing";
import { getAlgorandSigner } from "@wormhole-foundation/connect-sdk-algorand/src/testing";

// read in from `.env`
require("dotenv").config();
Expand Down Expand Up @@ -59,6 +60,9 @@ export async function getStuff<
case "Evm":
signer = await getEvmSigner(await chain.getRpc(), getEnv("ETH_PRIVATE_KEY"));
break;
case "Algorand":
signer = await getAlgorandSigner(await chain.getRpc(), getEnv("ALGORAND_MNEMONIC"));
break;
default:
throw new Error("Unrecognized platform: " + platform);
}
Expand Down
19 changes: 11 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions platforms/algorand/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Algorand Context

Supported chains:

- Algorand
9 changes: 4 additions & 5 deletions platforms/algorand/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wormhole-foundation/connect-sdk-algorand",
"version": "0.3.0-beta.3",
"version": "0.3.0-beta.5",
"repository": {
"type": "git",
"url": "git+https://github.com/wormhole-foundation/connect-sdk.git"
Expand All @@ -17,10 +17,9 @@
"module": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"author": "",
"description": "SDK for EVM chains, used in conjunction with @wormhole-foundation/connect-sdk",
"description": "SDK for Algorand, used in conjunction with @wormhole-foundation/connect-sdk",
"files": [
"dist/**/*",
"src/**/*"
"dist/**/*"
],
"keywords": [
"wormhole",
Expand All @@ -43,7 +42,7 @@
"prettier": "prettier --write ./src"
},
"dependencies": {
"@wormhole-foundation/connect-sdk": "^0.3.0-beta.3",
"@wormhole-foundation/connect-sdk": "^0.3.0-beta.5",
"algosdk": "2.7.0"
}
}
11 changes: 5 additions & 6 deletions platforms/algorand/protocols/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wormhole-foundation/connect-sdk-algorand-core",
"version": "0.3.0-beta.3",
"version": "0.3.0-beta.5",
"repository": {
"type": "git",
"url": "git+https://github.com/wormhole-foundation/connect-sdk.git"
Expand All @@ -17,10 +17,9 @@
"module": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"author": "",
"description": "SDK for EVM chains, used in conjunction with @wormhole-foundation/connect-sdk",
"description": "SDK for Algorand, used in conjunction with @wormhole-foundation/connect-sdk",
"files": [
"dist/**/*",
"src/**/*"
"dist/**/*"
],
"keywords": [
"wormhole",
Expand All @@ -43,7 +42,7 @@
"prettier": "prettier --write ./src"
},
"dependencies": {
"@wormhole-foundation/connect-sdk": "^0.3.0-beta.3",
"@wormhole-foundation/connect-sdk-algorand": "^0.3.0-beta.3"
"@wormhole-foundation/connect-sdk": "^0.3.0-beta.5",
"@wormhole-foundation/connect-sdk-algorand": "^0.3.0-beta.5"
}
}
Loading

0 comments on commit 7056e86

Please sign in to comment.