UNPKG

@depay/web3-payments-evm

Version:

JavaScript library to scan crypto wallets for liquefiable assets and perform cost-effective, auto-converted payments on-chain.

1,567 lines (1,454 loc) 147 kB
import Exchanges from '@depay/web3-exchanges-svm'; import Token$1 from '@depay/web3-tokens-svm'; import Blockchains from '@depay/web3-blockchains'; import { ethers } from 'ethers'; import { BN, struct, u64, i64, bool, u8, u32, publicKey, rustEnum, str, u16, option, vec, PublicKey, Buffer, TransactionInstruction, SystemProgram, ComputeBudgetProgram, u128 } from '@depay/solana-web3.js'; import { request, getProvider } from '@depay/web3-client'; const config = { endpoints: { routesBest: 'https://public.depay.com/routes/best' } }; const API = [{"inputs":[{"internalType":"address","name":"_PERMIT2","type":"address"},{"internalType":"address","name":"_FORWARDER","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"ExchangeCallFailed","type":"error"},{"inputs":[],"name":"ExchangeCallMissing","type":"error"},{"inputs":[],"name":"ExchangeNotApproved","type":"error"},{"inputs":[],"name":"ForwardingPaymentFailed","type":"error"},{"inputs":[],"name":"InsufficientBalanceInAfterPayment","type":"error"},{"inputs":[],"name":"InsufficientBalanceOutAfterPayment","type":"error"},{"inputs":[],"name":"InsufficientProtocolAmount","type":"error"},{"inputs":[],"name":"NativeFeePaymentFailed","type":"error"},{"inputs":[],"name":"NativePaymentFailed","type":"error"},{"inputs":[],"name":"PaymentDeadlineReached","type":"error"},{"inputs":[],"name":"PaymentToZeroAddressNotAllowed","type":"error"},{"inputs":[],"name":"WrongAmountPaidIn","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"exchange","type":"address"}],"name":"Disabled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"exchange","type":"address"}],"name":"Enabled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferStarted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"uint256","name":"deadline","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amountIn","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"paymentAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"feeAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"feeAmount2","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"protocolAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"slippageInAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"slippageOutAmount","type":"uint256"},{"indexed":false,"internalType":"address","name":"tokenInAddress","type":"address"},{"indexed":false,"internalType":"address","name":"tokenOutAddress","type":"address"},{"indexed":false,"internalType":"address","name":"feeReceiverAddress","type":"address"},{"indexed":false,"internalType":"address","name":"feeReceiverAddress2","type":"address"}],"name":"Payment","type":"event"},{"inputs":[],"name":"FORWARDER","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PERMIT2","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"acceptOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"exchange","type":"address"},{"internalType":"bool","name":"enabled","type":"bool"}],"name":"enable","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"exchanges","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"paymentAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount2","type":"uint256"},{"internalType":"uint256","name":"protocolAmount","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"address","name":"tokenInAddress","type":"address"},{"internalType":"address","name":"exchangeAddress","type":"address"},{"internalType":"address","name":"tokenOutAddress","type":"address"},{"internalType":"address","name":"paymentReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress2","type":"address"},{"internalType":"uint8","name":"exchangeType","type":"uint8"},{"internalType":"uint8","name":"receiverType","type":"uint8"},{"internalType":"bool","name":"permit2","type":"bool"},{"internalType":"bytes","name":"exchangeCallData","type":"bytes"},{"internalType":"bytes","name":"receiverCallData","type":"bytes"}],"internalType":"struct IDePayRouterV3.Payment","name":"payment","type":"tuple"},{"components":[{"components":[{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"internalType":"struct IPermit2.TokenPermissions","name":"permitted","type":"tuple"},{"internalType":"uint256","name":"nonce","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"internalType":"struct IPermit2.PermitTransferFrom","name":"permitTransferFrom","type":"tuple"},{"internalType":"bytes","name":"signature","type":"bytes"}],"internalType":"struct IDePayRouterV3.PermitTransferFromAndSignature","name":"permitTransferFromAndSignature","type":"tuple"}],"name":"pay","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"paymentAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount2","type":"uint256"},{"internalType":"uint256","name":"protocolAmount","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"address","name":"tokenInAddress","type":"address"},{"internalType":"address","name":"exchangeAddress","type":"address"},{"internalType":"address","name":"tokenOutAddress","type":"address"},{"internalType":"address","name":"paymentReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress2","type":"address"},{"internalType":"uint8","name":"exchangeType","type":"uint8"},{"internalType":"uint8","name":"receiverType","type":"uint8"},{"internalType":"bool","name":"permit2","type":"bool"},{"internalType":"bytes","name":"exchangeCallData","type":"bytes"},{"internalType":"bytes","name":"receiverCallData","type":"bytes"}],"internalType":"struct IDePayRouterV3.Payment","name":"payment","type":"tuple"}],"name":"pay","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"paymentAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount","type":"uint256"},{"internalType":"uint256","name":"feeAmount2","type":"uint256"},{"internalType":"uint256","name":"protocolAmount","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"address","name":"tokenInAddress","type":"address"},{"internalType":"address","name":"exchangeAddress","type":"address"},{"internalType":"address","name":"tokenOutAddress","type":"address"},{"internalType":"address","name":"paymentReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress","type":"address"},{"internalType":"address","name":"feeReceiverAddress2","type":"address"},{"internalType":"uint8","name":"exchangeType","type":"uint8"},{"internalType":"uint8","name":"receiverType","type":"uint8"},{"internalType":"bool","name":"permit2","type":"bool"},{"internalType":"bytes","name":"exchangeCallData","type":"bytes"},{"internalType":"bytes","name":"receiverCallData","type":"bytes"}],"internalType":"struct IDePayRouterV3.Payment","name":"payment","type":"tuple"},{"components":[{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint160","name":"amount","type":"uint160"},{"internalType":"uint48","name":"expiration","type":"uint48"},{"internalType":"uint48","name":"nonce","type":"uint48"}],"internalType":"struct IPermit2.PermitDetails","name":"details","type":"tuple"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"sigDeadline","type":"uint256"}],"internalType":"struct IPermit2.PermitSingle","name":"permitSingle","type":"tuple"},{"internalType":"bytes","name":"signature","type":"bytes"}],"name":"pay","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"pendingOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdraw","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]; var svmRouters = { ethereum: { address: '0x365f7B56D2fB16C8Af89D7d33b420E4e013461e8', api: API }, bsc: { address: '0x5F565EDfB9C446976a9F9910631cfeDb6A87220c', api: API }, polygon: { address: '0xe04b08Dfc6CaA0F4Ec523a3Ae283Ece7efE00019', api: API }, avalanche: { address: '0x39E7C98BF4ac3E4C394dD600397f5f7Ee3779BE8', api: API }, gnosis: { address: '0x328FE8bbd30487BB7b5A8eEb909f892E9E229271', api: API }, arbitrum: { address: '0x328FE8bbd30487BB7b5A8eEb909f892E9E229271', api: API }, optimism: { address: '0x558302715e3011Be6695605c11A65526D2ba2245', api: API }, base: { address: '0x48825133EF08327535D0b24d73779E82BE6Ea4d9', api: API }, worldchain: { address: '0x886eb82a7e5E7310F66A0E83748662A17E391eb0', api: API }, }; let evmRouters = {}; var routers$1 = {... evmRouters, ...svmRouters}; let supported$1 = ['solana']; supported$1.evm = []; supported$1.svm = ['solana']; let evmGetRouterApprovalTransaction = ()=>{}; let evmGetPermit2ApprovalTransaction = ()=>{}; let evmGetPermit2ApprovalSignature = ()=>{}; const getRouterApprovalTransaction = ({ paymentRoute, options })=>{ if(supported$1.evm.includes(paymentRoute.blockchain)) { return evmGetRouterApprovalTransaction() } else if(supported$1.svm.includes(paymentRoute.blockchain)) ; else { throw('Blockchain not supported!') } }; const getPermit2ApprovalTransaction = ({ paymentRoute, options })=>{ if(supported$1.evm.includes(paymentRoute.blockchain)) { return evmGetPermit2ApprovalTransaction() } else if(supported$1.svm.includes(paymentRoute.blockchain)) ; else { throw('Blockchain not supported!') } }; const getPermit2ApprovalSignature = ({ paymentRoute, options })=>{ if(supported$1.evm.includes(paymentRoute.blockchain)) { return evmGetPermit2ApprovalSignature() } else if(supported$1.svm.includes(paymentRoute.blockchain)) ; else { throw('Blockchain not supported!') } }; var routers = { solana: { address: 'DePayR1gQfDmViCPKctnZXNtUgqRwnEqMax8LX9ho1Zg', exchanges: { orca: 'whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc', raydiumCP: 'CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C', raydiumCL: 'CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK', }, alt: '8bYq3tcwX1NM2K2JYMjrEqAPtCXFPCjzPazFothc618e', api: { createEscrowSolAccount: { anchorDiscriminator: new BN("2482112285991870004"), layout: struct([ u64("anchorDiscriminator"), ]) }, createEscrowTokenAccount: { anchorDiscriminator: new BN("16156440424245087"), layout: struct([ u64("anchorDiscriminator"), ]) }, routeSol: { anchorDiscriminator: new BN("6497164560834983274"), layout: struct([ u64("anchorDiscriminator"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeToken: { anchorDiscriminator: new BN("13483873682232752277"), layout: struct([ u64("anchorDiscriminator"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaSwap: { anchorDiscriminator: new BN("9797248061404332986"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), bool("aToB"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaSwapSolOut: { anchorDiscriminator: new BN("13662217913752830165"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), bool("aToB"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaSwapSolIn: { anchorDiscriminator: new BN("16115018480206947614"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), bool("aToB"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaTwoHopSwap: { anchorDiscriminator: new BN("15695720599845325801"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), bool("aToBOne"), bool("aToBTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaTwoHopSwapSolOut: { anchorDiscriminator: new BN("15074061855608091530"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), bool("aToBOne"), bool("aToBTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeOrcaTwoHopSwapSolIn: { anchorDiscriminator: new BN("2678451299937372540"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), bool("aToBOne"), bool("aToBTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumClSwap: { anchorDiscriminator: new BN("2954182973248174268"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumClSwapSolOut: { anchorDiscriminator: new BN("18389700643710627390"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumClSwapSolIn: { anchorDiscriminator: new BN("564150378912976829"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumClTwoHopSwap: { anchorDiscriminator: new BN("3828760301615328551"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), u8("remainingAccountsSplit"), ]) }, routeRaydiumClTwoHopSwapSolOut: { anchorDiscriminator: new BN("11373220799455718953"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), u8("remainingAccountsSplit"), ]) }, routeRaydiumClTwoHopSwapSolIn: { anchorDiscriminator: new BN("1635173573630140652"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), u8("remainingAccountsSplit"), ]) }, routeRaydiumCpSwap: { anchorDiscriminator: new BN("7437765211943645137"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumCpSwapSolOut: { anchorDiscriminator: new BN("9045257739866411286"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumCpSwapSolIn: { anchorDiscriminator: new BN("432305509198797158"), layout: struct([ u64("anchorDiscriminator"), u64("amountIn"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumCpTwoHopSwap: { anchorDiscriminator: new BN("3384279312781294015"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumCpTwoHopSwapSolOut: { anchorDiscriminator: new BN("18428464202744806632"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) }, routeRaydiumCpTwoHopSwapSolIn: { anchorDiscriminator: new BN("16266677464406446072"), layout: struct([ u64("anchorDiscriminator"), u64("amountInOne"), u64("amountInTwo"), u64("paymentAmount"), u64("feeAmount"), u64("feeAmount2"), u64("protocolAmount"), i64("deadline"), ]) } } }, }; var allowanceOnEVM = ({ blockchain, address, api, owner, spender })=>{ return request( { blockchain, address, api, method: 'allowance', params: [owner, spender], // no cache for allowance! }, ) }; var balanceOnEVM = async ({ blockchain, address, account, api, id })=>{ if (address == Blockchains[blockchain].currency.address) { return await request( { blockchain: blockchain, address: account, method: 'balance', }, ) } else { return await request( { blockchain: blockchain, address: address, method: 'balanceOf', api, params: id ? [account, id] : [account], }, ) } }; var decimalsOnEVM = ({ blockchain, address, api })=>{ return request({ blockchain, address, api, method: 'decimals', cache: 86400000, // 1 day }) }; var ERC1155 = [ { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "account", "type": "address" }, { "indexed": true, "internalType": "address", "name": "operator", "type": "address" }, { "indexed": false, "internalType": "bool", "name": "approved", "type": "bool" } ], "name": "ApprovalForAll", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "operator", "type": "address" }, { "indexed": true, "internalType": "address", "name": "from", "type": "address" }, { "indexed": true, "internalType": "address", "name": "to", "type": "address" }, { "indexed": false, "internalType": "uint256[]", "name": "ids", "type": "uint256[]" }, { "indexed": false, "internalType": "uint256[]", "name": "values", "type": "uint256[]" } ], "name": "TransferBatch", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "internalType": "address", "name": "operator", "type": "address" }, { "indexed": true, "internalType": "address", "name": "from", "type": "address" }, { "indexed": true, "internalType": "address", "name": "to", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "id", "type": "uint256" }, { "indexed": false, "internalType": "uint256", "name": "value", "type": "uint256" } ], "name": "TransferSingle", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "string", "name": "value", "type": "string" }, { "indexed": true, "internalType": "uint256", "name": "id", "type": "uint256" } ], "name": "URI", "type": "event" }, { "inputs": [ { "internalType": "address", "name": "account", "type": "address" }, { "internalType": "uint256", "name": "id", "type": "uint256" } ], "name": "balanceOf", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "address[]", "name": "accounts", "type": "address[]" }, { "internalType": "uint256[]", "name": "ids", "type": "uint256[]" } ], "name": "balanceOfBatch", "outputs": [ { "internalType": "uint256[]", "name": "", "type": "uint256[]" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "account", "type": "address" }, { "internalType": "address", "name": "operator", "type": "address" } ], "name": "isApprovedForAll", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "from", "type": "address" }, { "internalType": "address", "name": "to", "type": "address" }, { "internalType": "uint256[]", "name": "ids", "type": "uint256[]" }, { "internalType": "uint256[]", "name": "amounts", "type": "uint256[]" }, { "internalType": "bytes", "name": "data", "type": "bytes" } ], "name": "safeBatchTransferFrom", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "from", "type": "address" }, { "internalType": "address", "name": "to", "type": "address" }, { "internalType": "uint256", "name": "id", "type": "uint256" }, { "internalType": "uint256", "name": "amount", "type": "uint256" }, { "internalType": "bytes", "name": "data", "type": "bytes" } ], "name": "safeTransferFrom", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "operator", "type": "address" }, { "internalType": "bool", "name": "approved", "type": "bool" } ], "name": "setApprovalForAll", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "bytes4", "name": "interfaceId", "type": "bytes4" } ], "name": "supportsInterface", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "id", "type": "uint256" } ], "name": "uri", "outputs": [ { "internalType": "string", "name": "", "type": "string" } ], "stateMutability": "view", "type": "function" } ]; var ERC20 = [ { constant: true, inputs: [], name: 'name', outputs: [{ name: '', type: 'string' }], payable: false, stateMutability: 'view', type: 'function', }, { constant: false, inputs: [ { name: '_spender', type: 'address' }, { name: '_value', type: 'uint256' }, ], name: 'approve', outputs: [{ name: '', type: 'bool' }], payable: false, stateMutability: 'nonpayable', type: 'function', }, { constant: true, inputs: [], name: 'totalSupply', outputs: [{ name: '', type: 'uint256' }], payable: false, stateMutability: 'view', type: 'function', }, { constant: false, inputs: [ { name: '_from', type: 'address' }, { name: '_to', type: 'address' }, { name: '_value', type: 'uint256' }, ], name: 'transferFrom', outputs: [{ name: '', type: 'bool' }], payable: false, stateMutability: 'nonpayable', type: 'function', }, { constant: true, inputs: [], name: 'decimals', outputs: [{ name: '', type: 'uint8' }], payable: false, stateMutability: 'view', type: 'function', }, { constant: true, inputs: [{ name: '_owner', type: 'address' }], name: 'balanceOf', outputs: [{ name: 'balance', type: 'uint256' }], payable: false, stateMutability: 'view', type: 'function', }, { constant: true, inputs: [], name: 'symbol', outputs: [{ name: '', type: 'string' }], payable: false, stateMutability: 'view', type: 'function', }, { constant: false, inputs: [ { name: '_to', type: 'address' }, { name: '_value', type: 'uint256' }, ], name: 'transfer', outputs: [{ name: '', type: 'bool' }], payable: false, stateMutability: 'nonpayable', type: 'function', }, { constant: true, inputs: [ { name: '_owner', type: 'address' }, { name: '_spender', type: 'address' }, ], name: 'allowance', outputs: [{ name: '', type: 'uint256' }], payable: false, stateMutability: 'view', type: 'function', }, { payable: true, stateMutability: 'payable', type: 'fallback' }, { anonymous: false, inputs: [ { indexed: true, name: 'owner', type: 'address' }, { indexed: true, name: 'spender', type: 'address' }, { indexed: false, name: 'value', type: 'uint256' }, ], name: 'Approval', type: 'event', }, { anonymous: false, inputs: [ { indexed: true, name: 'from', type: 'address' }, { indexed: true, name: 'to', type: 'address' }, { indexed: false, name: 'value', type: 'uint256' }, ], name: 'Transfer', type: 'event', }, ]; var WETH = [ { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "guy", "type": "address" }, { "name": "wad", "type": "uint256" } ], "name": "approve", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "src", "type": "address" }, { "name": "dst", "type": "address" }, { "name": "wad", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "wad", "type": "uint256" } ], "name": "withdraw", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "dst", "type": "address" }, { "name": "wad", "type": "uint256" } ], "name": "transfer", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [], "name": "deposit", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "address" }, { "name": "", "type": "address" } ], "name": "allowance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "payable": true, "stateMutability": "payable", "type": "fallback" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "src", "type": "address" }, { "indexed": true, "name": "guy", "type": "address" }, { "indexed": false, "name": "wad", "type": "uint256" } ], "name": "Approval", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "src", "type": "address" }, { "indexed": true, "name": "dst", "type": "address" }, { "indexed": false, "name": "wad", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "dst", "type": "address" }, { "indexed": false, "name": "wad", "type": "uint256" } ], "name": "Deposit", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "src", "type": "address" }, { "indexed": false, "name": "wad", "type": "uint256" } ], "name": "Withdrawal", "type": "event" } ]; const uriAPI = [{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"uri","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]; const uriToName = (tokenURI)=>{ return new Promise((resolve)=>{ if(tokenURI.match(/^ipfs/)) { tokenURI = `https://ipfs.io/ipfs/${tokenURI.split('://')[1]}`; } fetch(tokenURI).then((response) => { if (response.ok) { return response.json() } resolve(); }) .then((responseJson) => { if(responseJson) { let name = responseJson.name; if(name){ resolve(name); } else { resolve(); } } }).catch(()=>resolve()); }) }; var nameOnEVM = ({ blockchain, address, api, id })=>{ if(id) { return new Promise((resolve)=>{ request({ blockchain, address, api: uriAPI, method: 'uri', params: [id] }).then((uri)=>{ uri = uri.match('0x{id}') ? uri.replace('0x{id}', id) : uri; uriToName(uri).then(resolve); }).catch((error)=>{ console.log('error', error); resolve(); }); }) } else { return request( { blockchain: blockchain, address: address, api, method: 'name', cache: 86400000, // 1 day }, ) } }; var symbolOnEVM = ({ blockchain, address, api })=>{ return request( { blockchain, address, api, method: 'symbol', cache: 86400000, // 1 day } ) }; const TOKEN_PROGRAM = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'; const ASSOCIATED_TOKEN_PROGRAM = 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL'; function _optionalChain$4(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var findProgramAddress = async ({ token, owner })=>{ const [address] = await PublicKey.findProgramAddress( [ (new PublicKey(owner)).toBuffer(), (new PublicKey(TOKEN_PROGRAM)).toBuffer(), (new PublicKey(token)).toBuffer() ], new PublicKey(ASSOCIATED_TOKEN_PROGRAM) ); return _optionalChain$4([address, 'optionalAccess', _ => _.toString, 'call', _2 => _2()]) }; const MINT_LAYOUT = struct([ u32('mintAuthorityOption'), publicKey('mintAuthority'), u64('supply'), u8('decimals'), bool('isInitialized'), u32('freezeAuthorityOption'), publicKey('freezeAuthority') ]); const KEY_LAYOUT = rustEnum([ struct([], 'uninitialized'), struct([], 'editionV1'), struct([], 'masterEditionV1'), struct([], 'reservationListV1'), struct([], 'metadataV1'), struct([], 'reservationListV2'), struct([], 'masterEditionV2'), struct([], 'editionMarker'), ]); const CREATOR_LAYOUT = struct([ publicKey('address'), bool('verified'), u8('share'), ]); const DATA_LAYOUT = struct([ str('name'), str('symbol'), str('uri'), u16('sellerFeeBasisPoints'), option( vec( CREATOR_LAYOUT.replicate('creators') ), 'creators' ) ]); const METADATA_LAYOUT = struct([ KEY_LAYOUT.replicate('key'), publicKey('updateAuthority'), publicKey('mint'), DATA_LAYOUT.replicate('data'), bool('primarySaleHappened'), bool('isMutable'), option(u8(), 'editionNonce'), ]); const TRANSFER_LAYOUT = struct([ u8('instruction'), u64('amount'), ]); const TOKEN_LAYOUT = struct([ publicKey('mint'), publicKey('owner'), u64('amount'), u32('delegateOption'), publicKey('delegate'), u8('state'), u32('isNativeOption'), u64('isNative'), u64('delegatedAmount'), u32('closeAuthorityOption'), publicKey('closeAuthority') ]); const INITIALIZE_LAYOUT = struct([ u8('instruction'), publicKey('owner') ]); const CLOSE_LAYOUT = struct([ u8('instruction') ]); const createTransferInstruction = async ({ token, amount, from, to })=>{ let fromTokenAccount = await findProgramAddress({ token, owner: from }); let toTokenAccount = await findProgramAddress({ token, owner: to }); const keys = [ { pubkey: new PublicKey(fromTokenAccount), isSigner: false, isWritable: true }, { pubkey: new PublicKey(toTokenAccount), isSigner: false, isWritable: true }, { pubkey: new PublicKey(from), isSigner: true, isWritable: false } ]; const data = Buffer.alloc(TRANSFER_LAYOUT.span); TRANSFER_LAYOUT.encode({ instruction: 3, // TRANSFER amount: new BN(amount) }, data); return new TransactionInstruction({ keys, programId: new PublicKey(TOKEN_PROGRAM), data }) }; const createAssociatedTokenAccountInstruction = async ({ token, owner, payer }) => { let associatedToken = await findProgramAddress({ token, owner }); const keys = [ { pubkey: new PublicKey(payer), isSigner: true, isWritable: true }, { pubkey: new PublicKey(associatedToken), isSigner: false, isWritable: true }, { pubkey: new PublicKey(owner), isSigner: false, isWritable: false }, { pubkey: new PublicKey(token), isSigner: false, isWritable: false }, { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, { pubkey: new PublicKey(TOKEN_PROGRAM), isSigner: false, isWritable: false }, ]; return new TransactionInstruction({ keys, programId: new PublicKey(ASSOCIATED_TOKEN_PROGRAM), data: Buffer.alloc(0) }) }; const initializeAccountInstruction = ({ account, token, owner })=>{ const keys = [ { pubkey: new PublicKey(account), isSigner: false, isWritable: true }, { pubkey: new PublicKey(token), isSigner: false, isWritable: false }, ]; const data = Buffer.alloc(INITIALIZE_LAYOUT.span); INITIALIZE_LAYOUT.encode({ instruction: 18, // InitializeAccount3 owner: new PublicKey(owner) }, data); return new TransactionInstruction({ keys, programId: new PublicKey(TOKEN_PROGRAM), data }) }; const closeAccountInstruction = ({ account, owner })=>{ const keys = [ { pubkey: new PublicKey(account), isSigner: false, isWritable: true }, { pubkey: new PublicKey(owner), isSigner: false, isWritable: true }, { pubkey: new PublicKey(owner), isSigner: true, isWritable: false } ]; const data = Buffer.alloc(CLOSE_LAYOUT.span); CLOSE_LAYOUT.encode({ instruction: 9 // CloseAccount }, data); return new TransactionInstruction({ keys, programId: new PublicKey(TOKEN_PROGRAM), data }) }; var instructions = /*#__PURE__*/Object.freeze({ __proto__: null, createTransferInstruction: createTransferInstruction, createAssociatedTokenAccountInstruction: createAssociatedTokenAccountInstruction, initializeAccountInstruction: initializeAccountInstruction, closeAccountInstruction: closeAccountInstruction }); var balanceOnSolana = async ({ blockchain, address, account, api })=>{ if(address == Blockchains[blockchain].currency.address) { return ethers.BigNumber.from(await request(`solana://${account}/balance`)) } else { const tokenAccountAddress = await findProgramAddress({ token: address, owner: account }); const balance = await request(`solana://${tokenAccountAddress}/getTokenAccountBalance`); if (balance) { return ethers.BigNumber.from(balance.value.amount) } else { return ethers.BigNumber.from('0') } } }; var decimalsOnSolana = async ({ blockchain, address })=>{ let data = await request({ blockchain, address, api: MINT_LAYOUT }); return data.decimals }; var findAccount = async ({ token, owner })=>{ const address = await findProgramAddress({ token, owner }); const existingAccount = await request({ blockchain: 'solana', address, api: TOKEN_LAYOUT, cache: 1000 // 1s }); return existingAccount }; function _optionalChain$3(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } const METADATA_ACCOUNT = 'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'; const METADATA_REPLACE = new RegExp('\u0000', 'g'); const getMetaDataPDA = async ({ metaDataPublicKey, mintPublicKey }) => { let seed = [ Buffer.from('metadata'), metaDataPublicKey.toBuffer(), mintPublicKey.toBuffer() ]; return (await PublicKey.findProgramAddress(seed, metaDataPublicKey))[0] }; const getMetaData = async ({ blockchain, address })=> { let mintPublicKey = new PublicKey(address); let metaDataPublicKey = new PublicKey(METADATA_ACCOUNT); let tokenMetaDataPublicKey = await getMetaDataPDA({ metaDataPublicKey, mintPublicKey }); let metaData = await request({ blockchain, address: tokenMetaDataPublicKey.toString(), api: METADATA_LAYOUT, cache: 86400000, // 1 day }); return { name: _optionalChain$3([metaData, 'optionalAccess', _ => _.data, 'optionalAccess', _2 => _2.name, 'optionalAccess', _3 => _3.replace, 'call', _4 => _4(METADATA_REPLACE, '')]), symbol: _optionalChain$3([metaData, 'optionalAccess', _5 => _5.data, 'optionalAccess', _6 => _6.symbol, 'optionalAccess', _7 => _7.replace, 'call', _8 => _8(METADATA_REPLACE, '')]) } }; function _optionalChain$2(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var nameOnSolana = async ({ blockchain, address })=>{ let metaData = await getMetaData({ blockchain, address }); return _optionalChain$2([metaData, 'optionalAccess', _ => _.name]) }; function _optionalChain$1(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var symbolOnSolana = async ({ blockchain, address })=>{ let metaData = await getMetaData({ blockchain, address }); return _optionalChain$1([metaData, 'optionalAccess', _ => _.symbol]) }; let supported = ['ethereum', 'bsc', 'polygon', 'solana', 'fantom', 'arbitrum', 'avalanche', 'gnosis', 'optimism', 'base', 'worldchain']; supported.evm = ['ethereum', 'bsc', 'polygon', 'fantom', 'arbitrum', 'avalanche', 'gnosis', 'optimism', 'base', 'worldchain']; supported.svm = ['solana']; function _optionalChain$5(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } class Token { constructor({ blockchain, address, name, decimals, symbol }) { this.blockchain = blockchain; if(supported.evm.includes(this.blockchain)) { this.address = ethers.utils.getAddress(address); } else if(supported.svm.includes(this.blockchain)) { this.address = address; } this._name = name; this._decimals = decimals; this._symbol = symbol; } async decimals() { if(this._decimals) { return this._decimals } if (this.address == Blockchains.findByName(this.blockchain).currency.address) { this._decimals = Blockchains.findByName(this.blockchain).currency.decimals; return this._decimals } let decimals; try { if(supported.evm.includes(this.blockchain)) { decimals = await decimalsOnEVM({ blockchain: this.blockchain, address: this.address, api: Token[this.blockchain].DEFAULT }); } else if(supported.svm.includes(this.blockchain)) { decimals = await decimalsOnSolana({ blockchain: this.blockchain, address: this.address }); } } catch (e) {} this._decimals = decimals; return decimals } async symbol() { if(this._symbol) { return this._symbol } if (this.address == Blockchains.findByName(this.blockchain).currency.address) { this._symbol = Blockchains.findByName(this.blockchain).currency.symbol; return this._symbol } let symbol; if(supported.evm.includes(this.blockchain)) { symbol = await symbolOnEVM({ blockchain: this.blockchain, address: this.address, api: Token[this.blockchain].DEFAULT }); } else if(supported.svm.includes(this.blockchain)) { symbol = await symbolOnSolana({ blockchain: this.blockchain, address: this.address }); } this._symbol = symbol; return symbol } async name(args) { if(this._name) { return this._name } if (this.address == Blockchains.findByName(this.blockchain).currency.address) { this._name = Blockchains.findByName(this.blockchain).currency.name; return this._name } let name; if(supported.evm.includes(this.blockchain)) { name = await nameOnEVM({ blockchain: this.blockchain, address: this.address, api: Token[this.blockchain].DEFAULT, id: _optionalChain$5([args, 'optionalAccess', _ => _.id]) }); } else if(supported.svm.includes(this.blockchain)) { name = await nameOnSolana({ blockchain: this.blockchain, address: this.address }); } this._name = name; return name } async balance(account, id) { if(supported.evm.includes(this.blockchain)) { return await balanceOnEVM({ blockchain: this.blockchain, account, address: this.address, api: id ? Token[this.blockchain][1155] : Token[this.blockchain].DEFAULT, id }) } else if(supported.svm.includes(this.blockchain)) { return await balanceOnSolana({ blockchain: this.blockchain, account, address: this.address, api: Token[this.blockchain].DEFAULT }) } } async allowance(owner,