Skip to content

Commit

Permalink
Merge pull request #32 from gateway-fm/feat/SettlePythOrder
Browse files Browse the repository at this point in the history
not working SettlePythOrder - breaks down at the end
  • Loading branch information
Pashteto authored Nov 16, 2023
2 parents 7dc957c + dbfa016 commit abe23f8
Show file tree
Hide file tree
Showing 2 changed files with 256 additions and 4 deletions.
14 changes: 10 additions & 4 deletions utils/testing-contracts/perps-test/perps-market.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/big"
"os"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -191,7 +192,7 @@ func (m *TestPerpsMarket) CommitOrder(marketIDS string, sizeS string) {
MarketId: marketID,
AccountId: m.TestAccount,
SizeDelta: size,
SettlementStrategyId: big.NewInt(0),
SettlementStrategyId: big.NewInt(2),
AcceptablePrice: summary.IndexPrice.Add(summary.IndexPrice, big.NewInt(10000)),
TrackingCode: [32]byte{},
Referrer: common.HexToAddress("0x0000000000000000000000000000000000000000"),
Expand All @@ -202,20 +203,25 @@ func (m *TestPerpsMarket) CommitOrder(marketIDS string, sizeS string) {
"- MarketID: %v\n"+
"- AccountID: %v\n"+
"- SizeDelta: %v\n"+
"- SettlementStrategyId: 0\n"+
"- SettlementStrategyId: %s\n"+
"- AcceptablePrice: %v\n"+
"- TrackingCode: 0\n"+
"- Referrer: 0x0000000000000000000000000000000000000000",
marketID.String(), m.TestAccount, size.String(), summary.IndexPrice.Add(summary.IndexPrice, big.NewInt(10000)),
marketID.String(), m.TestAccount, size.String(), req.SettlementStrategyId.String(), summary.IndexPrice.Add(summary.IndexPrice, big.NewInt(10000)),
)

tx, err := m.perpsMarket.CommitOrder(aut, req)
if err != nil {

logger.Log().WithField("layer", "TestPerpsMarket-CommitOrder").Fatalf("commit order err: %v", err.Error())
}

logger.Log().WithField("layer", "TestPerpsMarket-CommitOrder").Infof("order commited, tx hash: %v", tx.Hash())

if err = m.SettlePythOrder(m.TestAccount, 10, 1*time.Second); err != nil {
logger.Log().WithField("layer", "SettlePythOrder").Fatalf(
"error get latest block: %v", err.Error(),
)
}
}

func (m *TestPerpsMarket) LiquidateAccount(accountIDs string) {
Expand Down
246 changes: 246 additions & 0 deletions utils/testing-contracts/perps-test/settle_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
package perps_test

import (
"context"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"math/big"
"net/http"
"strings"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"

"github.com/gateway-fm/perpsv3-Go/pkg/logger"
)

// SettlePythOrder settles an order by fetching data from the Pyth price feed and submitting a transaction
func (m *TestPerpsMarket) SettlePythOrder(accountID *big.Int, maxPythTries int, pythDelay time.Duration) error {
if accountID == nil {
accountID = m.TestAccount
}

latest, err := m.rpcClient.BlockNumber(context.Background())
if err != nil {
logger.Log().WithField("layer", "BlockNumber").Fatalf(
"error get latest block: %v", err.Error(),
)
}

opts := &bind.CallOpts{BlockNumber: big.NewInt(int64(latest))}

ordData, err := m.perpsMarket.GetOrder(opts, m.TestAccount)
if err != nil {
logger.Log().WithField("layer", "TestPerpsMarket-SettlePythOrder").Fatalf("GetOrder err: %v", err)
}

settlementTime := time.Unix(ordData.SettlementTime.Int64(), 2)
if time.Now().Before(settlementTime) {
duration := time.Until(settlementTime)
logger.Log().WithField("layer", "TestPerpsMarket-SettlePythOrder").Infof("Waiting %v until order can be settled??? Not in this attempt!", duration)
//time.Sleep(duration)
} else {
logger.Log().WithField("layer", "TestPerpsMarket-SettlePythOrder").Infof("Order is ready to be settled")
}

settlementStrategy := GetSettlementStrategy(int(ordData.Request.MarketId.Int64()), 2)

settlementTimeBytes := make([]byte, 8)
binary.BigEndian.PutUint64(settlementTimeBytes, ordData.SettlementTime.Uint64())
settlementTimeHex := hex.EncodeToString(settlementTimeBytes)
dataParam := fmt.Sprintf("0x%s%s", hex.EncodeToString(settlementStrategy.FeedID), settlementTimeHex)

url := fmt.Sprintf("%s%s", settlementStrategy.URL, dataParam)
logger.Log().WithField("layer", "TestPerpsMarket-SettlePythOrder").Infof("url=%s", url)

var priceUpdateData PriceUpdateData
for pythTries := 0; pythTries < maxPythTries; pythTries++ {
resp, err := http.Get(url)
if err != nil || resp.StatusCode != http.StatusOK {
if pythTries >= maxPythTries {
return fmt.Errorf("price update data not available")
}
log.Println("Price update data not available, waiting and retrying")
time.Sleep(pythDelay)
continue
}

defer resp.Body.Close()

if err = json.NewDecoder(resp.Body).Decode(&priceUpdateData); err != nil {
body, err := io.ReadAll(resp.Body)
if err != nil {
logger.Log().WithField("layer", "TestPerpsMarket-SettlePythOrder").Fatalf("Error reading response body: %v", err)
}
bodyString := string(body)

logger.Log().WithField("layer", "TestPerpsMarket-SettlePythOrder").Infof("res=\n\n%s", bodyString)

return fmt.Errorf("failed to decode price update data:%s, %v", bodyString, err)
}

logger.Log().WithField("layer", "TestPerpsMarket-SettlePythOrder").Infof("priceUpdateData:%+v", priceUpdateData.Data)

break
}

accountBytes := make([]byte, 32)
binary.BigEndian.PutUint32(accountBytes, uint32(accountID.Uint64()))
marketBytes := make([]byte, 32)
binary.BigEndian.PutUint32(marketBytes, uint32(ordData.Request.MarketId.Uint64()))

extraData := append(accountBytes, marketBytes...)

log.Printf("extra_data to string: %s", hex.EncodeToString(extraData))

data, err := hex.DecodeString(strings.TrimPrefix(priceUpdateData.Data, "0x"))
if err != nil {
logger.Log().WithField("layer", "TestPerpsMarket-SettlePythOrder").Fatalf("Failed to decode hex string priceUpdateData.Data: %v", err)
}
aut := m.getAut("SettlePythOrder")

tx, err := m.perpsMarket.SettlePythOrder(aut, data, extraData)
if err != nil {
logger.Log().WithField("layer", "TestPerpsMarket-SettlePythOrder").Fatalf("SettlePythOrder: %+v", err)
}
logger.Log().WithField("layer", "TestPerpsMarket-SettlePythOrder").Infof("tx=%+v", tx)

return nil
}

type PriceUpdateData struct {
Data string `json:"data"`
}

type SettlementStrategy struct {
StrategyType int `json:"strategy_type"`
SettlementDelay int `json:"settlement_delay"`
SettlementWindowDuration int `json:"settlement_window_duration"`
PriceWindowDuration int `json:"price_window_duration"`
PriceVerificationContract string `json:"price_verification_contract"`
FeedID []byte `json:"feed_id"`
FeedIDx string `json:"feed_id_x"`
URL string `json:"url"`
SettlementReward float64 `json:"settlement_reward"`
Disabled bool `json:"disabled"`
}

var market100Strategy0 = SettlementStrategy{
StrategyType: 0,
SettlementDelay: 15,
SettlementWindowDuration: 600,
PriceWindowDuration: 180,
PriceVerificationContract: "0xff1a0f4744e8582df1ae09d5611b887b6a12925c",
FeedIDx: "ca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6",
URL: "https://api.synthetix.io/pyth-testnet/api/get_vaa_ccip?data=",
SettlementReward: 0.0,
Disabled: true,
}
var market200Strategy0 = SettlementStrategy{
StrategyType: 0,
SettlementDelay: 15,
SettlementWindowDuration: 60,
PriceWindowDuration: 180,
PriceVerificationContract: "0xff1a0f4744e8582df1ae09d5611b887b6a12925c",
FeedIDx: "f9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b",
URL: "https://api.synthetix.io/pyth-testnet/api/get_vaa_ccip?data=",
SettlementReward: 0.0,
Disabled: true,
}
var market100Strategy2 = SettlementStrategy{
StrategyType: 0,
SettlementDelay: 15,
SettlementWindowDuration: 60,
PriceWindowDuration: 600,
PriceVerificationContract: "0xff1a0f4744e8582df1ae09d5611b887b6a12925c",
FeedIDx: "ca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6",
URL: "https://api.synthetix.io/pyth-testnet/api/get_vaa_ccip?data=",
SettlementReward: 0.0,
Disabled: false,
}
var market200Strategy2 = SettlementStrategy{
StrategyType: 0,
SettlementDelay: 15,
SettlementWindowDuration: 60,
PriceWindowDuration: 600,
PriceVerificationContract: "0xff1a0f4744e8582df1ae09d5611b887b6a12925c",
FeedIDx: "f9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b",
URL: "https://api.synthetix.io/pyth-testnet/api/get_vaa_ccip?data=",
SettlementReward: 0.0,
Disabled: false,
}

func GetSettlementStrategy(market, strategy int) *SettlementStrategy {
var res *SettlementStrategy
switch {
case market == 100 && strategy == 0:
res = &market100Strategy0
case market == 200 && strategy == 0:
res = &market200Strategy0
case market == 100 && strategy == 2:
res = &market100Strategy2
case market == 200 && strategy == 2:
res = &market200Strategy2
default:
// Return nil or a default strategy if no match is found
return nil
}
var err error
res.FeedID, err = hex.DecodeString(res.FeedIDx)
if err != nil {
log.Fatalf("Failed to decode feed ID %s: %v", res.FeedIDx, err)
}

return res
}

/*
market 100, strategy 0
{'strategy_type': 0,
'settlement_delay': 15,
'settlement_window_duration': 600,
'price_window_duration': 180,
'price_verification_contract': '0xff1a0f4744e8582df1ae09d5611b887b6a12925c',
'feed_id': b'\xca\x80\xbam\xc3.\x08\xd0o\x1a\xa8\x86\x01\x1e\xed\x1dw\xc7{\xe9\xebv\x1c\xc1\rr\xb7\xd0\xa2\xfdW\xa6',
'url': 'https://api.synthetix.io/pyth-testnet/api/get_vaa_ccip?data=',
'settlement_reward': 0.0,
'disabled': True}
market 200, strategy 0
{'strategy_type': 0,
'settlement_delay': 15,
'settlement_window_duration': 60,
'price_window_duration': 180,
'price_verification_contract': '0xff1a0f4744e8582df1ae09d5611b887b6a12925c',
'feed_id': b'\xf9\xc0\x17+\xa1\r\xfaM\x19\x08\x8d\x94\xf5\xbfa\xd3\xb5M[\xd7H:2*\x98.\x13s\xee\x8e\xa3\x1b',
'url': 'https://api.synthetix.io/pyth-testnet/api/get_vaa_ccip?data=',
'settlement_reward': 0.0,
'disabled': True}
*/
/*
market 100, strategy 2
{'strategy_type': 0,
'settlement_delay': 15,
'settlement_window_duration': 60,
'price_window_duration': 600,
'price_verification_contract': '0xff1a0f4744e8582df1ae09d5611b887b6a12925c',
'feed_id': b'\xca\x80\xbam\xc3.\x08\xd0o\x1a\xa8\x86\x01\x1e\xed\x1dw\xc7{\xe9\xebv\x1c\xc1\rr\xb7\xd0\xa2\xfdW\xa6',
'url': 'https://api.synthetix.io/pyth-testnet/api/get_vaa_ccip?data=',
'settlement_reward': 0.0,
'disabled': False}
market 200, strategy 2
{'strategy_type': 0,
'settlement_delay': 15,
'settlement_window_duration': 60,
'price_window_duration': 600,
'price_verification_contract': '0xff1a0f4744e8582df1ae09d5611b887b6a12925c',
'feed_id': b'\xf9\xc0\x17+\xa1\r\xfaM\x19\x08\x8d\x94\xf5\xbfa\xd3\xb5M[\xd7H:2*\x98.\x13s\xee\x8e\xa3\x1b',
'url': 'https://api.synthetix.io/pyth-testnet/api/get_vaa_ccip?data=',
'settlement_reward': 0.0,
'disabled': False}
*/

0 comments on commit abe23f8

Please sign in to comment.