github.com/cerberus-wallet/blockbook@v0.3.2/bchain/coins/eth/erc20_test.go (about) 1 // +build unittest 2 3 package eth 4 5 import ( 6 "blockbook/bchain" 7 "blockbook/tests/dbtestdata" 8 fmt "fmt" 9 "math/big" 10 "strings" 11 "testing" 12 ) 13 14 func TestErc20_erc20GetTransfersFromLog(t *testing.T) { 15 tests := []struct { 16 name string 17 args []*rpcLog 18 want []bchain.Erc20Transfer 19 wantErr bool 20 }{ 21 { 22 name: "1", 23 args: []*rpcLog{ 24 { 25 Address: "0x76a45e8976499ab9ae223cc584019341d5a84e96", 26 Topics: []string{ 27 "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", 28 "0x0000000000000000000000002aacf811ac1a60081ea39f7783c0d26c500871a8", 29 "0x000000000000000000000000e9a5216ff992cfa01594d43501a56e12769eb9d2", 30 }, 31 Data: "0x0000000000000000000000000000000000000000000000000000000000000123", 32 }, 33 }, 34 want: []bchain.Erc20Transfer{ 35 { 36 Contract: "0x76a45e8976499ab9ae223cc584019341d5a84e96", 37 From: "0x2aacf811ac1a60081ea39f7783c0d26c500871a8", 38 To: "0xe9a5216ff992cfa01594d43501a56e12769eb9d2", 39 Tokens: *big.NewInt(0x123), 40 }, 41 }, 42 }, 43 { 44 name: "2", 45 args: []*rpcLog{ 46 { // Transfer 47 Address: "0x0d0f936ee4c93e25944694d6c121de94d9760f11", 48 Topics: []string{ 49 "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", 50 "0x0000000000000000000000006f44cceb49b4a5812d54b6f494fc2febf25511ed", 51 "0x0000000000000000000000004bda106325c335df99eab7fe363cac8a0ba2a24d", 52 }, 53 Data: "0x0000000000000000000000000000000000000000000000006a8313d60b1f606b", 54 }, 55 { // Transfer 56 Address: "0xc778417e063141139fce010982780140aa0cd5ab", 57 Topics: []string{ 58 "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", 59 "0x0000000000000000000000004bda106325c335df99eab7fe363cac8a0ba2a24d", 60 "0x0000000000000000000000006f44cceb49b4a5812d54b6f494fc2febf25511ed", 61 }, 62 Data: "0x000000000000000000000000000000000000000000000000000308fd0e798ac0", 63 }, 64 { // not Transfer 65 Address: "0x479cc461fecd078f766ecc58533d6f69580cf3ac", 66 Topics: []string{ 67 "0x0d0b9391970d9a25552f37d436d2aae2925e2bfe1b2a923754bada030c498cb3", 68 "0x0000000000000000000000006f44cceb49b4a5812d54b6f494fc2febf25511ed", 69 "0x0000000000000000000000000000000000000000000000000000000000000000", 70 "0x5af266c0a89a07c1917deaa024414577e6c3c31c8907d079e13eb448c082594f", 71 }, 72 Data: "0x0000000000000000000000004bda106325c335df99eab7fe363cac8a0ba2a24d0000000000000", 73 }, 74 { // not Transfer 75 Address: "0x0d0f936ee4c93e25944694d6c121de94d9760f11", 76 Topics: []string{ 77 "0x0d0b9391970d9a25552f37d436d2aae2925e2bfe1b2a923754bada030c498cb3", 78 "0x0000000000000000000000007b62eb7fe80350dc7ec945c0b73242cb9877fb1b", 79 "0xb0b69dad58df6032c3b266e19b1045b19c87acd2c06fb0c598090f44b8e263aa", 80 }, 81 Data: "0x0000000000000000000000004bda106325c335df99eab7fe363cac8a0ba2a24d000000000000000000000000c778417e063141139fce010982780140aa0cd5ab0000000000000000000000000d0f936ee4c93e25944694d6c121de94d9760f1100000000000000000000000000000000000000000000000000031855667df7a80000000000000000000000000000000000000000000000006a8313d60b1f800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 82 }, 83 }, 84 want: []bchain.Erc20Transfer{ 85 { 86 Contract: "0x0d0f936ee4c93e25944694d6c121de94d9760f11", 87 From: "0x6f44cceb49b4a5812d54b6f494fc2febf25511ed", 88 To: "0x4bda106325c335df99eab7fe363cac8a0ba2a24d", 89 Tokens: *big.NewInt(0x6a8313d60b1f606b), 90 }, 91 { 92 Contract: "0xc778417e063141139fce010982780140aa0cd5ab", 93 From: "0x4bda106325c335df99eab7fe363cac8a0ba2a24d", 94 To: "0x6f44cceb49b4a5812d54b6f494fc2febf25511ed", 95 Tokens: *big.NewInt(0x308fd0e798ac0), 96 }, 97 }, 98 }, 99 } 100 for _, tt := range tests { 101 t.Run(tt.name, func(t *testing.T) { 102 got, err := erc20GetTransfersFromLog(tt.args) 103 if (err != nil) != tt.wantErr { 104 t.Errorf("erc20GetTransfersFromLog error = %v, wantErr %v", err, tt.wantErr) 105 return 106 } 107 // the addresses could have different case 108 if strings.ToLower(fmt.Sprint(got)) != strings.ToLower(fmt.Sprint(tt.want)) { 109 t.Errorf("erc20GetTransfersFromLog = %+v, want %+v", got, tt.want) 110 } 111 }) 112 } 113 } 114 115 func TestErc20_parseErc20StringProperty(t *testing.T) { 116 tests := []struct { 117 name string 118 args string 119 want string 120 }{ 121 { 122 name: "1", 123 args: "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000758504c4f44444500000000000000000000000000000000000000000000000000", 124 want: "XPLODDE", 125 }, 126 { 127 name: "2", 128 args: "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000022426974436c617665202d20436f6e73756d657220416374697669747920546f6b656e00000000000000", 129 want: "BitClave - Consumer Activity Token", 130 }, 131 { 132 name: "short", 133 args: "0x44616920537461626c65636f696e2076312e3000000000000000000000000000", 134 want: "Dai Stablecoin v1.0", 135 }, 136 { 137 name: "short2", 138 args: "0x44616920537461626c65636f696e2076312e3020444444444444444444444444", 139 want: "Dai Stablecoin v1.0 DDDDDDDDDDDD", 140 }, 141 } 142 for _, tt := range tests { 143 t.Run(tt.name, func(t *testing.T) { 144 got := parseErc20StringProperty(nil, tt.args) 145 // the addresses could have different case 146 if got != tt.want { 147 t.Errorf("parseErc20StringProperty = %v, want %v", got, tt.want) 148 } 149 }) 150 } 151 } 152 153 func TestErc20_erc20GetTransfersFromTx(t *testing.T) { 154 p := NewEthereumParser(1) 155 b := dbtestdata.GetTestEthereumTypeBlock1(p) 156 bn, _ := new(big.Int).SetString("21e19e0c9bab2400000", 16) 157 tests := []struct { 158 name string 159 args *rpcTransaction 160 want []bchain.Erc20Transfer 161 }{ 162 { 163 name: "0", 164 args: (b.Txs[0].CoinSpecificData.(completeTransaction)).Tx, 165 want: []bchain.Erc20Transfer{}, 166 }, 167 { 168 name: "1", 169 args: (b.Txs[1].CoinSpecificData.(completeTransaction)).Tx, 170 want: []bchain.Erc20Transfer{ 171 { 172 Contract: "0x4af4114f73d1c1c903ac9e0361b379d1291808a2", 173 From: "0x20cd153de35d469ba46127a0c8f18626b59a256a", 174 To: "0x555ee11fbddc0e49a9bab358a8941ad95ffdb48f", 175 Tokens: *bn, 176 }, 177 }, 178 }, 179 } 180 for _, tt := range tests { 181 t.Run(tt.name, func(t *testing.T) { 182 got, err := erc20GetTransfersFromTx(tt.args) 183 if err != nil { 184 t.Errorf("erc20GetTransfersFromTx error = %v", err) 185 return 186 } 187 // the addresses could have different case 188 if strings.ToLower(fmt.Sprint(got)) != strings.ToLower(fmt.Sprint(tt.want)) { 189 t.Errorf("erc20GetTransfersFromTx = %+v, want %+v", got, tt.want) 190 } 191 }) 192 } 193 }