github.com/ethersphere/bee/v2@v2.2.0/pkg/settlement/swap/chequebook/factory_test.go (about) 1 // Copyright 2020 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 chequebook_test 6 7 import ( 8 "context" 9 "errors" 10 "math/big" 11 "testing" 12 13 "github.com/ethereum/go-ethereum/common" 14 "github.com/ethereum/go-ethereum/core/types" 15 "github.com/ethersphere/bee/v2/pkg/settlement/swap/chequebook" 16 "github.com/ethersphere/bee/v2/pkg/transaction" 17 "github.com/ethersphere/bee/v2/pkg/transaction/backendmock" 18 transactionmock "github.com/ethersphere/bee/v2/pkg/transaction/mock" 19 "github.com/ethersphere/bee/v2/pkg/util/abiutil" 20 "github.com/ethersphere/go-sw3-abi/sw3abi" 21 ) 22 23 var ( 24 factoryABI = abiutil.MustParseABI(sw3abi.SimpleSwapFactoryABIv0_6_5) 25 simpleSwapDeployedEvent = factoryABI.Events["SimpleSwapDeployed"] 26 ) 27 28 func TestFactoryERC20Address(t *testing.T) { 29 t.Parallel() 30 31 factoryAddress := common.HexToAddress("0xabcd") 32 erc20Address := common.HexToAddress("0xeffff") 33 factory := chequebook.NewFactory(backendmock.New(), transactionmock.New( 34 transactionmock.WithABICall( 35 &factoryABI, 36 factoryAddress, 37 common.BytesToHash(erc20Address.Bytes()).Bytes(), 38 "ERC20Address", 39 ), 40 ), factoryAddress) 41 42 addr, err := factory.ERC20Address(context.Background()) 43 if err != nil { 44 t.Fatal(err) 45 } 46 47 if addr != erc20Address { 48 t.Fatalf("wrong erc20Address. wanted %x, got %x", erc20Address, addr) 49 } 50 } 51 52 func TestFactoryVerifyChequebook(t *testing.T) { 53 t.Parallel() 54 55 factoryAddress := common.HexToAddress("0xabcd") 56 chequebookAddress := common.HexToAddress("0xefff") 57 58 t.Run("valid", func(t *testing.T) { 59 t.Parallel() 60 61 factory := chequebook.NewFactory(backendmock.New(), transactionmock.New( 62 transactionmock.WithABICall( 63 &factoryABI, 64 factoryAddress, 65 common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), 66 "deployedContracts", 67 chequebookAddress, 68 ), 69 ), factoryAddress) 70 err := factory.VerifyChequebook(context.Background(), chequebookAddress) 71 if err != nil { 72 t.Fatal(err) 73 } 74 }) 75 } 76 77 func TestFactoryDeploy(t *testing.T) { 78 t.Parallel() 79 80 factoryAddress := common.HexToAddress("0xabcd") 81 issuerAddress := common.HexToAddress("0xefff") 82 defaultTimeout := big.NewInt(1) 83 deployTransactionHash := common.HexToHash("0xffff") 84 deployAddress := common.HexToAddress("0xdddd") 85 nonce := common.HexToHash("eeff") 86 87 factory := chequebook.NewFactory(backendmock.New(), transactionmock.New( 88 transactionmock.WithABISend(&factoryABI, deployTransactionHash, factoryAddress, big.NewInt(0), "deploySimpleSwap", issuerAddress, defaultTimeout, nonce), 89 transactionmock.WithWaitForReceiptFunc(func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) { 90 if txHash != deployTransactionHash { 91 t.Fatalf("waiting for wrong transaction. wanted %x, got %x", deployTransactionHash, txHash) 92 } 93 logData, err := simpleSwapDeployedEvent.Inputs.NonIndexed().Pack(deployAddress) 94 if err != nil { 95 t.Fatal(err) 96 } 97 return &types.Receipt{ 98 Status: 1, 99 Logs: []*types.Log{ 100 { 101 Data: logData, 102 }, 103 { 104 Address: factoryAddress, 105 Topics: []common.Hash{simpleSwapDeployedEvent.ID}, 106 Data: logData, 107 }, 108 }, 109 }, nil 110 }, 111 )), factoryAddress) 112 113 txHash, err := factory.Deploy(context.Background(), issuerAddress, defaultTimeout, nonce) 114 if err != nil { 115 t.Fatal(err) 116 } 117 118 if txHash != deployTransactionHash { 119 t.Fatalf("returning wrong transaction hash. wanted %x, got %x", deployTransactionHash, txHash) 120 } 121 122 chequebookAddress, err := factory.WaitDeployed(context.Background(), txHash) 123 if err != nil { 124 t.Fatal(err) 125 } 126 127 if chequebookAddress != deployAddress { 128 t.Fatalf("returning wrong address. wanted %x, got %x", deployAddress, chequebookAddress) 129 } 130 } 131 132 func TestFactoryDeployReverted(t *testing.T) { 133 t.Parallel() 134 135 factoryAddress := common.HexToAddress("0xabcd") 136 deployTransactionHash := common.HexToHash("0xffff") 137 factory := chequebook.NewFactory(backendmock.New(), transactionmock.New( 138 transactionmock.WithWaitForReceiptFunc(func(ctx context.Context, txHash common.Hash) (receipt *types.Receipt, err error) { 139 if txHash != deployTransactionHash { 140 t.Fatalf("waiting for wrong transaction. wanted %x, got %x", deployTransactionHash, txHash) 141 } 142 return &types.Receipt{ 143 Status: 0, 144 }, nil 145 }), 146 ), factoryAddress) 147 148 _, err := factory.WaitDeployed(context.Background(), deployTransactionHash) 149 if err == nil { 150 t.Fatal("returned failed chequebook deployment") 151 } 152 if !errors.Is(err, transaction.ErrTransactionReverted) { 153 t.Fatalf("wrong error. wanted %v, got %v", transaction.ErrTransactionReverted, err) 154 } 155 }