-
Notifications
You must be signed in to change notification settings - Fork 339
/
contract.go
232 lines (196 loc) · 5.95 KB
/
contract.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
// Copyright 2022 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 staking
import (
"context"
"errors"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethersphere/bee/pkg/sctx"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/transaction"
"github.com/ethersphere/go-sw3-abi/sw3abi"
)
var (
MinimumStakeAmount = big.NewInt(100000000000000000)
erc20ABI = parseABI(sw3abi.ERC20ABIv0_3_1)
stakingABI = parseABI(ABIv0_0_0)
ErrInsufficientStakeAmount = errors.New("insufficient stake amount")
ErrInsufficientFunds = errors.New("insufficient token balance")
ErrNotImplemented = errors.New("not implemented")
approveDescription = "Approve tokens for stake deposit operations"
depositStakeDescription = "Deposit Stake"
)
type Contract interface {
DepositStake(ctx context.Context, stakedAmount *big.Int) (common.Hash, error)
GetStake(ctx context.Context) (*big.Int, error)
}
type contract struct {
overlay swarm.Address
owner common.Address
stakingContractAddress common.Address
bzzTokenAddress common.Address
transactionService transaction.Service
overlayNonce common.Hash
}
func New(
overlay swarm.Address,
owner common.Address,
stakingContractAddress common.Address,
bzzTokenAddress common.Address,
transactionService transaction.Service,
nonce common.Hash,
) Contract {
return &contract{
overlay: overlay,
owner: owner,
stakingContractAddress: stakingContractAddress,
bzzTokenAddress: bzzTokenAddress,
transactionService: transactionService,
overlayNonce: nonce,
}
}
func (s *contract) sendApproveTransaction(ctx context.Context, amount *big.Int) (*types.Receipt, error) {
callData, err := erc20ABI.Pack("approve", s.stakingContractAddress, amount)
if err != nil {
return nil, err
}
txHash, err := s.transactionService.Send(ctx, &transaction.TxRequest{
To: &s.bzzTokenAddress,
Data: callData,
GasPrice: sctx.GetGasPrice(ctx),
GasLimit: 65000,
Value: big.NewInt(0),
Description: approveDescription,
}, 0)
if err != nil {
return nil, err
}
receipt, err := s.transactionService.WaitForReceipt(ctx, txHash)
if err != nil {
return nil, err
}
if receipt.Status == 0 {
return nil, transaction.ErrTransactionReverted
}
return receipt, nil
}
func (s *contract) sendTransaction(ctx context.Context, callData []byte, desc string) (*types.Receipt, error) {
request := &transaction.TxRequest{
To: &s.stakingContractAddress,
Data: callData,
GasPrice: sctx.GetGasPrice(ctx),
GasLimit: sctx.GetGasLimitWithDefault(ctx, 300_000),
Value: big.NewInt(0),
Description: desc,
}
txHash, err := s.transactionService.Send(ctx, request, transaction.DefaultTipBoostPercent)
if err != nil {
return nil, err
}
receipt, err := s.transactionService.WaitForReceipt(ctx, txHash)
if err != nil {
return nil, err
}
if receipt.Status == 0 {
return nil, transaction.ErrTransactionReverted
}
return receipt, nil
}
func (s *contract) sendDepositStakeTransaction(ctx context.Context, owner common.Address, stakedAmount *big.Int, nonce common.Hash) (*types.Receipt, error) {
callData, err := stakingABI.Pack("depositStake", owner, nonce, stakedAmount)
if err != nil {
return nil, err
}
receipt, err := s.sendTransaction(ctx, callData, depositStakeDescription)
if err != nil {
return nil, fmt.Errorf("deposit stake: stakedAmount %d: %w", stakedAmount, err)
}
return receipt, nil
}
func (s *contract) getStake(ctx context.Context, overlay swarm.Address) (*big.Int, error) {
var overlayAddr [32]byte
copy(overlayAddr[:], overlay.Bytes())
callData, err := stakingABI.Pack("stakeOfOverlay", overlayAddr)
if err != nil {
return nil, err
}
result, err := s.transactionService.Call(ctx, &transaction.TxRequest{
To: &s.stakingContractAddress,
Data: callData,
})
if err != nil {
return nil, fmt.Errorf("get stake: overlayAddress %d: %w", overlay, err)
}
results, err := stakingABI.Unpack("stakeOfOverlay", result)
if err != nil {
return nil, err
}
return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil
}
func (s *contract) DepositStake(ctx context.Context, stakedAmount *big.Int) (txHash common.Hash, err error) {
prevStakedAmount, err := s.GetStake(ctx)
if err != nil {
return
}
if len(prevStakedAmount.Bits()) == 0 {
if stakedAmount.Cmp(MinimumStakeAmount) == -1 {
err = ErrInsufficientStakeAmount
return
}
}
balance, err := s.getBalance(ctx)
if err != nil {
return
}
if balance.Cmp(stakedAmount) < 0 {
err = ErrInsufficientFunds
return
}
_, err = s.sendApproveTransaction(ctx, stakedAmount)
if err != nil {
return
}
receipt, err := s.sendDepositStakeTransaction(ctx, s.owner, stakedAmount, s.overlayNonce)
if receipt != nil {
txHash = receipt.TxHash
}
return
}
func (s *contract) GetStake(ctx context.Context) (*big.Int, error) {
stakedAmount, err := s.getStake(ctx, s.overlay)
if err != nil {
return nil, fmt.Errorf("staking contract: failed to get stake: %w", err)
}
return stakedAmount, nil
}
func (s *contract) getBalance(ctx context.Context) (*big.Int, error) {
callData, err := erc20ABI.Pack("balanceOf", s.owner)
if err != nil {
return nil, err
}
result, err := s.transactionService.Call(ctx, &transaction.TxRequest{
To: &s.bzzTokenAddress,
Data: callData,
})
if err != nil {
return nil, err
}
results, err := erc20ABI.Unpack("balanceOf", result)
if err != nil {
return nil, err
}
return abi.ConvertType(results[0], new(big.Int)).(*big.Int), nil
}
func parseABI(json string) abi.ABI {
cabi, err := abi.JSON(strings.NewReader(json))
if err != nil {
panic(fmt.Sprintf("error creating ABI for staking contract: %v", err))
}
return cabi
}