Skip to content

Commit

Permalink
cp -r juror
Browse files Browse the repository at this point in the history
  • Loading branch information
atvanguard committed Dec 21, 2023
1 parent 036cc2e commit 279a5fe
Show file tree
Hide file tree
Showing 14 changed files with 4,823 additions and 0 deletions.
23 changes: 23 additions & 0 deletions precompile/contracts/jurorv2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
There are some must-be-done changes waiting in the generated file. Each area requiring you to add your code is marked with CUSTOM CODE to make them easy to find and modify.
Additionally there are other files you need to edit to activate your precompile.
These areas are highlighted with comments "ADD YOUR PRECOMPILE HERE".
For testing take a look at other precompile tests in contract_test.go and config_test.go in other precompile folders.
See the tutorial in <https://docs.avax.network/subnets/hello-world-precompile-tutorial> for more information about precompile development.

General guidelines for precompile development:
1- Set a suitable config key in generated module.go. E.g: "yourPrecompileConfig"
2- Read the comment and set a suitable contract address in generated module.go. E.g:
ContractAddress = common.HexToAddress("ASUITABLEHEXADDRESS")
3- It is recommended to only modify code in the highlighted areas marked with "CUSTOM CODE STARTS HERE". Typically, custom codes are required in only those areas.
Modifying code outside of these areas should be done with caution and with a deep understanding of how these changes may impact the EVM.
4- Set gas costs in generated contract.go
5- Force import your precompile package in precompile/registry/registry.go
6- Add your config unit tests under generated package config_test.go
7- Add your contract unit tests under generated package contract_test.go
8- Additionally you can add a full-fledged VM test for your precompile under plugin/vm/vm_test.go. See existing precompile tests for examples.
9- Add your solidity interface and test contract to contracts/contracts
10- Write solidity contract tests for your precompile in contracts/contracts/test
11- Write TypeScript DS-Test counterparts for your solidity tests in contracts/test
12- Create your genesis with your precompile enabled in tests/precompile/genesis/
13- Create e2e test for your solidity test in tests/precompile/solidity/suites.go
14- Run your e2e precompile Solidity tests with './scripts/run_ginkgo.sh`
68 changes: 68 additions & 0 deletions precompile/contracts/jurorv2/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Code generated
// This file is a generated precompile contract config with stubbed abstract functions.
// The file is generated by a template. Please inspect every code and comment in this file before use.

package juror

import (
"math/big"

"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
)

var _ precompileconfig.Config = &Config{}

// Config implements the precompileconfig.Config interface and
// adds specific configuration for Juror.
type Config struct {
precompileconfig.Upgrade
// CUSTOM CODE STARTS HERE
// Add your own custom fields for Config here
}

// NewConfig returns a config for a network upgrade at [blockTimestamp] that enables
// Juror.
func NewConfig(blockTimestamp *big.Int) *Config {
val := blockTimestamp.Uint64()
return &Config{
Upgrade: precompileconfig.Upgrade{BlockTimestamp: &val},
}
}

// NewDisableConfig returns config for a network upgrade at [blockTimestamp]
// that disables Juror.
func NewDisableConfig(blockTimestamp *big.Int) *Config {
val := blockTimestamp.Uint64()
return &Config{
Upgrade: precompileconfig.Upgrade{
BlockTimestamp: &val,
Disable: true,
},
}
}

// Key returns the key for the Juror precompileconfig.
// This should be the same key as used in the precompile module.
func (*Config) Key() string { return ConfigKey }

// Verify tries to verify Config and returns an error accordingly.
func (c *Config) Verify(precompileconfig.ChainConfig) error {
// CUSTOM CODE STARTS HERE
// Add your own custom verify code for Config here
// and return an error accordingly
return nil
}

// Equal returns true if [s] is a [*Config] and it has been configured identical to [c].
func (c *Config) Equal(s precompileconfig.Config) bool {
// typecast before comparison
other, ok := (s).(*Config)
if !ok {
return false
}
// CUSTOM CODE STARTS HERE
// modify this boolean accordingly with your custom Config, to check if [other] and the current [c] are equal
// if Config contains only Upgrade you can skip modifying it.
equals := c.Upgrade.Equal(&other.Upgrade)
return equals
}
62 changes: 62 additions & 0 deletions precompile/contracts/jurorv2/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Code generated
// This file is a generated precompile config test with the skeleton of test functions.
// The file is generated by a template. Please inspect every code and comment in this file before use.

package juror

import (
"math/big"
"testing"

"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ava-labs/subnet-evm/precompile/testutils"
"go.uber.org/mock/gomock"
)

// TestVerify tests the verification of Config.
func TestVerify(t *testing.T) {
tests := map[string]testutils.ConfigVerifyTest{
"valid config": {
Config: NewConfig(big.NewInt(3)),
ExpectedError: "",
},
// CUSTOM CODE STARTS HERE
// Add your own Verify tests here, e.g.:
// "your custom test name": {
// Config: NewConfig(big.NewInt(3),),
// ExpectedError: ErrYourCustomError.Error(),
// },
}
// Run verify tests.
testutils.RunVerifyTests(t, tests)
}

// TestEqual tests the equality of Config with other precompile configs.
func TestEqual(t *testing.T) {
tests := map[string]testutils.ConfigEqualTest{
"non-nil config and nil other": {
Config: NewConfig(big.NewInt(3)),
Other: nil,
Expected: false,
},
"different type": {
Config: NewConfig(big.NewInt(3)),
Other: precompileconfig.NewMockConfig(gomock.NewController(t)),
Expected: false,
},
"different timestamp": {
Config: NewConfig(big.NewInt(3)),
Other: NewConfig(big.NewInt(4)),
Expected: false,
},
"same config": {
Config: NewConfig(big.NewInt(3)),
Other: NewConfig(big.NewInt(3)),
Expected: true,
},
// CUSTOM CODE STARTS HERE
// Add your own Equal tests here
}
// Run equal tests.
testutils.RunEqualTests(t, tests)
}
1 change: 1 addition & 0 deletions precompile/contracts/jurorv2/contract.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"address","name":"trader","type":"address"},{"internalType":"bool","name":"includeFundingPayments","type":"bool"},{"internalType":"uint8","name":"mode","type":"uint8"}],"name":"getNotionalPositionAndMargin","outputs":[{"internalType":"uint256","name":"notionalPosition","type":"uint256"},{"internalType":"int256","name":"margin","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"ammIndex","type":"uint256"},{"internalType":"address","name":"trader","type":"address"},{"internalType":"int256","name":"baseAssetQuantity","type":"int256"},{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"uint256","name":"salt","type":"uint256"},{"internalType":"bool","name":"reduceOnly","type":"bool"},{"internalType":"bool","name":"postOnly","type":"bool"}],"internalType":"struct ILimitOrderBook.Order","name":"order","type":"tuple"},{"internalType":"address","name":"sender","type":"address"},{"internalType":"bool","name":"assertLowMargin","type":"bool"}],"name":"validateCancelLimitOrder","outputs":[{"internalType":"string","name":"err","type":"string"},{"internalType":"bytes32","name":"orderHash","type":"bytes32"},{"components":[{"internalType":"int256","name":"unfilledAmount","type":"int256"},{"internalType":"address","name":"amm","type":"address"}],"internalType":"struct IOrderHandler.CancelOrderRes","name":"res","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"data","type":"bytes"},{"internalType":"uint256","name":"liquidationAmount","type":"uint256"}],"name":"validateLiquidationOrderAndDetermineFillPrice","outputs":[{"internalType":"string","name":"err","type":"string"},{"internalType":"enum IJuror.BadElement","name":"element","type":"uint8"},{"components":[{"components":[{"internalType":"uint256","name":"ammIndex","type":"uint256"},{"internalType":"address","name":"trader","type":"address"},{"internalType":"bytes32","name":"orderHash","type":"bytes32"},{"internalType":"enum IClearingHouse.OrderExecutionMode","name":"mode","type":"uint8"}],"internalType":"struct IClearingHouse.Instruction","name":"instruction","type":"tuple"},{"internalType":"uint8","name":"orderType","type":"uint8"},{"internalType":"bytes","name":"encodedOrder","type":"bytes"},{"internalType":"uint256","name":"fillPrice","type":"uint256"},{"internalType":"int256","name":"fillAmount","type":"int256"}],"internalType":"struct IOrderHandler.LiquidationMatchingValidationRes","name":"res","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes[2]","name":"data","type":"bytes[2]"},{"internalType":"int256","name":"fillAmount","type":"int256"}],"name":"validateOrdersAndDetermineFillPrice","outputs":[{"internalType":"string","name":"err","type":"string"},{"internalType":"enum IJuror.BadElement","name":"element","type":"uint8"},{"components":[{"components":[{"internalType":"uint256","name":"ammIndex","type":"uint256"},{"internalType":"address","name":"trader","type":"address"},{"internalType":"bytes32","name":"orderHash","type":"bytes32"},{"internalType":"enum IClearingHouse.OrderExecutionMode","name":"mode","type":"uint8"}],"internalType":"struct IClearingHouse.Instruction[2]","name":"instructions","type":"tuple[2]"},{"internalType":"uint8[2]","name":"orderTypes","type":"uint8[2]"},{"internalType":"bytes[2]","name":"encodedOrders","type":"bytes[2]"},{"internalType":"uint256","name":"fillPrice","type":"uint256"}],"internalType":"struct IOrderHandler.MatchingValidationRes","name":"res","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint8","name":"orderType","type":"uint8"},{"internalType":"uint256","name":"expireAt","type":"uint256"},{"internalType":"uint256","name":"ammIndex","type":"uint256"},{"internalType":"address","name":"trader","type":"address"},{"internalType":"int256","name":"baseAssetQuantity","type":"int256"},{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"uint256","name":"salt","type":"uint256"},{"internalType":"bool","name":"reduceOnly","type":"bool"}],"internalType":"struct IImmediateOrCancelOrders.Order","name":"order","type":"tuple"},{"internalType":"address","name":"sender","type":"address"}],"name":"validatePlaceIOCOrder","outputs":[{"internalType":"string","name":"err","type":"string"},{"internalType":"bytes32","name":"orderHash","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"ammIndex","type":"uint256"},{"internalType":"address","name":"trader","type":"address"},{"internalType":"int256","name":"baseAssetQuantity","type":"int256"},{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"uint256","name":"salt","type":"uint256"},{"internalType":"bool","name":"reduceOnly","type":"bool"},{"internalType":"bool","name":"postOnly","type":"bool"}],"internalType":"struct ILimitOrderBook.Order","name":"order","type":"tuple"},{"internalType":"address","name":"sender","type":"address"}],"name":"validatePlaceLimitOrder","outputs":[{"internalType":"string","name":"err","type":"string"},{"internalType":"bytes32","name":"orderhash","type":"bytes32"},{"components":[{"internalType":"uint256","name":"reserveAmount","type":"uint256"},{"internalType":"address","name":"amm","type":"address"}],"internalType":"struct IOrderHandler.PlaceOrderRes","name":"res","type":"tuple"}],"stateMutability":"view","type":"function"}]
Loading

0 comments on commit 279a5fe

Please sign in to comment.