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

Add support of registered ERC20 tokens to bank send method on SDK layer #252

Merged
merged 3 commits into from
Dec 1, 2023
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 .github/workflows/proto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Protobuf
# Protobuf runs buf (https://buf.build/) lint and check-breakage
# This workflow is only run when a .proto file has been changed
on:
workflow_dispatch:
pull_request:
# paths:
# - "proto/**"
Expand Down
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ func NewHaqq(
encodingConfig.TxConfig,
),
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
haqqbank.NewAppModule(bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), app.BankKeeper, app.Erc20Keeper),
haqqbank.NewAppModule(bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), app.BankKeeper, app.Erc20Keeper, app.AccountKeeper),
// haqqbank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
capability.NewAppModule(appCodec, *app.CapabilityKeeper),
crisis.NewAppModule(&app.CrisisKeeper, skipGenesisInvariants),
Expand Down Expand Up @@ -750,7 +750,7 @@ func NewHaqq(
// transactions
app.sm = module.NewSimulationManager(
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
haqqbank.NewAppModule(bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), app.BankKeeper, app.Erc20Keeper),
haqqbank.NewAppModule(bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), app.BankKeeper, app.Erc20Keeper, app.AccountKeeper),
capability.NewAppModule(appCodec, *app.CapabilityKeeper),
gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
// mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
Expand Down
82 changes: 40 additions & 42 deletions x/bank/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var _ types.QueryServer = BaseKeeper{}

// Balance implements the Query/Balance gRPC method
func (k WrappedBaseKeeper) Balance(ctx context.Context, req *types.QueryBalanceRequest) (*types.QueryBalanceResponse, error) {
res, err := k.wbk.Balance(ctx, req)
res, err := k.Keeper.Balance(ctx, req)
if err != nil {
return nil, err
}
Expand All @@ -26,38 +26,36 @@ func (k WrappedBaseKeeper) Balance(ctx context.Context, req *types.QueryBalanceR
return res, nil
}

tokenPairID := k.ek.GetTokenPairID(sdkCtx, req.Denom)
if len(tokenPairID) == 0 {
return res, nil
}

tokenPair, found := k.ek.GetTokenPair(sdkCtx, tokenPairID)
if !found || !tokenPair.Enabled {
return res, nil
}

// AccAddressFromBech32 error check already handled above in original method
address, _ := sdk.AccAddressFromBech32(req.Address)
tokenPairs := k.ek.GetTokenPairs(sdkCtx)
evmAddr := common.BytesToAddress(address.Bytes())
erc20 := contracts.ERC20MinterBurnerDecimalsContract.ABI

for _, tokenPair := range tokenPairs {
if !tokenPair.Enabled {
continue
}

if tokenPair.Denom != req.Denom {
continue
}

contract := tokenPair.GetERC20Contract()
balanceToken := k.ek.BalanceOf(sdkCtx, erc20, contract, evmAddr)
if balanceToken == nil {
return nil, errorsmod.Wrap(erc20types.ErrEVMCall, "failed to retrieve balance")
}
balanceTokenCoin := sdk.NewCoin(tokenPair.Denom, sdk.NewIntFromBigInt(balanceToken))

coin := res.Balance.Add(balanceTokenCoin)
res.Balance = &coin
contract := tokenPair.GetERC20Contract()
balanceToken := k.ek.BalanceOf(sdkCtx, erc20, contract, evmAddr)
if balanceToken == nil {
return nil, errorsmod.Wrap(erc20types.ErrEVMCall, "failed to retrieve balance")
}
balanceTokenCoin := sdk.NewCoin(tokenPair.Denom, sdk.NewIntFromBigInt(balanceToken))

coin := res.Balance.Add(balanceTokenCoin)
res.Balance = &coin

return res, nil
}

// AllBalances implements the Query/AllBalances gRPC method
func (k WrappedBaseKeeper) AllBalances(ctx context.Context, req *types.QueryAllBalancesRequest) (*types.QueryAllBalancesResponse, error) {
res, err := k.wbk.AllBalances(ctx, req)
res, err := k.Keeper.AllBalances(ctx, req)
if err != nil {
return nil, err
}
Expand All @@ -69,32 +67,32 @@ func (k WrappedBaseKeeper) AllBalances(ctx context.Context, req *types.QueryAllB

// AccAddressFromBech32 error check already handled above in original method
address, _ := sdk.AccAddressFromBech32(req.Address)
tokenPairs := k.ek.GetTokenPairs(sdkCtx)
evmAddr := common.BytesToAddress(address.Bytes())
erc20 := contracts.ERC20MinterBurnerDecimalsContract.ABI

for _, tokenPair := range tokenPairs {
if !tokenPair.Enabled {
continue
}

contract := tokenPair.GetERC20Contract()
balanceToken := k.ek.BalanceOf(sdkCtx, erc20, contract, evmAddr)
if balanceToken == nil {
return nil, errorsmod.Wrap(erc20types.ErrEVMCall, "failed to retrieve balance")
k.ek.IterateTokenPairs(sdkCtx, func(tokenPair erc20types.TokenPair) (stop bool) {
if tokenPair.Enabled {
contract := tokenPair.GetERC20Contract()
balanceToken := k.ek.BalanceOf(sdkCtx, erc20, contract, evmAddr)
if balanceToken == nil {
// TODO Log error
return false
}
balanceTokenCoin := sdk.NewCoin(tokenPair.Denom, sdk.NewIntFromBigInt(balanceToken))

res.Balances = res.Balances.Add(balanceTokenCoin)
}
balanceTokenCoin := sdk.NewCoin(tokenPair.Denom, sdk.NewIntFromBigInt(balanceToken))

res.Balances = res.Balances.Add(balanceTokenCoin)
}
return false
})

return res, nil
}

// SpendableBalances implements a gRPC query handler for retrieving an account's
// spendable balances.
func (k WrappedBaseKeeper) SpendableBalances(ctx context.Context, req *types.QuerySpendableBalancesRequest) (*types.QuerySpendableBalancesResponse, error) {
res, err := k.wbk.SpendableBalances(ctx, req)
res, err := k.Keeper.SpendableBalances(ctx, req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -130,32 +128,32 @@ func (k WrappedBaseKeeper) SpendableBalances(ctx context.Context, req *types.Que

// TotalSupply implements the Query/TotalSupply gRPC method
func (k WrappedBaseKeeper) TotalSupply(ctx context.Context, req *types.QueryTotalSupplyRequest) (*types.QueryTotalSupplyResponse, error) {
return k.wbk.TotalSupply(ctx, req)
return k.Keeper.TotalSupply(ctx, req)
}

// SupplyOf implements the Query/SupplyOf gRPC method
func (k WrappedBaseKeeper) SupplyOf(c context.Context, req *types.QuerySupplyOfRequest) (*types.QuerySupplyOfResponse, error) {
return k.wbk.SupplyOf(c, req)
return k.Keeper.SupplyOf(c, req)
}

// Params implements the gRPC service handler for querying x/bank parameters.
func (k WrappedBaseKeeper) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
return k.wbk.Params(ctx, req)
return k.Keeper.Params(ctx, req)
}

// DenomsMetadata implements Query/DenomsMetadata gRPC method.
func (k WrappedBaseKeeper) DenomsMetadata(c context.Context, req *types.QueryDenomsMetadataRequest) (*types.QueryDenomsMetadataResponse, error) {
return k.wbk.DenomsMetadata(c, req)
return k.Keeper.DenomsMetadata(c, req)
}

// DenomMetadata implements Query/DenomMetadata gRPC method.
func (k WrappedBaseKeeper) DenomMetadata(c context.Context, req *types.QueryDenomMetadataRequest) (*types.QueryDenomMetadataResponse, error) {
return k.wbk.DenomMetadata(c, req)
return k.Keeper.DenomMetadata(c, req)
}

func (k WrappedBaseKeeper) DenomOwners(
goCtx context.Context,
req *types.QueryDenomOwnersRequest,
) (*types.QueryDenomOwnersResponse, error) {
return k.wbk.DenomOwners(goCtx, req)
return k.Keeper.DenomOwners(goCtx, req)
}
Loading
Loading