code.vegaprotocol.io/vega@v0.79.0/core/datasource/external/ethcall/json_args_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package ethcall_test
    17  
    18  import (
    19  	"math/big"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/core/datasource/external/ethcall"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  var TEST_ABI = `[
    30      {
    31          "inputs": [
    32              {"internalType": "int64","name": "","type": "int64"},
    33              {"internalType": "uint256","name": "","type": "uint256"},
    34              {"internalType": "string","name": "","type": "string"},
    35              {"internalType": "bool","name": "","type": "bool"},
    36              {"internalType": "address","name": "","type": "address"},
    37              {"internalType": "int256[]","name": "","type": "int256[]"},
    38              {
    39                  "components": [
    40                      {"internalType": "string","name": "name","type": "string"},
    41                      {"internalType": "uint16","name": "age","type": "uint16"
    42                      }
    43                  ],"internalType": "struct MyContract.Person","name": "","type": "tuple"
    44              }
    45          ],
    46          "name": "testy",
    47          "outputs": [
    48              {"internalType": "uint256","name": "","type": "uint256"}
    49          ],
    50          "stateMutability": "pure",
    51          "type": "function"
    52      }
    53  ]`
    54  
    55  func TestJsonArgsToAny(t *testing.T) {
    56  	goArgs := []any{
    57  		int64(42),
    58  		big.NewInt(42),
    59  		"hello",
    60  		true,
    61  		common.HexToAddress("0xb794f5ea0ba39494ce839613fffba74279579268"),
    62  		[]*big.Int{big.NewInt(10), big.NewInt(20)},
    63  		struct {
    64  			Name string `json:"name"`
    65  			Age  uint16 `json:"age"`
    66  		}{Name: "test", Age: 42},
    67  	}
    68  
    69  	jsonArgs, err := ethcall.AnyArgsToJson(goArgs)
    70  	require.NoError(t, err)
    71  
    72  	anyArgs, err := ethcall.JsonArgsToAny("testy", jsonArgs, []byte(TEST_ABI))
    73  	require.NoError(t, err)
    74  	assert.Equal(t, goArgs, anyArgs)
    75  }