|
| 1 | +package types |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "maps" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common" |
| 8 | + "github.com/ethereum/go-ethereum/core/vm" |
| 9 | + |
| 10 | + bankprecompile "github.com/cosmos/evm/precompiles/bank" |
| 11 | + "github.com/cosmos/evm/precompiles/bech32" |
| 12 | + cmn "github.com/cosmos/evm/precompiles/common" |
| 13 | + distprecompile "github.com/cosmos/evm/precompiles/distribution" |
| 14 | + govprecompile "github.com/cosmos/evm/precompiles/gov" |
| 15 | + ics20precompile "github.com/cosmos/evm/precompiles/ics20" |
| 16 | + "github.com/cosmos/evm/precompiles/p256" |
| 17 | + slashingprecompile "github.com/cosmos/evm/precompiles/slashing" |
| 18 | + stakingprecompile "github.com/cosmos/evm/precompiles/staking" |
| 19 | + erc20Keeper "github.com/cosmos/evm/x/erc20/keeper" |
| 20 | + transferkeeper "github.com/cosmos/evm/x/ibc/transfer/keeper" |
| 21 | + channelkeeper "github.com/cosmos/ibc-go/v10/modules/core/04-channel/keeper" |
| 22 | + |
| 23 | + "github.com/cosmos/cosmos-sdk/codec" |
| 24 | + distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper" |
| 25 | + govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" |
| 26 | + slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper" |
| 27 | + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" |
| 28 | +) |
| 29 | + |
| 30 | +type StaticPrecompiles map[common.Address]vm.PrecompiledContract |
| 31 | + |
| 32 | +func NewStaticPrecompiles() StaticPrecompiles { |
| 33 | + return make(StaticPrecompiles) |
| 34 | +} |
| 35 | + |
| 36 | +func (s StaticPrecompiles) WithPraguePrecompiles() StaticPrecompiles { |
| 37 | + maps.Copy(s, vm.PrecompiledContractsPrague) |
| 38 | + return s |
| 39 | +} |
| 40 | + |
| 41 | +func (s StaticPrecompiles) WithP256Precompile() StaticPrecompiles { |
| 42 | + p256Precompile := &p256.Precompile{} |
| 43 | + s[p256Precompile.Address()] = p256Precompile |
| 44 | + return s |
| 45 | +} |
| 46 | + |
| 47 | +func (s StaticPrecompiles) WithBech32Precompile() StaticPrecompiles { |
| 48 | + bech32Precompile, err := bech32.NewPrecompile(bech32PrecompileBaseGas) |
| 49 | + if err != nil { |
| 50 | + panic(fmt.Errorf("failed to instantiate bech32 precompile: %w", err)) |
| 51 | + } |
| 52 | + s[bech32Precompile.Address()] = bech32Precompile |
| 53 | + return s |
| 54 | +} |
| 55 | + |
| 56 | +func (s StaticPrecompiles) WithStakingPrecompile( |
| 57 | + stakingKeeper stakingkeeper.Keeper, |
| 58 | + bankKeeper cmn.BankKeeper, |
| 59 | + opts ...Option, |
| 60 | +) StaticPrecompiles { |
| 61 | + options := defaultOptionals() |
| 62 | + for _, opt := range opts { |
| 63 | + opt(&options) |
| 64 | + } |
| 65 | + |
| 66 | + stakingPrecompile := stakingprecompile.NewPrecompile( |
| 67 | + stakingKeeper, |
| 68 | + stakingkeeper.NewMsgServerImpl(&stakingKeeper), |
| 69 | + stakingkeeper.NewQuerier(&stakingKeeper), |
| 70 | + bankKeeper, |
| 71 | + options.AddressCodec, |
| 72 | + ) |
| 73 | + |
| 74 | + s[stakingPrecompile.Address()] = stakingPrecompile |
| 75 | + return s |
| 76 | +} |
| 77 | + |
| 78 | +func (s StaticPrecompiles) WithDistributionPrecompile( |
| 79 | + distributionKeeper distributionkeeper.Keeper, |
| 80 | + stakingKeeper stakingkeeper.Keeper, |
| 81 | + bankKeeper cmn.BankKeeper, |
| 82 | + opts ...Option, |
| 83 | +) StaticPrecompiles { |
| 84 | + options := defaultOptionals() |
| 85 | + for _, opt := range opts { |
| 86 | + opt(&options) |
| 87 | + } |
| 88 | + |
| 89 | + distributionPrecompile := distprecompile.NewPrecompile( |
| 90 | + distributionKeeper, |
| 91 | + distributionkeeper.NewMsgServerImpl(distributionKeeper), |
| 92 | + distributionkeeper.NewQuerier(distributionKeeper), |
| 93 | + stakingKeeper, |
| 94 | + bankKeeper, |
| 95 | + options.AddressCodec, |
| 96 | + ) |
| 97 | + |
| 98 | + s[distributionPrecompile.Address()] = distributionPrecompile |
| 99 | + return s |
| 100 | +} |
| 101 | + |
| 102 | +func (s StaticPrecompiles) WithICS20Precompile( |
| 103 | + bankKeeper cmn.BankKeeper, |
| 104 | + stakingKeeper stakingkeeper.Keeper, |
| 105 | + transferKeeper *transferkeeper.Keeper, |
| 106 | + channelKeeper *channelkeeper.Keeper, |
| 107 | +) StaticPrecompiles { |
| 108 | + ibcTransferPrecompile := ics20precompile.NewPrecompile( |
| 109 | + bankKeeper, |
| 110 | + stakingKeeper, |
| 111 | + transferKeeper, |
| 112 | + channelKeeper, |
| 113 | + ) |
| 114 | + |
| 115 | + s[ibcTransferPrecompile.Address()] = ibcTransferPrecompile |
| 116 | + return s |
| 117 | +} |
| 118 | + |
| 119 | +func (s StaticPrecompiles) WithBankPrecompile( |
| 120 | + bankKeeper cmn.BankKeeper, |
| 121 | + erc20Keeper *erc20Keeper.Keeper, |
| 122 | +) StaticPrecompiles { |
| 123 | + bankPrecompile := bankprecompile.NewPrecompile(bankKeeper, erc20Keeper) |
| 124 | + s[bankPrecompile.Address()] = bankPrecompile |
| 125 | + return s |
| 126 | +} |
| 127 | + |
| 128 | +func (s StaticPrecompiles) WithGovPrecompile( |
| 129 | + govKeeper govkeeper.Keeper, |
| 130 | + bankKeeper cmn.BankKeeper, |
| 131 | + codec codec.Codec, |
| 132 | + opts ...Option, |
| 133 | +) StaticPrecompiles { |
| 134 | + options := defaultOptionals() |
| 135 | + for _, opt := range opts { |
| 136 | + opt(&options) |
| 137 | + } |
| 138 | + |
| 139 | + govPrecompile := govprecompile.NewPrecompile( |
| 140 | + govkeeper.NewMsgServerImpl(&govKeeper), |
| 141 | + govkeeper.NewQueryServer(&govKeeper), |
| 142 | + bankKeeper, |
| 143 | + codec, |
| 144 | + options.AddressCodec, |
| 145 | + ) |
| 146 | + |
| 147 | + s[govPrecompile.Address()] = govPrecompile |
| 148 | + return s |
| 149 | +} |
| 150 | + |
| 151 | +func (s StaticPrecompiles) WithSlashingPrecompile( |
| 152 | + slashingKeeper slashingkeeper.Keeper, |
| 153 | + bankKeeper cmn.BankKeeper, |
| 154 | + opts ...Option, |
| 155 | +) StaticPrecompiles { |
| 156 | + options := defaultOptionals() |
| 157 | + for _, opt := range opts { |
| 158 | + opt(&options) |
| 159 | + } |
| 160 | + |
| 161 | + slashingPrecompile := slashingprecompile.NewPrecompile( |
| 162 | + slashingKeeper, |
| 163 | + slashingkeeper.NewMsgServerImpl(slashingKeeper), |
| 164 | + bankKeeper, |
| 165 | + options.ValidatorAddrCodec, |
| 166 | + options.ConsensusAddrCodec, |
| 167 | + ) |
| 168 | + |
| 169 | + s[slashingPrecompile.Address()] = slashingPrecompile |
| 170 | + return s |
| 171 | +} |
0 commit comments