github.com/0xsequence/ethkit@v1.25.0/ethcoder/solidity_pack_test.go (about) 1 package ethcoder 2 3 import ( 4 "math/big" 5 "testing" 6 7 "github.com/0xsequence/ethkit/go-ethereum/common" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestSolidityPack(t *testing.T) { 12 // string 13 { 14 // ethers.utils.solidityPack(['string'], ['peϣer']) 15 // "0x7065cfa36572" 16 h, err := solidityArgumentPackHex("string", "peϣer", false) 17 assert.NoError(t, err) 18 assert.Equal(t, "0x7065cfa36572", h) 19 } 20 21 // address 22 { 23 // ethers.utils.solidityPack(['address'], ['0x39d28D4c4191a584acabe021F5B905887a6B5247']) 24 // "0x39d28d4c4191a584acabe021f5b905887a6b5247" 25 h, err := solidityArgumentPackHex("address", common.HexToAddress("0x39d28D4c4191a584acabe021F5B905887a6B5247"), false) 26 assert.NoError(t, err) 27 assert.Equal(t, "0x39d28d4c4191a584acabe021f5b905887a6b5247", h) 28 } 29 30 // bytes 31 { 32 // ethers.utils.solidityPack(['bytes'], [[0,1,2,3]]) 33 // "0x00010203" 34 h, err := solidityArgumentPackHex("bytes", []byte{0, 1, 2, 3}, false) 35 assert.NoError(t, err) 36 assert.Equal(t, "0x00010203", h) 37 } 38 39 // bool 40 { 41 // ethers.utils.solidityPack(['bool'], [true]) 42 // "0x01" 43 h, err := solidityArgumentPackHex("bool", true, false) 44 assert.NoError(t, err) 45 assert.Equal(t, "0x01", h) 46 47 h, err = solidityArgumentPackHex("bool", false, false) 48 assert.NoError(t, err) 49 assert.Equal(t, "0x00", h) 50 } 51 52 // uint256 / big.Int 53 { 54 // ethers.utils.solidityPack(['uint256'], [55]) 55 // "0x0000000000000000000000000000000000000000000000000000000000000037" 56 h, err := solidityArgumentPackHex("uint256", big.NewInt(55), false) 57 assert.NoError(t, err) 58 assert.Equal(t, "0x0000000000000000000000000000000000000000000000000000000000000037", h) 59 } 60 61 // int64 62 { 63 // ethers.utils.solidityPack(['int64'], [4242]) 64 // 0x0000000000001092 65 h, err := solidityArgumentPackHex("int64", int64(4242), false) 66 assert.NoError(t, err) 67 assert.Equal(t, "0x0000000000001092", h) 68 } 69 70 // int32 71 { 72 // ethers.utils.solidityPack(['int32'], [4242]) 73 // 0x00001092 74 h, err := solidityArgumentPackHex("int32", int32(4242), false) 75 assert.NoError(t, err) 76 assert.Equal(t, "0x00001092", h) 77 } 78 79 // uint32 80 { 81 // ethers.utils.solidityPack(['uint32'], [4242]) 82 // 0x00001092 83 h, err := solidityArgumentPackHex("uint32", uint32(4242), false) 84 assert.NoError(t, err) 85 assert.Equal(t, "0x00001092", h) 86 } 87 88 // bytes8 89 { 90 // ethers.utils.solidityPack(['bytes8'], [[0,1,2,3,4,5,6,7]]) 91 // "0x0001020304050607" 92 h, err := solidityArgumentPackHex("bytes8", [8]byte{0, 1, 2, 3, 4, 5, 6, 7}, false) 93 assert.NoError(t, err) 94 assert.Equal(t, "0x0001020304050607", h) 95 } 96 97 // address[] 98 { 99 // ethers.utils.solidityPack(['address[]'], [['0x39d28D4c4191a584acabe021F5B905887a6B5247']]) 100 // "0x00000000000000000000000039d28d4c4191a584acabe021f5b905887a6b5247" 101 h, err := solidityArgumentPackHex("address[]", []common.Address{common.HexToAddress("0x39d28D4c4191a584acabe021F5B905887a6B5247")}, false) 102 assert.NoError(t, err) 103 assert.Equal(t, "0x00000000000000000000000039d28d4c4191a584acabe021f5b905887a6b5247", h) 104 } 105 106 // string[] 107 { 108 // ethers.utils.solidityPack(['string[]'], [['sup','eth']]) 109 // "0x737570657468" 110 h, err := solidityArgumentPackHex("string[]", []string{"sup", "eth"}, false) 111 assert.NoError(t, err) 112 assert.Equal(t, "0x737570657468", h) 113 } 114 115 // bool[] 116 { 117 // ethers.utils.solidityPack(['bool[]'], [[true,true]]) 118 // "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001" 119 h, err := solidityArgumentPackHex("bool[]", []bool{true, true}, false) 120 assert.NoError(t, err) 121 assert.Equal(t, "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001", h) 122 } 123 }