@@ -29,13 +29,13 @@ import (
29
29
"github.com/ethereum/go-ethereum/accounts/abi/bind"
30
30
"github.com/ethereum/go-ethereum/common"
31
31
"github.com/ethereum/go-ethereum/core/types"
32
- "github.com/ethersphere/swarm /contracts/swap/contract "
32
+ contract "github.com/ethersphere/go-sw3 /contracts-v0-1-0/simpleswap "
33
33
)
34
34
35
35
var (
36
36
// ErrNotASwapContract is given when an address is verified not to have a SWAP contract based on its bytecode
37
37
ErrNotASwapContract = errors .New ("not a swap contract" )
38
- // ErrTransactionReverted is given when the transaction that submits or cashes a cheque is reverted
38
+ // ErrTransactionReverted is given when the transaction that cashes a cheque is reverted
39
39
ErrTransactionReverted = errors .New ("Transaction reverted" )
40
40
)
41
41
@@ -48,7 +48,7 @@ type Backend interface {
48
48
// Deploy deploys an instance of the underlying contract and returns its `Contract` abstraction
49
49
func Deploy (auth * bind.TransactOpts , backend bind.ContractBackend , owner common.Address , harddepositTimeout time.Duration ) (common.Address , Contract , * types.Transaction , error ) {
50
50
addr , tx , s , err := contract .DeploySimpleSwap (auth , backend , owner , big .NewInt (int64 (harddepositTimeout )))
51
- c := simpleContract {instance : s }
51
+ c := simpleContract {instance : s , address : addr }
52
52
return addr , c , tx , err
53
53
}
54
54
@@ -57,30 +57,31 @@ func Deploy(auth *bind.TransactOpts, backend bind.ContractBackend, owner common.
57
57
// This function is needed to communicate with remote Swap contracts (e.g. sending a cheque)
58
58
func InstanceAt (address common.Address , backend bind.ContractBackend ) (Contract , error ) {
59
59
simple , err := contract .NewSimpleSwap (address , backend )
60
- c := simpleContract {instance : simple }
60
+ c := simpleContract {instance : simple , address : address }
61
61
return c , err
62
62
}
63
63
64
64
// Contract interface defines the methods exported from the underlying go-bindings for the smart contract
65
65
type Contract interface {
66
- // SubmitChequeBeneficiary submits a cheque to a given beneficiary
67
- SubmitChequeBeneficiary (opts * bind.TransactOpts , backend Backend , serial * big.Int , amount * big.Int , timeout * big.Int , ownerSig []byte ) (* types.Receipt , error )
68
66
// CashChequeBeneficiary cashes the cheque by the beneficiary
69
- CashChequeBeneficiary (auth * bind.TransactOpts , backend Backend , beneficiary common.Address , requestPayout * big.Int ) (* types.Receipt , error )
67
+ CashChequeBeneficiary (auth * bind.TransactOpts , backend Backend , beneficiary common.Address , cumulativePayout * big.Int , ownerSig [] byte ) (* CashChequeResult , * types.Receipt , error )
70
68
// ContractParams returns contract info (e.g. deployed address)
71
69
ContractParams () * Params
72
70
// Issuer returns the contract owner from the blockchain
73
71
Issuer (opts * bind.CallOpts ) (common.Address , error )
74
- // Cheques returns the last cheque for the given address
75
- Cheques (opts * bind.CallOpts , addr common.Address ) (* ChequeResult , error )
72
+ // PaidOut returns the total paid out amount for the given address
73
+ PaidOut (opts * bind.CallOpts , addr common.Address ) (* big. Int , error )
76
74
}
77
75
78
- // ChequeResult is needed because the underlying `Cheques` method returns an untyped struct
79
- type ChequeResult struct {
80
- Serial * big.Int
81
- Amount * big.Int
82
- PaidOut * big.Int
83
- CashTimeout * big.Int
76
+ // CashChequeResult summarizes the result of a CashCheque or CashChequeBeneficiary call
77
+ type CashChequeResult struct {
78
+ Beneficiary common.Address // beneficiary of the cheque
79
+ Recipient common.Address // address which received the funds
80
+ Caller common.Address // caller of cashCheque
81
+ TotalPayout * big.Int // total amount that was paid out in this call
82
+ CumulativePayout * big.Int // cumulative payout of the cheque that was cashed
83
+ CallerPayout * big.Int // payout for the caller of cashCheque
84
+ Bounced bool // indicates wether parts of the cheque bounced
84
85
}
85
86
86
87
// Params encapsulates some contract parameters (currently mostly informational)
@@ -95,7 +96,7 @@ func ValidateCode(ctx context.Context, b bind.ContractBackend, address common.Ad
95
96
if err != nil {
96
97
return err
97
98
}
98
- referenceCode := common .FromHex (contract .ContractDeployedCode )
99
+ referenceCode := common .FromHex (contract .SimpleSwapDeployedCode )
99
100
if ! bytes .Equal (codeReadFromAddress , referenceCode ) {
100
101
return ErrNotASwapContract
101
102
}
@@ -122,6 +123,7 @@ func waitForTx(auth *bind.TransactOpts, backend Backend, tx *types.Transaction)
122
123
123
124
type simpleContract struct {
124
125
instance * contract.SimpleSwap
126
+ address common.Address
125
127
}
126
128
127
129
// ContractParams returns contract information
@@ -132,40 +134,46 @@ func (s simpleContract) ContractParams() *Params {
132
134
}
133
135
}
134
136
135
- // Cheques returns the last cheque from the smart contract
136
- func (s simpleContract ) Cheques (opts * bind.CallOpts , addr common.Address ) (* ChequeResult , error ) {
137
- r , err := s .instance .Cheques (opts , addr )
138
- if err != nil {
139
- return nil , err
140
- }
141
- result := & ChequeResult {
142
- Serial : r .Serial ,
143
- Amount : r .Amount ,
144
- PaidOut : r .PaidOut ,
145
- CashTimeout : r .CashTimeout ,
146
- }
147
- return result , nil
137
+ // PaidOut returns the total paid out amount for the given address
138
+ func (s simpleContract ) PaidOut (opts * bind.CallOpts , addr common.Address ) (* big.Int , error ) {
139
+ return s .instance .PaidOut (opts , addr )
148
140
}
149
141
150
142
// Issuer returns the contract owner from the blockchain
151
143
func (s simpleContract ) Issuer (opts * bind.CallOpts ) (common.Address , error ) {
152
144
return s .instance .Issuer (opts )
153
145
}
154
146
155
- // SubmitChequeBeneficiary prepares to send a call to submitChequeBeneficiary and blocks until the transaction is mined.
156
- func (s simpleContract ) SubmitChequeBeneficiary (auth * bind.TransactOpts , backend Backend , serial * big. Int , amount * big.Int , timeout * big. Int , ownerSig []byte ) (* types.Receipt , error ) {
157
- tx , err := s .instance .SubmitChequeBeneficiary (auth , serial , amount , timeout , ownerSig )
147
+ // CashChequeBeneficiary cashes the cheque on the blockchain and blocks until the transaction is mined.
148
+ func (s simpleContract ) CashChequeBeneficiary (auth * bind.TransactOpts , backend Backend , beneficiary common. Address , cumulativePayout * big.Int , ownerSig []byte ) (* CashChequeResult , * types.Receipt , error ) {
149
+ tx , err := s .instance .CashChequeBeneficiary (auth , beneficiary , cumulativePayout , ownerSig )
158
150
if err != nil {
159
- return nil , err
151
+ return nil , nil , err
160
152
}
161
- return WaitFunc (auth , backend , tx )
162
- }
163
-
164
- // CashChequeBeneficiary cashes the cheque.
165
- func (s simpleContract ) CashChequeBeneficiary (auth * bind.TransactOpts , backend Backend , beneficiary common.Address , requestPayout * big.Int ) (* types.Receipt , error ) {
166
- tx , err := s .instance .CashChequeBeneficiary (auth , beneficiary , requestPayout )
153
+ receipt , err := WaitFunc (auth , backend , tx )
167
154
if err != nil {
168
- return nil , err
155
+ return nil , nil , err
156
+ }
157
+
158
+ result := & CashChequeResult {
159
+ Bounced : false ,
169
160
}
170
- return WaitFunc (auth , backend , tx )
161
+
162
+ for _ , log := range receipt .Logs {
163
+ if log .Address != s .address {
164
+ continue
165
+ }
166
+ if event , err := s .instance .ParseChequeCashed (* log ); err == nil {
167
+ result .Beneficiary = event .Beneficiary
168
+ result .Caller = event .Caller
169
+ result .CallerPayout = event .CallerPayout
170
+ result .TotalPayout = event .TotalPayout
171
+ result .CumulativePayout = event .CumulativePayout
172
+ result .Recipient = event .Recipient
173
+ } else if _ , err := s .instance .ParseChequeBounced (* log ); err == nil {
174
+ result .Bounced = true
175
+ }
176
+ }
177
+
178
+ return result , receipt , nil
171
179
}
0 commit comments