forked from ava-labs/subnet-evm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
testing_apis.go
80 lines (66 loc) · 3.21 KB
/
testing_apis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package orderbook
import (
"bytes"
"context"
"encoding/gob"
"fmt"
"math/big"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/subnet-evm/eth"
"github.com/ava-labs/subnet-evm/precompile/contracts/bibliophile"
"github.com/ava-labs/subnet-evm/rpc"
"github.com/ethereum/go-ethereum/common"
)
type TestingAPI struct {
db LimitOrderDatabase
backend *eth.EthAPIBackend
configService IConfigService
hubbleDB database.Database
}
func NewTestingAPI(database LimitOrderDatabase, backend *eth.EthAPIBackend, configService IConfigService, hubbleDB database.Database) *TestingAPI {
return &TestingAPI{
db: database,
backend: backend,
configService: configService,
hubbleDB: hubbleDB,
}
}
func (api *TestingAPI) GetClearingHouseVars(ctx context.Context, trader common.Address) bibliophile.VariablesReadFromClearingHouseSlots {
stateDB, _, _ := api.backend.StateAndHeaderByNumber(ctx, rpc.BlockNumber(getCurrentBlockNumber(api.backend)))
return bibliophile.GetClearingHouseVariables(stateDB, trader)
}
func (api *TestingAPI) GetMarginAccountVars(ctx context.Context, collateralIdx *big.Int, traderAddress string) bibliophile.VariablesReadFromMarginAccountSlots {
stateDB, _, _ := api.backend.StateAndHeaderByNumber(ctx, rpc.BlockNumber(getCurrentBlockNumber(api.backend)))
return bibliophile.GetMarginAccountVariables(stateDB, collateralIdx, common.HexToAddress(traderAddress))
}
func (api *TestingAPI) GetAMMVars(ctx context.Context, ammAddress string, ammIndex int, traderAddress string) bibliophile.VariablesReadFromAMMSlots {
stateDB, _, _ := api.backend.StateAndHeaderByNumber(ctx, rpc.BlockNumber(getCurrentBlockNumber(api.backend)))
return bibliophile.GetAMMVariables(stateDB, common.HexToAddress(ammAddress), int64(ammIndex), common.HexToAddress(traderAddress))
}
func (api *TestingAPI) GetIOCOrdersVars(ctx context.Context, orderHash common.Hash) bibliophile.VariablesReadFromIOCOrdersSlots {
stateDB, _, _ := api.backend.StateAndHeaderByNumber(ctx, rpc.BlockNumber(getCurrentBlockNumber(api.backend)))
return bibliophile.GetIOCOrdersVariables(stateDB, orderHash)
}
func (api *TestingAPI) GetOrderBookVars(ctx context.Context, traderAddress string, senderAddress string, orderHash common.Hash) bibliophile.VariablesReadFromOrderbookSlots {
stateDB, _, _ := api.backend.StateAndHeaderByNumber(ctx, rpc.BlockNumber(getCurrentBlockNumber(api.backend)))
return bibliophile.GetOrderBookVariables(stateDB, traderAddress, senderAddress, orderHash)
}
func (api *TestingAPI) GetSnapshot(ctx context.Context) (Snapshot, error) {
var snapshot Snapshot
memoryDBSnapshotKey := "memoryDBSnapshot"
memorySnapshotBytes, err := api.hubbleDB.Get([]byte(memoryDBSnapshotKey))
if err != nil {
return snapshot, fmt.Errorf("Error in fetching snapshot from hubbleDB; err=%v", err)
}
buf := bytes.NewBuffer(memorySnapshotBytes)
err = gob.NewDecoder(buf).Decode(&snapshot)
if err != nil {
return snapshot, fmt.Errorf("Error in snapshot parsing; err=%v", err)
}
return snapshot, nil
}
func getCurrentBlockNumber(backend *eth.EthAPIBackend) uint64 {
return backend.LastAcceptedBlock().NumberU64()
}