github.com/status-im/status-go@v1.1.0/services/wallet/thirdparty/paraswap/request_price_route_test.go (about)

     1  package paraswap
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/ethereum/go-ethereum/common"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/status-im/status-go/services/wallet/bigint"
    13  )
    14  
    15  func TestUnmarshallPriceRoute(t *testing.T) {
    16  
    17  	data := []byte(`{
    18  			"blockNumber": 13015909,
    19  			"network": 1,
    20  			"srcToken": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
    21  			"srcDecimals": 18,
    22  			"srcAmount": "1000000000000000000",
    23  			"destToken": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
    24  			"destDecimals": 18,
    25  			"destAmount": "1000000000000000000",
    26  			"bestRoute": {
    27  				"percent": 100,
    28  				"swaps": [
    29  					{
    30  						"srcToken": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
    31  						"srcDecimals": 0,
    32  						"destToken": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
    33  						"destDecimals": 0,
    34  						"swapExchanges": [
    35  							{
    36  								"exchange": "UniswapV2",
    37  								"srcAmount": "1000000000000000000",
    38  								"destAmount": "1000000000000000000",
    39  								"percent": 100,
    40  								"data": {
    41  									"router": "0x0000000000000000000000000000000000000000",
    42  									"path": [
    43  										"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
    44  										"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
    45  									],
    46  									"factory": "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
    47  									"initCode": "0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f",
    48  									"feeFactor": 10000,
    49  									"pools": [
    50  										{
    51  											"address": "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc",
    52  											"fee": 30,
    53  											"direction": false
    54  										}
    55  									],
    56  									"gasUSD": "13.227195"
    57  								}
    58  							}
    59  						]
    60  					}
    61  				]
    62  			},
    63  			"others": {
    64  				"exchange": "UniswapV2",
    65  				"srcAmount": "1000000000000000000",
    66  				"destAmount": "3255989380",
    67  				"unit": "3255989380",
    68  				"data": {
    69  					"router": "0x0000000000000000000000000000000000000000",
    70  					"path": [
    71  						"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
    72  						"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
    73  					],
    74  					"factory": "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f",
    75  					"initCode": "0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f",
    76  					"feeFactor": 10000,
    77  					"pools": [
    78  						{
    79  							"address": "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc",
    80  							"fee": 30,
    81  							"direction": false
    82  						}
    83  					],
    84  					"gasUSD": "13.227195"
    85  				}
    86  			},
    87  			"gasCostUSD": "11.947163",
    88  			"gasCost": "111435",
    89  			"side": "SELL",
    90  			"tokenTransferProxy": "0x3e7d31751347BAacf35945074a4a4A41581B2271",
    91  			"contractAddress": "0x485D2446711E141D2C8a94bC24BeaA5d5A110D74",
    92  			"contractMethod": "swapOnUniswap",
    93  			"srcUSD": "3230.3000000000",
    94  			"destUSD": "3218.9300566052",
    95  			"partner": "paraswap.io",
    96  			"partnerFee": 0,
    97  			"maxImpactReached": false,
    98  			"hmac": "319c5cf83098a07aeebb11bed6310db51311201f"
    99  	}`)
   100  
   101  	responseData := []byte(fmt.Sprintf(`{"priceRoute":%s}`, string(data)))
   102  
   103  	route := Route{
   104  		GasCost:            &bigint.BigInt{Int: big.NewInt(111435)},
   105  		SrcAmount:          &bigint.BigInt{Int: big.NewInt(1000000000000000000)},
   106  		SrcTokenAddress:    common.HexToAddress("0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"),
   107  		SrcTokenDecimals:   18,
   108  		DestAmount:         &bigint.BigInt{Int: big.NewInt(1000000000000000000)},
   109  		DestTokenAddress:   common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"),
   110  		DestTokenDecimals:  18,
   111  		Side:               SellSide,
   112  		RawPriceRoute:      data,
   113  		ContractAddress:    common.HexToAddress("0x485D2446711E141D2C8a94bC24BeaA5d5A110D74"),
   114  		TokenTransferProxy: common.HexToAddress("0x3e7d31751347BAacf35945074a4a4A41581B2271"),
   115  	}
   116  
   117  	receivedRoute, err := handlePriceRouteResponse(responseData)
   118  	assert.NoError(t, err)
   119  	assert.Equal(t, route, receivedRoute)
   120  }
   121  
   122  func TestForErrorOnFetchingPriceRoute(t *testing.T) {
   123  	data := []byte(`{
   124  		"error": "Invalid tokens"
   125  	}`)
   126  
   127  	_, err := handlePriceRouteResponse(data)
   128  	assert.Error(t, err)
   129  }