Skip to content

Commit

Permalink
use bigint
Browse files Browse the repository at this point in the history
yo
  • Loading branch information
andrewkmin committed Mar 21, 2022
1 parent a9a4621 commit 0e7c7b1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
19 changes: 10 additions & 9 deletions services/construction/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 25 additions & 13 deletions services/construction/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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",
Expand Down

0 comments on commit 0e7c7b1

Please sign in to comment.