From 2b57fd9aab9000ed999d65a3a9e4ae1853edf0e4 Mon Sep 17 00:00:00 2001 From: atvanguard <3612498+atvanguard@users.noreply.github.com> Date: Tue, 27 Feb 2024 14:44:53 +0000 Subject: [PATCH] set signedOrderBook addy from precompile call (#161) * set signedOrderBook addy from precompile call * hardcode values for aylin --- genesis.json | 3 + .../orderbook/hubbleutils/signed_orders.go | 59 ------------------- precompile/contracts/bibliophile/client.go | 6 ++ .../contracts/bibliophile/client_mock.go | 15 +++++ .../contracts/jurorv2/matching_validation.go | 14 ++++- 5 files changed, 37 insertions(+), 60 deletions(-) diff --git a/genesis.json b/genesis.json index c7e854712a..35e1cb3dfa 100644 --- a/genesis.json +++ b/genesis.json @@ -30,6 +30,9 @@ "blockTimestamp": 0, "adminAddresses": ["0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC","0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"] }, + "jurorConfig": { + "blockTimestamp": 0 + }, "jurorV2Config": { "blockTimestamp": 0 }, diff --git a/plugin/evm/orderbook/hubbleutils/signed_orders.go b/plugin/evm/orderbook/hubbleutils/signed_orders.go index b93ff51708..7f73acc631 100644 --- a/plugin/evm/orderbook/hubbleutils/signed_orders.go +++ b/plugin/evm/orderbook/hubbleutils/signed_orders.go @@ -116,62 +116,3 @@ func (o *SignedOrder) Hash() (hash common.Hash, err error) { } return EncodeForSigning(typedData) } - -// Trading API methods - -// func (o *SignedOrder) UnmarshalJSON(data []byte) error { -// // Redefine the structs with simple types for JSON unmarshalling -// aux := &struct { -// AmmIndex uint64 `json:"ammIndex"` -// Trader common.Address `json:"trader"` -// BaseAssetQuantity string `json:"baseAssetQuantity"` -// Price string `json:"price"` -// Salt string `json:"salt"` -// ReduceOnly bool `json:"reduceOnly"` -// PostOnly bool `json:"postOnly"` -// OrderType uint8 `json:"orderType"` -// ExpireAt uint64 `json:"expireAt"` -// Sig string `json:"sig"` -// }{} - -// // Perform the unmarshalling -// if err := json.Unmarshal(data, aux); err != nil { -// return err -// } - -// // Convert and assign the values to the original struct -// o.AmmIndex = new(big.Int).SetUint64(aux.AmmIndex) - -// o.Trader = aux.Trader - -// o.BaseAssetQuantity = new(big.Int) -// o.BaseAssetQuantity.SetString(aux.BaseAssetQuantity, 10) - -// o.Price = new(big.Int) -// o.Price.SetString(aux.Price, 10) - -// o.Salt = new(big.Int) -// o.Salt.SetBytes(common.FromHex(aux.Salt)) - -// o.ReduceOnly = aux.ReduceOnly -// o.PostOnly = aux.PostOnly -// o.OrderType = aux.OrderType - -// o.ExpireAt = new(big.Int).SetUint64(aux.ExpireAt) -// o.Sig = common.FromHex(aux.Sig) -// return nil -// } - -// func (order *SignedOrder) DecodeAPIOrder(rawOrder interface{}) error { -// order_, ok := rawOrder.(string) -// if !ok { -// fmt.Println("invalid data format") -// } - -// orderJson := []byte(order_) -// err := json.Unmarshal(orderJson, &order) -// if err != nil { -// return err -// } -// return nil -// } diff --git a/precompile/contracts/bibliophile/client.go b/precompile/contracts/bibliophile/client.go index 9bdc64a7a1..c03a219964 100644 --- a/precompile/contracts/bibliophile/client.go +++ b/precompile/contracts/bibliophile/client.go @@ -56,6 +56,8 @@ type BibliophileClient interface { GetNotionalPositionAndMargin(trader common.Address, includeFundingPayments bool, mode uint8, upgradeVersion hu.UpgradeVersion) (*big.Int, *big.Int) HasReferrer(trader common.Address) bool GetActiveMarketsCount() int64 + + GetAccessibleState() contract.AccessibleState } // Define a structure that will implement the Bibliophile interface @@ -69,6 +71,10 @@ func NewBibliophileClient(accessibleState contract.AccessibleState) BibliophileC } } +func (b *bibliophileClient) GetAccessibleState() contract.AccessibleState { + return b.accessibleState +} + func (b *bibliophileClient) GetSignedOrderFilledAmount(orderHash [32]byte) *big.Int { return GetSignedOrderFilledAmount(b.accessibleState.GetStateDB(), orderHash) } diff --git a/precompile/contracts/bibliophile/client_mock.go b/precompile/contracts/bibliophile/client_mock.go index bbdf5b70c8..c48907e920 100644 --- a/precompile/contracts/bibliophile/client_mock.go +++ b/precompile/contracts/bibliophile/client_mock.go @@ -9,6 +9,7 @@ import ( reflect "reflect" hubbleutils "github.com/ava-labs/subnet-evm/plugin/evm/orderbook/hubbleutils" + contract "github.com/ava-labs/subnet-evm/precompile/contract" common "github.com/ethereum/go-ethereum/common" gomock "github.com/golang/mock/gomock" ) @@ -51,6 +52,20 @@ func (mr *MockBibliophileClientMockRecorder) GetAcceptableBoundsForLiquidation(m return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAcceptableBoundsForLiquidation", reflect.TypeOf((*MockBibliophileClient)(nil).GetAcceptableBoundsForLiquidation), marketId) } +// GetAccessibleState mocks base method. +func (m *MockBibliophileClient) GetAccessibleState() contract.AccessibleState { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetAccessibleState") + ret0, _ := ret[0].(contract.AccessibleState) + return ret0 +} + +// GetAccessibleState indicates an expected call of GetAccessibleState. +func (mr *MockBibliophileClientMockRecorder) GetAccessibleState() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAccessibleState", reflect.TypeOf((*MockBibliophileClient)(nil).GetAccessibleState)) +} + // GetActiveMarketsCount mocks base method. func (m *MockBibliophileClient) GetActiveMarketsCount() int64 { m.ctrl.T.Helper() diff --git a/precompile/contracts/jurorv2/matching_validation.go b/precompile/contracts/jurorv2/matching_validation.go index b959b6e905..63154ed8c0 100644 --- a/precompile/contracts/jurorv2/matching_validation.go +++ b/precompile/contracts/jurorv2/matching_validation.go @@ -3,6 +3,7 @@ package jurorv2 import ( "errors" "math/big" + "strings" ob "github.com/ava-labs/subnet-evm/plugin/evm/orderbook" hu "github.com/ava-labs/subnet-evm/plugin/evm/orderbook/hubbleutils" @@ -379,11 +380,22 @@ func validateExecuteIOCOrder(bibliophile b.BibliophileClient, order *ob.IOCOrder } func validateExecuteSignedOrder(bibliophile b.BibliophileClient, order *hu.SignedOrder, side Side, fillAmount *big.Int) (metadata *Metadata, err error) { + // these fields are only set in plugin/evm/limit_order.go.NewLimitOrderProcesser + // however, the above is not invoked until the node bootstraps completely, and hence causes the signed order match validations during bootstrap to fail + // here we hardcode the values for mainnet and aylin testnet + if hu.VerifyingContract == "" || hu.ChainId == 0 { + chainId := bibliophile.GetAccessibleState().GetSnowContext().ChainID + if strings.EqualFold(chainId.String(), "2jfjkB7NkK4v8zoaoWmh5eaABNW6ynjQvemPFZpgPQ7ugrmUXv") { // mainnet + hu.SetChainIdAndVerifyingSignedOrdersContract(1992, "0x211682829664a5e289885DE21897B094eF289d18") + } else if strings.EqualFold(chainId.String(), "2qR64ZGVHTJjTZTzEnQTDoD1oMVQMYFVaBtN5tDoYaDKfVY5Xz") { // aylin + hu.SetChainIdAndVerifyingSignedOrdersContract(486, "0xb589490250fAEaF7D80D0b5A41db5059d55A85Df") + } + } + orderHash, err := order.Hash() if err != nil { return &Metadata{OrderHash: common.Hash{}}, err } - trader, signer, err := hu.ValidateSignedOrder( order, hu.SignedOrderValidationFields{