Skip to content

Commit

Permalink
return nonce (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady authored Sep 10, 2021
1 parent 6e4de5e commit 9b8396c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
15 changes: 15 additions & 0 deletions service/service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ func (s AccountService) AccountBalance(
return nil, wrapError(errInternalError, balanceErr)
}

nonce, nonceErr := s.client.NonceAt(ctx, address, header.Number)
if nonceErr != nil {
return nil, wrapError(errClientError, nonceErr)
}

metadata := &accountMetadata{
Nonce: nonce,
}

metadataMap, metadataErr := marshalJSONMap(metadata)
if err != nil {
return nil, wrapError(errInternalError, metadataErr)
}

resp := &types.AccountBalanceResponse{
BlockIdentifier: &types.BlockIdentifier{
Index: header.Number.Int64(),
Expand All @@ -58,6 +72,7 @@ func (s AccountService) AccountBalance(
Balances: []*types.Amount{
mapper.AvaxAmount(balance),
},
Metadata: metadataMap,
}

return resp, nil
Expand Down
31 changes: 31 additions & 0 deletions service/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,34 @@ func (t *transaction) UnmarshalJSON(data []byte) error {
t.GasPrice = gasPrice
return nil
}

type accountMetadata struct {
Nonce uint64 `json:"nonce"`
}

type accountMetadataWire struct {
Nonce string `json:"nonce"`
}

func (m *accountMetadata) MarshalJSON() ([]byte, error) {
mw := &accountMetadataWire{
Nonce: hexutil.Uint64(m.Nonce).String(),
}

return json.Marshal(mw)
}

func (m *accountMetadata) UnmarshalJSON(data []byte) error {
var mw accountMetadataWire
if err := json.Unmarshal(data, &mw); err != nil {
return err
}

nonce, err := hexutil.DecodeUint64(mw.Nonce)
if err != nil {
return err
}

m.Nonce = nonce
return nil
}

0 comments on commit 9b8396c

Please sign in to comment.