UNPKG

@mayanfinance/swap-sdk

Version:
2,049 lines (2,039 loc) 261 kB
// src/api.ts import fetch2 from "cross-fetch"; // src/addresses.ts var addresses_default = { "MAYAN_FORWARDER_CONTRACT": "0x337685fdaB40D39bd02028545a4FfA7D287cC3E2", "MAYAN_PROGRAM_ID": "FC4eXxkyrMPTjiYUpp4EAnkmwMbQyZ6NDCh1kfLn6vsf", "AUCTION_PROGRAM_ID": "8QJmxZcEzwuYmCPy6XqgN2sHcYCcFq6AEfBMJZZuLo5a", "MCTP_PROGRAM_ID": "dkpZqrxHFrhziEMQ931GLtfy11nFkCsfMftH9u6QwBU", "SWIFT_PROGRAM_ID": "BLZRi6frs4X4DNLw56V4EXai1b6QVESN1BhHBTYM9VcY", "FEE_MANAGER_PROGRAM_ID": "5VtQHnhs2pfVEr68qQsbTRwKh4JV5GTu9mBHgHFxpHeQ", "WORMHOLE_PROGRAM_ID": "worm2ZoG2kUd4vFXhvjh93UUH596ayRfgQ2MgjNMTth", "CCTP_CORE_PROGRAM_ID": "CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd", "CCTP_TOKEN_PROGRAM_ID": "CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3", "TOKEN_PROGRAM_ID": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", "TOKEN_2022_PROGRAM_ID": "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb", "ASSOCIATED_TOKEN_PROGRAM_ID": "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL", "SPL_UTILS_PROGRAM_ID": "B96dV3Luxzo6SokJx3xt8i5y8Mb7HRR6Eec8hCjJDT69", "LOOKUP_TABLE": "75X8YgJPcadksw6Ag3qsYUfi1bsFwDoCKYtqPPqdydAG", "SUI_MCTP_STATE": "0xb787fe0f7530b4fd2162fa0cc92f4f6c5a97c54b4c5c55eb04ab29f4b803ac9c", "SUI_MCTP_FEE_MANAGER_STATE": "0xa1b4a96ce93d36dd0bbce0adc39533a07d2f32928918c80cd6fe7868320978f2", "SUI_CCTP_CORE_PACKAGE_ID": "0x08d87d37ba49e785dde270a83f8e979605b03dc552b5548f26fdf2f49bf7ed1b", "SUI_CCTP_CORE_STATE": "0xf68268c3d9b1df3215f2439400c1c4ea08ac4ef4bb7d6f3ca6a2a239e17510af", "SUI_CCTP_TOKEN_PACKAGE_ID": "0x2aa6c5d56376c371f88a6cc42e852824994993cb9bab8d3e6450cbe3cb32b94e", "SUI_CCTP_TOKEN_STATE": "0x45993eecc0382f37419864992c12faee2238f5cfe22b98ad3bf455baf65c8a2f", "SUI_WORMHOLE_PACKAGE_ID": "0x5306f64e312b581766351c07af79c72fcb1cd25147157fdc2f8ad76de9a3fb6a", "SUI_WORMHOLE_STATE": "0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c", "SUI_LOGGER_PACKAGE_ID": "0x05680e9030c147b413a489f7891273acc221d49bd061c433e5771bc170fc37ac", "EXPLORER_URL": "https://explorer-api.mayan.finance/v3", "PRICE_URL": "https://price-api.mayan.finance/v3", "RELAYER_URL": "https://relayer-api.mayan.finance/v3" }; // src/utils.ts import { zeroPadValue, parseUnits, formatUnits } from "ethers"; import { PublicKey, SystemProgram } from "@solana/web3.js"; import { Buffer } from "buffer"; import * as sha3 from "js-sha3"; var sha3_2562 = sha3.sha3_256; var isValidAptosType = (str) => /^(0x)?[0-9a-fA-F]+::\w+::\w+$/.test(str); function nativeAddressToHexString(address, wChainId) { if (wChainId === 1) { return zeroPadValue(new PublicKey(address).toBytes(), 32); } else if (wChainId === chains.ethereum || wChainId === chains.bsc || wChainId === chains.polygon || wChainId === chains.avalanche || wChainId === chains.arbitrum || wChainId === chains.optimism || wChainId === chains.base || wChainId === chains.unichain || wChainId === chains.linea) { return zeroPadValue(address, 32); } else if (wChainId === chains.aptos && isValidAptosType(address)) { return `0x${sha3_2562(address)}`; } else if (wChainId === chains.sui) { let addressStr = address.startsWith("0x") ? address.substring(2) : address; if (Buffer.from(addressStr, "hex").length !== 32) { throw new Error("Invalid sui address: " + address); } return zeroPadValue(address, 32); } else { console.log(`Unsupported chain id: ${wChainId}`, address); throw new Error("Unsupported token chain"); } } function hexToUint8Array(input) { return new Uint8Array( Buffer.from( input.startsWith("0x") ? input.substring(2) : input, "hex" ) ); } function getAssociatedTokenAddress(mint, owner, allowOwnerOffCurve = false, programId = new PublicKey(addresses_default.TOKEN_PROGRAM_ID), associatedTokenProgramId = new PublicKey(addresses_default.ASSOCIATED_TOKEN_PROGRAM_ID)) { if (!allowOwnerOffCurve && !PublicKey.isOnCurve(owner.toBuffer())) { throw new Error("TokenOwnerOffCurveError"); } const [address] = PublicKey.findProgramAddressSync( [owner.toBuffer(), programId.toBuffer(), mint.toBuffer()], associatedTokenProgramId ); return address; } function getAmountOfFractionalAmount(amount, decimals) { if (amount === null || amount === void 0) { throw new Error("getAmountOfFractionalAmount: Amount is null or undefined"); } if (typeof amount !== "string" && typeof amount !== "number") { throw new Error("getAmountOfFractionalAmount: Amount is not a string or number"); } if (typeof amount === "string" && amount.length === 0) { throw new Error("getAmountOfFractionalAmount: Amount is empty"); } if (!Number.isFinite(Number(amount))) { throw new Error("getAmountOfFractionalAmount: Amount is not a number"); } const cutFactor = Math.min(8, Number(decimals)); const numStr = Number(amount).toFixed(cutFactor + 1); const reg = new RegExp(`^-?\\d+(?:\\.\\d{0,${cutFactor}})?`); const matchResult = numStr.match(reg); if (!matchResult) { throw new Error("getAmountOfFractionalAmount: fixedAmount is null"); } const fixedAmount = matchResult[0]; return parseUnits(fixedAmount, Number(decimals)); } function getDisplayAmount(inputAmount, decimals) { return Number(formatUnits(inputAmount, decimals)); } var chains = { solana: 1, ethereum: 2, bsc: 4, polygon: 5, avalanche: 6, arbitrum: 23, optimism: 24, base: 30, aptos: 22, sui: 21, unichain: 44, linea: 38 }; function getWormholeChainIdByName(chain) { return chains[chain]; } var evmChainIdMap = { [1]: 2, [56]: 4, [137]: 5, [43114]: 6, [42161]: 23, [10]: 24, [8453]: 30, [130]: 44, [59144]: 38 }; function getEvmChainIdByName(chain) { const wormholeChainId = chains[chain]; const evmIds = Object.keys(evmChainIdMap); for (const evmId of evmIds) { if (evmChainIdMap[evmId] === wormholeChainId) { return Number(evmId); } } throw new Error(`Unsupported chain: ${chain}`); } function getWormholeChainIdById(chainId) { return evmChainIdMap[chainId]; } var sdkVersion = [10, 6, 0]; function getSdkVersion() { return sdkVersion.join("_"); } function checkSdkVersionSupport(minimumVersion) { if (sdkVersion[0] < minimumVersion[0]) { return false; } if (sdkVersion[0] > minimumVersion[0]) { return true; } if (sdkVersion[1] < minimumVersion[1]) { return false; } if (sdkVersion[1] > minimumVersion[1]) { return true; } if (sdkVersion[2] >= minimumVersion[2]) { return true; } return false; } function getGasDecimal(chain) { if (chain === "solana") { return 9; } return 18; } function getGasDecimalsInSolana(chain) { if (chain === "solana") { return 9; } return 8; } var MAX_U64 = BigInt(2) ** BigInt(64) - BigInt(1); function getSafeU64Blob(value) { if (value < BigInt(0) || value > MAX_U64) { throw new Error(`Invalid u64: ${value}`); } const buf = Buffer.alloc(8); buf.writeBigUInt64LE(value); return buf; } var ZeroPermit = { value: BigInt(0), deadline: 0, v: 0, r: `0x${SystemProgram.programId.toBuffer().toString("hex")}`, s: `0x${SystemProgram.programId.toBuffer().toString("hex")}` }; function wait(time) { return new Promise((resolve) => { setTimeout(() => { resolve(); }, time); }); } function getQuoteSuitableReferrerAddress(quote, referrerAddresses) { if (!quote || !referrerAddresses) { return null; } if (quote.type === "WH") { return referrerAddresses?.solana || null; } if (quote.type === "MCTP" || quote.type === "SWIFT") { if (quote.toChain === "solana") { return referrerAddresses?.solana || null; } if (quote.toChain === "sui") { return referrerAddresses?.sui || null; } return referrerAddresses?.evm || null; } if (quote.type === "FAST_MCTP") { if (quote.toChain !== "solana" && quote.toChain !== "sui") { return referrerAddresses?.evm || null; } } return null; } var MCTP_PAYLOAD_TYPE_DEFAULT = 1; var MCTP_PAYLOAD_TYPE_CUSTOM_PAYLOAD = 2; var MCTP_INIT_ORDER_PAYLOAD_ID = 1; var FAST_MCTP_PAYLOAD_TYPE_DEFAULT = 1; var FAST_MCTP_PAYLOAD_TYPE_CUSTOM_PAYLOAD = 2; var FAST_MCTP_PAYLOAD_TYPE_ORDER = 3; // src/api.ts function toQueryString(params) { return Object.entries(params).filter(([_, value]) => value !== void 0 && value !== null && !Array.isArray(value)).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join("&"); } async function check5xxError(res) { if (res.status.toString().startsWith("5")) { let error; try { const err = await res.json(); if ((err?.code || err?.statusCode) && (err?.message || err?.msg)) { error = { code: err?.code || err?.statusCode, message: err?.message || err?.msg }; } } catch (err) { error = new Error("Internal server error"); } throw error; } } async function fetchAllTokenList(tokenStandards) { const query = tokenStandards ? `?standard=${tokenStandards.join(",")}` : ""; const res = await fetch2(`${addresses_default.PRICE_URL}/tokens${query}`, { method: "GET", redirect: "follow" }); await check5xxError(res); if (res.status === 200) { const result = await res.json(); return result; } throw new Error("Cannot fetch Mayan tokens!"); } async function fetchTokenList(chain, nonPortal = false, tokenStandards) { const queryParams = { chain, nonPortal, standard: tokenStandards ? tokenStandards?.join(",") : void 0 }; const res = await fetch2(`${addresses_default.PRICE_URL}/tokens?${toQueryString(queryParams)}`); await check5xxError(res); if (res.status === 200) { const result = await res.json(); return result[chain]; } throw new Error("Cannot fetch Mayan tokens!"); } function generateFetchQuoteUrl(params, quoteOptions = { wormhole: true, swift: true, mctp: true, shuttle: true, gasless: false, onlyDirect: false, fastMctp: true }) { const { gasDrop, referrerBps } = params; let slippageBps = params.slippageBps; if (slippageBps !== "auto" && !Number.isFinite(slippageBps)) { slippageBps = params.slippage * 100; } const _quoteOptions = { wormhole: quoteOptions.wormhole !== false, // default to true swift: quoteOptions.swift !== false, // default to true mctp: quoteOptions.mctp !== false, // default to true shuttle: quoteOptions.shuttle === true, // default to false fastMctp: quoteOptions.fastMctp !== false, // default to true gasless: quoteOptions.gasless === true, // default to false onlyDirect: quoteOptions.onlyDirect === true // default to false }; const queryParams = { ..._quoteOptions, solanaProgram: addresses_default.MAYAN_PROGRAM_ID, forwarderAddress: addresses_default.MAYAN_FORWARDER_CONTRACT, amountIn: !params.amountIn64 && Number.isFinite(params.amount) ? params.amount : void 0, amountIn64: params.amountIn64, fromToken: params.fromToken, fromChain: params.fromChain, toToken: params.toToken, toChain: params.toChain, slippageBps, referrer: params.referrer, referrerBps: Number.isFinite(referrerBps) ? referrerBps : void 0, gasDrop: Number.isFinite(gasDrop) ? gasDrop : void 0, sdkVersion: getSdkVersion() }; const baseUrl = `${addresses_default.PRICE_URL}/quote?`; const queryString = toQueryString(queryParams); return baseUrl + queryString; } async function fetchQuote(params, quoteOptions = { swift: true, mctp: true, gasless: false, onlyDirect: false }) { const url = generateFetchQuoteUrl(params, quoteOptions); const res = await fetch2(url, { method: "GET", redirect: "follow" }); await check5xxError(res); const result = await res.json(); if (res.status !== 200 && res.status !== 201) { throw { code: result?.code || 0, message: result?.msg || result?.message || "Route not found", data: result?.data }; } if (!checkSdkVersionSupport(result.minimumSdkVersion)) { throw { code: 9999, message: "Swap SDK is outdated!" }; } return result.quotes; } async function getCurrentChainTime(chain) { const res = await fetch2(`${addresses_default.PRICE_URL}/clock/${chain}`, { method: "GET", redirect: "follow" }); await check5xxError(res); const result = await res.json(); if (res.status !== 200 && res.status !== 201) { throw result; } return result.clock; } async function getSuggestedRelayer() { const res = await fetch2(`${addresses_default.RELAYER_URL}/active-relayers?solanaProgram=${addresses_default.MAYAN_PROGRAM_ID}`, { method: "GET", redirect: "follow" }); await check5xxError(res); const result = await res.json(); if (res.status !== 200 && res.status !== 201) { throw result; } return result.suggested; } async function getSwapSolana(params) { const query = toQueryString({ ...params, sdkVersion: getSdkVersion() }); const res = await fetch2(`${addresses_default.PRICE_URL}/get-swap/solana?${query}`, { method: "GET", redirect: "follow" }); await check5xxError(res); const result = await res.json(); if (res.status !== 200 && res.status !== 201) { throw result; } return result; } async function getSwapSui(params) { const requestBody = JSON.stringify({ ...params, sdkVersion: getSdkVersion() }); const requestUrl = `${addresses_default.PRICE_URL}/get-swap/sui`; const res = await fetch2(requestUrl, { method: "POST", redirect: "follow", body: requestBody, headers: { "Content-Type": "application/json" } }); await check5xxError(res); const result = await res.json(); if (res.status !== 200 && res.status !== 201) { throw result; } return result; } async function submitSwiftEvmSwap(params, signature) { const res = await fetch2(`${addresses_default.EXPLORER_URL}/submit/evm`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...params, signature }, (_key, value) => { if (typeof value === "bigint") { return value.toString(); } return value; }) }); await check5xxError(res); } async function submitSwiftSolanaSwap(signedTx) { const res = await fetch2(`${addresses_default.EXPLORER_URL}/submit/solana`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ signedTx }) }); await check5xxError(res); const result = await res.json(); if (res.status !== 200 && res.status !== 201) { throw result; } return result; } // src/evm/evmSwap.ts import { Contract as Contract5, toBeHex as toBeHex5, ZeroAddress as ZeroAddress7 } from "ethers"; import { PublicKey as PublicKey8, SystemProgram as SystemProgram9 } from "@solana/web3.js"; // src/evm/MayanSwapArtifact.ts var MayanSwapArtifact_default = { "_format": "hh-sol-artifact-1", "contractName": "MayanSwap", "sourceName": "src/MayanSwap.sol", "abi": [ { "inputs": [ { "internalType": "address", "name": "_tokenBridge", "type": "address" }, { "internalType": "address", "name": "_weth", "type": "address" } ], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "uint16", "name": "emitterChainId", "type": "uint16" }, { "indexed": true, "internalType": "bytes32", "name": "emitterAddress", "type": "bytes32" }, { "indexed": true, "internalType": "uint64", "name": "sequence", "type": "uint64" } ], "name": "Redeemed", "type": "event" }, { "inputs": [ { "internalType": "address", "name": "newGuardian", "type": "address" } ], "name": "changeGuardian", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "claimGuardian", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "components": [ { "internalType": "uint8", "name": "payloadId", "type": "uint8" }, { "internalType": "bytes32", "name": "tokenAddr", "type": "bytes32" }, { "internalType": "uint16", "name": "tokenChainId", "type": "uint16" }, { "internalType": "bytes32", "name": "destAddr", "type": "bytes32" }, { "internalType": "uint16", "name": "destChainId", "type": "uint16" }, { "internalType": "bytes32", "name": "sourceAddr", "type": "bytes32" }, { "internalType": "uint16", "name": "sourceChainId", "type": "uint16" }, { "internalType": "uint64", "name": "sequence", "type": "uint64" }, { "internalType": "uint64", "name": "amountOutMin", "type": "uint64" }, { "internalType": "uint64", "name": "deadline", "type": "uint64" }, { "internalType": "uint64", "name": "swapFee", "type": "uint64" }, { "internalType": "uint64", "name": "redeemFee", "type": "uint64" }, { "internalType": "uint64", "name": "refundFee", "type": "uint64" }, { "internalType": "bytes32", "name": "auctionAddr", "type": "bytes32" }, { "internalType": "bool", "name": "unwrapRedeem", "type": "bool" }, { "internalType": "bool", "name": "unwrapRefund", "type": "bool" } ], "internalType": "struct MayanStructs.Swap", "name": "s", "type": "tuple" } ], "name": "encodeSwap", "outputs": [ { "internalType": "bytes", "name": "encoded", "type": "bytes" } ], "stateMutability": "pure", "type": "function" }, { "inputs": [], "name": "getWeth", "outputs": [ { "internalType": "address", "name": "", "type": "address" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "isPaused", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "bytes", "name": "encoded", "type": "bytes" } ], "name": "parseRedeemPayload", "outputs": [ { "components": [ { "internalType": "uint8", "name": "payloadId", "type": "uint8" }, { "internalType": "bytes32", "name": "recipient", "type": "bytes32" }, { "internalType": "uint64", "name": "relayerFee", "type": "uint64" }, { "internalType": "bool", "name": "unwrap", "type": "bool" }, { "internalType": "uint64", "name": "gasDrop", "type": "uint64" }, { "internalType": "bytes", "name": "customPayload", "type": "bytes" } ], "internalType": "struct MayanStructs.Redeem", "name": "r", "type": "tuple" } ], "stateMutability": "pure", "type": "function" }, { "inputs": [ { "internalType": "bytes", "name": "encodedVm", "type": "bytes" } ], "name": "redeem", "outputs": [], "stateMutability": "payable", "type": "function" }, { "inputs": [ { "internalType": "bytes", "name": "encodedVm", "type": "bytes" } ], "name": "redeemAndUnwrap", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "bool", "name": "_pause", "type": "bool" } ], "name": "setPause", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "components": [ { "internalType": "uint64", "name": "swapFee", "type": "uint64" }, { "internalType": "uint64", "name": "redeemFee", "type": "uint64" }, { "internalType": "uint64", "name": "refundFee", "type": "uint64" } ], "internalType": "struct MayanSwap.RelayerFees", "name": "relayerFees", "type": "tuple" }, { "components": [ { "internalType": "bytes32", "name": "mayanAddr", "type": "bytes32" }, { "internalType": "uint16", "name": "mayanChainId", "type": "uint16" }, { "internalType": "bytes32", "name": "auctionAddr", "type": "bytes32" }, { "internalType": "bytes32", "name": "destAddr", "type": "bytes32" }, { "internalType": "uint16", "name": "destChainId", "type": "uint16" }, { "internalType": "bytes32", "name": "referrer", "type": "bytes32" }, { "internalType": "bytes32", "name": "refundAddr", "type": "bytes32" } ], "internalType": "struct MayanSwap.Recepient", "name": "recipient", "type": "tuple" }, { "internalType": "bytes32", "name": "tokenOutAddr", "type": "bytes32" }, { "internalType": "uint16", "name": "tokenOutChainId", "type": "uint16" }, { "components": [ { "internalType": "uint256", "name": "transferDeadline", "type": "uint256" }, { "internalType": "uint64", "name": "swapDeadline", "type": "uint64" }, { "internalType": "uint64", "name": "amountOutMin", "type": "uint64" }, { "internalType": "bool", "name": "unwrap", "type": "bool" }, { "internalType": "uint64", "name": "gasDrop", "type": "uint64" }, { "internalType": "bytes", "name": "customPayload", "type": "bytes" } ], "internalType": "struct MayanSwap.Criteria", "name": "criteria", "type": "tuple" }, { "internalType": "address", "name": "tokenIn", "type": "address" }, { "internalType": "uint256", "name": "amountIn", "type": "uint256" } ], "name": "swap", "outputs": [ { "internalType": "uint64", "name": "sequence", "type": "uint64" } ], "stateMutability": "payable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "amount", "type": "uint256" }, { "internalType": "address payable", "name": "to", "type": "address" } ], "name": "sweepEth", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "token", "type": "address" }, { "internalType": "uint256", "name": "amount", "type": "uint256" }, { "internalType": "address", "name": "to", "type": "address" } ], "name": "sweepToken", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "components": [ { "internalType": "uint64", "name": "swapFee", "type": "uint64" }, { "internalType": "uint64", "name": "redeemFee", "type": "uint64" }, { "internalType": "uint64", "name": "refundFee", "type": "uint64" } ], "internalType": "struct MayanSwap.RelayerFees", "name": "relayerFees", "type": "tuple" }, { "components": [ { "internalType": "bytes32", "name": "mayanAddr", "type": "bytes32" }, { "internalType": "uint16", "name": "mayanChainId", "type": "uint16" }, { "internalType": "bytes32", "name": "auctionAddr", "type": "bytes32" }, { "internalType": "bytes32", "name": "destAddr", "type": "bytes32" }, { "internalType": "uint16", "name": "destChainId", "type": "uint16" }, { "internalType": "bytes32", "name": "referrer", "type": "bytes32" }, { "internalType": "bytes32", "name": "refundAddr", "type": "bytes32" } ], "internalType": "struct MayanSwap.Recepient", "name": "recipient", "type": "tuple" }, { "internalType": "bytes32", "name": "tokenOutAddr", "type": "bytes32" }, { "internalType": "uint16", "name": "tokenOutChainId", "type": "uint16" }, { "components": [ { "internalType": "uint256", "name": "transferDeadline", "type": "uint256" }, { "internalType": "uint64", "name": "swapDeadline", "type": "uint64" }, { "internalType": "uint64", "name": "amountOutMin", "type": "uint64" }, { "internalType": "bool", "name": "unwrap", "type": "bool" }, { "internalType": "uint64", "name": "gasDrop", "type": "uint64" }, { "internalType": "bytes", "name": "customPayload", "type": "bytes" } ], "internalType": "struct MayanSwap.Criteria", "name": "criteria", "type": "tuple" } ], "name": "wrapAndSwapETH", "outputs": [ { "internalType": "uint64", "name": "sequence", "type": "uint64" } ], "stateMutability": "payable", "type": "function" }, { "stateMutability": "payable", "type": "receive" } ] }; // src/evm/MayanForwarderArtifact.ts var MayanForwarderArtifact_default = { "_format": "hh-sol-artifact-1", "contractName": "MayanForwarder", "sourceName": "src/MayanForwarder.sol", "abi": [ { "inputs": [ { "internalType": "address", "name": "_guardian", "type": "address" }, { "internalType": "address[]", "name": "_swapProtocols", "type": "address[]" }, { "internalType": "address[]", "name": "_mayanProtocols", "type": "address[]" } ], "stateMutability": "nonpayable", "type": "constructor" }, { "inputs": [], "name": "UnsupportedProtocol", "type": "error" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "address", "name": "token", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "amount", "type": "uint256" }, { "indexed": false, "internalType": "address", "name": "mayanProtocol", "type": "address" }, { "indexed": false, "internalType": "bytes", "name": "protocolData", "type": "bytes" } ], "name": "ForwardedERC20", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "address", "name": "mayanProtocol", "type": "address" }, { "indexed": false, "internalType": "bytes", "name": "protocolData", "type": "bytes" } ], "name": "ForwardedEth", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "amount", "type": "uint256" } ], "name": "SwapAndForwarded", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "address", "name": "tokenIn", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "indexed": false, "internalType": "address", "name": "swapProtocol", "type": "address" }, { "indexed": false, "internalType": "address", "name": "middleToken", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "middleAmount", "type": "uint256" }, { "indexed": false, "internalType": "address", "name": "mayanProtocol", "type": "address" }, { "indexed": false, "internalType": "bytes", "name": "mayanData", "type": "bytes" } ], "name": "SwapAndForwardedERC20", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "indexed": false, "internalType": "address", "name": "swapProtocol", "type": "address" }, { "indexed": false, "internalType": "address", "name": "middleToken", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "middleAmount", "type": "uint256" }, { "indexed": false, "internalType": "address", "name": "mayanProtocol", "type": "address" }, { "indexed": false, "internalType": "bytes", "name": "mayanData", "type": "bytes" } ], "name": "SwapAndForwardedEth", "type": "event" }, { "inputs": [ { "internalType": "address", "name": "newGuardian", "type": "address" } ], "name": "changeGuardian", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "claimGuardian", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "tokenIn", "type": "address" }, { "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "components": [ { "internalType": "uint256", "name": "value", "type": "uint256" }, { "internalType": "uint256", "name": "deadline", "type": "uint256" }, { "internalType": "uint8", "name": "v", "type": "uint8" }, { "internalType": "bytes32", "name": "r", "type": "bytes32" }, { "internalType": "bytes32", "name": "s", "type": "bytes32" } ], "internalType": "struct MayanForwarder.PermitParams", "name": "permitParams", "type": "tuple" }, { "internalType": "address", "name": "mayanProtocol", "type": "address" }, { "internalType": "bytes", "name": "protocolData", "type": "bytes" } ], "name": "forwardERC20", "outputs": [], "stateMutability": "payable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "mayanProtocol", "type": "address" }, { "internalType": "bytes", "name": "protocolData", "type": "bytes" } ], "name": "forwardEth", "outputs": [], "stateMutability": "payable", "type": "function" }, { "inputs": [], "name": "guardian", "outputs": [ { "internalType": "address", "name": "", "type": "address" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "", "type": "address" } ], "name": "mayanProtocols", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "nextGuardian", "outputs": [ { "internalType": "address", "name": "", "type": "address" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "amount", "type": "uint256" }, { "internalType": "address payable", "name": "to", "type": "address" } ], "name": "rescueEth", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "token", "type": "address" }, { "internalType": "uint256", "name": "amount", "type": "uint256" }, { "internalType": "address", "name": "to", "type": "address" } ], "name": "rescueToken", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "mayanProtocol", "type": "address" }, { "internalType": "bool", "name": "enabled", "type": "bool" } ], "name": "setMayanProtocol", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "swapProtocol", "type": "address" }, { "internalType": "bool", "name": "enabled", "type": "bool" } ], "name": "setSwapProtocol", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "tokenIn", "type": "address" }, { "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "components": [ { "internalType": "uint256", "name": "value", "type": "uint256" }, { "internalType": "uint256", "name": "deadline", "type": "uint256" }, { "internalType": "uint8", "name": "v", "type": "uint8" }, { "internalType": "bytes32", "name": "r", "type": "bytes32" }, { "internalType": "bytes32", "name": "s", "type": "bytes32" } ], "internalType": "struct MayanForwarder.PermitParams", "name": "permitParams", "type": "tuple" }, { "internalType": "address", "name": "swapProtocol", "type": "address" }, { "internalType": "bytes", "name": "swapData", "type": "bytes" }, { "internalType": "address", "name": "middleToken", "type": "address" }, { "internalType": "uint256", "name": "minMiddleAmount", "type": "uint256" }, { "internalType": "address", "name": "mayanProtocol", "type": "address" }, { "internalType": "bytes", "name": "mayanData", "type": "bytes" } ], "name": "swapAndForwardERC20", "outputs": [], "stateMutability": "payable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "amountIn", "type": "uint256" }, { "internalType": "address", "name": "swapProtocol", "type": "address" }, { "internalType": "bytes", "name": "swapData", "type": "bytes" }, { "internalType": "address", "name": "middleToken", "type": "address" }, { "internalType": "uint256", "name": "minMiddleAmount", "type": "uint256" }, { "internalType": "address", "name": "mayanProtocol", "type": "address" }, { "internalType": "bytes", "name": "mayanData", "type": "bytes" } ], "name": "swapAndForwardEth", "outputs": [], "stateMutability": "payable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "", "type": "address" } ], "name": "swapProtocols", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "view", "type": "function" } ], "linkReferences": {}, "deployedLinkReferences": {} }; // src/evm/evmSwap.ts import { Buffer as Buffer11 } from "buffer"; // src/evm/evmMctp.ts import { Contract, toBeHex, ZeroAddress } from "ethers"; import { SystemProgram as SystemProgram2 } from "@solana/web3.js"; // src/evm/MayanCircleArtifact.ts var MayanCircleArtifact_default = { _format: "hh-sol-artifact-1", contractName: "MayanCircle", sourceName: "src/MayanCircle.sol", abi: [ { inputs: [ { internalType: "address", name: "_cctpTokenMessenger", type: "address" }, { internalType: "address", name: "_wormhole", type: "address" }, { internalType: "address", name: "_feeManager", type: "address" }, { internalType: "uint16", name: "_auctionChainId", type: "uint16" }, { internalType: "bytes32", name: "_auctionAddr", type: "bytes32" }, { internalType: "uint8", name: "_consistencyLevel", type: "uint8" } ], stateMutability: "nonpayable", type: "constructor" }, { inputs: [], name: "CallerAlreadySet", type: "error" }, { inputs: [], name: "CallerNotSet", type: "error" }, { inputs: [], name: "CctpReceiveFailed", type: "error" }, { inputs: [], name: "DeadlineViolation", type: "error" }, { inputs: [], name: "DomainAlreadySet", type: "error" }, { inputs: [], name: "DomainNotSet", type: "error" }, { inputs: [], name: "EmitterAlreadySet", type: "error" }, { inputs: [], name: "EthTransferFailed", type: "error" }, { inputs: [], name: "InvalidAction", type: "error" }, { inputs: [], name: "InvalidAddress", type: "error" }, { inputs: [], name: "InvalidAmountOut", type: "error" }, { inputs: [], name: "InvalidCaller", type: "error" }, { inputs: [], name: "InvalidDestAddr", type: "error" }, { inputs: [], name: "InvalidDomain", type: "error" }, { inputs: [], name: "InvalidEmitter", type: "error" }, { inputs: [], name: "InvalidGasDrop", type: "error" }, { inputs: [], name: "InvalidMintRecipient", type: "error" }, { inputs: [], name: "InvalidNonce", type: "error" }, { inputs: [], name: "InvalidOrder", type: "error" }, { inputs: [], name: "InvalidPayload", type: "error" }, { inputs: [], name: "InvalidProtocolFee", type: "error" }, { inputs: [], name: "InvalidRedeemFee", type: "error" }, { inputs: [], name: "InvalidReferrerFee", type: "error" }, { inputs: [], name: "MintRecipientAlreadySet", type: "error" }, { inputs: [], name: "MintRecipientNotSet", type: "error" }, { inputs: [], name: "Paused", type: "error" }, { inputs: [], name: "Unauthorized", type: "error" }, { anonymous: false, inputs: [ { indexed: false, internalType: "uint32", name: "sourceDomain", type: "uint32" }, { indexed: false, internalType: "uint64", name: "sourceNonce", type: "uint64" }, { indexed: false, internalType: "uint256", name: "amount", type: "uint256" } ], name: "OrderFulfilled", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "uint32", name: "sourceDomain", type: "uint32" }, { indexed: false, internalType: "uint64", name: "sourceNonce", type: "uint64" }, { indexed: false, internalType: "uint256", name: "amount", type: "uint256" } ], name: "OrderRefunded", type: "event" }, { inputs: [], name: "auctionAddr", outputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "auctionChainId", outputs: [ { internalType: "uint16", name: "", type: "uint16" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "tokenIn", type: "address" }, { internalType: "uint256", name: "amountIn", type: "uint256" }, { internalType: "uint64", name: "redeemFee", type: "uint64" }, { internalType: "uint64", name: "gasDrop", type: "uint64" }, { internalType: "bytes32", name: "destAddr", type: "bytes32" }, { internalType: "uint32", name: "destDomain", type: "uint32" }, { internalType: "uint8", name: "payloadType", type: "uint8" }, { internalType: "bytes", name: "customPayload", type: "bytes" } ], name: "bridgeWithFee", outputs: [ { internalType: "uint64", name: "sequence", type: "uint64" } ], stateMutability: "payable", type: "function" }, { inputs: [ { internalType: "address", name: "tokenIn", type: "address" }, { internalType: "uint256", name: "amountIn", type: "uint256" }, { internalType: "uint64", name: "gasDrop", type: "uint64" }, { internalType: "uint256", name: "redeemFee", type: "uint256" }, { internalType: "uint32", name: "destDomain", type: "uint32" }, { internalType: "bytes32", name: "destAddr", type: "bytes32" } ], name: "bridgeWithLockedFee", outputs: [ { internalType: "uint64", name: "cctpNonce", type: "uint64" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "cctpTokenMessenger", outputs: [ { internalType: "contract ITokenMessenger", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint16", name: "", type: "uint16" } ], name: "chainIdToEmitter", outputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "newGuardian", type: "address" } ], name: "changeGuardian", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "claimGuardian", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "consistencyLevel", outputs: [ { internalType: "uint8", name: "", type: "uint8" } ], stateMutability: "view", type: "function" }, { inputs: [ { components: [ { internalType: "address", name: "tokenIn", type: "address" }, { internalType: "uint256", name: "amountIn", type: "uint256" },