Skip to content

Commit

Permalink
squiggle
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Dec 27, 2023
1 parent 7056e86 commit 77761d8
Show file tree
Hide file tree
Showing 22 changed files with 930 additions and 1,902 deletions.
18 changes: 13 additions & 5 deletions core/base/src/utils/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,22 @@ export const b58 = {
export const bignum = {
decode: (input: string | Uint8Array) =>
typeof input === "string" ? BigInt(input) : BigInt(hex.encode(input, true)),
encode: (input: bigint, prefix: boolean = false) => (prefix ? "0x" : "") + input.toString(16),
encode: (input: bigint, prefix: boolean = false) => bignum.toString(input, prefix),
toString: (input: bigint, prefix: boolean = false) => {
let str = input.toString(16);
str = str.length % 2 === 1 ? (str = "0" + str) : str;
if (prefix) return "0x" + str;
return str;
},
toBytes: (input: bigint, length?: number) => {
const b = hex.decode(bignum.toString(input));
if (!length) return b;
return bytes.zpad(b, length);
},
};

export const bytes = {
encode: (value: string | bigint): Uint8Array =>
typeof value === "bigint"
? bytes.encode(bignum.encode(value))
: new TextEncoder().encode(value),
encode: (value: string): Uint8Array => new TextEncoder().encode(value),
decode: (value: Uint8Array): string => new TextDecoder().decode(value),
equals: (lhs: Uint8Array, rhs: Uint8Array): boolean =>
lhs.length === rhs.length && lhs.every((v, i) => v === rhs[i]),
Expand Down
84 changes: 25 additions & 59 deletions examples/src/algoTokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
TokenTransfer,
TransferState,
Wormhole,
encoding,
normalizeAmount,
serialize,
} from "@wormhole-foundation/connect-sdk";
import { TransferStuff, getStuff } from "./helpers";

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

Expand All @@ -29,67 +31,31 @@ import "@wormhole-foundation/connect-sdk-solana-tokenbridge";
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,
});
const xfer = await TokenTransfer.from(wh, {
chain: sendChain.chain,
txid: recoverTxid,
});

// Log out the results
console.log(xfer);
const destination = await getStuff(rcvChain);
const sender = destination.address.address;

const vaa = xfer.vaas![0]!.vaa!;
if (vaa.protocolName !== "TokenBridge") throw new Error("Not a tokenbridge VAA");

const tb = await rcvChain.getTokenBridge();
const txns = tb.redeem(sender, vaa);
for await (const tx of txns) {
const { transaction } = tx as { transaction: TransactionSignerPair };
console.log("from", transaction.tx.from);
console.log("args", transaction.tx.appArgs);
console.log("accts", transaction.tx.appAccounts);
}

// Log out the results
//console.log(xfer);
// console.log(destination.address.address.toString());
// console.log(encoding.b64.encode(serialize(xfer.vaas![0]!.vaa)));
if (xfer.getTransferState() <= TransferState.DestinationInitiated) {
console.log(await xfer.completeTransfer(destination.signer));
}
Expand Down
16 changes: 16 additions & 0 deletions platforms/algorand/__tests__/unit/address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AlgorandAddress } from "../../src";

describe("Algorand Address Tests", () => {
describe("Parse Address", () => {
test("An address parses", () => {
let address = new AlgorandAddress(
"6XHBAFTDDSGTD4AOR67SLCCY7HAQGYBUWCVP2DYPIE7HI7G7IPNOEYM6XE",
);
expect(address).toBeTruthy();
});

test("An invalid address is rejected", () => {
expect(() => new AlgorandAddress("bogusybogusybogus")).toThrow();
});
});
});
17 changes: 17 additions & 0 deletions platforms/algorand/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { JestConfigWithTsJest } from "ts-jest";

const jestConfig: JestConfigWithTsJest = {
preset: "ts-jest",
verbose: true,
modulePathIgnorePatterns: ["mocks"],
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
isolatedModules: true,
},
],
},
};

export default jestConfig;
3 changes: 2 additions & 1 deletion platforms/algorand/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"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"
"prettier": "prettier --write ./src",
"test": "jest --config ./jest.config.ts"
},
"dependencies": {
"@wormhole-foundation/connect-sdk": "^0.3.0-beta.5",
Expand Down
Loading

0 comments on commit 77761d8

Please sign in to comment.