-
Notifications
You must be signed in to change notification settings - Fork 2
/
evm_hooks.go
95 lines (81 loc) · 2.57 KB
/
evm_hooks.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
package keeper
import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/core"
ethtypes "github.com/ethereum/go-ethereum/core/types"
evmtypes "github.com/cvn-network/cvn/v2/x/evm/types"
"github.com/cvn-network/cvn/v2/x/revenue/v1/types"
)
var _ evmtypes.EvmHooks = Hooks{}
// Hooks wrapper struct for fees keeper
type Hooks struct {
k Keeper
}
// Hooks return the wrapper hooks struct for the Keeper
func (k Keeper) Hooks() Hooks {
return Hooks{k}
}
// PostTxProcessing is a wrapper for calling the EVM PostTxProcessing hook on
// the module keeper
func (h Hooks) PostTxProcessing(ctx sdk.Context, msg core.Message, receipt *ethtypes.Receipt) error {
return h.k.PostTxProcessing(ctx, msg, receipt)
}
// PostTxProcessing implements EvmHooks.PostTxProcessing. After each successful
// interaction with a registered contract, the contract deployer (or, if set,
// the withdraw address) receives a share from the transaction fees paid by the
// transaction sender.
func (k Keeper) PostTxProcessing(
ctx sdk.Context,
msg core.Message,
receipt *ethtypes.Receipt,
) error {
// check if the fees are globally enabled
params := k.GetParams(ctx)
if !params.EnableRevenue {
return nil
}
contract := msg.To()
if contract == nil {
return nil
}
// if the contract is not registered to receive fees, do nothing
revenue, found := k.GetRevenue(ctx, *contract)
if !found {
return nil
}
withdrawer := revenue.GetWithdrawerAddr()
if len(withdrawer) == 0 {
withdrawer = revenue.GetDeployerAddr()
}
txFee := sdk.NewIntFromUint64(receipt.GasUsed).Mul(sdk.NewIntFromBigInt(msg.GasPrice()))
developerFee := (params.DeveloperShares).MulInt(txFee).TruncateInt()
evmDenom := k.evmKeeper.GetParams(ctx).EvmDenom
fees := sdk.Coins{{Denom: evmDenom, Amount: developerFee}}
// distribute the fees to the contract deployer / withdraw address
err := k.bankKeeper.SendCoinsFromModuleToAccount(
ctx,
k.feeCollectorName,
withdrawer,
fees,
)
if err != nil {
return errorsmod.Wrapf(
err,
"fee collector account failed to distribute developer fees (%s) to withdraw address %s. contract %s",
fees, withdrawer, contract,
)
}
ctx.EventManager().EmitEvents(
sdk.Events{
sdk.NewEvent(
types.EventTypeDistributeDevRevenue,
sdk.NewAttribute(sdk.AttributeKeySender, msg.From().String()),
sdk.NewAttribute(types.AttributeKeyContract, contract.String()),
sdk.NewAttribute(types.AttributeKeyWithdrawerAddress, withdrawer.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, developerFee.String()),
),
},
)
return nil
}