@stable-io/cctp-sdk-cctpr-evm
Version:
EVM support for the CCTPR corridor of the CCTP SDK
44 lines • 2.84 kB
JavaScript
// Copyright (c) 2025 Stable Technologies Inc
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
import { gasTokenKindOf, init as initDefinitions, Usdc, } from "@stable-io/cctp-sdk-definitions";
import { init as initCctpr } from "@stable-io/cctp-sdk-cctpr-definitions";
import { EvmAddress, init as initEvm } from "@stable-io/cctp-sdk-evm";
import { Amount } from "@stable-io/amount";
import { CctpR, quoteIsInUsdc } from "./contractSdk/index.js";
export const transfer = (network) => {
const cctpr = initCctpr(network);
const { usdcContracts } = initDefinitions(network);
const { getTokenBalance, getTokenAllowance, composeApproveTx, composePermitMsg, } = initEvm(network);
return async function* (client, sender, destinationDomain, recipient, IoAmountUsdc, //TODO: this should use InOrOut too
quote, gasDropoff, corridor, takeFeesFromInput, //TODO: this should use InOrOut too
usePermit = true) {
const sourceDomain = client.domain;
const gasDropoffLimit = Amount.ofKind(gasTokenKindOf(destinationDomain))(cctpr.gasDropoffLimitOf[destinationDomain]);
if (gasDropoff.gt(gasDropoffLimit))
throw new Error("Gas Drop Off Limit Exceeded");
const usdcAddr = new EvmAddress(usdcContracts.contractAddressOf[sourceDomain]);
const cctprAddress = new EvmAddress(cctpr.contractAddressOf(sourceDomain));
const cctprSdk = new CctpR(client);
const [gasTokenBalance, usdcBalance, usdcAllowance] = await Promise.all([
client.getBalance(sender),
getTokenBalance(client, usdcAddr, sender, Usdc),
getTokenAllowance(client, usdcAddr, sender, cctprAddress, Usdc),
]);
const requiredAllowance = cctprSdk.checkCostAndCalcRequiredAllowance({ amount: IoAmountUsdc, type: takeFeesFromInput ? "in" : "out" }, quote, corridor);
if (usdcBalance.lt(requiredAllowance))
throw new Error("Insufficient USDC balance");
if (!quoteIsInUsdc(quote) && gasTokenBalance.lt(quote.maxRelayFee))
throw new Error("Insufficient gas token balance");
let permit;
if (usdcAllowance.lt(requiredAllowance)) {
permit = yield (usePermit
? composePermitMsg(client, usdcAddr, sender, cctprAddress, requiredAllowance)
: composeApproveTx(usdcAddr, sender, cctprAddress, requiredAllowance));
}
const recipientUniversal = recipient.toUniversalAddress();
return cctprSdk.transferWithRelay(destinationDomain, { amount: IoAmountUsdc, type: takeFeesFromInput ? "in" : "out" }, recipientUniversal, gasDropoff, corridor, quote, permit);
};
};
//# sourceMappingURL=transfer.js.map