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

Enhance TypeScript Compatibility Documentation and Improve Example Error Handling in Wormhole SDK #684

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,35 @@ npm install @wormhole-foundation/sdk

This package combines all the individual packages in a way that makes setup easier while still allowing for tree shaking.

## Troubleshooting TypeScript Compatibility

If you encounter issues when integrating the Wormhole SDK into a TypeScript project, such as errors related to dependencies like `@coral-xyz/anchor`, `@cosmjs`, or `ethers`, here are some steps to resolve them:

1. **Ensure all dependencies are installed correctly**:
```bash
npm install @wormhole-foundation/sdk @coral-xyz/anchor @cosmjs ethers

2. **Adjust your `tsconfig.json`** to include the following settings:
```json
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}

3. Check for conflicting versions of dependencies, particularly when using multiple packages that depend on the same underlying libraries (e.g., `@cosmjs`).
Ensure that all dependencies are using compatible versions.

4. **Consider using `npm dedupe`** to resolve any version conflicts:
```bash
npm dedupe

If issues persist, refer to the specific package documentation or reach out to the community for further assistance.

### Advanced

Alternatively, for an advanced user, install a specific set of the packages published.
Expand Down
51 changes: 36 additions & 15 deletions examples/src/cctp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ AutoRelayer takes a 0.1usdc fee when xfering to any chain beside goerli, which i
// );
})();

// Enhanced error handling and comments for cctpTransfer function
async function cctpTransfer<N extends Network>(
wh: Wormhole<N>,
src: SignerStuff<N, any>,
Expand All @@ -69,21 +70,41 @@ async function cctpTransfer<N extends Network>(
nativeGas?: bigint;
},
) {
// EXAMPLE_CCTP_TRANSFER
const xfer = await wh.circleTransfer(
// amount as bigint (base units)
req.amount,
// sender chain/address
src.address,
// receiver chain/address
dst.address,
// automatic delivery boolean
req.automatic,
// payload to be sent with the transfer
undefined,
// If automatic, native gas can be requested to be sent to the receiver
req.nativeGas,
);
try {
// Initiating a Circle Transfer
const xfer = await wh.circleTransfer(
req.amount,
src.address,
dst.address,
req.automatic,
undefined,
req.nativeGas,
);

const quote = await CircleTransfer.quoteTransfer(src.chain, dst.chain, xfer.transfer);
console.log("Quote", quote);

console.log("Starting Transfer");
const srcTxids = await xfer.initiateTransfer(src.signer);
console.log(`Started Transfer: `, srcTxids);

// Waiting for the attestation with a timeout
console.log("Waiting for Attestation");
const attestIds = await xfer.fetchAttestation(60_000);
console.log(`Got Attestation: `, attestIds);

// Completing the transfer on the destination chain
console.log("Completing Transfer");
const dstTxids = await xfer.completeTransfer(dst.signer);
console.log(`Completed Transfer: `, dstTxids);

} catch (error) {
console.error("Error during CCTP transfer:", error);
// Additional guidance for troubleshooting
console.error("Make sure the chains are correctly configured and the amount is sufficient to cover fees.");
}
}


// Note, if the transfer is requested to be Automatic, a fee for performing the relay
// will be present in the quote. The fee comes out of the amount requested to be sent.
Expand Down
112 changes: 59 additions & 53 deletions examples/src/tokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import { getSigner, waitLog } from "./helpers/index.js";
console.log(receipt);
})();

// Enhanced error handling in tokenTransfer function
async function tokenTransfer<N extends Network>(
wh: Wormhole<N>,
route: {
Expand All @@ -111,57 +112,62 @@ async function tokenTransfer<N extends Network>(
},
roundTrip?: boolean,
): Promise<TokenTransfer<N>> {
// 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 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,
});
try {
// 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 new Error("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);

// 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,
});
} catch (error) {
console.error("Error during token transfer:", error);
// Provide additional guidance for troubleshooting
console.error("Ensure the token is supported on both chains and that the transfer amount covers all fees.");
}
}
Loading