@wormhole-foundation/sdk-connect
Version:
The core package for the Connect SDK, used in conjunction with 1 or more of the chain packages
126 lines • 5.29 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.CCTPRoute = void 0;
const sdk_base_1 = require("@wormhole-foundation/sdk-base");
const sdk_definitions_1 = require("@wormhole-foundation/sdk-definitions");
const common_js_1 = require("../../common.js");
const cctpTransfer_js_1 = require("../../protocols/cctp/cctpTransfer.js");
const types_js_1 = require("../../types.js");
const wormhole_js_1 = require("../../wormhole.js");
const route_js_1 = require("../route.js");
class CCTPRoute extends route_js_1.ManualRoute {
static meta = {
name: "ManualCCTP",
provider: "Circle",
};
static supportedNetworks() {
return ["Mainnet", "Testnet"];
}
// get the list of chains this route supports
static supportedChains(network) {
if (sdk_base_1.contracts.circleContractChains.has(network)) {
return sdk_base_1.contracts.circleContractChains.get(network);
}
return [];
}
// get the list of destination tokens that may be received on the destination chain
static async supportedDestinationTokens(sourceToken, fromChain, toChain) {
// Ensure the source token is USDC
const sourceChainUsdcContract = sdk_base_1.circle.usdcContract.get(fromChain.network, fromChain.chain);
if (!sourceChainUsdcContract)
return [];
if (!(0, sdk_definitions_1.isSameToken)(sourceToken, wormhole_js_1.Wormhole.tokenId(fromChain.chain, sourceChainUsdcContract))) {
return [];
}
const { network, chain } = toChain;
if (!sdk_base_1.circle.usdcContract.has(network, chain))
return [];
return [wormhole_js_1.Wormhole.chainAddress(chain, sdk_base_1.circle.usdcContract.get(network, chain))];
}
getDefaultOptions() {
return {
payload: undefined,
};
}
async validate(request, params) {
const amount = request.parseAmount(params.amount);
const validatedParams = {
normalizedParams: {
amount,
},
options: params.options ?? this.getDefaultOptions(),
...params,
};
return { valid: true, params: validatedParams };
}
async quote(request, params) {
try {
return request.displayQuote(await cctpTransfer_js_1.CircleTransfer.quoteTransfer(request.fromChain, request.toChain, {
automatic: false,
amount: sdk_base_1.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 cctpTransfer_js_1.CircleTransfer.destinationOverrides(request.fromChain, request.toChain, this.toTransferDetails(params, wormhole_js_1.Wormhole.chainAddress(signer.chain(), signer.address()), to));
const txids = await cctpTransfer_js_1.CircleTransfer.transfer(request.fromChain, transfer, signer);
const msg = await cctpTransfer_js_1.CircleTransfer.getTransferMessage(request.fromChain, txids[txids.length - 1].txid);
return {
from: transfer.from.chain,
to: transfer.to.chain,
state: types_js_1.TransferState.SourceFinalized,
originTxs: txids,
attestation: { id: msg.id, attestation: { message: msg.message } },
};
}
async complete(signer, receipt) {
if (!(0, types_js_1.isAttested)(receipt))
throw new Error("The source must be finalized in order to complete the transfer");
const { id, attestation: att } = receipt.attestation;
if (sdk_definitions_1.CircleBridge.isCircleAttestation(att)) {
const { message, attestation } = att;
if (!attestation)
throw new Error(`No Circle attestation for ${id}`);
const toChain = this.wh.getChain(receipt.to);
const cb = await toChain.getCircleBridge();
const sender = wormhole_js_1.Wormhole.parseAddress(signer.chain(), signer.address());
const xfer = cb.redeem(sender, message, attestation);
const dstTxids = await (0, common_js_1.signSendWait)(toChain, xfer, signer);
return {
...receipt,
state: types_js_1.TransferState.DestinationInitiated,
destinationTxs: dstTxids,
};
}
else {
//
return receipt;
}
}
async resume(txid) {
const xfer = await cctpTransfer_js_1.CircleTransfer.from(this.wh, txid, 10 * 1000);
return cctpTransfer_js_1.CircleTransfer.getReceipt(xfer);
}
async *track(receipt, timeout) {
yield* cctpTransfer_js_1.CircleTransfer.track(this.wh, receipt, timeout);
}
toTransferDetails(params, from, to) {
return {
from,
to,
amount: sdk_base_1.amount.units(params.normalizedParams.amount),
automatic: false,
...params.options,
};
}
}
exports.CCTPRoute = CCTPRoute;
//# sourceMappingURL=manual.js.map
;