github.com/ethersphere/bee/v2@v2.2.0/pkg/settlement/swap/erc20/erc20_test.go (about) 1 // Copyright 2021 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package erc20_test 6 7 import ( 8 "context" 9 "math/big" 10 "testing" 11 12 "github.com/ethereum/go-ethereum/common" 13 "github.com/ethersphere/bee/v2/pkg/settlement/swap/erc20" 14 transactionmock "github.com/ethersphere/bee/v2/pkg/transaction/mock" 15 "github.com/ethersphere/bee/v2/pkg/util/abiutil" 16 "github.com/ethersphere/go-sw3-abi/sw3abi" 17 ) 18 19 var ( 20 erc20ABI = abiutil.MustParseABI(sw3abi.ERC20ABIv0_6_5) 21 ) 22 23 func TestBalanceOf(t *testing.T) { 24 t.Parallel() 25 26 erc20Address := common.HexToAddress("00") 27 account := common.HexToAddress("01") 28 expectedBalance := big.NewInt(100) 29 30 erc20 := erc20.New( 31 transactionmock.New( 32 transactionmock.WithABICall( 33 &erc20ABI, 34 erc20Address, 35 expectedBalance.FillBytes(make([]byte, 32)), 36 "balanceOf", 37 account, 38 ), 39 ), 40 erc20Address, 41 ) 42 43 balance, err := erc20.BalanceOf(context.Background(), account) 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 if expectedBalance.Cmp(balance) != 0 { 49 t.Fatalf("got wrong balance. wanted %d, got %d", expectedBalance, balance) 50 } 51 } 52 53 func TestTransfer(t *testing.T) { 54 t.Parallel() 55 56 address := common.HexToAddress("0xabcd") 57 account := common.HexToAddress("01") 58 value := big.NewInt(20) 59 txHash := common.HexToHash("0xdddd") 60 61 erc20 := erc20.New( 62 transactionmock.New( 63 transactionmock.WithABISend(&erc20ABI, txHash, address, big.NewInt(0), "transfer", account, value), 64 ), 65 address, 66 ) 67 68 returnedTxHash, err := erc20.Transfer(context.Background(), account, value) 69 if err != nil { 70 t.Fatal(err) 71 } 72 73 if txHash != returnedTxHash { 74 t.Fatalf("returned wrong transaction hash. wanted %v, got %v", txHash, returnedTxHash) 75 } 76 }