@@ -3,10 +3,11 @@ package distribution
33import (
44 "embed"
55 "fmt"
6+ sdk "github.com/cosmos/cosmos-sdk/types"
7+ "github.com/cosmos/evm/x/vm/statedb"
68
79 "github.com/ethereum/go-ethereum/accounts/abi"
810 "github.com/ethereum/go-ethereum/common"
9- "github.com/ethereum/go-ethereum/core/tracing"
1011 "github.com/ethereum/go-ethereum/core/vm"
1112
1213 cmn "github.com/cosmos/evm/precompiles/common"
@@ -84,71 +85,48 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
8485}
8586
8687// Run executes the precompiled contract distribution methods defined in the ABI.
87- func (p Precompile ) Run (evm * vm.EVM , contract * vm.Contract , readOnly bool ) (bz []byte , err error ) {
88- ctx , stateDB , method , initialGas , args , err := p .RunSetup (evm , contract , readOnly , p .IsTransaction )
89- if err != nil {
90- return nil , err
91- }
92-
93- // Start the balance change handler before executing the precompile.
94- p .GetBalanceHandler ().BeforeBalanceChange (ctx )
95-
96- // This handles any out of gas errors that may occur during the execution of a precompile tx or query.
97- // It avoids panics and returns the out of gas error so the EVM can continue gracefully.
98- defer cmn .HandleGasError (ctx , contract , initialGas , & err )()
99-
100- switch method .Name {
101- // Custom transactions
102- case ClaimRewardsMethod :
103- bz , err = p .ClaimRewards (ctx , contract , stateDB , method , args )
104- // Distribution transactions
105- case SetWithdrawAddressMethod :
106- bz , err = p .SetWithdrawAddress (ctx , contract , stateDB , method , args )
107- case WithdrawDelegatorRewardMethod :
108- bz , err = p .WithdrawDelegatorReward (ctx , contract , stateDB , method , args )
109- case WithdrawValidatorCommissionMethod :
110- bz , err = p .WithdrawValidatorCommission (ctx , contract , stateDB , method , args )
111- case FundCommunityPoolMethod :
112- bz , err = p .FundCommunityPool (ctx , contract , stateDB , method , args )
113- case DepositValidatorRewardsPoolMethod :
114- bz , err = p .DepositValidatorRewardsPool (ctx , contract , stateDB , method , args )
115- // Distribution queries
116- case ValidatorDistributionInfoMethod :
117- bz , err = p .ValidatorDistributionInfo (ctx , contract , method , args )
118- case ValidatorOutstandingRewardsMethod :
119- bz , err = p .ValidatorOutstandingRewards (ctx , contract , method , args )
120- case ValidatorCommissionMethod :
121- bz , err = p .ValidatorCommission (ctx , contract , method , args )
122- case ValidatorSlashesMethod :
123- bz , err = p .ValidatorSlashes (ctx , contract , method , args )
124- case DelegationRewardsMethod :
125- bz , err = p .DelegationRewards (ctx , contract , method , args )
126- case DelegationTotalRewardsMethod :
127- bz , err = p .DelegationTotalRewards (ctx , contract , method , args )
128- case DelegatorValidatorsMethod :
129- bz , err = p .DelegatorValidators (ctx , contract , method , args )
130- case DelegatorWithdrawAddressMethod :
131- bz , err = p .DelegatorWithdrawAddress (ctx , contract , method , args )
132- case CommunityPoolMethod :
133- bz , err = p .CommunityPool (ctx , contract , method , args )
134- }
135-
136- if err != nil {
137- return nil , err
138- }
139-
140- cost := ctx .GasMeter ().GasConsumed () - initialGas
141-
142- if ! contract .UseGas (cost , nil , tracing .GasChangeCallPrecompiledContract ) {
143- return nil , vm .ErrOutOfGas
144- }
145-
146- // Process the native balance changes after the method execution.
147- if err = p .GetBalanceHandler ().AfterBalanceChange (ctx , stateDB ); err != nil {
148- return nil , err
149- }
150-
151- return bz , nil
88+ func (p Precompile ) Run (evm * vm.EVM , contract * vm.Contract , readOnly bool ) ([]byte , error ) {
89+ return p .ExecuteWithBalanceHandling (
90+ evm , contract , readOnly , p .IsTransaction ,
91+ func (ctx sdk.Context , contract * vm.Contract , stateDB * statedb.StateDB , method * abi.Method , args []interface {}) ([]byte , error ) {
92+ switch method .Name {
93+ // Custom transactions
94+ case ClaimRewardsMethod :
95+ return p .ClaimRewards (ctx , contract , stateDB , method , args )
96+ // Distribution transactions
97+ case SetWithdrawAddressMethod :
98+ return p .SetWithdrawAddress (ctx , contract , stateDB , method , args )
99+ case WithdrawDelegatorRewardMethod :
100+ return p .WithdrawDelegatorReward (ctx , contract , stateDB , method , args )
101+ case WithdrawValidatorCommissionMethod :
102+ return p .WithdrawValidatorCommission (ctx , contract , stateDB , method , args )
103+ case FundCommunityPoolMethod :
104+ return p .FundCommunityPool (ctx , contract , stateDB , method , args )
105+ case DepositValidatorRewardsPoolMethod :
106+ return p .DepositValidatorRewardsPool (ctx , contract , stateDB , method , args )
107+ // Distribution queries
108+ case ValidatorDistributionInfoMethod :
109+ return p .ValidatorDistributionInfo (ctx , contract , method , args )
110+ case ValidatorOutstandingRewardsMethod :
111+ return p .ValidatorOutstandingRewards (ctx , contract , method , args )
112+ case ValidatorCommissionMethod :
113+ return p .ValidatorCommission (ctx , contract , method , args )
114+ case ValidatorSlashesMethod :
115+ return p .ValidatorSlashes (ctx , contract , method , args )
116+ case DelegationRewardsMethod :
117+ return p .DelegationRewards (ctx , contract , method , args )
118+ case DelegationTotalRewardsMethod :
119+ return p .DelegationTotalRewards (ctx , contract , method , args )
120+ case DelegatorValidatorsMethod :
121+ return p .DelegatorValidators (ctx , contract , method , args )
122+ case DelegatorWithdrawAddressMethod :
123+ return p .DelegatorWithdrawAddress (ctx , contract , method , args )
124+ case CommunityPoolMethod :
125+ return p .CommunityPool (ctx , contract , method , args )
126+ }
127+ return nil , nil
128+ },
129+ )
152130}
153131
154132// IsTransaction checks if the given method name corresponds to a transaction or query.
0 commit comments