Skip to content

Commit

Permalink
move tests to test dir
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Oct 18, 2023
1 parent f27d817 commit 4ca26fa
Show file tree
Hide file tree
Showing 9 changed files with 423 additions and 116 deletions.
32 changes: 16 additions & 16 deletions __tests__/integration/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,22 @@ describe("Chain Tests", () => {
});
});

describe("Attestation Tests", () => {
const wh = new Wormhole("Testnet", []);

describe("VAA Tests", () => {
test("GetVAA", async () => {
const parsedVaa = await wh.getVAA(
"Celo",
new UniversalAddress(
"0x00000000000000000000000005ca6037eC51F8b712eD2E6Fa72219FEaE74E153",
),
469n,
);
expect(parsedVaa).toBeTruthy();
});
});
});
// describe("Attestation Tests", () => {
// const wh = new Wormhole("Testnet", []);
// describe("VAA Tests", () => {
// test("GetVAA", async () => {
// const parsedVaa = await wh.getVAA(
// "Celo",
// new UniversalAddress(
// "0x00000000000000000000000005ca6037eC51F8b712eD2E6Fa72219FEaE74E153",
// ),
// 469n,
// "AttestMeta"
// );
// expect(parsedVaa).toBeTruthy();
// });
// });
// });

// test("Recover Transfer Message ID", async () => {
// const solEmitter = new SolanaAddress(SOL_TB_EMITTER).toUniversalAddress();
Expand Down
102 changes: 5 additions & 97 deletions examples/src/testing.ts → __tests__/tilt/helpers/consts.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,18 @@
import { CONFIG, ChainConfig, ChainContext, ChainName, Network, PlatformName, Wormhole, WormholeConfig, nativeChainAddress, normalizeAmount } from "@wormhole-foundation/connect-sdk";
import { EvmPlatform } from "@wormhole-foundation/connect-sdk-evm";
import { SolanaPlatform } from "@wormhole-foundation/connect-sdk-solana";
import { getEvmSigner, getSolSigner } from "./helpers";
import base58 from "bs58";
import { ethers } from "ethers";
import { signSendWait } from "@wormhole-foundation/connect-sdk/src";
import base58 from 'bs58';

const ci = true

export const ETH_PRIVATE_KEY =
"0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"; // account 0

export const SOLANA_PRIVATE_KEY = base58.encode(new Uint8Array([
export const SOLANA_PRIVATE_KEY = new Uint8Array([
14, 173, 153, 4, 176, 224, 201, 111, 32, 237, 183, 185, 159, 247, 22, 161, 89,
84, 215, 209, 212, 137, 10, 92, 157, 49, 29, 192, 101, 164, 152, 70, 87, 65,
8, 174, 214, 157, 175, 126, 98, 90, 54, 24, 100, 177, 247, 77, 19, 112, 47,
44, 165, 109, 233, 102, 14, 86, 109, 29, 134, 145, 132, 141,
]));

function overrideChainSetting(conf: WormholeConfig, chain: ChainName, setting: keyof ChainConfig, value: any): WormholeConfig {
// @ts-ignore
conf.chains[chain] = { ...conf.chains[chain], [setting]: value }
return conf
}

async function getNative(chain: ChainContext<PlatformName>): Promise<string> {
try {
const tb = await chain.getTokenBridge()
const addy = await tb.getWrappedNative()
return addy.toString()
} catch (e) {
console.error("Could not get native for: ", chain.chain)
console.error(e)
}
return ""
}

(async function () {
const network = "Devnet"

let cnf: WormholeConfig = CONFIG[network]
cnf = overrideChainSetting(cnf, "Ethereum", "rpc", "http://localhost:8545")
cnf = overrideChainSetting(cnf, "Bsc", "rpc", "http://localhost:8546")
cnf = overrideChainSetting(cnf, "Solana", "rpc", "http://localhost:8899")

const wh = new Wormhole(network, [EvmPlatform, SolanaPlatform], cnf);

const eth = wh.getChain("Ethereum");
const sol = wh.getChain("Solana")

const evmSigner = await getEvmSigner(
eth.chain,
(await eth.getRpc()) as ethers.Provider,
ETH_PRIVATE_KEY
);
const solSigner = getSolSigner(sol.chain, SOLANA_PRIVATE_KEY)

const evmAcct = nativeChainAddress(evmSigner)
const solAcct = nativeChainAddress(solSigner)

const amt = normalizeAmount("1", eth.config.nativeTokenDecimals)


const etb = await eth.getTokenBridge()
const stb = await sol.getTokenBridge()
]);

const wrappedNativeToken = await etb.getWrappedNative()

// Check if a wrapped version exists, if not, created it
if (!await stb.hasWrappedAsset({ chain: eth.chain, address: wrappedNativeToken })) {
// Create attestation txns
const attest = etb.createAttestation(await etb.getWrappedNative())
// Sign/send
const txns = await signSendWait(eth, attest, evmSigner)

// Get the wormhole message id
const [msgid] = await eth.parseTransaction(txns[txns.length - 1].txid)
// Get the VAA, deserialized to payload type of AttestMeta
const vaa = await wh.getVAA(msgid.chain, msgid.emitter, msgid.sequence, "AttestMeta")

// Create the submit attestation txns
const completeAttest = stb.submitAttestation(vaa!, solAcct.address)
// Sign/send
const solTxns = await signSendWait(sol, completeAttest, solSigner)
console.log("Completion txns on solana: ", solTxns)
}

// Now we can send the transfer
console.log("Creating transfer")
const xfer = await wh.tokenTransfer("native", amt, evmAcct, solAcct, false)
console.log("Created", xfer)

console.log("Initiating transfer")
const txids = await xfer.initiateTransfer(evmSigner)
console.log("Initiated with txids: ", txids)

console.log("Getting attestations")
const att = await xfer.fetchAttestation()
console.log("Got attests: ", att)

console.log("Completing transfer")
const completed = await xfer.completeTransfer(solSigner)
console.log("Finished transfer: ", completed)
})();



const ci = true

// see devnet.md
export const ETH_NODE_URL = ci ? "ws://eth-devnet:8545" : "ws://localhost:8545";
Expand Down Expand Up @@ -182,3 +89,4 @@ export const SUI_FAUCET_URL = ci
: "http://localhost:5003/gas";



96 changes: 96 additions & 0 deletions __tests__/tilt/helpers/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import {
ChainAddress,
ChainContext,
ChainName,
PlatformName,
PlatformToChains,
Signer,
nativeChainAddress,
rpcAddress,
} from "@wormhole-foundation/connect-sdk";

import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { Keypair } from "@solana/web3.js";
import {
chainToAddressPrefix,
cosmwasmNetworkChainToChainId,
cosmwasmNetworkChainToRestUrls,
} from "@wormhole-foundation/connect-sdk-cosmwasm";
import { ethers } from "ethers";

import { ChainRestAuthApi } from "@injectivelabs/sdk-ts";
import { CosmosEvmSigner, CosmosSigner, EvmSigner, SolSigner } from "./signers";
import { ETH_PRIVATE_KEY, SOLANA_PRIVATE_KEY, TERRA2_PRIVATE_KEY, TERRA_PRIVATE_KEY } from "./consts";

export interface TransferStuff {
chain: ChainContext<PlatformName>;
signer: Signer;
address: ChainAddress;
}

// TODO: err msg instructing dev to `cp .env.template .env` and set values
export async function getStuff(
chain: ChainContext<PlatformName>,
): Promise<TransferStuff> {
let signer: Signer;
switch (chain.platform.platform) {
case "Solana":
signer = getSolSigner(chain);
break;
case "Cosmwasm":
signer = await getCosmosSigner(chain);
break;
default:
signer = await getEvmSigner(chain);
}

return { chain, signer, address: nativeChainAddress(signer) };
}

export async function getEvmSigner(
chain: ChainContext<PlatformName>,
pk?: string
): Promise<Signer> {
const provider = (await chain.getRpc()) as ethers.Provider
const wallet = new ethers.Wallet(pk ?? ETH_PRIVATE_KEY);
const txCount = await provider.getTransactionCount(wallet.address);
return new EvmSigner(chain.chain, wallet, txCount, provider);
}

export function getSolSigner(chain: ChainContext<PlatformName>): Signer {
return new SolSigner(chain.chain, Keypair.fromSecretKey(SOLANA_PRIVATE_KEY));
}

export async function getCosmosSigner(
chain: ChainContext<PlatformName>,
mnemonic?: string,
): Promise<Signer> {
mnemonic = mnemonic ?? TERRA_PRIVATE_KEY;
// Use the EVM signer for Evmos and Injective
if (["Evmos", "Injective"].includes(chain.chain)) {
const restRpc = new ChainRestAuthApi(
// @ts-ignore
cosmwasmNetworkChainToRestUrls(chain.platform.network, chain.chain),
);
const chainId = cosmwasmNetworkChainToChainId(
chain.platform.network,
// @ts-ignore
chain.chain,
);
return new CosmosEvmSigner(chain.chain, chainId, mnemonic, restRpc);
}

// Otherwise use the default signer
const signer = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
prefix: chainToAddressPrefix(chain.chain as PlatformToChains<"Cosmwasm">),
});

const acct = (await signer.getAccounts())[0];
const signingClient = await SigningCosmWasmClient.connectWithSigner(
rpcAddress(chain.platform.network, chain.chain)!,
signer
);

return new CosmosSigner(chain.chain, signingClient, acct);
}
3 changes: 3 additions & 0 deletions __tests__/tilt/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./helpers";
export * from "./signers";
export * from "./consts";
Loading

0 comments on commit 4ca26fa

Please sign in to comment.