Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a switch for enabling erc20 swap function #409

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [\#402](https://github.com/irisnet/irismod/pull/402) Add cli for swap from/to ERC20.
* [\#404](https://github.com/irisnet/irismod/pull/404) Implement token Balances api.
* [\#405](https://github.com/irisnet/irismod/pull/405) Rolled back the cosmos-sdk version to v0.47.9.
* [\#409](https://github.com/irisnet/irismod/pull/409) feat: add a switch for enabling erc20 swap function.

### Bug Fixes

Expand Down
94 changes: 79 additions & 15 deletions api/irismod/token/v1/token.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions modules/token/keeper/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func (k Keeper) SwapFromERC20(
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
Expand Down Expand Up @@ -146,6 +150,10 @@ func (k Keeper) SwapToERC20(
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()) {
Expand Down
5 changes: 5 additions & 0 deletions modules/token/keeper/evm_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type erc20Hook struct {
//
// Return type: error
func (hook erc20Hook) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error {
disable := !hook.k.ERC20Enabled(ctx)
erc20 := contracts.ERC20TokenContract.ABI
for _, log := range receipt.Logs {
// Note: the `SwapToNative` event contains 1 topics
Expand All @@ -54,6 +55,10 @@ func (hook erc20Hook) PostTxProcessing(ctx sdk.Context, msg core.Message, receip
continue
}

if disable {
return types.ErrERC20Disabled
}

eventArgs, err := erc20.Unpack(event.Name, log.Data)
if err != nil {
return errorsmod.Wrap(types.ErrInvalidContract, "failed to unpack SwapToNative event")
Expand Down
6 changes: 6 additions & 0 deletions modules/token/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ func (k Keeper) SetParams(ctx sdk.Context, params v1.Params) error {

return nil
}

// ERC20Enabled returns true if ERC20 is enabled
func (k Keeper) ERC20Enabled(ctx sdk.Context) bool {
params := k.GetParams(ctx)
return params.EnableErc20
}
1 change: 1 addition & 0 deletions modules/token/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ var (
ErrERC20NotDeployed = errorsmod.Register(ModuleName, 23, "erc20 contract not deployed")
ErrUnsupportedKey = errorsmod.Register(ModuleName, 24, "evm not supported public key")
ErrInvalidContract = errorsmod.Register(ModuleName, 25, "invalid contract")
ErrERC20Disabled = errorsmod.Register(ModuleName, 26, "erc20 swap is disabled")
)
3 changes: 2 additions & 1 deletion modules/token/types/v1/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ func DefaultParams() Params {
TokenTaxRate: sdk.NewDecWithPrec(4, 1), // 0.4 (40%)
IssueTokenBaseFee: sdk.NewCoin(defaultToken.Symbol, sdk.NewInt(60000)),
MintTokenFeeRatio: sdk.NewDecWithPrec(1, 1), // 0.1 (10%)
EnableErc20: true,
}
}

// ValidateParams validates the given params
// Validate validates the given params
func (p Params) Validate() error {
if err := validateTaxRate(p.TokenTaxRate); err != nil {
return err
Expand Down
Loading
Loading