-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
213 lines (188 loc) · 5.49 KB
/
token.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
package web3sdks
import (
"context"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/web3sdks/go-sdk/v2/abi"
)
// You can access the Token interface from the SDK as follows:
//
// import (
// "github.com/web3sdks/go-sdk/v2/web3sdks"
// )
//
// privateKey = "..."
//
// sdk, err := web3sdks.NewWeb3sdksSDK("mumbai", &web3sdks.SDKOptions{
// PrivateKey: privateKey,
// })
//
// contract, err := sdk.GetToken("{{contract_address}}")
type Token struct {
*ERC20
abi *abi.TokenERC20
Helper *contractHelper
Encoder *ContractEncoder
Events *ContractEvents
}
func newToken(provider *ethclient.Client, address common.Address, privateKey string, storage storage) (*Token, error) {
if contractAbi, err := abi.NewTokenERC20(address, provider); err != nil {
return nil, err
} else if helper, err := newContractHelper(address, provider, privateKey); err != nil {
return nil, err
} else {
if erc20, err := newERC20(provider, address, privateKey, storage); err != nil {
return nil, err
} else {
encoder, err := newContractEncoder(abi.TokenERC20ABI, helper)
if err != nil {
return nil, err
}
events, err := newContractEvents(abi.TokenERC20ABI, helper)
if err != nil {
return nil, err
}
token := &Token{
erc20,
contractAbi,
helper,
encoder,
events,
}
return token, nil
}
}
}
// Get the connected wallets voting power in this token.
//
// returns: vote balance of the connected wallet
func (token *Token) GetVoteBalance(ctx context.Context) (*CurrencyValue, error) {
return token.GetVoteBalanceOf(ctx, token.Helper.GetSignerAddress().String())
}
// Get the voting power of the specified wallet in this token.
//
// address: wallet address to check the vote balance of
//
// returns: vote balance of the specified wallet
func (token *Token) GetVoteBalanceOf(ctx context.Context, address string) (*CurrencyValue, error) {
votes, err := token.abi.GetVotes(&bind.CallOpts{Context: ctx}, common.HexToAddress(address))
if err != nil {
return nil, err
}
return token.getValue(ctx, votes)
}
// Get the connected wallets delegatee address for this token.
//
// returns: delegation address of the connected wallet
func (token *Token) GetDelegation(ctx context.Context) (string, error) {
return token.GetDelegationOf(ctx, token.Helper.GetSignerAddress().String())
}
// Get a specified wallets delegatee for this token.
//
// returns: delegation address of the connected wallet
func (token *Token) GetDelegationOf(ctx context.Context, address string) (string, error) {
delegation, err := token.abi.Delegates(&bind.CallOpts{Context: ctx}, common.HexToAddress(address))
if err != nil {
return "", err
}
return delegation.String(), nil
}
// Mint tokens to the connected wallet.
//
// amount: amount of tokens to mint
//
// returns: transaction receipt of the mint
func (token *Token) Mint(ctx context.Context, amount float64) (*types.Transaction, error) {
return token.MintTo(ctx, token.Helper.GetSignerAddress().String(), amount)
}
// Mint tokens to a specified wallet.
//
// to: wallet address to mint tokens to
//
// amount: amount of tokens to mint
//
// returns: transaction receipt of the mint
//
// Example
//
// tx, err := contract.MintTo(context.Background(), "{{wallet_address}}", 1)
func (token *Token) MintTo(ctx context.Context, to string, amount float64) (*types.Transaction, error) {
amountWithDecimals, err := token.normalizeAmount(ctx, amount)
if err != nil {
return nil, err
}
txOpts, err := token.Helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := token.abi.MintTo(txOpts, common.HexToAddress(to), amountWithDecimals)
if err != nil {
return nil, err
}
return token.Helper.AwaitTx(ctx, tx.Hash())
}
// Mint tokens to a list of wallets.
//
// args: list of wallet addresses and amounts to mint
//
// returns: transaction receipt of the mint
//
// Example
//
// args = []*web3sdks.TokenAmount{
// &web3sdks.TokenAmount{
// ToAddress: "{{wallet_address}}",
// Amount: 1
// }
// &web3sdks.TokenAmount{
// ToAddress: "{{wallet_address}}",
// Amount: 2
// }
// }
//
// tx, err := contract.MintBatchTo(context.Background(), args)
func (token *Token) MintBatchTo(ctx context.Context, args []*TokenAmount) (*types.Transaction, error) {
encoded := [][]byte{}
for _, arg := range args {
amountWithDecimals, err := token.normalizeAmount(ctx, arg.Amount)
if err != nil {
return nil, err
}
txOpts, err := token.Helper.getEncodedTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := token.abi.MintTo(txOpts, common.HexToAddress(arg.ToAddress), amountWithDecimals)
if err != nil {
return nil, err
}
encoded = append(encoded, tx.Data())
}
txOpts, err := token.Helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := token.abi.Multicall(txOpts, encoded)
if err != nil {
return nil, err
}
return token.Helper.AwaitTx(ctx, tx.Hash())
}
// Delegate the connected wallets tokens to a specified wallet.
//
// delegateeAddress: wallet address to delegate tokens to
//
// returns: transaction receipt of the delegation
func (token *Token) DelegateTo(ctx context.Context, delegatreeAddress string) (*types.Transaction, error) {
txOpts, err := token.Helper.GetTxOptions(ctx)
if err != nil {
return nil, err
}
tx, err := token.abi.Delegate(txOpts, common.HexToAddress(delegatreeAddress))
if err != nil {
return nil, err
}
return token.Helper.AwaitTx(ctx, tx.Hash())
}