diff --git a/client/chain/chain.go b/client/chain/chain.go index fff7581e..a7815c23 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -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 @@ -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 @@ -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 { diff --git a/client/chain/orderhash.go b/client/chain/orderhash.go index 8f323c7d..0a260f7b 100644 --- a/client/chain/orderhash.go +++ b/client/chain/orderhash.go @@ -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() @@ -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() @@ -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 }