Commit 145b3643 authored by luxq's avatar luxq

update exchain

parent a2cce9fe
package exchain
import "github.com/holiman/uint256"
type Wallet struct {
Coin string `json:"coin"`
Balance uint256.Int `json:"balance"`
FrozenBalance uint256.Int `json:"frozenBalance"`
}
type ProxyPublickey [64]byte
type AccountInfo struct {
ProxySigner ProxyPublickey
Wallets []Wallet // a sorted wallet list.
}
func (acc *AccountInfo) GetWallet(coin string) *Wallet {
for i := range acc.Wallets {
if acc.Wallets[i].Coin == coin {
return &acc.Wallets[i]
}
}
return nil
}
func (acc *AccountInfo) GetWallets() []Wallet {
return acc.Wallets
}
package exchain
// BackendService define all api that provide the info in server.
type BackendService interface {
// Get all pending orders info.
// Get all history orders info.
// Get all order books info.
}
package exchain
import (
"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
)
type StateDB interface {
GetAccount(address common.Address) *AccountInfo
FrozenBalance(address common.Address, coin common.Address, balance uint256.Int) error
UnFrozenBalance(address common.Address, coin common.Address, balance uint256.Int) error
SubBalance(address common.Address, coin common.Address, balance uint256.Int) error
AddBalance(address common.Address, coin common.Address, balance uint256.Int) error
}
type ChainDB interface {
}
package exchain
import (
"context"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/common"
)
// Engine define all api that op-node needed to build a block and process a block.
type Engine interface {
// NewPayload generate a new execution payload.
NewPayload(ctx context.Context, params PayloadParams) (*ExecutionPayload, error)
// ProcessPayload execute a new execute payload that received from other node.
ProcessPayload(ctx context.Context, payload *ExecutionPayload) error
}
type PayloadParams struct {
ParentHash common.Hash
Timestamp eth.Uint64Quantity
Transactions []eth.Data
}
type ExecutionPayload struct {
ParentHash common.Hash `json:"parentHash"`
BlockHash common.Hash `json:"blockHash"`
BlockNumber eth.Uint64Quantity `json:"blockNumber"`
Timestamp eth.Uint64Quantity `json:"timestamp"`
StateRoot eth.Bytes32 `json:"stateRoot"`
ReceiptsRoot eth.Bytes32 `json:"receiptsRoot"`
ExtraData eth.BytesMax32 `json:"extraData"`
Transactions []eth.Data `json:"transactions"`
}
package exchain
import (
"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
)
// ChainRPC define all rpc that provide the info on chain.
type ChainRPC interface {
// Account Reference
GetAccount(address common.Address) AccountInfo
// Block Reference
GetHeaderByNumber(number *uint256.Int)
GetBlockByNumber(number *uint256.Int)
GetBlockByHash(hash common.Hash)
// Tx Reference
SendRawTransaction(tx []byte) (common.Hash, error)
GetTransactionByHash(hash common.Hash) (Transaction, error)
GetTransactionReceipt(hash common.Hash) (TransactionReceipt, error)
}
package exchain
import (
cryptorand "crypto/rand"
"github.com/holiman/uint256"
"github.com/oklog/ulid"
"time"
)
func GetNonce() *uint256.Int {
id, _ := ulid.New(ulid.Timestamp(time.Now()), cryptorand.Reader)
return new(uint256.Int).SetBytes(id[:])
}
package exchain
import (
"encoding/json"
"github.com/ethereum-optimism/optimism/op-service/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256"
"math/big"
)
type TxType string
const (
SignProxyTx TxType = "sign_proxy" // update a signer proxy public key.
DepositTx TxType = "deposit" // deposit usdt/usdc from l1 to exchain.
WithdrawTx TxType = "withdraw" // withdraw usdt/usdc from exchain to l1.
CreateOrderBookTx TxType = "create_order_book" // create a new orderbook.
DisableOrderBookTx TxType = "disable_order_book" // disable an existing orderbook.
LimitTx TxType = "limit" // make a limit order.
MarketTx TxType = "market" // make a market order.
CancelTx TxType = "cancel" // cancel an order.
)
type Signature struct {
R *uint256.Int
S *uint256.Int
V byte
}
type Transaction struct {
User common.Address `json:"user"`
Nonce uint256.Int `json:"nonce"`
Type TxType `json:"type"`
Proxy bool `json:"proxy"`
Data json.RawMessage `json:"data"`
Signature Signature `json:"signature"`
}
type TransactionReceipt struct {
Hash common.Hash `json:"hash"`
TxType TxType `json:"txType"`
Status uint8 `json:"status"`
BlockHash common.Hash `json:"blockHash"`
Content []eth.Data `json:"content"` // content is difference by txType.
}
type DepositTxData struct {
// SourceHash uniquely identifies the source of the deposit
SourceHash common.Hash
// From is exposed through the types.Signer, not through TxData
From common.Address
// nil means contract creation
To *common.Address `rlp:"nil"`
// Mint is minted on L2, locked on L1, nil if no minting.
Mint *big.Int `rlp:"nil"`
// Value is transferred from L2 balance, executed after Mint (if any)
Value *big.Int
// gas limit
Gas uint64
// Field indicating if this transaction is exempt from the L2 gas limit.
IsSystemTransaction bool
// Normal Tx data
Data []byte
}
type LimitTxData struct {
Symbol string `json:"symbol"`
Side uint8 `json:"side"`
Price uint256.Int `json:"price"`
Quantity uint256.Int `json:"quantity"`
}
type MarketTxData struct {
Symbol string `json:"symbol"`
Side uint8 `json:"side"`
Quantity uint256.Int `json:"quantity"`
}
func (tx Transaction) SigHash() common.Hash {
data := make([]byte, 0)
data = append(data, tx.User.Bytes()...)
data = append(data, tx.Nonce.Bytes()...)
data = append(data, tx.Type...)
if tx.Proxy {
data = append(data, byte(1))
} else {
data = append(data, byte(0))
}
txdata, _ := json.Marshal(tx.Data)
data = append(data, txdata...)
return crypto.Keccak256Hash(data)
}
func (tx Transaction) Verify() bool {
return true
//if tx.Proxy {
// // get user proxy public.
// var public ProxyPublickey
// pubkey, err := crypto.UnmarshalPubkey(public[:])
// if err != nil {
// return false
// } else {
// crypto.VerifySignature()
// }
//
//}
}
package exchain
import (
"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
)
type BlockHeader struct {
Height uint256.Int
Hash common.Hash
Parent common.Hash
Timestamp uint64
Proposer common.Address
}
type BlockBody struct {
}
......@@ -179,6 +179,7 @@ require (
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nwaples/rardecode v1.1.3 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/onsi/ginkgo/v2 v2.20.0 // indirect
github.com/opencontainers/runtime-spec v1.2.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
......
......@@ -594,6 +594,8 @@ github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY=
github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc=
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment