github.com/ethersphere/bee/v2@v2.2.0/pkg/settlement/swap/chequebook/contract.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 6 7 import ( 8 "context" 9 "math/big" 10 11 "github.com/ethereum/go-ethereum/accounts/abi" 12 "github.com/ethereum/go-ethereum/common" 13 "github.com/ethersphere/bee/v2/pkg/transaction" 14 ) 15 16 type chequebookContract struct { 17 address common.Address 18 transactionService transaction.Service 19 } 20 21 func newChequebookContract(address common.Address, transactionService transaction.Service) *chequebookContract { 22 return &chequebookContract{ 23 address: address, 24 transactionService: transactionService, 25 } 26 } 27 28 func (c *chequebookContract) Issuer(ctx context.Context) (common.Address, error) { 29 callData, err := chequebookABI.Pack("issuer") 30 if err != nil { 31 return common.Address{}, err 32 } 33 34 output, err := c.transactionService.Call(ctx, &transaction.TxRequest{ 35 To: &c.address, 36 Data: callData, 37 }) 38 if err != nil { 39 return common.Address{}, err 40 } 41 42 results, err := chequebookABI.Unpack("issuer", output) 43 if err != nil { 44 return common.Address{}, err 45 } 46 47 return *abi.ConvertType(results[0], new(common.Address)).(*common.Address), nil 48 } 49 50 // Balance returns the token balance of the chequebook. 51 func (c *chequebookContract) Balance(ctx context.Context) (*big.Int, error) { 52 callData, err := chequebookABI.Pack("balance") 53 if err != nil { 54 return nil, err 55 } 56 57 output, err := c.transactionService.Call(ctx, &transaction.TxRequest{ 58 To: &c.address, 59 Data: callData, 60 }) 61 if err != nil { 62 return nil, err 63 } 64 65 results, err := chequebookABI.Unpack("balance", output) 66 if err != nil { 67 return nil, err 68 } 69 70 return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil 71 } 72 73 func (c *chequebookContract) PaidOut(ctx context.Context, address common.Address) (*big.Int, error) { 74 callData, err := chequebookABI.Pack("paidOut", address) 75 if err != nil { 76 return nil, err 77 } 78 79 output, err := c.transactionService.Call(ctx, &transaction.TxRequest{ 80 To: &c.address, 81 Data: callData, 82 }) 83 if err != nil { 84 return nil, err 85 } 86 87 results, err := chequebookABI.Unpack("paidOut", output) 88 if err != nil { 89 return nil, err 90 } 91 92 return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil 93 } 94 95 func (c *chequebookContract) TotalPaidOut(ctx context.Context) (*big.Int, error) { 96 callData, err := chequebookABI.Pack("totalPaidOut") 97 if err != nil { 98 return nil, err 99 } 100 101 output, err := c.transactionService.Call(ctx, &transaction.TxRequest{ 102 To: &c.address, 103 Data: callData, 104 }) 105 if err != nil { 106 return nil, err 107 } 108 109 results, err := chequebookABI.Unpack("totalPaidOut", output) 110 if err != nil { 111 return nil, err 112 } 113 114 return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil 115 }