forked from rocket-pool/rocketpool-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpl.go
171 lines (149 loc) · 5.27 KB
/
rpl.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
package tokens
import (
"fmt"
"math/big"
"sync"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/PatriceVignola/rocketpool-go/rocketpool"
)
//
// Core ERC-20 functions
//
// Get RPL total supply
func GetRPLTotalSupply(rp *rocketpool.RocketPool, opts *bind.CallOpts) (*big.Int, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return nil, err
}
return totalSupply(rocketTokenRPL, "RPL", opts)
}
// Get RPL balance
func GetRPLBalance(rp *rocketpool.RocketPool, address common.Address, opts *bind.CallOpts) (*big.Int, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return nil, err
}
return balanceOf(rocketTokenRPL, "RPL", address, opts)
}
// Get RPL allowance
func GetRPLAllowance(rp *rocketpool.RocketPool, owner, spender common.Address, opts *bind.CallOpts) (*big.Int, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return nil, err
}
return allowance(rocketTokenRPL, "RPL", owner, spender, opts)
}
// Estimate the gas of TransferRPL
func EstimateTransferRPLGas(rp *rocketpool.RocketPool, to common.Address, amount *big.Int, opts *bind.TransactOpts) (rocketpool.GasInfo, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return rocketpool.GasInfo{}, err
}
return estimateTransferGas(rocketTokenRPL, "RPL", to, amount, opts)
}
// Transfer RPL
func TransferRPL(rp *rocketpool.RocketPool, to common.Address, amount *big.Int, opts *bind.TransactOpts) (common.Hash, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return common.Hash{}, err
}
return transfer(rocketTokenRPL, "RPL", to, amount, opts)
}
// Estimate the gas of ApproveRPL
func EstimateApproveRPLGas(rp *rocketpool.RocketPool, spender common.Address, amount *big.Int, opts *bind.TransactOpts) (rocketpool.GasInfo, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return rocketpool.GasInfo{}, err
}
return estimateApproveGas(rocketTokenRPL, "RPL", spender, amount, opts)
}
// Approve an RPL spender
func ApproveRPL(rp *rocketpool.RocketPool, spender common.Address, amount *big.Int, opts *bind.TransactOpts) (common.Hash, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return common.Hash{}, err
}
return approve(rocketTokenRPL, "RPL", spender, amount, opts)
}
// Estimate the gas of TransferFromRPL
func EstimateTransferFromRPLGas(rp *rocketpool.RocketPool, from, to common.Address, amount *big.Int, opts *bind.TransactOpts) (rocketpool.GasInfo, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return rocketpool.GasInfo{}, err
}
return estimateTransferFromGas(rocketTokenRPL, "RPL", from, to, amount, opts)
}
// Transfer RPL from a sender
func TransferFromRPL(rp *rocketpool.RocketPool, from, to common.Address, amount *big.Int, opts *bind.TransactOpts) (common.Hash, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return common.Hash{}, err
}
return transferFrom(rocketTokenRPL, "RPL", from, to, amount, opts)
}
//
// RPL functions
//
// Estimate the gas of MintInflationRPL
func EstimateMintInflationRPLGas(rp *rocketpool.RocketPool, opts *bind.TransactOpts) (rocketpool.GasInfo, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return rocketpool.GasInfo{}, err
}
return rocketTokenRPL.GetTransactionGasInfo(opts, "inflationMintTokens")
}
// Mint new RPL tokens from inflation
func MintInflationRPL(rp *rocketpool.RocketPool, opts *bind.TransactOpts) (common.Hash, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return common.Hash{}, err
}
hash, err := rocketTokenRPL.Transact(opts, "inflationMintTokens")
if err != nil {
return common.Hash{}, fmt.Errorf("Could not mint RPL tokens from inflation: %w", err)
}
return hash, nil
}
// Estimate the gas of SwapFixedSupplyRPLForRPL
func EstimateSwapFixedSupplyRPLForRPLGas(rp *rocketpool.RocketPool, amount *big.Int, opts *bind.TransactOpts) (rocketpool.GasInfo, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return rocketpool.GasInfo{}, err
}
return rocketTokenRPL.GetTransactionGasInfo(opts, "swapTokens", amount)
}
// Swap fixed-supply RPL for new RPL tokens
func SwapFixedSupplyRPLForRPL(rp *rocketpool.RocketPool, amount *big.Int, opts *bind.TransactOpts) (common.Hash, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return common.Hash{}, err
}
hash, err := rocketTokenRPL.Transact(opts, "swapTokens", amount)
if err != nil {
return common.Hash{}, fmt.Errorf("Could not swap fixed-supply RPL for new RPL: %w", err)
}
return hash, nil
}
// Get the RPL inflation interval rate
func GetRPLInflationIntervalRate(rp *rocketpool.RocketPool, opts *bind.CallOpts) (*big.Int, error) {
rocketTokenRPL, err := getRocketTokenRPL(rp)
if err != nil {
return nil, err
}
rate := new(*big.Int)
if err := rocketTokenRPL.Call(opts, rate, "getInflationIntervalRate"); err != nil {
return nil, fmt.Errorf("Could not get RPL inflation interval rate: %w", err)
}
return *rate, nil
}
//
// Contracts
//
// Get contracts
var rocketTokenRPLLock sync.Mutex
func getRocketTokenRPL(rp *rocketpool.RocketPool) (*rocketpool.Contract, error) {
rocketTokenRPLLock.Lock()
defer rocketTokenRPLLock.Unlock()
return rp.GetContract("rocketTokenRPL")
}