|
| 1 | +package precompile |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + |
| 7 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 8 | + gethabi "github.com/ethereum/go-ethereum/accounts/abi" |
| 9 | + gethcommon "github.com/ethereum/go-ethereum/common" |
| 10 | + "github.com/ethereum/go-ethereum/core/vm" |
| 11 | + gethparams "github.com/ethereum/go-ethereum/params" |
| 12 | + |
| 13 | + "github.com/NibiruChain/nibiru/v2/app/keepers" |
| 14 | + "github.com/NibiruChain/nibiru/v2/x/common/asset" |
| 15 | + "github.com/NibiruChain/nibiru/v2/x/evm/embeds" |
| 16 | + "github.com/NibiruChain/nibiru/v2/x/evm/statedb" |
| 17 | + oraclekeeper "github.com/NibiruChain/nibiru/v2/x/oracle/keeper" |
| 18 | +) |
| 19 | + |
| 20 | +var _ vm.PrecompiledContract = (*precompileOracle)(nil) |
| 21 | + |
| 22 | +// Precompile address for "Oracle.sol", the contract that enables queries for exchange rates |
| 23 | +var PrecompileAddr_Oracle = gethcommon.HexToAddress("0x0000000000000000000000000000000000000801") |
| 24 | + |
| 25 | +func (p precompileOracle) Address() gethcommon.Address { |
| 26 | + return PrecompileAddr_Oracle |
| 27 | +} |
| 28 | + |
| 29 | +func (p precompileOracle) RequiredGas(input []byte) (gasPrice uint64) { |
| 30 | + // Since [gethparams.TxGas] is the cost per (Ethereum) transaction that does not create |
| 31 | + // a contract, it's value can be used to derive an appropriate value for the precompile call. |
| 32 | + return gethparams.TxGas |
| 33 | +} |
| 34 | + |
| 35 | +const ( |
| 36 | + OracleMethod_QueryExchangeRate OracleMethod = "queryExchangeRate" |
| 37 | +) |
| 38 | + |
| 39 | +type OracleMethod string |
| 40 | + |
| 41 | +// Run runs the precompiled contract |
| 42 | +func (p precompileOracle) Run( |
| 43 | + evm *vm.EVM, contract *vm.Contract, readonly bool, |
| 44 | +) (bz []byte, err error) { |
| 45 | + // This is a `defer` pattern to add behavior that runs in the case that the error is |
| 46 | + // non-nil, creating a concise way to add extra information. |
| 47 | + defer func() { |
| 48 | + if err != nil { |
| 49 | + precompileType := reflect.TypeOf(p).Name() |
| 50 | + err = fmt.Errorf("precompile error: failed to run %s: %w", precompileType, err) |
| 51 | + } |
| 52 | + }() |
| 53 | + |
| 54 | + // 1 | Get context from StateDB |
| 55 | + stateDB, ok := evm.StateDB.(*statedb.StateDB) |
| 56 | + if !ok { |
| 57 | + err = fmt.Errorf("failed to load the sdk.Context from the EVM StateDB") |
| 58 | + return |
| 59 | + } |
| 60 | + ctx := stateDB.GetContext() |
| 61 | + |
| 62 | + method, args, err := DecomposeInput(embeds.SmartContract_Oracle.ABI, contract.Input) |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + switch OracleMethod(method.Name) { |
| 68 | + case OracleMethod_QueryExchangeRate: |
| 69 | + bz, err = p.queryExchangeRate(ctx, method, args, readonly) |
| 70 | + default: |
| 71 | + err = fmt.Errorf("invalid method called with name \"%s\"", method.Name) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + return |
| 76 | +} |
| 77 | + |
| 78 | +func PrecompileOracle(keepers keepers.PublicKeepers) vm.PrecompiledContract { |
| 79 | + return precompileOracle{ |
| 80 | + oracleKeeper: keepers.OracleKeeper, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +type precompileOracle struct { |
| 85 | + oracleKeeper oraclekeeper.Keeper |
| 86 | +} |
| 87 | + |
| 88 | +func (p precompileOracle) queryExchangeRate( |
| 89 | + ctx sdk.Context, |
| 90 | + method *gethabi.Method, |
| 91 | + args []interface{}, |
| 92 | + readOnly bool, |
| 93 | +) (bz []byte, err error) { |
| 94 | + pair, err := p.decomposeQueryExchangeRateArgs(args) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + assetPair, err := asset.TryNewPair(pair) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + price, err := p.oracleKeeper.GetExchangeRate(ctx, assetPair) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + return method.Outputs.Pack(price.String()) |
| 109 | +} |
| 110 | + |
| 111 | +func (p precompileOracle) decomposeQueryExchangeRateArgs(args []any) ( |
| 112 | + pair string, |
| 113 | + err error, |
| 114 | +) { |
| 115 | + if len(args) != 1 { |
| 116 | + err = fmt.Errorf("expected 3 arguments but got %d", len(args)) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + pair, ok := args[0].(string) |
| 121 | + if !ok { |
| 122 | + err = ErrArgTypeValidation("string pair", args[0]) |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + return pair, nil |
| 127 | +} |
0 commit comments