Documentation
¶
Index ¶
- Constants
- Variables
- func CalculateFunctionSelector(functionSignature string) []byte
- func CheckConfigure(parentTimestamp *big.Int, currentTimestamp *big.Int, ...)
- func PackGetPriceInput(identifier *PriceFeedId) ([]byte, error)
- func WritePriceToState(state StateDB, price *streamer.Price) error
- type PrecompileAccessibleState
- type PriceFeedId
- type PriceOracleConfig
- type RunStatefulPrecompileFunc
- type StateDB
- type StatefulPrecompileConfig
- type StatefulPrecompiledContract
Constants ¶
const (
GetPriceGasCost = 5_000
)
Gas costs for stateful precompiles
Variables ¶
var ( PriceOracleAddress = common.HexToAddress("0x0300000000000000000000000000000000000001") UsedAddresses = []common.Address{ PriceOracleAddress, } )
Designated addresses of stateful precompiles Note: it is important that none of these addresses conflict with each other or any other precompiles in core/vm/contracts.go. We start at 0x0200000000000000000000000000000000000000 and will increment by 1 from here to reduce the risk of conflicts. For forks of subnet-evm, users should start at 0x0300000000000000000000000000000000000000 to ensure that their own modifications do not conflict with stateful precompiles that may be added to subnet-evm in the future.
var ( // Singleton StatefulPrecompiledContract for GetPriceing native assets by permissioned callers. PriceOraclePreCompile StatefulPrecompiledContract = CreateNativeGetPriceerPrecompile(PriceOracleAddress) ErrCannotGetPrice = errors.New("non-enabled cannot GetPrice") GetPriceInputLen = common.HashLength SetPriceInputLen = common.HashLength + common.HashLength )
var (
AVAX_USD = PriceFeedId(common.BigToHash(big.NewInt(0)))
)
var SymbolToFeedId = map[string]PriceFeedId{ "AVAX/USD": AVAX_USD, }
Functions ¶
func CalculateFunctionSelector ¶
CalculateFunctionSelector returns the 4 byte function selector that results from [functionSignature] Ex. the function setBalance(addr address, balance uint256) should be passed in as the string: "setBalance(address,uint256)"
func CheckConfigure ¶
func CheckConfigure(parentTimestamp *big.Int, currentTimestamp *big.Int, config StatefulPrecompileConfig, state StateDB)
CheckConfigure checks if [config] is activated by the transition from block at [parentTimestamp] to [currentTimestamp]. If it does, then it calls Configure on [config] to make the necessary state update to enable the StatefulPrecompile. Note: this function is called within genesis to configure the starting state if it [config] specifies that it should be configured at genesis, or happens during block processing to update the state before processing the given block. TODO: add ability to call Configure at different timestamps, so that developers can easily re-configure by updating the stateful precompile config. Assumes that [config] is non-nil.
func PackGetPriceInput ¶
func PackGetPriceInput(identifier *PriceFeedId) ([]byte, error)
PackGetPriceInput packs [address] and [amount] into the appropriate arguments for GetPriceing operation.
Types ¶
type PrecompileAccessibleState ¶
type PrecompileAccessibleState interface {
GetStateDB() StateDB
}
PrecompileAccessibleState defines the interface exposed to stateful precompile contracts
type PriceFeedId ¶
func BytesToPriceFeedId ¶
func BytesToPriceFeedId(b []byte) PriceFeedId
func UnpackGetPriceInput ¶
func UnpackGetPriceInput(input []byte) (*PriceFeedId, error)
UnpackGetPriceInput attempts to unpack [input] into the arguments to the GetPrice precompile assumes that [input] does not include selector (omits first 4 bytes in PackGetPriceInput)
func (*PriceFeedId) Bytes ¶
func (p *PriceFeedId) Bytes() []byte
type PriceOracleConfig ¶
type PriceOracleConfig struct { BlockTimestamp *big.Int `json:"blockTimestamp"` AllowListAdmins []common.Address `json:"adminAddresses"` }
PriceOracleConfig wraps [AllowListConfig] and uses it to implement the StatefulPrecompileConfig interface while adding in the contract deployer specific precompile address.
func (*PriceOracleConfig) Address ¶
func (c *PriceOracleConfig) Address() common.Address
Address returns the address of the native GetPriceer contract.
func (*PriceOracleConfig) Configure ¶
func (c *PriceOracleConfig) Configure(state StateDB)
Configure configures [state] with the desired admins based on [c].
func (*PriceOracleConfig) Contract ¶
func (c *PriceOracleConfig) Contract() StatefulPrecompiledContract
Contract returns the singleton stateful precompiled contract to be used for the native GetPriceer.
func (*PriceOracleConfig) Timestamp ¶
func (c *PriceOracleConfig) Timestamp() *big.Int
Contract returns the singleton stateful precompiled contract to be used for the native GetPriceer.
type StateDB ¶
type StateDB interface { GetState(common.Address, common.Hash) common.Hash SetState(common.Address, common.Hash, common.Hash) SetCode(common.Address, []byte) SetNonce(common.Address, uint64) GetNonce(common.Address) uint64 GetBalance(common.Address) *big.Int AddBalance(common.Address, *big.Int) SubBalance(common.Address, *big.Int) CreateAccount(common.Address) Exist(common.Address) bool }
StateDB is the interface for accessing EVM state
type StatefulPrecompileConfig ¶
type StatefulPrecompileConfig interface { // Address returns the address where the stateful precompile is accessible. Address() common.Address // Timestamp returns the timestamp at which this stateful precompile should be enabled. // 1) 0 indicates that the precompile should be enabled from genesis. // 2) n indicates that the precompile should be enabled in the first block with timestamp >= [n]. // 3) nil indicates that the precompile is never enabled. Timestamp() *big.Int // Configure is called on the first block where the stateful precompile should be enabled. // This allows the stateful precompile to configure its own state via [StateDB] as necessary. // This function must be deterministic since it will impact the EVM state. If a change to the // config causes a change to the state modifications made in Configure, then it cannot be safely // made to the config after the network upgrade has gone into effect. // // Configure is called on the first block where the stateful precompile should be enabled. This // provides the config the ability to set its initial state and should only modify the state within // its own address space. Configure(StateDB) // Contract returns a thread-safe singleton that can be used as the StatefulPrecompiledContract when // this config is enabled. Contract() StatefulPrecompiledContract }
StatefulPrecompileConfig defines the interface for a stateful precompile to
type StatefulPrecompiledContract ¶
type StatefulPrecompiledContract interface { // Run executes the precompiled contract. Run(accessibleState PrecompileAccessibleState, caller common.Address, addr common.Address, input []byte, suppliedGas uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) }
StatefulPrecompiledContract is the interface for executing a precompiled contract
func CreateNativeGetPriceerPrecompile ¶
func CreateNativeGetPriceerPrecompile(precompileAddr common.Address) StatefulPrecompiledContract
createNativeGetPriceerPrecompile returns a StatefulPrecompiledContract with R/W control of an allow list at [precompileAddr] and a native coin GetPriceer.