Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding back algorand stuff #182

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions examples/src/algoTokenBridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import {
Chain,
Network,
Platform,
TokenId,
TokenTransfer,
Wormhole,
normalizeAmount,
} from "@wormhole-foundation/connect-sdk";
import { TransferStuff, getStuff, waitLog } from "./helpers";

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

// Register the protocols
import "@wormhole-foundation/connect-sdk-algorand-core";
import "@wormhole-foundation/connect-sdk-algorand-tokenbridge";
import "@wormhole-foundation/connect-sdk-solana-core";
import "@wormhole-foundation/connect-sdk-solana-tokenbridge";
import "@wormhole-foundation/connect-sdk-evm-core";
import "@wormhole-foundation/connect-sdk-evm-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 = undefined;
// 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);
})();

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 (await waitLog(xfer)) as TokenTransfer<N>;

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

// 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,
});
}
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
20 changes: 20 additions & 0 deletions platforms/algorand/eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"env": {
"node": true
},
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"comma-dangle": ["error", "always-multiline"],
"semi": ["error", "always"],
"@typescript-eslint/explicit-module-boundary-types": ["error"],
"@typescript-eslint/no-non-null-assertion": ["error"],
"@typescript-eslint/no-explicit-any": ["error", { "ignoreRestArgs": true }]
}
}
48 changes: 48 additions & 0 deletions platforms/algorand/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@wormhole-foundation/connect-sdk-algorand",
"version": "0.3.0-beta.3",
"repository": {
"type": "git",
"url": "git+https://github.com/wormhole-foundation/connect-sdk.git"
},
"bugs": {
"url": "https://github.com/wormhole-foundation/connect-sdk/issues"
},
"homepage": "https://github.com/wormhole-foundation/connect-sdk#readme",
"directories": {
"test": "tests"
},
"license": "Apache-2.0",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"author": "",
"description": "SDK for Algorand, used in conjunction with @wormhole-foundation/connect-sdk",
"files": [
"dist/**/*"
],
"keywords": [
"wormhole",
"sdk",
"typescript",
"connect",
"algorand"
],
"engines": {
"node": ">=16"
},
"sideEffects": false,
"scripts": {
"build:cjs": "tsc -p ./tsconfig.cjs.json",
"build:esm": "tsc -p ./tsconfig.esm.json",
"build": "npm run build:cjs && npm run build:esm",
"rebuild": "npm run clean && npm run build:cjs && npm run build:esm",
"clean": "rm -rf ./dist && rm -f ./*.tsbuildinfo",
"lint": "npm run prettier && eslint --fix",
"prettier": "prettier --write ./src"
},
"dependencies": {
"@wormhole-foundation/connect-sdk": "^0.3.0-beta.3",
"algosdk": "2.7.0"
}
}
48 changes: 48 additions & 0 deletions platforms/algorand/protocols/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@wormhole-foundation/connect-sdk-algorand-core",
"version": "0.3.0-beta.3",
"repository": {
"type": "git",
"url": "git+https://github.com/wormhole-foundation/connect-sdk.git"
},
"bugs": {
"url": "https://github.com/wormhole-foundation/connect-sdk/issues"
},
"homepage": "https://github.com/wormhole-foundation/connect-sdk#readme",
"directories": {
"test": "tests"
},
"license": "Apache-2.0",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"author": "",
"description": "SDK for Algorand, used in conjunction with @wormhole-foundation/connect-sdk",
"files": [
"dist/**/*"
],
"keywords": [
"wormhole",
"sdk",
"typescript",
"connect",
"algorand"
],
"engines": {
"node": ">=16"
},
"sideEffects": false,
"scripts": {
"build:cjs": "tsc -p ./tsconfig.cjs.json",
"build:esm": "tsc -p ./tsconfig.esm.json",
"build": "npm run build:cjs && npm run build:esm",
"rebuild": "npm run clean && npm run build:cjs && npm run build:esm",
"clean": "rm -rf ./dist && rm -f ./*.tsbuildinfo",
"lint": "npm run prettier && eslint --fix",
"prettier": "prettier --write ./src"
},
"dependencies": {
"@wormhole-foundation/connect-sdk": "^0.3.0-beta.3",
"@wormhole-foundation/connect-sdk-algorand": "^0.3.0-beta.3"
}
}
Loading