This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
/
erc20.go
254 lines (234 loc) · 7.29 KB
/
erc20.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package erc20
import (
"context"
"encoding/base64"
"fmt"
"math/big"
"time"
"github.com/republicprotocol/beth-go"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/republicprotocol/swapperd/core/swapper"
"github.com/republicprotocol/swapperd/foundation"
)
type erc20SwapContractBinder struct {
id [32]byte
account beth.Account
swap foundation.Swap
logger swapper.Logger
swapperAddress common.Address
tokenAddress common.Address
swapperBinder *RenExAtomicSwapper
tokenBinder *CompatibleERC20
}
// NewERC20SwapContractBinder returns a new ERC20 Atom instance
func NewERC20SwapContractBinder(account beth.Account, swap foundation.Swap, logger swapper.Logger) (swapper.Contract, error) {
tokenAddress, err := account.ReadAddress(fmt.Sprintf("ERC20:%s", swap.Token.Name))
if err != nil {
return nil, err
}
swapperAddress, err := account.ReadAddress(fmt.Sprintf("SWAPPER:%s", swap.Token.Name))
if err != nil {
return nil, err
}
client := account.EthClient()
tokenBinder, err := NewCompatibleERC20(tokenAddress, bind.ContractBackend(client.EthClient()))
if err != nil {
return nil, err
}
swapperBinder, err := NewRenExAtomicSwapper(swapperAddress, bind.ContractBackend(client.EthClient()))
if err != nil {
return nil, err
}
id, err := swapperBinder.SwapID(&bind.CallOpts{}, swap.SecretHash, big.NewInt(swap.TimeLock))
if err != nil {
return nil, err
}
logger.LogInfo(swap.ID, fmt.Sprintf("%s atomic swap = %s", swap.Token.Name, base64.StdEncoding.EncodeToString(id[:])))
return &erc20SwapContractBinder{
account: account,
swapperAddress: swapperAddress,
tokenAddress: tokenAddress,
swapperBinder: swapperBinder,
tokenBinder: tokenBinder,
logger: logger,
swap: swap,
id: id,
}, nil
}
// Initiate a new Atom swap by calling a function on ethereum
func (atom *erc20SwapContractBinder) Initiate() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
initiatable, err := atom.swapperBinder.Initiatable(&bind.CallOpts{}, atom.id)
if err != nil {
return err
}
if !initiatable {
atom.logger.LogInfo(atom.swap.ID, fmt.Sprintf("Skipping initiate on Ethereum blockchain"))
return nil
}
atom.logger.LogInfo(atom.swap.ID, fmt.Sprintf("Initiating on Ethereum blockchain"))
// Approve the contract to transfer tokens
if err := atom.account.Transact(
ctx,
nil,
func(tops *bind.TransactOpts) (*types.Transaction, error) {
tx, err := atom.tokenBinder.Approve(tops, atom.swapperAddress, atom.swap.Value)
if err != nil {
return tx, err
}
msg, _ := atom.account.FormatTransactionView("Approved on Ethereum blockchain", tx.Hash().String())
atom.logger.LogInfo(atom.swap.ID, msg)
return tx, nil
},
nil,
1,
); err != nil {
return err
}
// Initiate the Atomic Swap
return atom.account.Transact(
ctx,
nil,
func(tops *bind.TransactOpts) (*types.Transaction, error) {
tx, err := atom.swapperBinder.Initiate(tops, atom.id, common.HexToAddress(atom.swap.SpendingAddress), atom.swap.SecretHash, big.NewInt(atom.swap.TimeLock), atom.swap.Value)
if err != nil {
return tx, err
}
msg, _ := atom.account.FormatTransactionView("Initiated on Ethereum blockchain", tx.Hash().String())
atom.logger.LogInfo(atom.swap.ID, msg)
return tx, nil
},
func() bool {
initiatable, err := atom.swapperBinder.Initiatable(&bind.CallOpts{}, atom.id)
if err != nil {
return false
}
return !initiatable
},
1,
)
}
// Refund an Atom swap by calling a function on ethereum
func (atom *erc20SwapContractBinder) Refund() error {
atom.logger.LogInfo(atom.swap.ID, "Refunding on Ethereum blockchain")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
if err := atom.account.Transact(
ctx,
func() bool {
refundable, err := atom.swapperBinder.Refundable(&bind.CallOpts{}, atom.id)
if err != nil {
return false
}
return refundable
},
func(tops *bind.TransactOpts) (*types.Transaction, error) {
tx, err := atom.swapperBinder.Refund(tops, atom.id)
if err != nil {
return nil, err
}
msg, _ := atom.account.FormatTransactionView("Refunded on Ethereum blockchain", tx.Hash().String())
atom.logger.LogInfo(atom.swap.ID, msg)
return tx, nil
},
func() bool {
refundable, err := atom.swapperBinder.Refundable(&bind.CallOpts{}, atom.id)
if err != nil {
return false
}
return !refundable
},
1,
); err != nil && err != beth.ErrPreConditionCheckFailed {
return err
}
return nil
}
// AuditSecret audits the secret of an Atom swap by calling a function on ethereum
func (atom *erc20SwapContractBinder) AuditSecret() ([32]byte, error) {
for {
atom.logger.LogInfo(atom.swap.ID, "Auditing secret on Ethereum blockchain")
redeemable, err := atom.swapperBinder.Redeemable(&bind.CallOpts{}, atom.id)
if err != nil {
atom.logger.LogError(atom.swap.ID, err)
return [32]byte{}, err
}
if !redeemable {
break
}
if time.Now().Unix() > atom.swap.TimeLock {
return [32]byte{}, fmt.Errorf("timeout")
}
time.Sleep(15 * time.Second)
}
secret, err := atom.swapperBinder.AuditSecret(&bind.CallOpts{}, atom.id)
if err != nil {
return [32]byte{}, err
}
atom.logger.LogInfo(atom.swap.ID, fmt.Sprintf("Audit succeeded on Ethereum blockchain secret = %s", base64.StdEncoding.EncodeToString(secret[:])))
return secret, nil
}
// Audit an Atom swap by calling a function on ethereum
func (atom *erc20SwapContractBinder) Audit() error {
atom.logger.LogInfo(atom.swap.ID, fmt.Sprintf("Waiting for initiation on Ethereum blockchain"))
for {
initiatable, err := atom.swapperBinder.Initiatable(&bind.CallOpts{}, atom.id)
if err != nil {
return err
}
if !initiatable {
break
}
time.Sleep(15 * time.Second)
}
auditReport, err := atom.swapperBinder.Audit(&bind.CallOpts{}, atom.id)
if err != nil {
return err
}
if auditReport.Value.Cmp(atom.swap.Value) != 0 {
return fmt.Errorf("Receive value mismatch: expected %v, got %v", atom.swap.Value, auditReport.Value)
}
atom.logger.LogInfo(atom.swap.ID, fmt.Sprintf("Audit successful on Ethereum blockchain"))
return nil
}
// Redeem an Atom swap by calling a function on ethereum
func (atom *erc20SwapContractBinder) Redeem(secret [32]byte) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
if err := atom.account.Transact(
ctx,
func() bool {
redeemable, err := atom.swapperBinder.Redeemable(&bind.CallOpts{}, atom.id)
if err != nil {
return false
}
return redeemable
},
func(tops *bind.TransactOpts) (*types.Transaction, error) {
tx, err := atom.swapperBinder.Redeem(tops, atom.id, secret)
if err != nil {
return nil, err
}
msg, _ := atom.account.FormatTransactionView("Redeemed the atomic swap on Ethereum blockchain", tx.Hash().String())
atom.logger.LogInfo(atom.swap.ID, msg)
return tx, nil
},
func() bool {
refundable, err := atom.swapperBinder.Refundable(&bind.CallOpts{}, atom.id)
if err != nil {
return false
}
return !refundable
},
1,
); err != nil {
if err == beth.ErrPreConditionCheckFailed {
atom.logger.LogInfo(atom.swap.ID, "Skipping redeem on Ethereum Blockchain")
}
return err
}
return nil
}