@f5i23q999d/cow-sdk
Version:
<p align="center"> <img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" /> </p>
63 lines • 4.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAcrossDepositCall = void 0;
const contracts_1 = require("@ethersproject/contracts");
const contracts_2 = require("./const/contracts.js");
const weiroll_1 = require("../../../weiroll/index.js");
const abi_1 = require("./abi.js");
const ERC20_BALANCE_OF_ABI = ['function balanceOf(address account) external view returns (uint256)'];
const ERC20_APPROVE_OF_ABI = ['function approve(address spender, uint256 amount) external returns (bool)'];
function getSpookPoolContract(sellTokenChainId) {
const spokePoolAddress = contracts_2.ACROSS_SPOOK_CONTRACT_ADDRESSES[sellTokenChainId];
if (!spokePoolAddress) {
throw new Error('Spoke pool address not found for chain: ' + sellTokenChainId);
}
return (0, weiroll_1.createWeirollContract)(new contracts_1.Contract(spokePoolAddress, abi_1.ACROSS_SPOKE_POOL_ABI), weiroll_1.WeirollCommandFlags.CALL);
}
function getMathContract(sellTokenChainId) {
const mathContractAddress = contracts_2.ACROSS_MATH_CONTRACT_ADDRESSES[sellTokenChainId];
if (!mathContractAddress) {
throw new Error('Math contract address not found for chain: ' + sellTokenChainId);
}
return (0, weiroll_1.createWeirollContract)(new contracts_1.Contract(mathContractAddress, abi_1.ACROSS_MATH_ABI), weiroll_1.WeirollCommandFlags.CALL);
}
function getBalanceOfSellTokenContract(sellTokenAddress) {
return (0, weiroll_1.createWeirollContract)(new contracts_1.Contract(sellTokenAddress, ERC20_BALANCE_OF_ABI), weiroll_1.WeirollCommandFlags.STATICCALL);
}
function getApproveSellTokenContract(sellTokenAddress) {
return (0, weiroll_1.createWeirollContract)(new contracts_1.Contract(sellTokenAddress, ERC20_APPROVE_OF_ABI), weiroll_1.WeirollCommandFlags.CALL);
}
function createAcrossDepositCall(params) {
const { request, quote, cowShedSdk } = params;
const { sellTokenChainId, sellTokenAddress, buyTokenChainId, buyTokenAddress, account, receiver } = request;
// Create the relevant weiroll contracts
const spokePoolContract = getSpookPoolContract(sellTokenChainId);
const mathContract = getMathContract(sellTokenChainId);
const balanceOfSellTokenContract = getBalanceOfSellTokenContract(sellTokenAddress);
const approveSellTokenContract = getApproveSellTokenContract(sellTokenAddress);
// Get the cow shed account
const cowShedAccount = cowShedSdk.getCowShedAccount(sellTokenChainId, account);
const { suggestedFees } = quote;
// Get the weiroll call of the deposit into spoke pool
const depositCall = (0, weiroll_1.createWeirollDelegateCall)((planner) => {
// Get bridged amount (balance of the intermediate token at swap time)
const sourceAmountIncludingSurplus = planner.add(balanceOfSellTokenContract.balanceOf(cowShedAccount));
// Calculate the new output amount using the actual received intermediate tokens (uses the original quoted fee)
const relayFeePercentage = BigInt(suggestedFees.totalRelayFee.pct);
const outputAmountIncludingSurplus = planner.add(mathContract.multiplyAndSubtract(sourceAmountIncludingSurplus, relayFeePercentage));
// Set allowance for SpokePool to transfer bridged tokens
planner.add(approveSellTokenContract.approve(spokePoolContract.address, sourceAmountIncludingSurplus));
// Prepare deposit params
const quoteTimestamp = BigInt(suggestedFees.timestamp);
const fillDeadline = suggestedFees.fillDeadline;
const exclusivityDeadline = suggestedFees.exclusivityDeadline;
const exclusiveRelayer = suggestedFees.exclusiveRelayer;
const message = '0x';
// Deposit into spoke pool
planner.add(spokePoolContract.depositV3(cowShedAccount, receiver || account, sellTokenAddress, buyTokenAddress, sourceAmountIncludingSurplus, outputAmountIncludingSurplus, buyTokenChainId, exclusiveRelayer, quoteTimestamp, fillDeadline, exclusivityDeadline, message));
});
// Return the deposit into spoke pool call
return depositCall;
}
exports.createAcrossDepositCall = createAcrossDepositCall;
//# sourceMappingURL=createAcrossDepositCall.js.map