New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@avalabs/bridge-unified

Package Overview
Dependencies
Maintainers
1
Versions
188
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@avalabs/bridge-unified - npm Package Compare versions

Comparing version 0.0.0-feat-unified-bridge-cctp-revie-20240110175344 to 0.0.0-feat-unified-bridge-cctp-revie-20240110184532

2

CHANGELOG.md
# @avalabs/unified-bridge
## 0.0.0-feat-unified-bridge-cctp-revie-20240110175344
## 0.0.0-feat-unified-bridge-cctp-revie-20240110184532

@@ -5,0 +5,0 @@ ### Major Changes

import { custom, http, createWalletClient, publicActions, isAddress, encodeFunctionData, decodeEventLog } from 'viem';
import { mergeWith, isArray } from 'lodash';
var H=(r=>(r.NATIVE="native",r.ERC20="erc20",r))(H||{});var R=(t=>(t.CCTP="cctp",t))(R||{});var V=(r=>(r.PROD="production",r.TEST="test",r))(V||{});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=(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))(h||{});var I=class extends Error{constructor(r,a,u){super(r);this.code=a;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"},K=async e=>{try{return (await(await fetch(re[e])).json()).map(a=>({...a,chainId:`eip155:${a.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 $(e){await e.ensureHasConfig();let t=e.config.map(r=>r.chainId);return e.config.reduce((r,a)=>(r[a.chainId]=a.tokens.map(u=>({...u,type:"erc20",destinations:t.reduce((n,o)=>(a.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}$",G=":",ie=e=>{let[t,r]=e.split(G);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}${G}${t}`,E={toJSON:ie,toString:oe};var me=e=>{let{reference:t}=E.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}}}}},w=({chain:e,provider:t})=>{let r=me(e),a=t?custom(t):http(e.rpcUrl,{batch:!0,retryCount:0});return createWalletClient({chain:r,transport:a}).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 _=({sourceChain:e,targetChain:t,amount:r,asset:a},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(i=>i.chainId===e.chainId);if(!n)throw new f("CHAIN_NOT_SUPPORTED",`Not supported on source chain "${e.chainId}"`);let o=u.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===a.symbol),l=o.tokens.find(i=>i.symbol===a.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:a,asset:u,amount:n,provider:o}=t,{sourceChainData:d,targetChainData:l,burnToken:i}=_({sourceChain:r,targetChain:a,asset:u,amount:n},e.config),p=await w({chain:r,provider:o}).readContract({address:d.tokenRouterAddress,abi:N,functionName:"calculateFee",args:[n,l.domain]});return {[i.address]:p}}var F=[{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:a,asset:u,amount:n,fromAddress:o,toAddress:d,sourceProvider:l,sign:i}=t,s=d??o;if(!isAddress(o)||!isAddress(s))throw new f("INCORRECT_ADDRESS_PROVIDED");let{sourceChainData:p,targetChainData:y,burnToken:m}=_({sourceChain:r,targetChain:a,asset:u,amount:n},e.config),c=w({chain:r,provider:l});if(await c.readContract({address:m.address,abi:F,functionName:"allowance",args:[o,p.tokenRouterAddress]})<n)if(i){let T=encodeFunctionData({abi:F,functionName:"approve",args:[p.tokenRouterAddress,n]}),g=await i({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:F,functionName:"approve",args:[p.tokenRouterAddress,n]}),g=await c.writeContract(T);await c.waitForTransactionReceipt({hash:g,pollingInterval:1e3});}if(i){let T=encodeFunctionData({abi:N,functionName:"transferTokens",args:[n,y.domain,s,m.address]});return i({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,s,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:a}=e.config.find(p=>p.chainId===t.sourceChain.chainId)??{},{minimumConfirmations:u}=e.config.find(p=>p.chainId===t.targetChain.chainId)??{};if(!a||!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(),i=w({chain:t.targetChain,provider:t.targetProvider}),s=await le(i);return {type:e.type,environment:r,fromAddress:t.fromAddress,toAddress:t.toAddress??t.fromAddress,amount:t.amount,amountDecimals:t.asset.decimals,symbol:t.asset.symbol,bridgeFee:o,sourceChain:t.sourceChain,sourceStartedAt:l,sourceTxHash:d,sourceConfirmationCount:0,requiredSourceConfirmationCount:a,targetChain:t.targetChain,targetConfirmationCount:0,requiredTargetConfirmationCount:u,startBlockNumber:s}}async function J(e){if(!e.config&&(await e.updateConfig(),!e.config))throw new B("CONFIG_NOT_AVAILABLE")}var S=(e,t)=>e.gasPrice&&BigInt(e.gasPrice*t.gasUsed);var L=async e=>new Promise(t=>{setTimeout(t,e);});var U=({promise:e,delay:t,startAfter:r})=>{let a=!1,u=!1,n=0,o,d,l=p=>{o&&a&&(a=!1,o(p));},i=()=>{u=!0,d&&a&&(a=!1,d("cancelled"));};return {result:new Promise((p,y)=>{a=!0,o=p,d=y;let m=async()=>{if(!(!a||u)){try{if(await e(l),!a||u)return;await L(t);}catch(c){console.error(c.message),n+=1,await L(2**n*t);}await m();}};r?setTimeout(m,r):m();}),cancel:i}};var Y=1e3*60*60*3,X=1024n,Q=5e3,A=(e,t,r)=>{Object.assign(e,Object.fromEntries(Object.entries(t).filter(([,a])=>a!==void 0))),r({...e});},fe=async(e,t)=>{let{sourceProvider:r,targetProvider:a,updateListener:u,bridgeTransfer:n}=t,o=w({chain:n.sourceChain,provider:r}),d=e.find(y=>y.chainId===n.sourceChain.chainId),l=w({chain:n.targetChain,provider:a}),i=e.find(y=>y.chainId===n.targetChain.chainId),s={...n};if(!d||!i)throw new f("CHAIN_NOT_SUPPORTED");return U({promise:async y=>{if(s.completedAt||s.metadata?.nonce)return y(s);if(s.sourceStartedAt+Y<=Date.now())return A(s,{completedAt:Date.now(),errorCode:5004},u),y(s);let m=await o.getTransactionReceipt({hash:s.sourceTxHash});if(!s.sourceNetworkFee){let v=await o.getTransaction({hash:s.sourceTxHash}),D=S(v,m);D&&A(s,{sourceNetworkFee:D},u);}if(m.status==="reverted")return A(s,{completedAt:Date.now(),errorCode:5005},u),y(s);let c=await o.getTransactionConfirmations({hash:s.sourceTxHash}),C=c>s.sourceConfirmationCount,T=c>=s.requiredSourceConfirmationCount;if(C){let v={};v.sourceConfirmationCount=Number(c),T||(v.startBlockNumber=await l.getBlockNumber()),A(s,v,u);}if(!T)return;s.startBlockNumber||A(s,{startBlockNumber:await l.getBlockNumber()},u);let g=m.logs.find(v=>v.address===d.tokenRouterAddress?decodeEventLog({abi:N,...v}).eventName==="TransferTokens":!1);if(!g)throw new f("INVALID_PARAMS",`unable to find a TransferTokens event in source transaction "${s.sourceTxHash}"`);let x=decodeEventLog({abi:N,eventName:"TransferTokens",...g}).args.nonce;return A(s,{targetStartedAt:Date.now(),metadata:{nonce:x}},u),y(s)},delay:O(d.chainId),startAfter:Q})},Te=async(e,t)=>{let{targetProvider:r,updateListener:a,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=w({chain:u.targetChain,provider:r}),d=e.find(i=>i.chainId===u.targetChain.chainId);if(!d)throw new f("INVALID_PARAMS",`unknown target chain "${u.targetChain.chainId}"`);return U({promise:async i=>{if(n.completedAt)return i(n);if(!n.startBlockNumber)return A(n,{completedAt:Date.now(),errorCode:5003},a),i(n);if(n.sourceStartedAt+Y<=Date.now())return A(n,{completedAt:Date.now(),errorCode:5004},a),i(n);if(!n.targetTxHash){let c=await o.getBlockNumber(),C=n.startBlockNumber-X,T=C>=0n?C:"earliest",g=n.startBlockNumber+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},a);else return}let s=await o.getTransactionReceipt({hash:n.targetTxHash});if(!n.targetNetworkFee){let c=await o.getTransaction({hash:n.targetTxHash}),C=S(c,s);C&&A(n,{targetNetworkFee:C},a);}if(s.status==="reverted")return A(n,{completedAt:Date.now(),errorCode:5005},a),i(n);let p=await o.getTransactionConfirmations({hash:n.targetTxHash}),y=p>n.targetConfirmationCount,m=p>=n.requiredTargetConfirmationCount;if(y&&A(n,{targetConfirmationCount:Number(p)},a),!!m)return A(n,{completedAt:Date.now()},a),i(n)},delay:O(d.chainId),startAfter:Q})};function ee(e,t){let r,a=()=>{r?.();};return {result:(async()=>{await e.ensureHasConfig();let{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:l}=t,{result:i,cancel:s}=await fe(e.config,{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:l});r=s;let p=await i,{result:y,cancel:m}=await Te(e.config,{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:p});return r=m,y})(),cancel:a}}var te=e=>({type:"cctp",config:null,updateConfig:async function(){this.config=await K(e);},ensureHasConfig:async function(){return J(this)},getAssets:async function(){return $(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,a])=>[r,a(e)])),M=(e,t,r)=>{let a=t.destinations[r]?.find(u=>e.has(u));if(!a)throw new P;return {type:a,bridge:e.get(a)}};var un=({environment:e,disabledBridgeTypes:t})=>{let r=ne(e,t),a=async()=>{await Promise.allSettled(Array.from(r).map(([,s])=>s.updateConfig()));};return {environment:e,bridges:r,init:async()=>{await a();},updateConfigs:a,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 s=>{let{bridge:p}=M(r,s.asset,s.targetChain.chainId);return p.getFees(s)},canTransferAsset:(s,p)=>{try{return M(r,s,p),!0}catch{return !1}},transferAsset:async s=>{let{bridge:p}=M(r,s.asset,s.targetChain.chainId);return p.transferAsset(s)},trackTransfer:s=>{let p=r.get(s.bridgeTransfer.type);if(!p)throw new P;return p.trackTransfer(s)}}};
var H=(r=>(r.NATIVE="native",r.ERC20="erc20",r))(H||{});var R=(t=>(t.CCTP="cctp",t))(R||{});var V=(r=>(r.PROD="production",r.TEST="test",r))(V||{});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=(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))(h||{});var I=class extends Error{constructor(r,a,u){super(r);this.code=a;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"},K=async e=>{try{return (await(await fetch(re[e])).json()).map(a=>({...a,chainId:`eip155:${a.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 $(e){await e.ensureHasConfig();let t=e.config.map(r=>r.chainId);return e.config.reduce((r,a)=>(r[a.chainId]=a.tokens.map(u=>({...u,type:"erc20",destinations:t.reduce((n,o)=>(a.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}$",G=":",ie=e=>{let[t,r]=e.split(G);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}${G}${t}`,E={toJSON:ie,toString:oe};var me=e=>{let{reference:t}=E.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}}}}},w=({chain:e,provider:t})=>{let r=me(e),a=t?custom(t):http(e.rpcUrl,{batch:!0,retryCount:0});return createWalletClient({chain:r,transport:a}).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 _=({sourceChain:e,targetChain:t,amount:r,asset:a},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(i=>i.chainId===e.chainId);if(!n)throw new f("CHAIN_NOT_SUPPORTED",`Not supported on source chain "${e.chainId}"`);let o=u.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===a.symbol),l=o.tokens.find(i=>i.symbol===a.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:a,asset:u,amount:n,provider:o}=t,{sourceChainData:d,targetChainData:l,burnToken:i}=_({sourceChain:r,targetChain:a,asset:u,amount:n},e.config),p=await w({chain:r,provider:o}).readContract({address:d.tokenRouterAddress,abi:N,functionName:"calculateFee",args:[n,l.domain]});return {[i.address]:p}}var F=[{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:a,asset:u,amount:n,fromAddress:o,toAddress:d,sourceProvider:l,sign:i}=t,s=d??o;if(!isAddress(o)||!isAddress(s))throw new f("INCORRECT_ADDRESS_PROVIDED");let{sourceChainData:p,targetChainData:y,burnToken:m}=_({sourceChain:r,targetChain:a,asset:u,amount:n},e.config),c=w({chain:r,provider:l});if(await c.readContract({address:m.address,abi:F,functionName:"allowance",args:[o,p.tokenRouterAddress]})<n)if(i){let T=encodeFunctionData({abi:F,functionName:"approve",args:[p.tokenRouterAddress,n]}),g=await i({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:F,functionName:"approve",args:[p.tokenRouterAddress,n]}),g=await c.writeContract(T);await c.waitForTransactionReceipt({hash:g,pollingInterval:1e3});}if(i){let T=encodeFunctionData({abi:N,functionName:"transferTokens",args:[n,y.domain,s,m.address]});return i({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,s,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:a}=e.config.find(p=>p.chainId===t.sourceChain.chainId)??{},{minimumConfirmations:u}=e.config.find(p=>p.chainId===t.targetChain.chainId)??{};if(!a||!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(),i=w({chain:t.targetChain,provider:t.targetProvider}),s=await le(i);return {type:e.type,environment:r,fromAddress:t.fromAddress,toAddress:t.toAddress??t.fromAddress,amount:t.amount,amountDecimals:t.asset.decimals,symbol:t.asset.symbol,bridgeFee:o,sourceChain:t.sourceChain,sourceStartedAt:l,sourceTxHash:d,sourceConfirmationCount:0,requiredSourceConfirmationCount:a,targetChain:t.targetChain,targetConfirmationCount:0,requiredTargetConfirmationCount:u,startBlockNumber:s}}async function J(e){if(!e.config&&(await e.updateConfig(),!e.config))throw new B("CONFIG_NOT_AVAILABLE")}var S=(e,t)=>e.gasPrice&&BigInt(e.gasPrice*t.gasUsed);var L=async e=>new Promise(t=>{setTimeout(t,e);});var U=({promise:e,delay:t,startAfter:r})=>{let a=!1,u=!1,n=0,o,d,l=p=>{o&&a&&(a=!1,o(p));},i=()=>{u=!0,d&&a&&(a=!1,d("cancelled"));};return {result:new Promise((p,y)=>{a=!0,o=p,d=y;let m=async()=>{if(!(!a||u)){try{if(await e(l),!a||u)return;await L(t);}catch(c){console.error(c.message),n+=1,await L(2**n*t);}await m();}};r?setTimeout(m,r):m();}),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(([,a])=>a!==void 0))),r({...e});},fe=async(e,t)=>{let{sourceProvider:r,targetProvider:a,updateListener:u,bridgeTransfer:n}=t,o=w({chain:n.sourceChain,provider:r}),d=e.find(y=>y.chainId===n.sourceChain.chainId),l=w({chain:n.targetChain,provider:a}),i=e.find(y=>y.chainId===n.targetChain.chainId),s={...n};if(!d||!i)throw new f("CHAIN_NOT_SUPPORTED");return U({promise:async y=>{if(s.completedAt||s.metadata?.nonce)return y(s);if(s.sourceStartedAt+Y<=Date.now())return C(s,{completedAt:Date.now(),errorCode:5004},u),y(s);let m=await o.getTransactionReceipt({hash:s.sourceTxHash});if(!s.sourceNetworkFee){let v=await o.getTransaction({hash:s.sourceTxHash}),D=S(v,m);D&&C(s,{sourceNetworkFee:D},u);}if(m.status==="reverted")return C(s,{completedAt:Date.now(),errorCode:5005},u),y(s);let c=await o.getTransactionConfirmations({hash:s.sourceTxHash}),A=c>s.sourceConfirmationCount,T=c>=s.requiredSourceConfirmationCount;if(A){let v={};v.sourceConfirmationCount=Number(c),T||(v.startBlockNumber=await l.getBlockNumber()),C(s,v,u);}if(!T)return;s.startBlockNumber||C(s,{startBlockNumber:await l.getBlockNumber()},u);let g=m.logs.find(v=>v.address===d.tokenRouterAddress?decodeEventLog({abi:N,...v}).eventName==="TransferTokens":!1);if(!g)throw new f("INVALID_PARAMS",`unable to find a TransferTokens event in source transaction "${s.sourceTxHash}"`);let x=decodeEventLog({abi:N,eventName:"TransferTokens",...g}).args.nonce;return C(s,{targetStartedAt:Date.now(),metadata:{nonce:x}},u),y(s)},delay:O(d.chainId),startAfter:Q})},Te=async(e,t)=>{let{targetProvider:r,updateListener:a,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=w({chain:u.targetChain,provider:r}),d=e.find(i=>i.chainId===u.targetChain.chainId);if(!d)throw new f("INVALID_PARAMS",`unknown target chain "${u.targetChain.chainId}"`);return U({promise:async i=>{if(n.completedAt)return i(n);if(!n.startBlockNumber)return C(n,{completedAt:Date.now(),errorCode:5003},a),i(n);if(n.sourceStartedAt+Y<=Date.now())return C(n,{completedAt:Date.now(),errorCode:5004},a),i(n);if(!n.targetTxHash){let c=await o.getBlockNumber(),A=n.startBlockNumber-X,T=A>=0n?A:"earliest",g=n.startBlockNumber+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)C(n,{targetTxHash:x[0].transactionHash},a);else {C(n,{startBlockNumber:c},a);return}}let s=await o.getTransactionReceipt({hash:n.targetTxHash});if(!n.targetNetworkFee){let c=await o.getTransaction({hash:n.targetTxHash}),A=S(c,s);A&&C(n,{targetNetworkFee:A},a);}if(s.status==="reverted")return C(n,{completedAt:Date.now(),errorCode:5005},a),i(n);let p=await o.getTransactionConfirmations({hash:n.targetTxHash}),y=p>n.targetConfirmationCount,m=p>=n.requiredTargetConfirmationCount;if(y&&C(n,{targetConfirmationCount:Number(p)},a),!!m)return C(n,{completedAt:Date.now()},a),i(n)},delay:O(d.chainId),startAfter:Q})};function ee(e,t){let r,a=()=>{r?.();};return {result:(async()=>{await e.ensureHasConfig();let{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:l}=t,{result:i,cancel:s}=await fe(e.config,{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:l});r=s;let p=await i,{result:y,cancel:m}=await Te(e.config,{sourceProvider:n,targetProvider:o,updateListener:d,bridgeTransfer:p});return r=m,y})(),cancel:a}}var te=e=>({type:"cctp",config:null,updateConfig:async function(){this.config=await K(e);},ensureHasConfig:async function(){return J(this)},getAssets:async function(){return $(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,a])=>[r,a(e)])),M=(e,t,r)=>{let a=t.destinations[r]?.find(u=>e.has(u));if(!a)throw new P;return {type:a,bridge:e.get(a)}};var un=({environment:e,disabledBridgeTypes:t})=>{let r=ne(e,t),a=async()=>{await Promise.allSettled(Array.from(r).map(([,s])=>s.updateConfig()));};return {environment:e,bridges:r,init:async()=>{await a();},updateConfigs:a,getAssets:async()=>(await Promise.all(Array.from(r).map(([,p])=>p.getAssets()))).reduce((p,y)=>{for(let[m,c]of Object.entries(y)){let A=p[m];if(A)for(let T of c){let g=A.findIndex(({symbol:b})=>b===T.symbol);g===-1?A.push(T):mergeWith(A[g],T,(b,x)=>{if(isArray(b))return [...new Set(b.concat(x))]});}else p[m]=c;}return p},{}),getFees:async s=>{let{bridge:p}=M(r,s.asset,s.targetChain.chainId);return p.getFees(s)},canTransferAsset:(s,p)=>{try{return M(r,s,p),!0}catch{return !1}},transferAsset:async s=>{let{bridge:p}=M(r,s.asset,s.targetChain.chainId);return p.transferAsset(s)},trackTransfer:s=>{let p=r.get(s.bridgeTransfer.type);if(!p)throw new P;return p.trackTransfer(s)}}};

@@ -6,0 +6,0 @@ export { R as BridgeType, V as Environment, k as ErrorCode, h as ErrorReason, H as TokenType, E as caip2, un as createUnifiedBridgeService };

{
"name": "@avalabs/bridge-unified",
"version": "0.0.0-feat-unified-bridge-cctp-revie-20240110175344",
"version": "0.0.0-feat-unified-bridge-cctp-revie-20240110184532",
"main": "dist/index.js",

@@ -18,3 +18,3 @@ "type": "module",

"@internal/tsup-config": "0.0.1",
"eslint-config-custom": "0.0.0-feat-unified-bridge-cctp-revie-20240110175344"
"eslint-config-custom": "0.0.0-feat-unified-bridge-cctp-revie-20240110184532"
},

@@ -21,0 +21,0 @@ "scripts": {

@@ -550,2 +550,45 @@ import { decodeEventLog } from 'viem';

it('does not resolve if transaction hash is unknown', async () => {
const targetBlockNumber = 50;
const params = getBridgeTrackinParams({
bridgeTransfer: {
requiredTargetConfirmationCount: 5,
startBlockNumber,
metadata: {
nonce,
},
},
updateListener,
});
targetClientMock.getBlockNumber.mockResolvedValueOnce(targetBlockNumber);
targetClientMock.getLogs.mockResolvedValueOnce([]);
const { result } = await trackTargetTx(bridgeMock.config!, params);
expect(await result).toBeUndefined();
expect(doneMock).not.toHaveBeenCalled();
expect(updateListenerWithoutReference).toHaveBeenCalledTimes(1);
expect(updateListenerWithoutReference).toHaveBeenCalledWith({
...params.bridgeTransfer,
startBlockNumber: targetBlockNumber,
});
expect(targetClientMock.getLogs).toHaveBeenCalledWith({
address: TARGET_TRANSMITTER_ADDRESS,
event: msgReceivedEvent,
args: { nonce },
fromBlock: 'earliest',
toBlock: 'latest',
});
expect(targetClientMock.getTransactionReceipt).not.toHaveBeenCalled();
expect(retryPromise).toHaveBeenCalledWith({
promise: expect.any(Function),
delay,
startAfter: INITIAL_DELAY,
});
});
it('does not resolve if more confirmations are needed', async () => {

@@ -552,0 +595,0 @@ const targetBlockNumber = 50;

@@ -263,2 +263,3 @@ import { decodeEventLog, type Address } from 'viem';

} else {
updateTransfer(updateableTransfer, { startBlockNumber: lastBlockNumber }, updateListener);
return;

@@ -265,0 +266,0 @@ }

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc