-
Notifications
You must be signed in to change notification settings - Fork 28
/
erc20.go
421 lines (375 loc) · 11.2 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package keeper
import (
"math/big"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/irisnet/irismod/contracts"
"github.com/irisnet/irismod/modules/token/types"
v1 "github.com/irisnet/irismod/modules/token/types/v1"
)
// DeployERC20 deploys an ERC20 token contract.
//
// Parameters:
// - ctx: the context
// - name: the name of the token
// - symbol: the symbol of the token
// - minUnit: the symbol of the minUnit
// - scale: the scale of the token
//
// Returns:
// - Address: the contract address.
// - error: error if any.
func (k Keeper) DeployERC20(
ctx sdk.Context,
name string,
symbol string,
minUnit string,
scale uint8,
) (common.Address, error) {
token, err := k.buildERC20Token(ctx, name, symbol, minUnit, uint32(scale))
if err != nil {
return common.Address{}, err
}
if len(token.Contract) > 0 {
return common.Address{}, errorsmod.Wrapf(types.ErrERC20AlreadyExists, "token: %s already deployed erc20 contract: %s", token.Symbol, token.Contract)
}
params := k.GetParams(ctx)
if !params.EnableErc20 {
return common.Address{}, errorsmod.Wrapf(types.ErrERC20Disabled, "erc20 is disabled")
}
if len(params.Beacon) == 0 {
return common.Address{}, errorsmod.Wrapf(types.ErrBeaconNotSet, "beacon not set")
}
deployer := k.getModuleEthAddress(ctx)
initArgs, err := contracts.ERC20TokenContract.ABI.Pack(
contracts.MethodInitialize,
name,
symbol,
scale,
deployer,
)
if err != nil {
return common.Address{}, err
}
contractArgs, err := contracts.TokenProxyContract.ABI.Pack(
"",
common.HexToAddress(params.Beacon),
initArgs,
)
if err != nil {
return common.Address{}, errorsmod.Wrapf(types.ErrABIPack, "erc20 metadata is invalid %s: %s", name, err.Error())
}
data := make([]byte, len(contracts.TokenProxyContract.Bin)+len(contractArgs))
copy(data[:len(contracts.TokenProxyContract.Bin)], contracts.TokenProxyContract.Bin)
copy(data[len(contracts.TokenProxyContract.Bin):], contractArgs)
nonce, err := k.accountKeeper.GetSequence(ctx, sdk.AccAddress(deployer.Bytes()))
if err != nil {
return common.Address{}, err
}
contractAddr := crypto.CreateAddress(deployer, nonce)
result, err := k.CallEVMWithData(ctx, deployer, nil, data, true)
if err != nil {
return common.Address{}, errorsmod.Wrapf(err, "failed to deploy contract for token %s", name)
}
if result.Failed() {
return common.Address{}, errorsmod.Wrapf(types.ErrVMExecution, "failed to deploy contract for %s, reason: %s", name, result.Revert())
}
token.Contract = contractAddr.String()
k.upsertToken(ctx, *token)
ctx.EventManager().EmitTypedEvent(&v1.EventDeployERC20{
Symbol: symbol,
Name: name,
Scale: uint32(scale),
MinUnit: minUnit,
Contract: contractAddr.String(),
})
return contractAddr, nil
}
// SwapFromERC20 executes a swap from an ERC20 token to its native counterpart
//
// Parameters:
//
// ctx - the context in which the swap is executed
// sender - the address of the sender
// receiver - the address of the receiver
// wantedAmount - the amount of the token to be swapped out
//
// Return type: error
func (k Keeper) SwapFromERC20(
ctx sdk.Context,
sender common.Address,
receiver sdk.AccAddress,
wantedAmount sdk.Coin,
) error {
if !k.ERC20Enabled(ctx) {
return types.ErrERC20Disabled
}
token, err := k.getTokenByMinUnit(ctx, wantedAmount.Denom)
if err != nil {
return err
}
if len(token.Contract) == 0 {
return errorsmod.Wrapf(types.ErrERC20NotDeployed, "The token %s is not bound to the corresponding erc20 token", wantedAmount.Denom)
}
contract := common.HexToAddress(token.Contract)
if err := k.BurnERC20(ctx, contract, sender, wantedAmount.Amount.BigInt()); err != nil {
return err
}
mintedCoins := sdk.NewCoins(wantedAmount)
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, mintedCoins); err != nil {
return err
}
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, receiver, mintedCoins); err != nil {
return err
}
ctx.EventManager().EmitTypedEvent(&v1.EventSwapFromERC20{
FromContract: contract.String(),
WantedAmount: &wantedAmount,
Sender: sender.String(),
Receiver: receiver.String(),
})
return nil
}
// SwapToERC20 executes a swap from a native token to its ERC20 token counterpart
//
// Parameters:
// - ctx: the context
// - sender: the sender of the amount
// - receiver: the receiver of the erc20 token
// - amount: the amount to be swapped
//
// Returns:
// - error: error if any.
func (k Keeper) SwapToERC20(
ctx sdk.Context,
sender sdk.AccAddress,
receiver common.Address,
amount sdk.Coin,
) error {
if !k.ERC20Enabled(ctx) {
return types.ErrERC20Disabled
}
receiverAcc := k.accountKeeper.GetAccount(ctx, sdk.AccAddress(receiver.Bytes()))
if receiverAcc != nil {
if !k.evmKeeper.SupportedKey(receiverAcc.GetPubKey()) {
return errorsmod.Wrapf(types.ErrUnsupportedKey, "key %s", receiverAcc.GetPubKey())
}
}
token, err := k.getTokenByMinUnit(ctx, amount.Denom)
if err != nil {
return err
}
if len(token.Contract) == 0 {
return errorsmod.Wrapf(types.ErrERC20NotDeployed, "token: %s is not bound to the corresponding erc20 token", amount.Denom)
}
contract := common.HexToAddress(token.Contract)
amt := sdk.NewCoins(amount)
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, amt); err != nil {
return err
}
if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, amt); err != nil {
return err
}
if err := k.MintERC20(ctx, contract, receiver, amount.Amount.BigInt()); err != nil {
return err
}
ctx.EventManager().EmitTypedEvent(&v1.EventSwapToERC20{
Amount: amount,
Sender: sender.String(),
Receiver: receiver.String(),
ToContract: token.Contract,
})
return nil
}
// MintERC20 mints ERC20 tokens to an account.
//
// Parameters:
// - ctx: the sdk.Context for the function
// - contract: the address of the contract
// - to: the address of the receiver
// - amount: the amount to mint
//
// Returns:
// - err : error if any
func (k Keeper) MintERC20(
ctx sdk.Context,
contract, to common.Address,
amount *big.Int,
) error {
balanceBefore, err := k.BalanceOf(ctx, contract, to)
if err != nil {
return err
}
abi := contracts.ERC20TokenContract.ABI
res, err := k.CallEVM(ctx, abi, k.getModuleEthAddress(ctx), contract, true, contracts.MethodMint, to, amount)
if err != nil {
return err
}
if res.Failed() {
return errorsmod.Wrapf(
types.ErrVMExecution, "failed to mint contract: %s, reason: %s",
contract.String(),
res.Revert(),
)
}
balanceAfter, err := k.BalanceOf(ctx, contract, to)
if err != nil {
return err
}
expectBalance := big.NewInt(0).Add(balanceBefore, amount)
if r := expectBalance.Cmp(balanceAfter); r != 0 {
return errorsmod.Wrapf(
types.ErrVMExecution, "failed to mint token correctly, expected after-mint amount is incorrect: %s, expected %d, actual %d",
contract.String(),
expectBalance.Int64(),
balanceAfter.Int64(),
)
}
return nil
}
// BurnERC20 burns a specific amount of ERC20 tokens from a given contract and address.
//
// Parameters:
// - ctx: the context in which the transaction is executed
// - contract: the contract address of the ERC20 token
// - from: the address from which the tokens are burned
// - amount: the amount of tokens to burn
//
// Returns an error.
func (k Keeper) BurnERC20(
ctx sdk.Context,
contract, from common.Address,
amount *big.Int,
) error {
balanceBefore, err := k.BalanceOf(ctx, contract, from)
if err != nil {
return err
}
if r := balanceBefore.Cmp(amount); r < 0 {
return errorsmod.Wrapf(
sdkerrors.ErrInsufficientFunds,
"balance: %d, swap: %d",
balanceBefore.Int64(),
amount,
)
}
abi := contracts.ERC20TokenContract.ABI
res, err := k.CallEVM(ctx, abi, k.getModuleEthAddress(ctx), contract, true, contracts.MethodBurn, from, amount)
if err != nil {
return err
}
if res.Failed() {
return errorsmod.Wrapf(types.ErrVMExecution, "failed to burn %d", amount)
}
balanceAfter, err := k.BalanceOf(ctx, contract, from)
if err != nil {
return err
}
expectBalance := big.NewInt(0).Sub(balanceBefore, amount)
if r := expectBalance.Cmp(balanceAfter); r != 0 {
return errorsmod.Wrapf(
types.ErrVMExecution, "failed to burn token correctly, expected after-burn amount is incorrect: %s, expected %d, actual %d",
contract.String(),
expectBalance.Int64(),
balanceAfter.Int64(),
)
}
return nil
}
// UpgradeERC20 upgrades the ERC20 contract to a new implementation.
//
// Parameters:
// - ctx: the SDK context.
// - implementation: the address of the new implementation contract.
//
// Returns:
// - error: an error if the upgrade fails.
func (k Keeper) UpgradeERC20(
ctx sdk.Context,
implementation common.Address,
) error {
params := k.GetParams(ctx)
if !params.EnableErc20 {
return errorsmod.Wrapf(types.ErrERC20Disabled, "erc20 is disabled")
}
if len(params.Beacon) == 0 {
return errorsmod.Wrapf(types.ErrBeaconNotSet, "beacon not set")
}
beacon := common.HexToAddress(params.Beacon)
abi := contracts.BeaconContract.ABI
res, err := k.CallEVM(ctx, abi, k.getModuleEthAddress(ctx), beacon, true, contracts.MethodUpgradeTo, implementation)
if err != nil {
return err
}
if res.Failed() {
return errorsmod.Wrapf(
types.ErrVMExecution, "failed to upgrade contract reason: %s",
res.Revert(),
)
}
return nil
}
// BalanceOf retrieves the balance of a specific account in the contract.
//
// Parameters:
// - ctx: the sdk.Context for the function
// - contract: the address of the contract
// - account: the address of the account to retrieve the balance for
//
// Returns:
// - *big.Int: the balance of the specified account
func (k Keeper) BalanceOf(
ctx sdk.Context,
contract, account common.Address,
) (*big.Int, error) {
abi := contracts.ERC20TokenContract.ABI
res, err := k.CallEVM(ctx, abi, k.getModuleEthAddress(ctx), contract, false, contracts.MethodBalanceOf, account)
if err != nil {
return nil, err
}
unpacked, err := abi.Unpack(contracts.MethodBalanceOf, res.Ret)
if err != nil || len(unpacked) == 0 {
return nil, err
}
balance, ok := unpacked[0].(*big.Int)
if !ok {
return nil, err
}
return balance, nil
}
func (k Keeper) getModuleEthAddress(ctx sdk.Context) common.Address {
moduleAccount := k.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
return common.BytesToAddress(moduleAccount.GetAddress().Bytes())
}
func (k Keeper) buildERC20Token(
ctx sdk.Context,
name string,
symbol string,
minUnit string,
scale uint32,
) (*v1.Token, error) {
if !k.HasMinUint(ctx, minUnit) {
if k.HasSymbol(ctx, symbol) {
return nil, errorsmod.Wrapf(types.ErrSymbolAlreadyExists, "symbol already exists: %s", symbol)
}
if !k.ics20Keeper.HasTrace(ctx, minUnit) {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "token: %s does not exist", minUnit)
}
return &v1.Token{
Symbol: symbol,
Name: name,
Scale: scale,
MinUnit: minUnit,
Mintable: true,
Owner: k.accountKeeper.GetModuleAddress(types.ModuleName).String(),
}, nil
}
token, err := k.getTokenByMinUnit(ctx, minUnit)
if err != nil {
return nil, err
}
return &token, nil
}