11import Token from '@depay/web3-tokens' ;
22import { request , getProvider } from '@depay/web3-client' ;
3- import { ethers as ethers$1 } from 'ethers' ;
3+ import { ethers } from 'ethers' ;
44import Blockchains from '@depay/web3-blockchains' ;
55import { BN , struct , publicKey , u128 , u64 as u64$1 , seq , u8 , u16 , i32 , bool , i128 , PublicKey , Buffer , Keypair , SystemProgram , TransactionInstruction , blob , u32 , Transaction } from '@depay/solana-web3.js' ;
66import Decimal from 'decimal.js' ;
@@ -198,7 +198,7 @@ const calculateAmountsWithSlippage = async ({
198198
199199const fixAddress = ( address ) => {
200200 if ( address . match ( '0x' ) ) {
201- return ethers$1 . utils . getAddress ( address )
201+ return ethers . utils . getAddress ( address )
202202 } else {
203203 return address
204204 }
@@ -467,9 +467,9 @@ const getExchangePath$5 = ({ blockchain, exchange, path }) => {
467467
468468const minReserveRequirements = ( { reserves, min, token, token0, token1, decimals } ) => {
469469 if ( token0 . toLowerCase ( ) == token . toLowerCase ( ) ) {
470- return reserves [ 0 ] . gte ( ethers$1 . utils . parseUnits ( min . toString ( ) , decimals ) )
470+ return reserves [ 0 ] . gte ( ethers . utils . parseUnits ( min . toString ( ) , decimals ) )
471471 } else if ( token1 . toLowerCase ( ) == token . toLowerCase ( ) ) {
472- return reserves [ 1 ] . gte ( ethers$1 . utils . parseUnits ( min . toString ( ) , decimals ) )
472+ return reserves [ 1 ] . gte ( ethers . utils . parseUnits ( min . toString ( ) , decimals ) )
473473 } else {
474474 return false
475475 }
@@ -1975,11 +1975,11 @@ let getPairsWithPrice$3 = async({ tokenIn, tokenOut, amountIn, amountInMax, amou
19751975} ;
19761976
19771977let getHighestPrice$1 = ( pairs ) => {
1978- return pairs . reduce ( ( bestPricePair , currentPair ) => ethers$1 . BigNumber . from ( currentPair . price ) . gt ( ethers$1 . BigNumber . from ( bestPricePair . price ) ) ? currentPair : bestPricePair )
1978+ return pairs . reduce ( ( bestPricePair , currentPair ) => ethers . BigNumber . from ( currentPair . price ) . gt ( ethers . BigNumber . from ( bestPricePair . price ) ) ? currentPair : bestPricePair )
19791979} ;
19801980
19811981let getLowestPrice$1 = ( pairs ) => {
1982- return pairs . reduce ( ( bestPricePair , currentPair ) => ethers$1 . BigNumber . from ( currentPair . price ) . lt ( ethers$1 . BigNumber . from ( bestPricePair . price ) ) ? currentPair : bestPricePair )
1982+ return pairs . reduce ( ( bestPricePair , currentPair ) => ethers . BigNumber . from ( currentPair . price ) . lt ( ethers . BigNumber . from ( bestPricePair . price ) ) ? currentPair : bestPricePair )
19831983} ;
19841984
19851985let getBestPair$1 = async ( { tokenIn, tokenOut, amountIn, amountInMax, amountOut, amountOutMin } ) => {
@@ -2082,16 +2082,16 @@ let findPath$5 = async ({ tokenIn, tokenOut, amountIn, amountOut, amountInMax, a
20822082
20832083let getAmountsOut$1 = async ( { path, amountIn, amountInMax } ) => {
20842084
2085- let amounts = [ ethers$1 . BigNumber . from ( amountIn || amountInMax ) ] ;
2085+ let amounts = [ ethers . BigNumber . from ( amountIn || amountInMax ) ] ;
20862086
20872087 let bestPair = await getBestPair$1 ( { tokenIn : path [ 0 ] , tokenOut : path [ 1 ] , amountIn, amountInMax } ) ;
20882088 if ( ! bestPair ) { return }
2089- amounts . push ( ethers$1 . BigNumber . from ( bestPair . price ) ) ;
2089+ amounts . push ( ethers . BigNumber . from ( bestPair . price ) ) ;
20902090
20912091 if ( path . length === 3 ) {
20922092 let bestPair = await getBestPair$1 ( { tokenIn : path [ 1 ] , tokenOut : path [ 2 ] , amountIn : amountIn ? amounts [ 1 ] : undefined , amountInMax : amountInMax ? amounts [ 1 ] : undefined } ) ;
20932093 if ( ! bestPair ) { return }
2094- amounts . push ( ethers$1 . BigNumber . from ( bestPair . price ) ) ;
2094+ amounts . push ( ethers . BigNumber . from ( bestPair . price ) ) ;
20952095 }
20962096
20972097 if ( amounts . length != path . length ) { return }
@@ -2102,16 +2102,16 @@ let getAmountsOut$1 = async ({ path, amountIn, amountInMax }) => {
21022102let getAmountsIn$1 = async ( { path, amountOut, amountOutMin } ) => {
21032103
21042104 path = path . slice ( ) . reverse ( ) ;
2105- let amounts = [ ethers$1 . BigNumber . from ( amountOut || amountOutMin ) ] ;
2105+ let amounts = [ ethers . BigNumber . from ( amountOut || amountOutMin ) ] ;
21062106
21072107 let bestPair = await getBestPair$1 ( { tokenIn : path [ 1 ] , tokenOut : path [ 0 ] , amountOut, amountOutMin } ) ;
21082108 if ( ! bestPair ) { return }
2109- amounts . push ( ethers$1 . BigNumber . from ( bestPair . price ) ) ;
2109+ amounts . push ( ethers . BigNumber . from ( bestPair . price ) ) ;
21102110
21112111 if ( path . length === 3 ) {
21122112 let bestPair = await getBestPair$1 ( { tokenIn : path [ 2 ] , tokenOut : path [ 1 ] , amountOut : amountOut ? amounts [ 1 ] : undefined , amountOutMin : amountOutMin ? amounts [ 1 ] : undefined } ) ;
21132113 if ( ! bestPair ) { return }
2114- amounts . push ( ethers$1 . BigNumber . from ( bestPair . price ) ) ;
2114+ amounts . push ( ethers . BigNumber . from ( bestPair . price ) ) ;
21152115 }
21162116
21172117 if ( amounts . length != path . length ) { return }
@@ -3005,11 +3005,11 @@ const getPairsWithPrice = async({ tokenIn, tokenOut, amountIn, amountInMax, amou
30053005} ;
30063006
30073007let getHighestPrice = ( pairs ) => {
3008- return pairs . reduce ( ( bestPricePair , currentPair ) => ethers$1 . BigNumber . from ( currentPair . price ) . gt ( ethers$1 . BigNumber . from ( bestPricePair . price ) ) ? currentPair : bestPricePair )
3008+ return pairs . reduce ( ( bestPricePair , currentPair ) => ethers . BigNumber . from ( currentPair . price ) . gt ( ethers . BigNumber . from ( bestPricePair . price ) ) ? currentPair : bestPricePair )
30093009} ;
30103010
30113011let getLowestPrice = ( pairs ) => {
3012- return pairs . reduce ( ( bestPricePair , currentPair ) => ethers$1 . BigNumber . from ( currentPair . price ) . lt ( ethers$1 . BigNumber . from ( bestPricePair . price ) ) ? currentPair : bestPricePair )
3012+ return pairs . reduce ( ( bestPricePair , currentPair ) => ethers . BigNumber . from ( currentPair . price ) . lt ( ethers . BigNumber . from ( bestPricePair . price ) ) ? currentPair : bestPricePair )
30133013} ;
30143014
30153015let getBestPair = async ( { tokenIn, tokenOut, amountIn, amountInMax, amountOut, amountOutMin } ) => {
@@ -3113,16 +3113,16 @@ let findPath$4 = async ({ tokenIn, tokenOut, amountIn, amountOut, amountInMax, a
31133113
31143114let getAmountsOut = async ( { path, amountIn, amountInMax } ) => {
31153115
3116- let amounts = [ ethers$1 . BigNumber . from ( amountIn || amountInMax ) ] ;
3116+ let amounts = [ ethers . BigNumber . from ( amountIn || amountInMax ) ] ;
31173117
31183118 let bestPair = await getBestPair ( { tokenIn : path [ 0 ] , tokenOut : path [ 1 ] , amountIn, amountInMax } ) ;
31193119 if ( ! bestPair ) { return }
3120- amounts . push ( ethers$1 . BigNumber . from ( bestPair . price ) ) ;
3120+ amounts . push ( ethers . BigNumber . from ( bestPair . price ) ) ;
31213121
31223122 if ( path . length === 3 ) {
31233123 let bestPair = await getBestPair ( { tokenIn : path [ 1 ] , tokenOut : path [ 2 ] , amountIn : amountIn ? amounts [ 1 ] : undefined , amountInMax : amountInMax ? amounts [ 1 ] : undefined } ) ;
31243124 if ( ! bestPair ) { return }
3125- amounts . push ( ethers$1 . BigNumber . from ( bestPair . price ) ) ;
3125+ amounts . push ( ethers . BigNumber . from ( bestPair . price ) ) ;
31263126 }
31273127
31283128 if ( amounts . length != path . length ) { return }
@@ -3133,16 +3133,16 @@ let getAmountsOut = async ({ path, amountIn, amountInMax }) => {
31333133let getAmountsIn = async ( { path, amountOut, amountOutMin } ) => {
31343134
31353135 path = path . slice ( ) . reverse ( ) ;
3136- let amounts = [ ethers$1 . BigNumber . from ( amountOut || amountOutMin ) ] ;
3136+ let amounts = [ ethers . BigNumber . from ( amountOut || amountOutMin ) ] ;
31373137
31383138 let bestPair = await getBestPair ( { tokenIn : path [ 1 ] , tokenOut : path [ 0 ] , amountOut, amountOutMin } ) ;
31393139 if ( ! bestPair ) { return }
3140- amounts . push ( ethers$1 . BigNumber . from ( bestPair . price ) ) ;
3140+ amounts . push ( ethers . BigNumber . from ( bestPair . price ) ) ;
31413141
31423142 if ( path . length === 3 ) {
31433143 let bestPair = await getBestPair ( { tokenIn : path [ 2 ] , tokenOut : path [ 1 ] , amountOut : amountOut ? amounts [ 1 ] : undefined , amountOutMin : amountOutMin ? amounts [ 1 ] : undefined } ) ;
31443144 if ( ! bestPair ) { return }
3145- amounts . push ( ethers$1 . BigNumber . from ( bestPair . price ) ) ;
3145+ amounts . push ( ethers . BigNumber . from ( bestPair . price ) ) ;
31463146 }
31473147
31483148 if ( amounts . length != path . length ) { return }
@@ -3631,7 +3631,7 @@ const getInputAmount$1 = async ({ exchange, pool, outputAmount })=>{
36313631 api : exchange [ pool . blockchain ] . quoter . api ,
36323632 method : 'quoteExactOutput' ,
36333633 params : {
3634- path : ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pool . path [ 1 ] , pool . fee , pool . path [ 0 ] ] ) ,
3634+ path : ethers . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pool . path [ 1 ] , pool . fee , pool . path [ 0 ] ] ) ,
36353635 amountOut : outputAmount
36363636 } ,
36373637 cache : 5
@@ -3648,7 +3648,7 @@ const getOutputAmount$1 = async ({ exchange, pool, inputAmount })=>{
36483648 api : exchange [ pool . blockchain ] . quoter . api ,
36493649 method : 'quoteExactInput' ,
36503650 params : {
3651- path : ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pool . path [ 0 ] , pool . fee , pool . path [ 1 ] ] ) ,
3651+ path : ethers . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pool . path [ 0 ] , pool . fee , pool . path [ 1 ] ] ) ,
36523652 amountIn : inputAmount
36533653 } ,
36543654 cache : 5
@@ -3812,11 +3812,11 @@ let getAmountIn$2 = async ({ blockchain, exchange, path, pools, amountOut, block
38123812
38133813 let path ;
38143814 if ( pools . length == 2 ) {
3815- path = ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" , "uint24" , "address" ] , [
3815+ path = ethers . utils . solidityPack ( [ "address" , "uint24" , "address" , "uint24" , "address" ] , [
38163816 pools [ 1 ] . path [ 1 ] , pools [ 1 ] . fee , pools [ 0 ] . path [ 1 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 0 ]
38173817 ] ) ;
38183818 } else if ( pools . length == 1 ) {
3819- path = ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [
3819+ path = ethers . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [
38203820 pools [ 0 ] . path [ 1 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 0 ]
38213821 ] ) ;
38223822 }
@@ -3954,7 +3954,7 @@ let getTransaction$3 = async({
39543954 transaction . method = 'multicall' ;
39553955
39563956 // multicall calls itself
3957- const routerInterface = new ethers$1 . utils . Interface ( exchange [ blockchain ] . smartRouter . api ) ;
3957+ const routerInterface = new ethers . utils . Interface ( exchange [ blockchain ] . smartRouter . api ) ;
39583958 transaction . params = { data : [ ] } ;
39593959
39603960 if ( exchangePath . length === 2 ) { // single swap
@@ -3988,7 +3988,7 @@ let getTransaction$3 = async({
39883988 }
39893989 } else { // multi swap
39903990
3991- const packedPath = ethers$1 . utils . solidityPack (
3991+ const packedPath = ethers . utils . solidityPack (
39923992 [ "address" , "uint24" , "address" , "uint24" , "address" ] ,
39933993 [ pools [ 0 ] . path [ 0 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 1 ] , pools [ 1 ] . fee , pools [ 1 ] . path [ 1 ] ]
39943994 ) ;
@@ -4066,7 +4066,7 @@ let getTransaction$3 = async({
40664066 }
40674067 } else { // multi swap
40684068
4069- const packedPath = ethers$1 . utils . solidityPack (
4069+ const packedPath = ethers . utils . solidityPack (
40704070 [ "address" , "uint24" , "address" , "uint24" , "address" ] ,
40714071 [ pools [ 0 ] . path [ 0 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 1 ] , pools [ 1 ] . fee , pools [ 1 ] . path [ 1 ] ]
40724072 ) ;
@@ -4779,7 +4779,7 @@ const getInputAmount = async ({ exchange, pool, outputAmount })=>{
47794779 api : exchange [ pool . blockchain ] . quoter . api ,
47804780 method : 'quoteExactOutput' ,
47814781 params : {
4782- path : ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pool . path [ 1 ] , pool . fee , pool . path [ 0 ] ] ) ,
4782+ path : ethers . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pool . path [ 1 ] , pool . fee , pool . path [ 0 ] ] ) ,
47834783 amountOut : outputAmount
47844784 } ,
47854785 cache : 5
@@ -4796,7 +4796,7 @@ const getOutputAmount = async ({ exchange, pool, inputAmount })=>{
47964796 api : exchange [ pool . blockchain ] . quoter . api ,
47974797 method : 'quoteExactInput' ,
47984798 params : {
4799- path : ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pool . path [ 0 ] , pool . fee , pool . path [ 1 ] ] ) ,
4799+ path : ethers . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pool . path [ 0 ] , pool . fee , pool . path [ 1 ] ] ) ,
48004800 amountIn : inputAmount
48014801 } ,
48024802 cache : 5
@@ -4840,17 +4840,17 @@ const getBestPool = async ({ blockchain, exchange, path, amountIn, amountOut, bl
48404840 let amount ;
48414841 if ( amountIn ) {
48424842 amount = await getOutputAmount ( { exchange, pool, inputAmount : amountIn } ) ;
4843- const amountScaled = await getOutputAmount ( { exchange, pool, inputAmount : ethers$1 . BigNumber . from ( amountIn ) . mul ( ethers$1 . BigNumber . from ( 10 ) ) . toString ( ) } ) ;
4844- const amountScaledDown = amountScaled . div ( ethers$1 . BigNumber . from ( 10 ) ) ;
4843+ const amountScaled = await getOutputAmount ( { exchange, pool, inputAmount : ethers . BigNumber . from ( amountIn ) . mul ( ethers . BigNumber . from ( 10 ) ) . toString ( ) } ) ;
4844+ const amountScaledDown = amountScaled . div ( ethers . BigNumber . from ( 10 ) ) ;
48454845 const difference = amountScaledDown . sub ( amount ) . abs ( ) ;
4846- const enoughLiquidity = ! difference . gt ( amount . mul ( ethers$1 . BigNumber . from ( 5 ) ) . div ( ethers$1 . BigNumber . from ( 100 ) ) ) ; // up to 5% diff allowed
4846+ const enoughLiquidity = ! difference . gt ( amount . mul ( ethers . BigNumber . from ( 5 ) ) . div ( ethers . BigNumber . from ( 100 ) ) ) ; // up to 5% diff allowed
48474847 if ( ! enoughLiquidity ) { return }
48484848 } else {
48494849 amount = await getInputAmount ( { exchange, pool, outputAmount : amountOut } ) ;
4850- const amountScaled = await getInputAmount ( { exchange, pool, outputAmount : ethers$1 . BigNumber . from ( amountOut ) . mul ( ethers$1 . BigNumber . from ( 10 ) ) . toString ( ) } ) ;
4851- const amountScaledDown = amountScaled . div ( ethers$1 . BigNumber . from ( 10 ) ) ;
4850+ const amountScaled = await getInputAmount ( { exchange, pool, outputAmount : ethers . BigNumber . from ( amountOut ) . mul ( ethers . BigNumber . from ( 10 ) ) . toString ( ) } ) ;
4851+ const amountScaledDown = amountScaled . div ( ethers . BigNumber . from ( 10 ) ) ;
48524852 const difference = amountScaledDown . sub ( amount ) . abs ( ) ;
4853- const enoughLiquidity = ! difference . gt ( amount . mul ( ethers$1 . BigNumber . from ( 5 ) ) . div ( ethers$1 . BigNumber . from ( 100 ) ) ) ; // up to 5% diff allowed
4853+ const enoughLiquidity = ! difference . gt ( amount . mul ( ethers . BigNumber . from ( 5 ) ) . div ( ethers . BigNumber . from ( 100 ) ) ) ; // up to 5% diff allowed
48544854 if ( ! enoughLiquidity ) { return }
48554855 }
48564856
@@ -4970,11 +4970,11 @@ let getAmountIn = async ({ blockchain, exchange, path, pools, amountOut, block }
49704970
49714971 let path ;
49724972 if ( pools . length == 2 ) {
4973- path = ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" , "uint24" , "address" ] , [
4973+ path = ethers . utils . solidityPack ( [ "address" , "uint24" , "address" , "uint24" , "address" ] , [
49744974 pools [ 1 ] . path [ 1 ] , pools [ 1 ] . fee , pools [ 0 ] . path [ 1 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 0 ]
49754975 ] ) ;
49764976 } else if ( pools . length == 1 ) {
4977- path = ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [
4977+ path = ethers . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [
49784978 pools [ 0 ] . path [ 1 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 0 ]
49794979 ] ) ;
49804980 }
@@ -5072,19 +5072,19 @@ let getPrep = async({
50725072
50735073let packPath = ( pools ) => {
50745074 if ( pools . length == 1 ) {
5075- return ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pools [ 0 ] . path [ 0 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 1 ] ] )
5075+ return ethers . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pools [ 0 ] . path [ 0 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 1 ] ] )
50765076 } else if ( pools . length == 2 ) {
5077- return ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" , "uint24" , "address" ] , [ pools [ 0 ] . path [ 0 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 1 ] , pools [ 1 ] . fee , pools [ 1 ] . path [ 1 ] ] )
5077+ return ethers . utils . solidityPack ( [ "address" , "uint24" , "address" , "uint24" , "address" ] , [ pools [ 0 ] . path [ 0 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 1 ] , pools [ 1 ] . fee , pools [ 1 ] . path [ 1 ] ] )
50785078 } else {
50795079 throw 'more than 2 pools not supported!'
50805080 }
50815081} ;
50825082
50835083let packPathReverse = ( pools ) => {
50845084 if ( pools . length == 1 ) {
5085- return ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pools [ 0 ] . path [ 1 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 0 ] ] )
5085+ return ethers . utils . solidityPack ( [ "address" , "uint24" , "address" ] , [ pools [ 0 ] . path [ 1 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 0 ] ] )
50865086 } else if ( pools . length == 2 ) {
5087- return ethers$1 . utils . solidityPack ( [ "address" , "uint24" , "address" , "uint24" , "address" ] , [ pools [ 1 ] . path [ 1 ] , pools [ 1 ] . fee , pools [ 1 ] . path [ 0 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 0 ] ] )
5087+ return ethers . utils . solidityPack ( [ "address" , "uint24" , "address" , "uint24" , "address" ] , [ pools [ 1 ] . path [ 1 ] , pools [ 1 ] . fee , pools [ 1 ] . path [ 0 ] , pools [ 0 ] . fee , pools [ 0 ] . path [ 0 ] ] )
50885088 } else {
50895089 throw 'more than 2 pools not supported!'
50905090 }
@@ -5108,7 +5108,7 @@ let getTransaction$1 = async({
51085108} ) => {
51095109
51105110 let value = "0" ;
5111- const contract = new ethers$1 . Contract ( exchange [ blockchain ] . router . address , exchange [ blockchain ] . router . api ) ;
5111+ const contract = new ethers . Contract ( exchange [ blockchain ] . router . address , exchange [ blockchain ] . router . api ) ;
51125112 const exactInput = ! ! ( amountOutMinInput || amountInInput ) ;
51135113 const wrapETH = path [ 0 ] === Blockchains [ blockchain ] . currency . address ;
51145114 const unwrapETH = path [ path . length - 1 ] === Blockchains [ blockchain ] . currency . address ;
0 commit comments