wormhole-foundation / wormhole-sdk-ts

Wormhole TypeScript SDK
https://wormhole-foundation.github.io/wormhole-sdk-ts/
Apache License 2.0
39 stars 45 forks source link

error on Wormhole constructor #522

Closed MarcoMandar closed 4 months ago

MarcoMandar commented 4 months ago

hello, I keep getting this error on a fresh Nestjs project, and I am using the latest version ^0.6.5

Failed to load required packages TypeError: Cannot convert undefined or null to object at Function.values () at @wormhole-foundation/sdk/src/index.ts:36:36 at Array.map ()

for const wh = await wormhole('Mainnet', [evm]);

barnjamin commented 4 months ago

Would need to see more of the code to say why this is happening

The complaint is coming from this section: https://github.com/wormhole-foundation/wormhole-sdk-ts/blob/0.6.5/sdk/src/index.ts#L29-L38 likely that the platforms var is not correct

Are you importing evm as

import evm from '@wormhole-foundation/sdk/evm'
MarcoMandar commented 4 months ago

import { ethers } from 'ethers';

import evm from '@wormhole-foundation/sdk/evm';

import { getSigner, SignerStuff } from './helpers';

import { Chain, TokenTransfer, Network, TokenId, Wormhole, wormhole, } from '@wormhole-foundation/sdk';

const ETH_NETWORK_URL = process.env.HTTP_PROVIDER_MAINNET const PRIVATE_KEY = process.env.PRIVATE_KEY

const provider = new ethers.JsonRpcProvider(ETH_NETWORK_URL); const wallet = new ethers.Wallet(PRIVATE_KEY, provider); const providerOasis = new ethers.JsonRpcProvider('https://emerald.oasis.dev');

const USDT_OASIS_CONTRACT_ADDRESS = '0xdc19a122e268128b5ee20366299fc7b5b199c8e3';

export async function executeTransferUSDTfromEthToOasis(amount: string) { try { const wh = await wormhole('Mainnet', [evm]); const destinationchain = wh.getChain('Ethereum'); const sourcechain = wh.getChain('Oasis');

const leg1 = await getSigner(sourcechain);

const leg2 = await getSigner(destinationchain);

const token: TokenId = Wormhole.tokenId(
  sourcechain.chain,
  USDT_OASIS_CONTRACT_ADDRESS,
);

const amt = ethers.parseUnits(amount, 6);

try {

  const route1 = await tokenTransfer(wh, {
    token: token,
    amount: amt,
    source: leg1,
    destination: leg2,
    delivery: {
      automatic: false,
    },
  });
  console.log('Route 1 (External => Cosmos)', route1);
} catch (e) {
  console.error('Route 1 Failed', e);
}

} catch (e) { console.error(e); } }

export async function tokenTransfer( wh: Wormhole, route: { token: TokenId; amount: bigint; source: SignerStuff<N, Chain>; destination: SignerStuff<N, Chain>; delivery?: { automatic: boolean; nativeGas?: bigint; }; payload?: Uint8Array; }, roundTrip?: boolean, ): Promise<TokenTransfer> { // EXAMPLE_TOKEN_TRANSFER // 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, );

const quote = await TokenTransfer.quoteTransfer( wh, route.source.chain, route.destination.chain, xfer.transfer, ); // console.log(quote);

if (xfer.transfer.automatic && 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 srcChain = wh.getChain(route.source.chain.chain); // console.log('srcChain', srcChain); // const tb = await srcChain.getTokenBridge(); // console.log('tb', tb);

// check gas limit const balance = await xfer.fromChain.getBalance( route.source.address.address.toString(), route.token.address, ); // console.log('balance', balance); 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); // EXAMPLE_TOKEN_TRANSFER

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

const { destinationToken: token } = quote; return await tokenTransfer(wh, { ...route, token: token.token, amount: token.amount, source: route.destination, destination: route.source, }); } this is the code

barnjamin commented 4 months ago

This looks ok, might be related to the way nestjs handles dynamic imports

As an experiment, can you please bump to 0.6.6 and use the static imports?

eg

import evm from "@wormhole-foundation/sdk/platforms/evm"

//...

// This may be subject to the same issue as the dynamic loader for the platform but
// the error should be different, complaining about "No protocol registered..."
await loadProtocols(evm, ["TokenBridge", "WormholeCore"]);

const wh = new Wormhole("Mainnet", [evm.Platform]);

//...
MarcoMandar commented 4 months ago

Failed Error: Token Bridge not supported on Oasis at Function.validateTransferDetails the issue with the first error was fixed, I am getting this now from: (fromChain.supportsTokenBridge)

barnjamin commented 4 months ago

ah yes, seems like something related to the dynamic imports then

for now just add

import "@wormhole-foundation/sdk-evm-tokenbridge"

to the imports at the top which should register it unless there is another issue with tree shaking

if the above still doesnt work try:

import * as x from "@wormhole-foundation/sdk-evm-tokenbridge"
console.log(x)

to prevent it from getting tree-shaken out