@f5i23q999d/cow-sdk
Version:
<p align="center"> <img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" /> </p>
146 lines • 6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIsValidResult = exports.fromStructToOrder = exports.formatEpoch = exports.getBlockInfo = exports.isValidAbi = exports.decodeParams = exports.encodeParams = exports.createSetDomainVerifierTx = exports.getDomainVerifier = exports.isComposableCow = exports.isExtensibleFallbackHandler = exports.DEFAULT_TOKEN_FORMATTER = exports.CONDITIONAL_ORDER_PARAMS_ABI = void 0;
const ethers_1 = require("ethers");
const common_1 = require("../common/index.js");
const generated_1 = require("../common/generated/index.js");
const contracts_1 = require("@cowprotocol/contracts");
const ERC20_BALANCE_VALUES = ['erc20', '0x5a28e9363bb942b639270062aa6bb295f434bcdfc42c97267bf003f272060dc9'];
const EXTERNAL_BALANCE_VALUES = ['external', '0xabee3b73373acd583a130924aad6dc38cfdc44ba0555ba94ce2ff63980ea0632'];
const INTERNAL_BALANCE_VALUES = ['internal', '0x4ac99ace14ee0a5ef932dc609df0943ab7ac16b7583634612f8dc35a4289a6ce'];
const SELL_KIND_VALUES = ['sell', '0xf3b277728b3fee749481eb3e0b3b48980dbbab78658fc419025cb16eee346775'];
const BUY_KIND_VALUES = ['buy', '0x6ed88e868af0a1983e3886d5f3e95a2fafbd6c3450bc229e27342283dc429ccc'];
// Define the ABI tuple for the ConditionalOrderParams struct
exports.CONDITIONAL_ORDER_PARAMS_ABI = ['tuple(address handler, bytes32 salt, bytes staticInput)'];
const DEFAULT_TOKEN_FORMATTER = (address, amount) => `${amount}@${address}`;
exports.DEFAULT_TOKEN_FORMATTER = DEFAULT_TOKEN_FORMATTER;
function isExtensibleFallbackHandler(handler, chainId) {
return handler === common_1.EXTENSIBLE_FALLBACK_HANDLER_CONTRACT_ADDRESS[chainId];
}
exports.isExtensibleFallbackHandler = isExtensibleFallbackHandler;
function isComposableCow(handler, chainId) {
return handler === common_1.COMPOSABLE_COW_CONTRACT_ADDRESS[chainId];
}
exports.isComposableCow = isComposableCow;
async function getDomainVerifier(safe, domain, chainId, provider) {
const contract = generated_1.ExtensibleFallbackHandler__factory.connect(common_1.EXTENSIBLE_FALLBACK_HANDLER_CONTRACT_ADDRESS[chainId], provider);
return await contract.callStatic.domainVerifiers(safe, domain);
}
exports.getDomainVerifier = getDomainVerifier;
function createSetDomainVerifierTx(domain, verifier) {
return generated_1.ExtensibleFallbackHandler__factory.createInterface().encodeFunctionData('setDomainVerifier', [
domain,
verifier,
]);
}
exports.createSetDomainVerifierTx = createSetDomainVerifierTx;
/**
* Encode the `ConditionalOrderParams` for the conditional order.
*
* @param params The `ConditionalOrderParams` struct representing the conditional order as taken from a merkle tree.
* @returns The ABI-encoded conditional order.
* @see ConditionalOrderParams
*/
function encodeParams(params) {
return ethers_1.utils.defaultAbiCoder.encode(exports.CONDITIONAL_ORDER_PARAMS_ABI, [params]);
}
exports.encodeParams = encodeParams;
/**
* Decode the `ConditionalOrderParams` for the conditional order.
*
* @param encoded The encoded conditional order.
* @returns The decoded conditional order.
*/
function decodeParams(encoded) {
const { handler, salt, staticInput } = ethers_1.utils.defaultAbiCoder.decode(exports.CONDITIONAL_ORDER_PARAMS_ABI, encoded)[0];
return { handler, salt, staticInput };
}
exports.decodeParams = decodeParams;
/**
* Helper method for validating ABI types.
* @param types ABI types to validate against.
* @param values The values to validate.
* @returns {boolean} Whether the values are valid ABI for the given types.
*/
function isValidAbi(types, values) {
try {
ethers_1.utils.defaultAbiCoder.encode(types, values);
}
catch (e) {
return false;
}
return true;
}
exports.isValidAbi = isValidAbi;
async function getBlockInfo(provider) {
const block = await provider.getBlock('latest');
return {
blockNumber: block.number,
blockTimestamp: block.timestamp,
};
}
exports.getBlockInfo = getBlockInfo;
function formatEpoch(epoch) {
return new Date(epoch * 1000).toISOString();
}
exports.formatEpoch = formatEpoch;
/**
* Convert a balance source/destination hash to a string
*
* @param balance balance source/destination hash
* @returns string representation of the balance
* @throws if the balance is not recognized
*/
function balanceToString(balance) {
if (ERC20_BALANCE_VALUES.includes(balance)) {
return contracts_1.OrderBalance.ERC20;
}
else if (EXTERNAL_BALANCE_VALUES.includes(balance)) {
return contracts_1.OrderBalance.EXTERNAL;
}
else if (INTERNAL_BALANCE_VALUES.includes(balance)) {
return contracts_1.OrderBalance.INTERNAL;
}
else {
throw new Error(`Unknown balance type: ${balance}`);
}
}
/**
* Convert an order kind hash to a string
* @param kind of order in hash format
* @returns string representation of the order kind
*/
function kindToString(kind) {
if (SELL_KIND_VALUES.includes(kind)) {
return contracts_1.OrderKind.SELL;
}
else if (BUY_KIND_VALUES.includes(kind)) {
return contracts_1.OrderKind.BUY;
}
else {
throw new Error(`Unknown kind: ${kind}`);
}
}
function fromStructToOrder(order) {
const { sellToken, sellAmount, buyToken, buyAmount, buyTokenBalance, sellTokenBalance, feeAmount, kind, receiver, validTo, partiallyFillable, appData, } = order;
return {
sellToken,
sellAmount,
buyToken,
buyAmount,
feeAmount,
receiver,
partiallyFillable,
appData,
validTo: Number(validTo),
kind: kindToString(kind.toString()),
sellTokenBalance: balanceToString(sellTokenBalance.toString()),
buyTokenBalance: balanceToString(buyTokenBalance.toString()),
};
}
exports.fromStructToOrder = fromStructToOrder;
function getIsValidResult(result) {
return result.isValid;
}
exports.getIsValidResult = getIsValidResult;
//# sourceMappingURL=utils.js.map