Skip to content

Commit 8b2580a

Browse files
authored
Send consumed rent to fee collector (#719)
1 parent 8d80c50 commit 8b2580a

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

x/dex/contract/abci.go

+21
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import (
66
"sync"
77
"time"
88

9+
seiutils "github.com/sei-protocol/sei-chain/utils"
910
"github.com/sei-protocol/sei-chain/utils/logging"
1011
"github.com/sei-protocol/sei-chain/utils/tracing"
1112

1213
sdk "github.com/cosmos/cosmos-sdk/types"
14+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
15+
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
1316
"github.com/sei-protocol/sei-chain/store/whitelist/multi"
1417
seisync "github.com/sei-protocol/sei-chain/sync"
1518
"github.com/sei-protocol/sei-chain/utils/datastructures"
@@ -46,6 +49,8 @@ func EndBlockerAtomic(ctx sdk.Context, keeper *keeper.Keeper, validContractsInfo
4649
env := newEnv(ctx, validContractsInfo, keeper)
4750
cachedCtx, msCached := cacheContext(ctx, env)
4851
memStateCopy := dexutils.GetMemState(cachedCtx.Context()).DeepCopy()
52+
preRunRents := keeper.GetRentsForContracts(ctx, seiutils.Map(validContractsInfo, func(c types.ContractInfoV2) string { return c.ContractAddr }))
53+
4954
handleDeposits(spanCtx, cachedCtx, env, keeper, tracer)
5055

5156
runner := NewParallelRunner(func(contract types.ContractInfoV2) {
@@ -66,6 +71,8 @@ func EndBlockerAtomic(ctx sdk.Context, keeper *keeper.Keeper, validContractsInfo
6671

6772
// No error is thrown for any contract. This should happen most of the time.
6873
if env.failedContractAddresses.Size() == 0 {
74+
postRunRents := keeper.GetRentsForContracts(ctx, seiutils.Map(validContractsInfo, func(c types.ContractInfoV2) string { return c.ContractAddr }))
75+
TransferRentFromDexToCollector(ctx, keeper.BankKeeper, preRunRents, postRunRents)
6976
msCached.Write()
7077
return env.validContractsInfo, ctx, true
7178
}
@@ -241,3 +248,17 @@ func filterNewValidContracts(ctx sdk.Context, env *environment) []types.Contract
241248
}
242249
return newValidContracts
243250
}
251+
252+
func TransferRentFromDexToCollector(ctx sdk.Context, bankKeeper bankkeeper.Keeper, preRents map[string]uint64, postRents map[string]uint64) {
253+
total := uint64(0)
254+
for addr, preRent := range preRents {
255+
if postRent, ok := postRents[addr]; ok {
256+
total += preRent - postRent
257+
} else {
258+
total += preRent
259+
}
260+
}
261+
if err := bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, authtypes.FeeCollectorName, sdk.NewCoins(sdk.NewCoin("usei", sdk.NewInt(int64(total))))); err != nil {
262+
ctx.Logger().Error("sending coints from dex to fee collector failed due to %s", err)
263+
}
264+
}

x/dex/contract/abci_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package contract_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
sdk "github.com/cosmos/cosmos-sdk/types"
8+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
9+
keepertest "github.com/sei-protocol/sei-chain/testutil/keeper"
10+
"github.com/sei-protocol/sei-chain/x/dex/contract"
11+
"github.com/sei-protocol/sei-chain/x/dex/types"
12+
minttypes "github.com/sei-protocol/sei-chain/x/mint/types"
13+
"github.com/stretchr/testify/require"
14+
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
15+
)
16+
17+
func TestTransferRentFromDexToCollector(t *testing.T) {
18+
preRents := map[string]uint64{"abc": 100, "def": 50}
19+
postRents := map[string]uint64{"abc": 70}
20+
21+
// expected total is (100 - 70) + 50 = 80
22+
testApp := keepertest.TestApp()
23+
ctx := testApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})
24+
bankkeeper := testApp.BankKeeper
25+
err := bankkeeper.MintCoins(ctx, minttypes.ModuleName, sdk.NewCoins(sdk.NewCoin("usei", sdk.NewInt(100))))
26+
require.Nil(t, err)
27+
err = bankkeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, types.ModuleName, sdk.NewCoins(sdk.NewCoin("usei", sdk.NewInt(100))))
28+
require.Nil(t, err)
29+
contract.TransferRentFromDexToCollector(ctx, bankkeeper, preRents, postRents)
30+
dexBalance := bankkeeper.GetBalance(ctx, testApp.AccountKeeper.GetModuleAddress(types.ModuleName), "usei")
31+
require.Equal(t, int64(20), dexBalance.Amount.Int64())
32+
collectorBalance := bankkeeper.GetBalance(ctx, testApp.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName), "usei")
33+
require.Equal(t, int64(80), collectorBalance.Amount.Int64())
34+
}

x/dex/keeper/contract.go

+10
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,13 @@ func (k Keeper) ChargeRentForGas(ctx sdk.Context, contractAddr string, gasUsed u
103103
contract.RentBalance -= uint64(gasPrice)
104104
return k.SetContract(ctx, &contract)
105105
}
106+
107+
func (k Keeper) GetRentsForContracts(ctx sdk.Context, contractAddrs []string) map[string]uint64 {
108+
res := map[string]uint64{}
109+
for _, contractAddr := range contractAddrs {
110+
if contract, err := k.GetContract(ctx, contractAddr); err == nil {
111+
res[contractAddr] = contract.RentBalance
112+
}
113+
}
114+
return res
115+
}

x/dex/keeper/contract_test.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package keeper_test
22

33
import (
4-
"github.com/cosmos/cosmos-sdk/store/prefix"
54
"testing"
65

6+
"github.com/cosmos/cosmos-sdk/store/prefix"
7+
78
sdk "github.com/cosmos/cosmos-sdk/types"
89
keepertest "github.com/sei-protocol/sei-chain/testutil/keeper"
910
"github.com/sei-protocol/sei-chain/x/dex/types"
@@ -126,3 +127,17 @@ func TestGetContractGasLimit(t *testing.T) {
126127
require.Nil(t, err)
127128
require.Equal(t, uint64(10000000), gasLimit)
128129
}
130+
131+
func TestGetRentsForContracts(t *testing.T) {
132+
keeper, ctx := keepertest.DexKeeper(t)
133+
addr := "sei1suhgf5svhu4usrurvxzlgn54ksxmn8gljarjtxqnapv8kjnp4nrsgshtdj"
134+
require.Equal(t, 0, len(keeper.GetRentsForContracts(ctx, []string{addr})))
135+
136+
keeper.SetContract(ctx, &types.ContractInfoV2{
137+
Creator: keepertest.TestAccount,
138+
ContractAddr: addr,
139+
CodeId: 1,
140+
RentBalance: 100,
141+
})
142+
require.Equal(t, map[string]uint64{addr: uint64(100)}, keeper.GetRentsForContracts(ctx, []string{addr}))
143+
}

0 commit comments

Comments
 (0)