-
Notifications
You must be signed in to change notification settings - Fork 337
/
factory.go
229 lines (192 loc) · 6.86 KB
/
factory.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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package chequebook
import (
"bytes"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/sctx"
"github.com/ethersphere/bee/pkg/transaction"
"github.com/ethersphere/go-sw3-abi/sw3abi"
"golang.org/x/net/context"
)
var (
ErrInvalidFactory = errors.New("not a valid factory contract")
ErrNotDeployedByFactory = errors.New("chequebook not deployed by factory")
errDecodeABI = errors.New("could not decode abi data")
factoryABI = transaction.ParseABIUnchecked(sw3abi.SimpleSwapFactoryABIv0_4_0)
simpleSwapDeployedEventType = factoryABI.Events["SimpleSwapDeployed"]
)
// Factory is the main interface for interacting with the chequebook factory.
type Factory interface {
// ERC20Address returns the token for which this factory deploys chequebooks.
ERC20Address(ctx context.Context) (common.Address, error)
// Deploy deploys a new chequebook and returns once the transaction has been submitted.
Deploy(ctx context.Context, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int, nonce common.Hash) (common.Hash, error)
// WaitDeployed waits for the deployment transaction to confirm and returns the chequebook address
WaitDeployed(ctx context.Context, txHash common.Hash) (common.Address, error)
// VerifyBytecode checks that the factory is valid.
VerifyBytecode(ctx context.Context) error
// VerifyChequebook checks that the supplied chequebook has been deployed by this factory.
VerifyChequebook(ctx context.Context, chequebook common.Address) error
}
type factory struct {
backend transaction.Backend
transactionService transaction.Service
address common.Address // address of the factory to use for deployments
legacyAddresses []common.Address // addresses of old factories which were allowed for deployment
}
type simpleSwapDeployedEvent struct {
ContractAddress common.Address
}
// the bytecode of factories which can be used for deployment
var currentDeployVersion []byte = common.FromHex(sw3abi.SimpleSwapFactoryDeployedBinv0_4_0)
// the bytecode of factories from which we accept chequebooks
var supportedVersions = [][]byte{
currentDeployVersion,
common.FromHex(sw3abi.SimpleSwapFactoryDeployedBinv0_3_1),
}
// NewFactory creates a new factory service for the provided factory contract.
func NewFactory(backend transaction.Backend, transactionService transaction.Service, address common.Address, legacyAddresses []common.Address) Factory {
return &factory{
backend: backend,
transactionService: transactionService,
address: address,
legacyAddresses: legacyAddresses,
}
}
// Deploy deploys a new chequebook and returns once the transaction has been submitted.
func (c *factory) Deploy(ctx context.Context, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int, nonce common.Hash) (common.Hash, error) {
callData, err := factoryABI.Pack("deploySimpleSwap", issuer, big.NewInt(0).Set(defaultHardDepositTimeoutDuration), nonce)
if err != nil {
return common.Hash{}, err
}
request := &transaction.TxRequest{
To: &c.address,
Data: callData,
GasPrice: sctx.GetGasPrice(ctx),
GasLimit: 175000,
Value: big.NewInt(0),
Description: "chequebook deployment",
}
txHash, err := c.transactionService.Send(ctx, request)
if err != nil {
return common.Hash{}, err
}
return txHash, nil
}
// WaitDeployed waits for the deployment transaction to confirm and returns the chequebook address
func (c *factory) WaitDeployed(ctx context.Context, txHash common.Hash) (common.Address, error) {
receipt, err := c.transactionService.WaitForReceipt(ctx, txHash)
if err != nil {
return common.Address{}, err
}
var event simpleSwapDeployedEvent
err = transaction.FindSingleEvent(&factoryABI, receipt, c.address, simpleSwapDeployedEventType, &event)
if err != nil {
return common.Address{}, fmt.Errorf("contract deployment failed: %w", err)
}
return event.ContractAddress, nil
}
// VerifyBytecode checks that the factory is valid.
func (c *factory) VerifyBytecode(ctx context.Context) (err error) {
code, err := c.backend.CodeAt(ctx, c.address, nil)
if err != nil {
return err
}
if !bytes.Equal(code, currentDeployVersion) {
return ErrInvalidFactory
}
LOOP:
for _, factoryAddress := range c.legacyAddresses {
code, err := c.backend.CodeAt(ctx, factoryAddress, nil)
if err != nil {
return err
}
for _, referenceCode := range supportedVersions {
if bytes.Equal(code, referenceCode) {
continue LOOP
}
}
return fmt.Errorf("failed to find matching bytecode for factory %x: %w", factoryAddress, ErrInvalidFactory)
}
return nil
}
func (c *factory) verifyChequebookAgainstFactory(ctx context.Context, factory, chequebook common.Address) (bool, error) {
callData, err := factoryABI.Pack("deployedContracts", chequebook)
if err != nil {
return false, err
}
output, err := c.transactionService.Call(ctx, &transaction.TxRequest{
To: &factory,
Data: callData,
})
if err != nil {
return false, err
}
results, err := factoryABI.Unpack("deployedContracts", output)
if err != nil {
return false, err
}
if len(results) != 1 {
return false, errDecodeABI
}
deployed, ok := abi.ConvertType(results[0], new(bool)).(*bool)
if !ok || deployed == nil {
return false, errDecodeABI
}
if !*deployed {
return false, nil
}
return true, nil
}
// VerifyChequebook checks that the supplied chequebook has been deployed by a supported factory.
func (c *factory) VerifyChequebook(ctx context.Context, chequebook common.Address) error {
deployed, err := c.verifyChequebookAgainstFactory(ctx, c.address, chequebook)
if err != nil {
return err
}
if deployed {
return nil
}
for _, factoryAddress := range c.legacyAddresses {
deployed, err := c.verifyChequebookAgainstFactory(ctx, factoryAddress, chequebook)
if err != nil {
return err
}
if deployed {
return nil
}
}
return ErrNotDeployedByFactory
}
// ERC20Address returns the token for which this factory deploys chequebooks.
func (c *factory) ERC20Address(ctx context.Context) (common.Address, error) {
callData, err := factoryABI.Pack("ERC20Address")
if err != nil {
return common.Address{}, err
}
output, err := c.transactionService.Call(ctx, &transaction.TxRequest{
To: &c.address,
Data: callData,
})
if err != nil {
return common.Address{}, err
}
results, err := factoryABI.Unpack("ERC20Address", output)
if err != nil {
return common.Address{}, err
}
if len(results) != 1 {
return common.Address{}, errDecodeABI
}
erc20Address, ok := abi.ConvertType(results[0], new(common.Address)).(*common.Address)
if !ok || erc20Address == nil {
return common.Address{}, errDecodeABI
}
return *erc20Address, nil
}