@wormhole-foundation/sdk-connect
Version:
The core package for the Connect SDK, used in conjunction with 1 or more of the chain packages
92 lines • 3.45 kB
JavaScript
import { amount, contracts } from "@wormhole-foundation/sdk-base";
import { TokenTransfer } from "../../protocols/tokenBridge/tokenTransfer.js";
import { TransferState, isAttested } from "../../types.js";
import { Wormhole } from "../../wormhole.js";
import { ManualRoute } from "../route.js";
export class TokenBridgeRoute extends ManualRoute {
static meta = {
name: "ManualTokenBridge",
};
static supportedNetworks() {
return ["Mainnet", "Testnet"];
}
// get the list of chains this route supports
static supportedChains(network) {
return contracts.tokenBridgeChains(network);
}
// get the list of destination tokens that may be received on the destination chain
static async supportedDestinationTokens(sourceToken, fromChain, toChain) {
try {
return [await TokenTransfer.lookupDestinationToken(fromChain, toChain, sourceToken)];
}
catch (e) {
return [];
}
}
getDefaultOptions() {
return { payload: undefined };
}
async validate(request, params) {
const amt = amount.parse(params.amount, request.source.decimals);
const validatedParams = {
amount: params.amount,
normalizedParams: { amount: amt },
options: {},
};
return { valid: true, params: validatedParams };
}
async quote(request, params) {
try {
return request.displayQuote(await TokenTransfer.quoteTransfer(this.wh, request.fromChain, request.toChain, {
token: request.source.id,
amount: amount.units(params.normalizedParams.amount),
...params.options,
}), params);
}
catch (e) {
return {
success: false,
error: e,
};
}
}
async initiate(request, signer, quote, to) {
const { params } = quote;
const transfer = await TokenTransfer.destinationOverrides(request.fromChain, request.toChain, this.toTransferDetails(request, params, Wormhole.chainAddress(signer.chain(), signer.address()), to));
const txids = await TokenTransfer.transfer(request.fromChain, transfer, signer);
return {
from: transfer.from.chain,
to: transfer.to.chain,
state: TransferState.SourceInitiated,
originTxs: txids,
};
}
async complete(signer, receipt) {
if (!isAttested(receipt))
throw new Error("The source must be finalized in order to complete the transfer");
const toChain = this.wh.getChain(receipt.to);
const dstTxIds = await TokenTransfer.redeem(toChain, receipt.attestation.attestation, signer);
return {
...receipt,
state: TransferState.DestinationInitiated,
destinationTxs: dstTxIds,
};
}
async resume(txid) {
const xfer = await TokenTransfer.from(this.wh, txid, 10 * 1000);
return TokenTransfer.getReceipt(xfer);
}
async *track(receipt, timeout) {
yield* TokenTransfer.track(this.wh, receipt, timeout);
}
toTransferDetails(request, params, from, to) {
return {
from,
to,
token: request.source.id,
amount: amount.units(params.normalizedParams.amount),
...params.options,
};
}
}
//# sourceMappingURL=manual.js.map