Skip to content

Commit

Permalink
[view] Allow querying APT balance and FA properties by version
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario committed Nov 22, 2024
1 parent 5c28851 commit db6654a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ adheres to the format set out by [Keep a Changelog](https://keepachangelog.com/e

# Unreleased

- Add support for optional ledger version in FA functions and APT balance

# v1.2.0 (11/15/2024)

- [`Fix`][`Breaking`] Fix MultiKey implementation to be more consistent with the rest of the SDKs
Expand Down
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ type AptosRpcClient interface {
EstimateGasPrice() (info EstimateGasInfo, err error)

// AccountAPTBalance retrieves the APT balance in the account
AccountAPTBalance(address AccountAddress) (uint64, error)
AccountAPTBalance(address AccountAddress, ledgerVersion ...uint64) (uint64, error)

// NodeAPIHealthCheck checks if the node is within durationSecs of the current time, if not provided the node default is used
NodeAPIHealthCheck(durationSecs ...uint64) (api.HealthCheckResponse, error)
Expand Down Expand Up @@ -825,8 +825,8 @@ func (client *Client) EstimateGasPrice() (info EstimateGasInfo, err error) {
}

// AccountAPTBalance retrieves the APT balance in the account
func (client *Client) AccountAPTBalance(address AccountAddress) (uint64, error) {
return client.nodeClient.AccountAPTBalance(address)
func (client *Client) AccountAPTBalance(address AccountAddress, ledgerVersion ...uint64) (uint64, error) {
return client.nodeClient.AccountAPTBalance(address, ledgerVersion...)
}

// QueryIndexer queries the indexer using GraphQL to fill the `query` struct with data. See examples in the indexer client on how to make queries
Expand Down
56 changes: 28 additions & 28 deletions fungible_asset_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func (client *FungibleAssetClient) PrimaryStoreExists(owner *AccountAddress) (ex
}

// PrimaryBalance returns the balance of the primary store for the owner
func (client *FungibleAssetClient) PrimaryBalance(owner *AccountAddress) (balance uint64, err error) {
val, err := client.viewPrimaryStoreMetadata([][]byte{owner[:], client.metadataAddress[:]}, "balance")
func (client *FungibleAssetClient) PrimaryBalance(owner *AccountAddress, ledgerVersion ...uint64) (balance uint64, err error) {
val, err := client.viewPrimaryStoreMetadata([][]byte{owner[:], client.metadataAddress[:]}, "balance", ledgerVersion...)
if err != nil {
return
}
Expand All @@ -100,8 +100,8 @@ func (client *FungibleAssetClient) PrimaryBalance(owner *AccountAddress) (balanc
}

// PrimaryIsFrozen returns true if the primary store for the owner is frozen
func (client *FungibleAssetClient) PrimaryIsFrozen(owner *AccountAddress) (isFrozen bool, err error) {
val, err := client.viewPrimaryStore([][]byte{owner[:], client.metadataAddress[:]}, "is_frozen")
func (client *FungibleAssetClient) PrimaryIsFrozen(owner *AccountAddress, ledgerVersion ...uint64) (isFrozen bool, err error) {
val, err := client.viewPrimaryStore([][]byte{owner[:], client.metadataAddress[:]}, "is_frozen", ledgerVersion...)
if err != nil {
return
}
Expand All @@ -110,8 +110,8 @@ func (client *FungibleAssetClient) PrimaryIsFrozen(owner *AccountAddress) (isFro
}

// Balance returns the balance of the store
func (client *FungibleAssetClient) Balance(storeAddress *AccountAddress) (balance uint64, err error) {
val, err := client.viewStore([][]byte{storeAddress[:]}, "balance")
func (client *FungibleAssetClient) Balance(storeAddress *AccountAddress, ledgerVersion ...uint64) (balance uint64, err error) {
val, err := client.viewStore([][]byte{storeAddress[:]}, "balance", ledgerVersion...)
if err != nil {
return
}
Expand All @@ -120,8 +120,8 @@ func (client *FungibleAssetClient) Balance(storeAddress *AccountAddress) (balanc
}

// IsFrozen returns true if the store is frozen
func (client *FungibleAssetClient) IsFrozen(storeAddress *AccountAddress) (isFrozen bool, err error) {
val, err := client.viewStore([][]byte{storeAddress[:]}, "is_frozen")
func (client *FungibleAssetClient) IsFrozen(storeAddress *AccountAddress, ledgerVersion ...uint64) (isFrozen bool, err error) {
val, err := client.viewStore([][]byte{storeAddress[:]}, "is_frozen", ledgerVersion...)
if err != nil {
return
}
Expand All @@ -130,8 +130,8 @@ func (client *FungibleAssetClient) IsFrozen(storeAddress *AccountAddress) (isFro
}

// IsUntransferable returns true if the store can't be transferred
func (client *FungibleAssetClient) IsUntransferable(storeAddress *AccountAddress) (isFrozen bool, err error) {
val, err := client.viewStore([][]byte{storeAddress[:]}, "is_untransferable")
func (client *FungibleAssetClient) IsUntransferable(storeAddress *AccountAddress, ledgerVersion ...uint64) (isFrozen bool, err error) {
val, err := client.viewStore([][]byte{storeAddress[:]}, "is_untransferable", ledgerVersion...)
if err != nil {
return
}
Expand All @@ -140,8 +140,8 @@ func (client *FungibleAssetClient) IsUntransferable(storeAddress *AccountAddress
}

// StoreExists returns true if the store exists
func (client *FungibleAssetClient) StoreExists(storeAddress *AccountAddress) (exists bool, err error) {
val, err := client.viewStore([][]byte{storeAddress[:]}, "store_exists")
func (client *FungibleAssetClient) StoreExists(storeAddress *AccountAddress, ledgerVersion ...uint64) (exists bool, err error) {
val, err := client.viewStore([][]byte{storeAddress[:]}, "store_exists", ledgerVersion...)
if err != nil {
return
}
Expand All @@ -151,26 +151,26 @@ func (client *FungibleAssetClient) StoreExists(storeAddress *AccountAddress) (ex
}

// StoreMetadata returns the [AccountAddress] of the metadata for the store
func (client *FungibleAssetClient) StoreMetadata(storeAddress *AccountAddress) (metadataAddress *AccountAddress, err error) {
val, err := client.viewStore([][]byte{storeAddress[:]}, "store_metadata")
func (client *FungibleAssetClient) StoreMetadata(storeAddress *AccountAddress, ledgerVersion ...uint64) (metadataAddress *AccountAddress, err error) {
val, err := client.viewStore([][]byte{storeAddress[:]}, "store_metadata", ledgerVersion...)
if err != nil {
return
}
return unwrapObject(val)
}

// Supply returns the total supply of the fungible asset
func (client *FungibleAssetClient) Supply() (supply *big.Int, err error) {
val, err := client.viewMetadata([][]byte{client.metadataAddress[:]}, "supply")
func (client *FungibleAssetClient) Supply(ledgerVersion ...uint64) (supply *big.Int, err error) {
val, err := client.viewMetadata([][]byte{client.metadataAddress[:]}, "supply", ledgerVersion...)
if err != nil {
return
}
return unwrapAggregator(val)
}

// Maximum returns the maximum possible supply of the fungible asset
func (client *FungibleAssetClient) Maximum() (maximum *big.Int, err error) {
val, err := client.viewMetadata([][]byte{client.metadataAddress[:]}, "maximum")
func (client *FungibleAssetClient) Maximum(ledgerVersion ...uint64) (maximum *big.Int, err error) {
val, err := client.viewMetadata([][]byte{client.metadataAddress[:]}, "maximum", ledgerVersion...)
if err != nil {
return
}
Expand Down Expand Up @@ -228,7 +228,7 @@ func (client *FungibleAssetClient) ProjectUri(uri string, err error) {
}

// viewMetadata calls a view function on the fungible asset metadata
func (client *FungibleAssetClient) viewMetadata(args [][]byte, functionName string) (result any, err error) {
func (client *FungibleAssetClient) viewMetadata(args [][]byte, functionName string, ledgerVersion ...uint64) (result any, err error) {
payload := &ViewPayload{
Module: ModuleId{
Address: AccountOne,
Expand All @@ -238,11 +238,11 @@ func (client *FungibleAssetClient) viewMetadata(args [][]byte, functionName stri
ArgTypes: []TypeTag{metadataStructTag()},
Args: args,
}
return client.view(payload)
return client.view(payload, ledgerVersion...)
}

// viewStore calls a view function on the fungible asset store
func (client *FungibleAssetClient) viewStore(args [][]byte, functionName string) (result any, err error) {
func (client *FungibleAssetClient) viewStore(args [][]byte, functionName string, ledgerVersion ...uint64) (result any, err error) {
payload := &ViewPayload{
Module: ModuleId{
Address: AccountOne,
Expand All @@ -252,11 +252,11 @@ func (client *FungibleAssetClient) viewStore(args [][]byte, functionName string)
ArgTypes: []TypeTag{storeStructTag()},
Args: args,
}
return client.view(payload)
return client.view(payload, ledgerVersion...)
}

// viewPrimaryStore calls a view function on the primary fungible asset store
func (client *FungibleAssetClient) viewPrimaryStore(args [][]byte, functionName string) (result any, err error) {
func (client *FungibleAssetClient) viewPrimaryStore(args [][]byte, functionName string, ledgerVersion ...uint64) (result any, err error) {
payload := &ViewPayload{
Module: ModuleId{
Address: AccountOne,
Expand All @@ -266,11 +266,11 @@ func (client *FungibleAssetClient) viewPrimaryStore(args [][]byte, functionName
ArgTypes: []TypeTag{storeStructTag()},
Args: args,
}
return client.view(payload)
return client.view(payload, ledgerVersion...)
}

// viewPrimaryStoreMetadata calls a view function on the primary fungible asset store metadata
func (client *FungibleAssetClient) viewPrimaryStoreMetadata(args [][]byte, functionName string) (result any, err error) {
func (client *FungibleAssetClient) viewPrimaryStoreMetadata(args [][]byte, functionName string, ledgerVersion ...uint64) (result any, err error) {
payload := &ViewPayload{
Module: ModuleId{
Address: AccountOne,
Expand All @@ -280,7 +280,7 @@ func (client *FungibleAssetClient) viewPrimaryStoreMetadata(args [][]byte, funct
ArgTypes: []TypeTag{metadataStructTag()},
Args: args,
}
return client.view(payload)
return client.view(payload, ledgerVersion...)
}

func metadataStructTag() TypeTag {
Expand All @@ -291,8 +291,8 @@ func storeStructTag() TypeTag {
return TypeTag{Value: &StructTag{Address: AccountOne, Module: "fungible_asset", Name: "FungibleStore"}}
}

func (client *FungibleAssetClient) view(payload *ViewPayload) (result any, err error) {
vals, err := client.aptosClient.View(payload)
func (client *FungibleAssetClient) view(payload *ViewPayload, ledgerVersion ...uint64) (result any, err error) {
vals, err := client.aptosClient.View(payload, ledgerVersion...)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions nodeClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ func (rc *NodeClient) EstimateGasPrice() (info EstimateGasInfo, err error) {
}

// AccountAPTBalance fetches the balance of an account of APT. Response is in octas or 1/10^8 APT.
func (rc *NodeClient) AccountAPTBalance(account AccountAddress) (balance uint64, err error) {
func (rc *NodeClient) AccountAPTBalance(account AccountAddress, ledgerVersion ...uint64) (balance uint64, err error) {
accountBytes, err := bcs.Serialize(&account)
if err != nil {
return 0, err
Expand All @@ -986,7 +986,7 @@ func (rc *NodeClient) AccountAPTBalance(account AccountAddress) (balance uint64,
Function: "balance",
ArgTypes: []TypeTag{AptosCoinTypeTag},
Args: [][]byte{accountBytes},
})
}, ledgerVersion...)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit db6654a

Please sign in to comment.