Skip to content

Commit

Permalink
feat: support local order hash (#119)
Browse files Browse the repository at this point in the history
* feat: improve local order hash

* chore: expose updateSubaccountNonce

* fix: return error
  • Loading branch information
vinhphuctadang authored Apr 18, 2023
1 parent 8ab298f commit 7bf5a6e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ type ChainClient interface {

GetSubAccountNonce(ctx context.Context, subaccountId eth.Hash) (*exchangetypes.QuerySubaccountTradeNonceResponse, error)
GetFeeDiscountInfo(ctx context.Context, account string) (*exchangetypes.QueryFeeDiscountAccountInfoResponse, error)

UpdateSubaccountNonceFromChain() error
ComputeOrderHashes(spotOrders []exchangetypes.SpotOrder, derivativeOrders []exchangetypes.DerivativeOrder) (OrderHashes, error)

SpotOrder(defaultSubaccountID eth.Hash, network common.Network, d *SpotOrderData) *exchangetypes.SpotOrder
Expand Down Expand Up @@ -155,6 +157,7 @@ type chainClient struct {
bankQueryClient banktypes.QueryClient
authzQueryClient authztypes.QueryClient
wasmQueryClient wasmtypes.QueryClient
subaccountToNonce map[ethcommon.Hash]uint32

closed int64
canSign bool
Expand Down Expand Up @@ -230,6 +233,7 @@ func NewChainClient(
bankQueryClient: banktypes.NewQueryClient(conn),
authzQueryClient: authztypes.NewQueryClient(conn),
wasmQueryClient: wasmtypes.NewQueryClient(conn),
subaccountToNonce: make(map[ethcommon.Hash]uint32),
}

if cc.canSign {
Expand Down
27 changes: 20 additions & 7 deletions client/chain/orderhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,34 @@ var domain = gethsigner.TypedDataDomain{
Salt: "0x0000000000000000000000000000000000000000000000000000000000000000",
}

func (c *chainClient) UpdateSubaccountNonceFromChain() error {
subaccountId := c.DefaultSubaccount(c.ctx.FromAddress)
res, err := c.GetSubAccountNonce(context.Background(), subaccountId)
if err != nil {
return err
}

c.subaccountToNonce[subaccountId] = res.Nonce
return nil
}

func (c *chainClient) ComputeOrderHashes(spotOrders []exchangetypes.SpotOrder, derivativeOrders []exchangetypes.DerivativeOrder) (OrderHashes, error) {
if len(spotOrders)+len(derivativeOrders) == 0 {
return OrderHashes{}, nil
}

orderHashes := OrderHashes{}

// get nonce
subaccountId := c.DefaultSubaccount(c.ctx.FromAddress)
res, err := c.GetSubAccountNonce(context.Background(), subaccountId)
if err != nil {
return OrderHashes{}, err
if _, exist := c.subaccountToNonce[subaccountId]; !exist {
if err := c.UpdateSubaccountNonceFromChain(); err != nil {
return OrderHashes{}, err
}
}
nonce := res.Nonce + 1

nonce := c.subaccountToNonce[subaccountId]
for _, o := range spotOrders {
nonce += 1
triggerPrice := ""
if o.TriggerPrice != nil {
triggerPrice = o.TriggerPrice.String()
Expand Down Expand Up @@ -111,10 +123,10 @@ func (c *chainClient) ComputeOrderHashes(spotOrders []exchangetypes.SpotOrder, d

hash := common.BytesToHash(w.Sum(nil))
orderHashes.Spot = append(orderHashes.Spot, hash)
nonce += 1
}

for _, o := range derivativeOrders {
nonce += 1
triggerPrice := ""
if o.TriggerPrice != nil {
triggerPrice = o.TriggerPrice.String()
Expand Down Expand Up @@ -154,8 +166,9 @@ func (c *chainClient) ComputeOrderHashes(spotOrders []exchangetypes.SpotOrder, d

hash := common.BytesToHash(w.Sum(nil))
orderHashes.Derivative = append(orderHashes.Derivative, hash)
nonce += 1
}

c.subaccountToNonce[subaccountId] = nonce

return orderHashes, nil
}

0 comments on commit 7bf5a6e

Please sign in to comment.