Skip to content

Commit

Permalink
add a switch for enabling erc20 swap function
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-zq committed Apr 20, 2024
1 parent 1eefdb1 commit ba708ee
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 47 deletions.
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

0 comments on commit ba708ee

Please sign in to comment.