@avalabs/bridge-unified
Advanced tools
Comparing version 0.0.0-feat-unified-api-interface-20240105201043 to 0.0.0-feat-unified-api-interface-20240108105628
# @avalabs/unified-bridge | ||
## 0.0.0-feat-unified-api-interface-20240105201043 | ||
## 0.0.0-feat-unified-api-interface-20240108105628 | ||
@@ -5,0 +5,0 @@ ### Major Changes |
@@ -40,2 +40,14 @@ import { Address } from 'viem'; | ||
type RequestArguments = { | ||
method: string; | ||
params?: unknown[] | Record<string | number, unknown>; | ||
}; | ||
type Provider = { | ||
/** | ||
* EIP-1193 compatible request method | ||
* https://eips.ethereum.org/EIPS/eip-1193#request-1 | ||
*/ | ||
request: (args: RequestArguments) => Promise<unknown>; | ||
}; | ||
type Hex = `0x${string}`; | ||
@@ -45,6 +57,6 @@ type TransactionRequest = { | ||
data?: Hex | null; | ||
from: Address; | ||
from: Hex; | ||
gas?: bigint; | ||
nonce?: number; | ||
to?: Address | null; | ||
to?: Hex | null; | ||
value?: bigint; | ||
@@ -56,14 +68,4 @@ gasPrice?: bigint | null; | ||
}; | ||
type RequestArguments = { | ||
method: string; | ||
params?: unknown[] | Record<string | number, unknown>; | ||
}; | ||
type Provider = { | ||
/** | ||
* EIP-1193 compatible request method | ||
* https://eips.ethereum.org/EIPS/eip-1193#request-1 | ||
*/ | ||
request: (args: RequestArguments) => Promise<unknown>; | ||
sign?: (data: TransactionRequest) => Promise<Hex>; | ||
}; | ||
type Dispatch = (signedTxHash: Hex) => Promise<Hex>; | ||
type Signer = (data: TransactionRequest, dispatch: Dispatch) => Promise<Hex>; | ||
@@ -134,2 +136,3 @@ declare enum ErrorCode { | ||
targetProvider?: Provider; | ||
sign?: Signer; | ||
}; | ||
@@ -190,2 +193,2 @@ type TrackingParams = { | ||
export { Asset, AssetFeeMap, BridgeAsset, BridgeConfig, BridgeService, BridgeServiceConfig, BridgeServiceFactory, BridgeTransfer, BridgeType, Chain, ChainAssetMap, DestinationInfo, Environment, ErrorCode, ErrorReason, FeeParams, Hex, Provider, TokenType, TrackingParams, TransactionRequest, TransferParams, createUnifiedBridgeService }; | ||
export { Asset, AssetFeeMap, BridgeAsset, BridgeConfig, BridgeService, BridgeServiceConfig, BridgeServiceFactory, BridgeTransfer, BridgeType, Chain, ChainAssetMap, DestinationInfo, Dispatch, Environment, ErrorCode, ErrorReason, FeeParams, Hex, Provider, Signer, TokenType, TrackingParams, TransactionRequest, TransferParams, createUnifiedBridgeService }; |
import { custom, http, createWalletClient, publicActions, isAddress, encodeFunctionData, decodeEventLog } from 'viem'; | ||
import { mergeWith, isArray } from 'lodash'; | ||
var U=(r=>(r.NATIVE="native",r.ERC20="erc20",r))(U||{});var R=(t=>(t.CCTP="cctp",t))(R||{});var H=(r=>(r.PROD="production",r.TEST="test",r))(H||{});var k=(n=>(n[n.BRIDGE_NOT_AVAILABLE=5001]="BRIDGE_NOT_AVAILABLE",n[n.INITIALIZATION_FAILED=5002]="INITIALIZATION_FAILED",n[n.INVALID_PARAMS=5003]="INVALID_PARAMS",n[n.TIMEOUT=5004]="TIMEOUT",n[n.TRANSACTION_REVERTED=5005]="TRANSACTION_REVERTED",n))(k||{}),b=(i=>(i.UNKNOWN="UNKNOWN",i.CONFIG_NOT_AVAILABLE="CONFIG_NOT_AVAILABLE",i.INVALID_PARAMS="INVALID_PARAMS",i.IDENTICAL_CHAINS_PROVIDED="IDENTICAL_CHAINS_PROVIDED",i.INCORRECT_AMOUNT_PROVIDED="INCORRECT_AMOUNT_PROVIDED",i.INCORRECT_ADDRESS_PROVIDED="INCORRECT_ADDRESS_PROVIDED",i.CHAIN_NOT_SUPPORTED="CHAIN_NOT_SUPPORTED",i.ASSET_NOT_SUPPORTED="ASSET_NOT_SUPPORTED",i.CONFIRMATION_COUNT_UNKNOWN="CONFIRMATION_COUNT_UNKNOWN",i))(b||{});var v=class extends Error{constructor(r,s,p){super(r);this.code=s;this.details=p;}};var P=class extends v{constructor(t="UNKNOWN",r){super(t,5001,r),this.name="BridgeUnavailableError";}};var B=class extends v{constructor(t="UNKNOWN",r){super(t,5002,r),this.name="BridgeInitializationError";}};var f=class extends v{constructor(t="INVALID_PARAMS",r){super(t,5003,r),this.name="InvalidParamsError";}};var re={test:"https://raw.githubusercontent.com/ava-labs/avalanche-bridge-resources/main/cctp/cctp_config.test.json",production:"https://raw.githubusercontent.com/ava-labs/avalanche-bridge-resources/main/cctp/cctp_config.json"},V=async e=>{try{return (await(await fetch(re[e])).json()).map(s=>({...s,chainId:`eip155:${s.chainId}`}))}catch(t){throw new B("CONFIG_NOT_AVAILABLE",`Error while fetching CCTP config: ${t.message}`)}},O=e=>{switch(e){case"eip155:43114":case"eip155:43113":return 1e3;default:return 2e4}};async function K(e){await e.ensureHasConfig();let t=e.config.map(r=>r.chainId);return e.config.reduce((r,s)=>(r[s.chainId]=s.tokens.map(p=>({...p,type:"erc20",destinations:t.reduce((n,o)=>(s.chainId!==o&&(n[o]||(n[o]=[]),n[o]?.push("cctp")),n),{})})),r),{})}var ae="^[-a-z0-9]{3,8}$",se="^[-_a-zA-Z0-9]{1,50}$",$=":",ie=e=>{let[t,r]=e.split($);if(!t||!r)throw new Error("Invalid identifier provided.");if(!new RegExp(ae).test(t))throw new Error("Invalid namespace provided.");if(!new RegExp(se).test(r))throw new Error("Invalid reference provided.");return {namespace:t,reference:r}},oe=({namespace:e,reference:t})=>`${e}${$}${t}`,G={toJSON:ie,toString:oe};var me=e=>{let{reference:t}=G.toJSON(e.chainId);return {id:Number(t),name:e.chainName,nativeCurrency:{decimals:e.networkToken.decimals,symbol:e.networkToken.symbol,name:e.networkToken.name},network:e.chainName,rpcUrls:{default:{http:[e.rpcUrl]},public:{http:[e.rpcUrl]}},...e.utilityAddresses?.multicall&&{contracts:{multicall3:{address:e.utilityAddresses.multicall}}}}},h=({chain:e,provider:t})=>{let r=me(e),s=t?custom(t):http(e.rpcUrl,{batch:!0,retryCount:0});return createWalletClient({chain:r,transport:s}).extend(publicActions)};var I=[{inputs:[{internalType:"address",name:"circleTokenMessenger_",type:"address"},{internalType:"address",name:"burnToken_",type:"address"}],stateMutability:"nonpayable",type:"constructor"},{inputs:[],name:"AlreadyAdmin",type:"error"},{inputs:[],name:"AlreadyFeeCollector",type:"error"},{inputs:[],name:"AlreadySupportedBurnToken",type:"error"},{inputs:[],name:"AmountLessThanFee",type:"error"},{inputs:[],name:"BalanceNotIncreased",type:"error"},{inputs:[],name:"CannotRemoveLastAdmin",type:"error"},{inputs:[],name:"FeePercentageGreaterThanMax",type:"error"},{inputs:[],name:"InvalidAdminAddress",type:"error"},{inputs:[],name:"InvalidFeeCollector",type:"error"},{inputs:[],name:"InvalidMintRecipientAddress",type:"error"},{inputs:[],name:"InvalidTokenAddress",type:"error"},{inputs:[],name:"InvalidTokenMessengerAddress",type:"error"},{inputs:[],name:"MaxFeeLessThanMinFee",type:"error"},{inputs:[],name:"NotAdmin",type:"error"},{inputs:[],name:"NotFeeCollector",type:"error"},{inputs:[],name:"UnSupportedBurnToken",type:"error"},{inputs:[],name:"UnsupportedDomain",type:"error"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"admin",type:"address"}],name:"AdminAdded",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"admin",type:"address"}],name:"AdminRemoved",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"token",type:"address"}],name:"BurnTokenAdded",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"token",type:"address"}],name:"BurnTokenRemoved",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"feeCollector",type:"address"}],name:"FeeCollectorAdded",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"feeCollector",type:"address"}],name:"FeeCollectorRemoved",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint32",name:"domain",type:"uint32"},{components:[{internalType:"uint256",name:"maxFee",type:"uint256"},{internalType:"uint256",name:"minFee",type:"uint256"},{internalType:"uint32",name:"feePercentage",type:"uint32"},{internalType:"uint256",name:"txnFee",type:"uint256"},{internalType:"bool",name:"supported",type:"bool"}],indexed:!1,internalType:"struct FeeCalculator.FeeConfiguration",name:"feeConfiguration",type:"tuple"}],name:"FeeConfigurationUpdated",type:"event"},{anonymous:!1,inputs:[{indexed:!0,internalType:"address",name:"previousOwner",type:"address"},{indexed:!0,internalType:"address",name:"newOwner",type:"address"}],name:"OwnershipTransferred",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"account",type:"address"}],name:"Paused",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint64",name:"nonce",type:"uint64"},{indexed:!1,internalType:"address",name:"burnToken",type:"address"},{indexed:!1,internalType:"uint256",name:"amount",type:"uint256"},{indexed:!1,internalType:"address",name:"depositor",type:"address"},{indexed:!1,internalType:"address",name:"mintRecipient",type:"address"},{indexed:!1,internalType:"uint32",name:"destinationDomain",type:"uint32"},{indexed:!1,internalType:"uint256",name:"totalFee",type:"uint256"}],name:"TransferTokens",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"account",type:"address"}],name:"Unpaused",type:"event"},{inputs:[{internalType:"address",name:"account",type:"address"}],name:"addAdmin",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"feeCollector",type:"address"}],name:"addFeeCollector",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"token",type:"address"}],name:"addSupportedBurnToken",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint32",name:"destinationDomain",type:"uint32"}],name:"calculateFee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"circleTokenMessenger",outputs:[{internalType:"contract ICircleTokenMessenger",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"circleTokenMessengerAddress",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"token",type:"address"}],name:"collectFees",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"token",type:"address"}],name:"getFeeAmounts",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"getFeeConfiguration",outputs:[{components:[{internalType:"uint256",name:"maxFee",type:"uint256"},{internalType:"uint256",name:"minFee",type:"uint256"},{internalType:"uint32",name:"feePercentage",type:"uint32"},{internalType:"uint256",name:"txnFee",type:"uint256"},{internalType:"bool",name:"supported",type:"bool"}],internalType:"struct FeeCalculator.FeeConfiguration",name:"",type:"tuple"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"getFeePercentage",outputs:[{internalType:"uint32",name:"",type:"uint32"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"getMaxFee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"getMinFee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"getTxnFee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"account",type:"address"}],name:"isAdmin",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"account",type:"address"}],name:"isFeeCollector",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"token",type:"address"}],name:"isSupportedBurnToken",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"isSupportedDomain",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[],name:"owner",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"pause",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"paused",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"account",type:"address"}],name:"removeAdmin",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"feeCollector",type:"address"}],name:"removeFeeCollector",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"token",type:"address"}],name:"removeSupportedBurnToken",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"renounceOwnership",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"},{components:[{internalType:"uint256",name:"maxFee",type:"uint256"},{internalType:"uint256",name:"minFee",type:"uint256"},{internalType:"uint32",name:"feePercentage",type:"uint32"},{internalType:"uint256",name:"txnFee",type:"uint256"},{internalType:"bool",name:"supported",type:"bool"}],internalType:"struct FeeCalculator.FeeConfiguration",name:"feeConfiguration",type:"tuple"}],name:"setFeeConfiguration",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"",type:"address"}],name:"supportedBurnTokens",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"newOwner",type:"address"}],name:"transferOwnership",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint32",name:"destinationDomain",type:"uint32"},{internalType:"address",name:"mintRecipient",type:"address"},{internalType:"address",name:"burnToken",type:"address"}],name:"transferTokens",outputs:[{internalType:"uint64",name:"nonce",type:"uint64"}],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"unpause",outputs:[],stateMutability:"nonpayable",type:"function"}];var E=({sourceChain:e,targetChain:t,amount:r,asset:s},p)=>{if(e.chainId===t.chainId)throw new f("IDENTICAL_CHAINS_PROVIDED");if(r<=0n)throw new f("INCORRECT_AMOUNT_PROVIDED","Amount must be greater than zero");let n=p.find(i=>i.chainId===e.chainId);if(!n)throw new f("CHAIN_NOT_SUPPORTED",`Not supported on source chain "${e.chainId}"`);let o=p.find(i=>i.chainId===t.chainId);if(!o)throw new f("CHAIN_NOT_SUPPORTED",`Not supported on target chain "${t.chainId}"`);let d=n.tokens.find(i=>i.symbol===s.symbol),y=o.tokens.find(i=>i.symbol===s.symbol);if(!d||!y)throw new f("ASSET_NOT_SUPPORTED");return {sourceChainData:n,targetChainData:o,burnToken:d,mintToken:y}};async function W(e,t){await e.ensureHasConfig();let{sourceChain:r,targetChain:s,asset:p,amount:n,provider:o}=t,{sourceChainData:d,targetChainData:y,burnToken:i}=E({sourceChain:r,targetChain:s,asset:p,amount:n},e.config),u=await h({chain:r,provider:o}).readContract({address:d.tokenRouterAddress,abi:I,functionName:"calculateFee",args:[n,y.domain]});return {[i.address]:u}}var _=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"totalSupply",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_from",type:"address"},{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transferFrom",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[],name:"symbol",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transfer",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"},{name:"_spender",type:"address"}],name:"allowance",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{payable:!0,stateMutability:"payable",type:"fallback"},{anonymous:!1,inputs:[{indexed:!0,name:"owner",type:"address"},{indexed:!0,name:"spender",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Approval",type:"event"},{anonymous:!1,inputs:[{indexed:!0,name:"from",type:"address"},{indexed:!0,name:"to",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Transfer",type:"event"}];var ye=async(e,t)=>{let{sourceChain:r,targetChain:s,asset:p,amount:n,fromAddress:o,toAddress:d,sourceProvider:y}=t,i=d??o;if(!isAddress(o)||!isAddress(i))throw new f("INCORRECT_ADDRESS_PROVIDED");let{sourceChainData:a,targetChainData:u,burnToken:m}=E({sourceChain:r,targetChain:s,asset:p,amount:n},e.config),c=h({chain:r,provider:y});if(await c.readContract({address:m.address,abi:_,functionName:"allowance",args:[o,a.tokenRouterAddress]})<n)if(y.sign){let l=encodeFunctionData({abi:_,functionName:"approve",args:[a.tokenRouterAddress,n]}),T=await y.sign({from:o,to:m.address,data:l}),A=await c.sendRawTransaction({serializedTransaction:T});await c.waitForTransactionReceipt({hash:A,pollingInterval:1e3});}else {let{request:l}=await c.simulateContract({account:o,address:m.address,abi:_,functionName:"approve",args:[a.tokenRouterAddress,n]}),T=await c.writeContract(l);await c.waitForTransactionReceipt({hash:T,pollingInterval:1e3});}if(y.sign){let l=encodeFunctionData({abi:I,functionName:"transferTokens",args:[n,u.domain,i,m.address]}),T=await y.sign({from:o,to:a.tokenRouterAddress,data:l});return c.sendRawTransaction({serializedTransaction:T})}else {let{request:l}=await c.simulateContract({account:o,address:a.tokenRouterAddress,abi:I,functionName:"transferTokens",args:[n,u.domain,i,m.address]});return c.writeContract(l)}},le=async e=>{try{return await e.getBlockNumber()}catch{return}};async function z(e,t,r){await e.ensureHasConfig();let{minimumConfirmations:s}=e.config.find(u=>u.chainId===t.sourceChain.chainId)??{},{minimumConfirmations:p}=e.config.find(u=>u.chainId===t.targetChain.chainId)??{};if(!s||!p)throw new f("CONFIRMATION_COUNT_UNKNOWN");let n=await e.getFees({...t,provider:t.sourceProvider}),o=(t.asset.address&&n[t.asset.address])??0n,d=await ye(e,t),y=Date.now(),i=h({chain:t.targetChain,provider:t.targetProvider}),a=await le(i);return {type:e.type,environment:r,fromAddress:t.fromAddress,toAddress:t.toAddress??t.fromAddress,amount:t.amount,symbol:t.asset.symbol,bridgeFee:o,sourceChain:t.sourceChain,sourceStartedAt:y,sourceTxHash:d,sourceConfirmationCount:0,requiredSourceConfirmationCount:s,targetChain:t.targetChain,targetConfirmationCount:0,requiredTargetConfirmationCount:p,startBlockNumber:a}}async function J(e){if(!e.config&&(await e.updateConfig(),!e.config))throw new B("CONFIG_NOT_AVAILABLE")}var D=(e,t)=>e.gasPrice&&BigInt(e.gasPrice*t.gasUsed);var S=async e=>new Promise(t=>{setTimeout(t,e);});var L=({promise:e,delay:t,startAfter:r})=>{let s=!1,p=!1,n=0,o,d,y=u=>{o&&s&&(s=!1,o(u));},i=()=>{p=!0,d&&s&&(s=!1,d("cancelled"));};return {result:new Promise((u,m)=>{s=!0,o=u,d=m;let c=async()=>{if(!(!s||p)){try{if(await e(y),!s||p)return;await S(t);}catch(g){console.error(g.message),n+=1,await S(2**n*t);}await c();}};r?setTimeout(c,r):c();}),cancel:i}};var Y=1e3*60*60*3,X=1024n,Q=5e3,C=(e,t,r)=>{Object.assign(e,Object.fromEntries(Object.entries(t).filter(([,s])=>s!==void 0))),r({...e});},fe=async(e,t)=>{let{sourceProvider:r,targetProvider:s,updateListener:p,bridgeTransfer:n}=t,o=h({chain:n.sourceChain,provider:r}),d=e.find(m=>m.chainId===n.sourceChain.chainId),y=h({chain:n.targetChain,provider:s}),i=e.find(m=>m.chainId===n.targetChain.chainId),a={...n};if(!d||!i)throw new f("CHAIN_NOT_SUPPORTED");return L({promise:async m=>{if(a.completedAt||a.metadata?.nonce)return m(a);if(a.sourceStartedAt+Y<=Date.now())return C(a,{completedAt:Date.now(),errorCode:5004},p),m(a);let c=await o.getTransactionReceipt({hash:a.sourceTxHash});if(!a.sourceNetworkFee){let w=await o.getTransaction({hash:a.sourceTxHash}),M=D(w,c);M&&C(a,{sourceNetworkFee:M},p);}if(c.status==="reverted")return C(a,{completedAt:Date.now(),errorCode:5005},p),m(a);let g=await o.getTransactionConfirmations({hash:a.sourceTxHash}),l=g>a.sourceConfirmationCount,T=g>=a.requiredSourceConfirmationCount;if(l){let w={};w.sourceConfirmationCount=Number(g),T||(w.startBlockNumber=await y.getBlockNumber()),C(a,w,p);}if(!T)return;a.startBlockNumber||C(a,{startBlockNumber:await y.getBlockNumber()},p);let A=c.logs.find(w=>w.address===d.tokenRouterAddress?decodeEventLog({abi:I,...w}).eventName==="TransferTokens":!1);if(!A)throw new f("INVALID_PARAMS",`unable to find a TransferTokens event in source transaction "${a.sourceTxHash}"`);let x=decodeEventLog({abi:I,eventName:"TransferTokens",...A}).args.nonce;return C(a,{targetStartedAt:Date.now(),metadata:{nonce:x}},p),m(a)},delay:O(d.chainId),startAfter:Q})},Te=async(e,t)=>{let{targetProvider:r,updateListener:s,bridgeTransfer:p}=t,n={...p};if(!p.completedAt&&!p.metadata?.nonce)throw new f("INVALID_PARAMS","nonce is missing");if(!p.startBlockNumber)throw new f("INVALID_PARAMS","startBlockNumber is missing");let o=h({chain:p.targetChain,provider:r}),d=e.find(i=>i.chainId===p.targetChain.chainId);if(!d)throw new f("INVALID_PARAMS",`unknown target chain "${p.targetChain.chainId}"`);return L({promise:async i=>{if(n.completedAt)return i(n);if(n.sourceStartedAt+Y<=Date.now())return C(n,{completedAt:Date.now(),errorCode:5004},s),i(n);if(!n.targetTxHash){let g=await o.getBlockNumber(),l=(n.startBlockNumber??0n)-X,T=l>=0n?l:"earliest",A=(n.startBlockNumber??0n)+X,N=A<g?A:"latest",x=await o.getLogs({address:d.messageTransmitterAddress,event:{name:"MessageReceived",type:"event",inputs:[{indexed:!0,internalType:"address",name:"caller",type:"address"},{indexed:!1,internalType:"uint32",name:"sourceDomain",type:"uint32"},{indexed:!0,internalType:"uint64",name:"nonce",type:"uint64"},{indexed:!1,internalType:"bytes32",name:"sender",type:"bytes32"},{indexed:!1,internalType:"bytes",name:"messageBody",type:"bytes"}]},args:{nonce:n.metadata.nonce},fromBlock:T,toBlock:N});if(x[0]?.transactionHash)C(n,{targetTxHash:x[0].transactionHash},s);else return}let a=await o.getTransactionReceipt({hash:n.targetTxHash});if(!n.targetNetworkFee){let g=await o.getTransaction({hash:n.targetTxHash}),l=D(g,a);l&&C(n,{targetNetworkFee:l},s);}if(a.status==="reverted")return C(n,{completedAt:Date.now(),errorCode:5005},s),i(n);let u=await o.getTransactionConfirmations({hash:n.targetTxHash}),m=u>n.targetConfirmationCount,c=u>=n.requiredTargetConfirmationCount;if(m&&C(n,{targetConfirmationCount:Number(u)},s),!!c)return C(n,{completedAt:Date.now()},s),i(n)},delay:O(d.chainId),startAfter:Q})};function ee(e,t){let r,s=()=>{r?.();};return {result:(async()=>{await e.ensureHasConfig();let{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:y}=t,{result:i,cancel:a}=await fe(e.config,{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:y});r=a;let u=await i,{result:m,cancel:c}=await Te(e.config,{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:u});return r=c,m})(),cancel:s}}var te=e=>({type:"cctp",config:null,updateConfig:async function(){this.config=await V(e);},ensureHasConfig:async function(){return J(this)},getAssets:async function(){return K(this)},getFees:async function(t){return W(this,t)},transferAsset:async function(t){return z(this,t,e)},trackTransfer:function(t){return ee(this,t)}});var ge=new Map([["cctp",te]]),ne=(e,t)=>new Map([...ge].filter(([r])=>!t?.includes(r)).map(([r,s])=>[r,s(e)])),F=(e,t,r)=>{let s=t.destinations[r]?.find(p=>e.has(p));if(!s)throw new P;return {type:s,bridge:e.get(s)}};var pn=({environment:e,disabledBridgeTypes:t})=>{let r=ne(e,t),s=async()=>{await Promise.allSettled(Array.from(r).map(([,a])=>a.updateConfig()));};return {environment:e,bridges:r,init:async()=>{await s();},updateConfigs:s,getAssets:async()=>(await Promise.all(Array.from(r).map(([,u])=>u.getAssets()))).reduce((u,m)=>{for(let[c,g]of Object.entries(m)){let l=u[c];if(l)for(let T of g){let A=l.findIndex(({symbol:N})=>N===T.symbol);A===-1?l.push(T):mergeWith(l[A],T,(N,x)=>{if(isArray(N))return [...new Set(N.concat(x))]});}else u[c]=g;}return u},{}),getFees:async a=>{let{bridge:u}=F(r,a.asset,a.targetChain.chainId);return u.getFees(a)},canTransferAsset:(a,u)=>{try{return F(r,a,u),!0}catch{return !1}},transferAsset:async a=>{let{bridge:u}=F(r,a.asset,a.targetChain.chainId);return u.transferAsset(a)},trackTransfer:a=>{let u=r.get(a.bridgeTransfer.type);if(!u)throw new P;return u.trackTransfer(a)}}}; | ||
var U=(r=>(r.NATIVE="native",r.ERC20="erc20",r))(U||{});var R=(t=>(t.CCTP="cctp",t))(R||{});var H=(r=>(r.PROD="production",r.TEST="test",r))(H||{});var k=(n=>(n[n.BRIDGE_NOT_AVAILABLE=5001]="BRIDGE_NOT_AVAILABLE",n[n.INITIALIZATION_FAILED=5002]="INITIALIZATION_FAILED",n[n.INVALID_PARAMS=5003]="INVALID_PARAMS",n[n.TIMEOUT=5004]="TIMEOUT",n[n.TRANSACTION_REVERTED=5005]="TRANSACTION_REVERTED",n))(k||{}),h=(s=>(s.UNKNOWN="UNKNOWN",s.CONFIG_NOT_AVAILABLE="CONFIG_NOT_AVAILABLE",s.INVALID_PARAMS="INVALID_PARAMS",s.IDENTICAL_CHAINS_PROVIDED="IDENTICAL_CHAINS_PROVIDED",s.INCORRECT_AMOUNT_PROVIDED="INCORRECT_AMOUNT_PROVIDED",s.INCORRECT_ADDRESS_PROVIDED="INCORRECT_ADDRESS_PROVIDED",s.CHAIN_NOT_SUPPORTED="CHAIN_NOT_SUPPORTED",s.ASSET_NOT_SUPPORTED="ASSET_NOT_SUPPORTED",s.CONFIRMATION_COUNT_UNKNOWN="CONFIRMATION_COUNT_UNKNOWN",s))(h||{});var I=class extends Error{constructor(r,i,u){super(r);this.code=i;this.details=u;}};var P=class extends I{constructor(t="UNKNOWN",r){super(t,5001,r),this.name="BridgeUnavailableError";}};var B=class extends I{constructor(t="UNKNOWN",r){super(t,5002,r),this.name="BridgeInitializationError";}};var f=class extends I{constructor(t="INVALID_PARAMS",r){super(t,5003,r),this.name="InvalidParamsError";}};var re={test:"https://raw.githubusercontent.com/ava-labs/avalanche-bridge-resources/main/cctp/cctp_config.test.json",production:"https://raw.githubusercontent.com/ava-labs/avalanche-bridge-resources/main/cctp/cctp_config.json"},V=async e=>{try{return (await(await fetch(re[e])).json()).map(i=>({...i,chainId:`eip155:${i.chainId}`}))}catch(t){throw new B("CONFIG_NOT_AVAILABLE",`Error while fetching CCTP config: ${t.message}`)}},O=e=>{switch(e){case"eip155:43114":case"eip155:43113":return 1e3;default:return 2e4}};async function K(e){await e.ensureHasConfig();let t=e.config.map(r=>r.chainId);return e.config.reduce((r,i)=>(r[i.chainId]=i.tokens.map(u=>({...u,type:"erc20",destinations:t.reduce((n,o)=>(i.chainId!==o&&(n[o]||(n[o]=[]),n[o]?.push("cctp")),n),{})})),r),{})}var ae="^[-a-z0-9]{3,8}$",ie="^[-_a-zA-Z0-9]{1,50}$",$=":",se=e=>{let[t,r]=e.split($);if(!t||!r)throw new Error("Invalid identifier provided.");if(!new RegExp(ae).test(t))throw new Error("Invalid namespace provided.");if(!new RegExp(ie).test(r))throw new Error("Invalid reference provided.");return {namespace:t,reference:r}},oe=({namespace:e,reference:t})=>`${e}${$}${t}`,G={toJSON:se,toString:oe};var me=e=>{let{reference:t}=G.toJSON(e.chainId);return {id:Number(t),name:e.chainName,nativeCurrency:{decimals:e.networkToken.decimals,symbol:e.networkToken.symbol,name:e.networkToken.name},network:e.chainName,rpcUrls:{default:{http:[e.rpcUrl]},public:{http:[e.rpcUrl]}},...e.utilityAddresses?.multicall&&{contracts:{multicall3:{address:e.utilityAddresses.multicall}}}}},v=({chain:e,provider:t})=>{let r=me(e),i=t?custom(t):http(e.rpcUrl,{batch:!0,retryCount:0});return createWalletClient({chain:r,transport:i}).extend(publicActions)};var N=[{inputs:[{internalType:"address",name:"circleTokenMessenger_",type:"address"},{internalType:"address",name:"burnToken_",type:"address"}],stateMutability:"nonpayable",type:"constructor"},{inputs:[],name:"AlreadyAdmin",type:"error"},{inputs:[],name:"AlreadyFeeCollector",type:"error"},{inputs:[],name:"AlreadySupportedBurnToken",type:"error"},{inputs:[],name:"AmountLessThanFee",type:"error"},{inputs:[],name:"BalanceNotIncreased",type:"error"},{inputs:[],name:"CannotRemoveLastAdmin",type:"error"},{inputs:[],name:"FeePercentageGreaterThanMax",type:"error"},{inputs:[],name:"InvalidAdminAddress",type:"error"},{inputs:[],name:"InvalidFeeCollector",type:"error"},{inputs:[],name:"InvalidMintRecipientAddress",type:"error"},{inputs:[],name:"InvalidTokenAddress",type:"error"},{inputs:[],name:"InvalidTokenMessengerAddress",type:"error"},{inputs:[],name:"MaxFeeLessThanMinFee",type:"error"},{inputs:[],name:"NotAdmin",type:"error"},{inputs:[],name:"NotFeeCollector",type:"error"},{inputs:[],name:"UnSupportedBurnToken",type:"error"},{inputs:[],name:"UnsupportedDomain",type:"error"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"admin",type:"address"}],name:"AdminAdded",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"admin",type:"address"}],name:"AdminRemoved",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"token",type:"address"}],name:"BurnTokenAdded",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"token",type:"address"}],name:"BurnTokenRemoved",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"feeCollector",type:"address"}],name:"FeeCollectorAdded",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"feeCollector",type:"address"}],name:"FeeCollectorRemoved",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint32",name:"domain",type:"uint32"},{components:[{internalType:"uint256",name:"maxFee",type:"uint256"},{internalType:"uint256",name:"minFee",type:"uint256"},{internalType:"uint32",name:"feePercentage",type:"uint32"},{internalType:"uint256",name:"txnFee",type:"uint256"},{internalType:"bool",name:"supported",type:"bool"}],indexed:!1,internalType:"struct FeeCalculator.FeeConfiguration",name:"feeConfiguration",type:"tuple"}],name:"FeeConfigurationUpdated",type:"event"},{anonymous:!1,inputs:[{indexed:!0,internalType:"address",name:"previousOwner",type:"address"},{indexed:!0,internalType:"address",name:"newOwner",type:"address"}],name:"OwnershipTransferred",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"account",type:"address"}],name:"Paused",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint64",name:"nonce",type:"uint64"},{indexed:!1,internalType:"address",name:"burnToken",type:"address"},{indexed:!1,internalType:"uint256",name:"amount",type:"uint256"},{indexed:!1,internalType:"address",name:"depositor",type:"address"},{indexed:!1,internalType:"address",name:"mintRecipient",type:"address"},{indexed:!1,internalType:"uint32",name:"destinationDomain",type:"uint32"},{indexed:!1,internalType:"uint256",name:"totalFee",type:"uint256"}],name:"TransferTokens",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"address",name:"account",type:"address"}],name:"Unpaused",type:"event"},{inputs:[{internalType:"address",name:"account",type:"address"}],name:"addAdmin",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"feeCollector",type:"address"}],name:"addFeeCollector",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"token",type:"address"}],name:"addSupportedBurnToken",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint32",name:"destinationDomain",type:"uint32"}],name:"calculateFee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"circleTokenMessenger",outputs:[{internalType:"contract ICircleTokenMessenger",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"circleTokenMessengerAddress",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"token",type:"address"}],name:"collectFees",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"token",type:"address"}],name:"getFeeAmounts",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"getFeeConfiguration",outputs:[{components:[{internalType:"uint256",name:"maxFee",type:"uint256"},{internalType:"uint256",name:"minFee",type:"uint256"},{internalType:"uint32",name:"feePercentage",type:"uint32"},{internalType:"uint256",name:"txnFee",type:"uint256"},{internalType:"bool",name:"supported",type:"bool"}],internalType:"struct FeeCalculator.FeeConfiguration",name:"",type:"tuple"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"getFeePercentage",outputs:[{internalType:"uint32",name:"",type:"uint32"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"getMaxFee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"getMinFee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"getTxnFee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"account",type:"address"}],name:"isAdmin",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"account",type:"address"}],name:"isFeeCollector",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"token",type:"address"}],name:"isSupportedBurnToken",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"}],name:"isSupportedDomain",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[],name:"owner",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"pause",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"paused",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"account",type:"address"}],name:"removeAdmin",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"feeCollector",type:"address"}],name:"removeFeeCollector",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"token",type:"address"}],name:"removeSupportedBurnToken",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"renounceOwnership",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint32",name:"domain",type:"uint32"},{components:[{internalType:"uint256",name:"maxFee",type:"uint256"},{internalType:"uint256",name:"minFee",type:"uint256"},{internalType:"uint32",name:"feePercentage",type:"uint32"},{internalType:"uint256",name:"txnFee",type:"uint256"},{internalType:"bool",name:"supported",type:"bool"}],internalType:"struct FeeCalculator.FeeConfiguration",name:"feeConfiguration",type:"tuple"}],name:"setFeeConfiguration",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"",type:"address"}],name:"supportedBurnTokens",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"newOwner",type:"address"}],name:"transferOwnership",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"uint32",name:"destinationDomain",type:"uint32"},{internalType:"address",name:"mintRecipient",type:"address"},{internalType:"address",name:"burnToken",type:"address"}],name:"transferTokens",outputs:[{internalType:"uint64",name:"nonce",type:"uint64"}],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"unpause",outputs:[],stateMutability:"nonpayable",type:"function"}];var E=({sourceChain:e,targetChain:t,amount:r,asset:i},u)=>{if(e.chainId===t.chainId)throw new f("IDENTICAL_CHAINS_PROVIDED");if(r<=0n)throw new f("INCORRECT_AMOUNT_PROVIDED","Amount must be greater than zero");let n=u.find(s=>s.chainId===e.chainId);if(!n)throw new f("CHAIN_NOT_SUPPORTED",`Not supported on source chain "${e.chainId}"`);let o=u.find(s=>s.chainId===t.chainId);if(!o)throw new f("CHAIN_NOT_SUPPORTED",`Not supported on target chain "${t.chainId}"`);let d=n.tokens.find(s=>s.symbol===i.symbol),l=o.tokens.find(s=>s.symbol===i.symbol);if(!d||!l)throw new f("ASSET_NOT_SUPPORTED");return {sourceChainData:n,targetChainData:o,burnToken:d,mintToken:l}};async function W(e,t){await e.ensureHasConfig();let{sourceChain:r,targetChain:i,asset:u,amount:n,provider:o}=t,{sourceChainData:d,targetChainData:l,burnToken:s}=E({sourceChain:r,targetChain:i,asset:u,amount:n},e.config),p=await v({chain:r,provider:o}).readContract({address:d.tokenRouterAddress,abi:N,functionName:"calculateFee",args:[n,l.domain]});return {[s.address]:p}}var _=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"totalSupply",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_from",type:"address"},{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transferFrom",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[],name:"symbol",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transfer",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"},{name:"_spender",type:"address"}],name:"allowance",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{payable:!0,stateMutability:"payable",type:"fallback"},{anonymous:!1,inputs:[{indexed:!0,name:"owner",type:"address"},{indexed:!0,name:"spender",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Approval",type:"event"},{anonymous:!1,inputs:[{indexed:!0,name:"from",type:"address"},{indexed:!0,name:"to",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Transfer",type:"event"}];var ye=async(e,t)=>{let{sourceChain:r,targetChain:i,asset:u,amount:n,fromAddress:o,toAddress:d,sourceProvider:l,sign:s}=t,a=d??o;if(!isAddress(o)||!isAddress(a))throw new f("INCORRECT_ADDRESS_PROVIDED");let{sourceChainData:p,targetChainData:y,burnToken:m}=E({sourceChain:r,targetChain:i,asset:u,amount:n},e.config),c=v({chain:r,provider:l});if(await c.readContract({address:m.address,abi:_,functionName:"allowance",args:[o,p.tokenRouterAddress]})<n)if(s){let T=encodeFunctionData({abi:_,functionName:"approve",args:[p.tokenRouterAddress,n]}),g=await s({from:o,to:m.address,data:T},b=>c.sendRawTransaction({serializedTransaction:b}));await c.waitForTransactionReceipt({hash:g,pollingInterval:1e3});}else {let{request:T}=await c.simulateContract({account:o,address:m.address,abi:_,functionName:"approve",args:[p.tokenRouterAddress,n]}),g=await c.writeContract(T);await c.waitForTransactionReceipt({hash:g,pollingInterval:1e3});}if(s){let T=encodeFunctionData({abi:N,functionName:"transferTokens",args:[n,y.domain,a,m.address]});return s({from:o,to:p.tokenRouterAddress,data:T},g=>c.sendRawTransaction({serializedTransaction:g}))}else {let{request:T}=await c.simulateContract({account:o,address:p.tokenRouterAddress,abi:N,functionName:"transferTokens",args:[n,y.domain,a,m.address]});return c.writeContract(T)}},le=async e=>{try{return await e.getBlockNumber()}catch{return}};async function z(e,t,r){await e.ensureHasConfig();let{minimumConfirmations:i}=e.config.find(p=>p.chainId===t.sourceChain.chainId)??{},{minimumConfirmations:u}=e.config.find(p=>p.chainId===t.targetChain.chainId)??{};if(!i||!u)throw new f("CONFIRMATION_COUNT_UNKNOWN");let n=await e.getFees({...t,provider:t.sourceProvider}),o=(t.asset.address&&n[t.asset.address])??0n,d=await ye(e,t),l=Date.now(),s=v({chain:t.targetChain,provider:t.targetProvider}),a=await le(s);return {type:e.type,environment:r,fromAddress:t.fromAddress,toAddress:t.toAddress??t.fromAddress,amount:t.amount,symbol:t.asset.symbol,bridgeFee:o,sourceChain:t.sourceChain,sourceStartedAt:l,sourceTxHash:d,sourceConfirmationCount:0,requiredSourceConfirmationCount:i,targetChain:t.targetChain,targetConfirmationCount:0,requiredTargetConfirmationCount:u,startBlockNumber:a}}async function J(e){if(!e.config&&(await e.updateConfig(),!e.config))throw new B("CONFIG_NOT_AVAILABLE")}var D=(e,t)=>e.gasPrice&&BigInt(e.gasPrice*t.gasUsed);var S=async e=>new Promise(t=>{setTimeout(t,e);});var L=({promise:e,delay:t,startAfter:r})=>{let i=!1,u=!1,n=0,o,d,l=p=>{o&&i&&(i=!1,o(p));},s=()=>{u=!0,d&&i&&(i=!1,d("cancelled"));};return {result:new Promise((p,y)=>{i=!0,o=p,d=y;let m=async()=>{if(!(!i||u)){try{if(await e(l),!i||u)return;await S(t);}catch(c){console.error(c.message),n+=1,await S(2**n*t);}await m();}};r?setTimeout(m,r):m();}),cancel:s}};var Y=1e3*60*60*3,X=1024n,Q=5e3,A=(e,t,r)=>{Object.assign(e,Object.fromEntries(Object.entries(t).filter(([,i])=>i!==void 0))),r({...e});},fe=async(e,t)=>{let{sourceProvider:r,targetProvider:i,updateListener:u,bridgeTransfer:n}=t,o=v({chain:n.sourceChain,provider:r}),d=e.find(y=>y.chainId===n.sourceChain.chainId),l=v({chain:n.targetChain,provider:i}),s=e.find(y=>y.chainId===n.targetChain.chainId),a={...n};if(!d||!s)throw new f("CHAIN_NOT_SUPPORTED");return L({promise:async y=>{if(a.completedAt||a.metadata?.nonce)return y(a);if(a.sourceStartedAt+Y<=Date.now())return A(a,{completedAt:Date.now(),errorCode:5004},u),y(a);let m=await o.getTransactionReceipt({hash:a.sourceTxHash});if(!a.sourceNetworkFee){let w=await o.getTransaction({hash:a.sourceTxHash}),M=D(w,m);M&&A(a,{sourceNetworkFee:M},u);}if(m.status==="reverted")return A(a,{completedAt:Date.now(),errorCode:5005},u),y(a);let c=await o.getTransactionConfirmations({hash:a.sourceTxHash}),C=c>a.sourceConfirmationCount,T=c>=a.requiredSourceConfirmationCount;if(C){let w={};w.sourceConfirmationCount=Number(c),T||(w.startBlockNumber=await l.getBlockNumber()),A(a,w,u);}if(!T)return;a.startBlockNumber||A(a,{startBlockNumber:await l.getBlockNumber()},u);let g=m.logs.find(w=>w.address===d.tokenRouterAddress?decodeEventLog({abi:N,...w}).eventName==="TransferTokens":!1);if(!g)throw new f("INVALID_PARAMS",`unable to find a TransferTokens event in source transaction "${a.sourceTxHash}"`);let x=decodeEventLog({abi:N,eventName:"TransferTokens",...g}).args.nonce;return A(a,{targetStartedAt:Date.now(),metadata:{nonce:x}},u),y(a)},delay:O(d.chainId),startAfter:Q})},Te=async(e,t)=>{let{targetProvider:r,updateListener:i,bridgeTransfer:u}=t,n={...u};if(!u.completedAt&&!u.metadata?.nonce)throw new f("INVALID_PARAMS","nonce is missing");if(!u.startBlockNumber)throw new f("INVALID_PARAMS","startBlockNumber is missing");let o=v({chain:u.targetChain,provider:r}),d=e.find(s=>s.chainId===u.targetChain.chainId);if(!d)throw new f("INVALID_PARAMS",`unknown target chain "${u.targetChain.chainId}"`);return L({promise:async s=>{if(n.completedAt)return s(n);if(n.sourceStartedAt+Y<=Date.now())return A(n,{completedAt:Date.now(),errorCode:5004},i),s(n);if(!n.targetTxHash){let c=await o.getBlockNumber(),C=(n.startBlockNumber??0n)-X,T=C>=0n?C:"earliest",g=(n.startBlockNumber??0n)+X,b=g<c?g:"latest",x=await o.getLogs({address:d.messageTransmitterAddress,event:{name:"MessageReceived",type:"event",inputs:[{indexed:!0,internalType:"address",name:"caller",type:"address"},{indexed:!1,internalType:"uint32",name:"sourceDomain",type:"uint32"},{indexed:!0,internalType:"uint64",name:"nonce",type:"uint64"},{indexed:!1,internalType:"bytes32",name:"sender",type:"bytes32"},{indexed:!1,internalType:"bytes",name:"messageBody",type:"bytes"}]},args:{nonce:n.metadata.nonce},fromBlock:T,toBlock:b});if(x[0]?.transactionHash)A(n,{targetTxHash:x[0].transactionHash},i);else return}let a=await o.getTransactionReceipt({hash:n.targetTxHash});if(!n.targetNetworkFee){let c=await o.getTransaction({hash:n.targetTxHash}),C=D(c,a);C&&A(n,{targetNetworkFee:C},i);}if(a.status==="reverted")return A(n,{completedAt:Date.now(),errorCode:5005},i),s(n);let p=await o.getTransactionConfirmations({hash:n.targetTxHash}),y=p>n.targetConfirmationCount,m=p>=n.requiredTargetConfirmationCount;if(y&&A(n,{targetConfirmationCount:Number(p)},i),!!m)return A(n,{completedAt:Date.now()},i),s(n)},delay:O(d.chainId),startAfter:Q})};function ee(e,t){let r,i=()=>{r?.();};return {result:(async()=>{await e.ensureHasConfig();let{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:l}=t,{result:s,cancel:a}=await fe(e.config,{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:l});r=a;let p=await s,{result:y,cancel:m}=await Te(e.config,{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:p});return r=m,y})(),cancel:i}}var te=e=>({type:"cctp",config:null,updateConfig:async function(){this.config=await V(e);},ensureHasConfig:async function(){return J(this)},getAssets:async function(){return K(this)},getFees:async function(t){return W(this,t)},transferAsset:async function(t){return z(this,t,e)},trackTransfer:function(t){return ee(this,t)}});var ge=new Map([["cctp",te]]),ne=(e,t)=>new Map([...ge].filter(([r])=>!t?.includes(r)).map(([r,i])=>[r,i(e)])),F=(e,t,r)=>{let i=t.destinations[r]?.find(u=>e.has(u));if(!i)throw new P;return {type:i,bridge:e.get(i)}};var un=({environment:e,disabledBridgeTypes:t})=>{let r=ne(e,t),i=async()=>{await Promise.allSettled(Array.from(r).map(([,a])=>a.updateConfig()));};return {environment:e,bridges:r,init:async()=>{await i();},updateConfigs:i,getAssets:async()=>(await Promise.all(Array.from(r).map(([,p])=>p.getAssets()))).reduce((p,y)=>{for(let[m,c]of Object.entries(y)){let C=p[m];if(C)for(let T of c){let g=C.findIndex(({symbol:b})=>b===T.symbol);g===-1?C.push(T):mergeWith(C[g],T,(b,x)=>{if(isArray(b))return [...new Set(b.concat(x))]});}else p[m]=c;}return p},{}),getFees:async a=>{let{bridge:p}=F(r,a.asset,a.targetChain.chainId);return p.getFees(a)},canTransferAsset:(a,p)=>{try{return F(r,a,p),!0}catch{return !1}},transferAsset:async a=>{let{bridge:p}=F(r,a.asset,a.targetChain.chainId);return p.transferAsset(a)},trackTransfer:a=>{let p=r.get(a.bridgeTransfer.type);if(!p)throw new P;return p.trackTransfer(a)}}}; | ||
export { R as BridgeType, H as Environment, k as ErrorCode, b as ErrorReason, U as TokenType, pn as createUnifiedBridgeService }; | ||
export { R as BridgeType, H as Environment, k as ErrorCode, h as ErrorReason, U as TokenType, un as createUnifiedBridgeService }; | ||
//# sourceMappingURL=out.js.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@avalabs/bridge-unified", | ||
"version": "0.0.0-feat-unified-api-interface-20240105201043", | ||
"version": "0.0.0-feat-unified-api-interface-20240108105628", | ||
"main": "dist/index.js", | ||
@@ -18,3 +18,3 @@ "type": "module", | ||
"@internal/tsup-config": "0.0.1", | ||
"eslint-config-custom": "0.0.0-feat-unified-api-interface-20240105201043" | ||
"eslint-config-custom": "0.0.0-feat-unified-api-interface-20240108105628" | ||
}, | ||
@@ -21,0 +21,0 @@ "scripts": { |
import { encodeFunctionData, isAddress } from 'viem'; | ||
import { Environment, type BridgeService, type TransferParams, BridgeType, ErrorReason } from '../../../types'; | ||
import { | ||
Environment, | ||
type BridgeService, | ||
type TransferParams, | ||
BridgeType, | ||
ErrorReason, | ||
type Signer, | ||
} from '../../../types'; | ||
import { getClientForChain } from '../../../utils/client'; | ||
@@ -95,6 +102,5 @@ import { BRIDGE_ASSET } from '../__mocks__/asset.mock'; | ||
const providerWithSigner = { | ||
request: jest.fn(), | ||
sign: jest.fn().mockResolvedValueOnce(approvalSignedHex).mockResolvedValueOnce(bridgeSignedHex), | ||
}; | ||
const approvalSignMock: Signer = jest.fn((_, dispatch) => dispatch(approvalSignedHex)); | ||
const bridgeSignMock: Signer = jest.fn((_, dispatch) => dispatch(bridgeSignedHex)); | ||
const customSigner = jest.fn().mockImplementationOnce(approvalSignMock).mockImplementationOnce(bridgeSignMock); | ||
@@ -105,3 +111,3 @@ sourceClientMock.readContract.mockResolvedValue(0n); | ||
const params = getTransferParams({ sourceProvider: providerWithSigner }); | ||
const params = getTransferParams({ sourceProvider: sourceProviderMock, sign: customSigner }); | ||
const bridgeTransfer = await transferAsset(bridgeMock, params, environment); | ||
@@ -122,3 +128,3 @@ | ||
chain: params.sourceChain, | ||
provider: providerWithSigner, | ||
provider: sourceProviderMock, | ||
}); | ||
@@ -149,13 +155,21 @@ expect(getClientForChain).toHaveBeenNthCalledWith(2, { | ||
expect(providerWithSigner.sign).toHaveBeenCalledTimes(2); | ||
expect(providerWithSigner.sign).toHaveBeenNthCalledWith(1, { | ||
from: params.fromAddress, | ||
to: BRIDGE_ASSET.address, | ||
data: approvalTxData, | ||
}); | ||
expect(providerWithSigner.sign).toHaveBeenNthCalledWith(2, { | ||
from: params.fromAddress, | ||
to: SOURCE_ROUTER_ADDRESS, | ||
data: bridgeTxData, | ||
}); | ||
expect(customSigner).toHaveBeenCalledTimes(2); | ||
expect(customSigner).toHaveBeenNthCalledWith( | ||
1, | ||
{ | ||
from: params.fromAddress, | ||
to: BRIDGE_ASSET.address, | ||
data: approvalTxData, | ||
}, | ||
expect.any(Function), | ||
); | ||
expect(customSigner).toHaveBeenNthCalledWith( | ||
2, | ||
{ | ||
from: params.fromAddress, | ||
to: SOURCE_ROUTER_ADDRESS, | ||
data: bridgeTxData, | ||
}, | ||
expect.any(Function), | ||
); | ||
@@ -259,7 +273,5 @@ expect(sourceClientMock.sendRawTransaction).toHaveBeenCalledTimes(2); | ||
const providerWithSigner = { | ||
request: jest.fn(), | ||
sign: jest.fn().mockResolvedValueOnce(bridgeSignedHex), | ||
}; | ||
const params = getTransferParams({ sourceProvider: providerWithSigner }); | ||
const bridgeSignMock: Signer = jest.fn((_, dispatch) => dispatch(bridgeSignedHex)); | ||
const customSigner = jest.fn().mockImplementationOnce(bridgeSignMock); | ||
const params = getTransferParams({ sourceProvider: sourceProviderMock, sign: customSigner }); | ||
@@ -285,3 +297,3 @@ sourceClientMock.readContract.mockResolvedValue(params.amount); | ||
chain: params.sourceChain, | ||
provider: providerWithSigner, | ||
provider: sourceProviderMock, | ||
}); | ||
@@ -307,8 +319,11 @@ expect(getClientForChain).toHaveBeenNthCalledWith(2, { | ||
expect(providerWithSigner.sign).toHaveBeenCalledTimes(1); | ||
expect(providerWithSigner.sign).toHaveBeenCalledWith({ | ||
from: params.fromAddress, | ||
to: SOURCE_ROUTER_ADDRESS, | ||
data: bridgeTxData, | ||
}); | ||
expect(customSigner).toHaveBeenCalledTimes(1); | ||
expect(customSigner).toHaveBeenCalledWith( | ||
{ | ||
from: params.fromAddress, | ||
to: SOURCE_ROUTER_ADDRESS, | ||
data: bridgeTxData, | ||
}, | ||
expect.any(Function), | ||
); | ||
@@ -315,0 +330,0 @@ expect(sourceClientMock.sendRawTransaction).toHaveBeenCalledTimes(1); |
import { encodeFunctionData, isAddress, type PublicClient } from 'viem'; | ||
import { ErrorReason, type BridgeService, type Environment, type TransferParams } from '../../../types'; | ||
import { ErrorReason, type BridgeService, type Environment, type TransferParams, type Hex } from '../../../types'; | ||
import { getClientForChain } from '../../../utils/client'; | ||
@@ -10,3 +10,12 @@ import { ERC20_ABI } from '../abis/erc20'; | ||
const approveAndTransfer = async (bridge: BridgeService, params: TransferParams) => { | ||
const { sourceChain, targetChain, asset, amount, fromAddress, toAddress: maybeToAddress, sourceProvider } = params; | ||
const { | ||
sourceChain, | ||
targetChain, | ||
asset, | ||
amount, | ||
fromAddress, | ||
toAddress: maybeToAddress, | ||
sourceProvider, | ||
sign, | ||
} = params; | ||
const toAddress = maybeToAddress ?? fromAddress; | ||
@@ -32,3 +41,3 @@ | ||
if (allowance < amount) { | ||
if (sourceProvider.sign) { | ||
if (sign) { | ||
const data = encodeFunctionData({ | ||
@@ -40,9 +49,11 @@ abi: ERC20_ABI, | ||
const signedHex = await sourceProvider.sign({ | ||
from: fromAddress, | ||
to: burnToken.address, | ||
data, | ||
}); | ||
const txHash = await sign( | ||
{ | ||
from: fromAddress, | ||
to: burnToken.address, | ||
data, | ||
}, | ||
(signedTxHash: Hex) => client.sendRawTransaction({ serializedTransaction: signedTxHash }), | ||
); | ||
const txHash = await client.sendRawTransaction({ serializedTransaction: signedHex }); | ||
await client.waitForTransactionReceipt({ hash: txHash, pollingInterval: 1_000 }); | ||
@@ -63,3 +74,3 @@ } else { | ||
if (sourceProvider.sign) { | ||
if (sign) { | ||
const data = encodeFunctionData({ | ||
@@ -71,9 +82,10 @@ abi: TOKEN_ROUTER_ABI, | ||
const signedHex = await sourceProvider.sign({ | ||
from: fromAddress, | ||
to: sourceChainData.tokenRouterAddress, | ||
data, | ||
}); | ||
return client.sendRawTransaction({ serializedTransaction: signedHex }); | ||
return sign( | ||
{ | ||
from: fromAddress, | ||
to: sourceChainData.tokenRouterAddress, | ||
data, | ||
}, | ||
(signedTxHash: Hex) => client.sendRawTransaction({ serializedTransaction: signedTxHash }), | ||
); | ||
} else { | ||
@@ -80,0 +92,0 @@ const { request } = await client.simulateContract({ |
@@ -6,2 +6,3 @@ import type { AssetFeeMap, BridgeAsset, ChainAssetMap } from './asset'; | ||
import type { Provider } from './provider'; | ||
import type { Signer } from './signer'; | ||
import type { BridgeTransfer } from './transfer'; | ||
@@ -30,2 +31,3 @@ | ||
targetProvider?: Provider; | ||
sign?: Signer; | ||
}; | ||
@@ -32,0 +34,0 @@ |
@@ -9,1 +9,2 @@ export * from './asset'; | ||
export * from './provider'; | ||
export * from './signer'; |
@@ -1,18 +0,1 @@ | ||
import type { Address } from 'viem'; | ||
export type Hex = `0x${string}`; | ||
export type TransactionRequest = { | ||
type?: number | null; | ||
data?: Hex | null; | ||
from: Address; | ||
gas?: bigint; | ||
nonce?: number; | ||
to?: Address | null; | ||
value?: bigint; | ||
gasPrice?: bigint | null; | ||
gasLimit?: bigint | null; | ||
maxPriorityFeePerGas?: bigint | null; | ||
maxFeePerGas?: bigint | null; | ||
}; | ||
type RequestArguments = { | ||
@@ -29,3 +12,2 @@ method: string; | ||
request: (args: RequestArguments) => Promise<unknown>; | ||
sign?: (data: TransactionRequest) => Promise<Hex>; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
374102
73
4627