From 0e7c7b1379aecfe64e14fac5a7c5ab1137c1caa1 Mon Sep 17 00:00:00 2001 From: Andrew Min Date: Mon, 21 Mar 2022 00:55:00 -0400 Subject: [PATCH] use bigint yo --- services/construction/preprocess.go | 19 ++++++------ services/construction/preprocess_test.go | 38 ++++++++++++++++-------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/services/construction/preprocess.go b/services/construction/preprocess.go index d1c565f..b6054e6 100644 --- a/services/construction/preprocess.go +++ b/services/construction/preprocess.go @@ -176,16 +176,17 @@ func matchTransferOperations(operations []*types.Operation, isContractCall bool) *types.Operation, error, ) { - valueOne, err := strconv.ParseInt(operations[0].Amount.Value, 10, 64) - if err != nil { - log.Fatal(err) + valueOne, valueTwo := new(big.Int), new(big.Int) + valueOne, ok := valueOne.SetString(operations[0].Amount.Value, 10) + if !ok { + log.Fatal("unable to convert valueOne to bigint") } - valueTwo, err := strconv.ParseInt(operations[1].Amount.Value, 10, 64) - if err != nil { - log.Fatal(err) + valueTwo, ok = valueTwo.SetString(operations[1].Amount.Value, 10) + if !ok { + log.Fatal("unable to convert valueTwo to bigint") } - if isContractCall && valueOne == 0 { - if valueOne != valueTwo { + if isContractCall && valueOne.BitLen() == 0 { + if valueOne.Cmp(valueTwo) != 0 { return nil, nil, errors.New("for generic call both values should be zero") } descriptions := &parser.Descriptions{ @@ -357,7 +358,7 @@ func constructContractCallData(methodSig string, methodArgs []string) ([]byte, e splitSigByComma := strings.Split(splitSigByTrailingParenthesis[0], ",") if len(splitSigByComma) != len(methodArgs) { - return nil, errors.New("Invalid method arguments") + return nil, errors.New("invalid method arguments") } for i, v := range splitSigByComma { diff --git a/services/construction/preprocess_test.go b/services/construction/preprocess_test.go index c4cbdd7..77ed8d7 100644 --- a/services/construction/preprocess_test.go +++ b/services/construction/preprocess_test.go @@ -26,19 +26,21 @@ import ( ) var ( - preprocessFromAddress = fromAddress - preprocessToAddress = toAddress - preprocessTokenContractAddress = tokenContractAddress - preprocessZeroTransferValue = uint64(0) - preprocessTransferValue = uint64(1) - preprocessTransferValueHex = hexutil.EncodeUint64(preprocessTransferValue) - preprocessData = "0xa9059cbb000000000000000000000000efd3dc58d60af3295b92ecd484caeb3a2f30b3e70000000000000000000000000000000000000000000000000000000000000001" // nolint - preprocessGasPrice = uint64(100000000000) - preprocessGasPriceHex = hexutil.EncodeUint64(preprocessGasPrice) - preprocessGenericData = "0x095ea7b3000000000000000000000000d10a72cf054650931365cc44d912a4fd7525705800000000000000000000000000000000000000000000000000000000000003e8" - methodSignature = "approve(address,uint256)" - methodArgs = []string{"0xD10a72Cf054650931365Cc44D912a4FD75257058", "1000"} - expectedMethodArgs = []interface{}{"0xD10a72Cf054650931365Cc44D912a4FD75257058", "1000"} + preprocessFromAddress = fromAddress + preprocessToAddress = toAddress + preprocessTokenContractAddress = tokenContractAddress + preprocessZeroTransferValue = uint64(0) + preprocessTransferValue = uint64(1) + preprocessTransferValueLargeValue = uint64(100000000000) + preprocessTransferValueHex = hexutil.EncodeUint64(preprocessTransferValue) + preprocessTransferValueLargeHex = hexutil.EncodeUint64(preprocessTransferValueLargeValue) + preprocessData = "0xa9059cbb000000000000000000000000efd3dc58d60af3295b92ecd484caeb3a2f30b3e70000000000000000000000000000000000000000000000000000000000000001" // nolint + preprocessGasPrice = uint64(100000000000) + preprocessGasPriceHex = hexutil.EncodeUint64(preprocessGasPrice) + preprocessGenericData = "0x095ea7b3000000000000000000000000d10a72cf054650931365cc44d912a4fd7525705800000000000000000000000000000000000000000000000000000000000003e8" + methodSignature = "approve(address,uint256)" + methodArgs = []string{"0xD10a72Cf054650931365Cc44D912a4FD75257058", "1000"} + expectedMethodArgs = []interface{}{"0xD10a72Cf054650931365Cc44D912a4FD75257058", "1000"} ) func TestPreprocess(t *testing.T) { @@ -59,6 +61,16 @@ func TestPreprocess(t *testing.T) { }, }, }, + "happy path: native currency with large amount": { + operations: templateOperations(preprocessTransferValueLargeValue, polygon.Currency), + expectedResponse: &types.ConstructionPreprocessResponse{ + Options: map[string]interface{}{ + "from": preprocessFromAddress, + "to": preprocessToAddress, + "value": preprocessTransferValueLargeHex, + }, + }, + }, "happy path: ERC20 currency": { operations: templateOperations(preprocessTransferValue, &types.Currency{ Symbol: "USDC",