@@ -55,12 +55,89 @@ export async function queryBalances(chain: Chain, address: string) {
55
55
await updateBalancesCosmos ( chain , address )
56
56
break
57
57
case "aptos" :
58
- console . error ( "aptos balance fetching currently unsupported" )
58
+ await updateBalancesAptos ( chain , address )
59
+ // console.error("aptos balance fetching currently unsupported")
59
60
break
60
61
default :
61
62
console . error ( "invalid rpc type in balance fetching" )
62
63
}
63
64
}
65
+ export async function updateBalancesAptos ( chain : Chain , address : string ) {
66
+ // Optionally mark expected tokens as "loading" (if chain.tokens exists)
67
+ if ( chain . tokens && chain . tokens . length ) {
68
+ chain . tokens . forEach ( token =>
69
+ updateBalance ( chain . chain_id , token . denom , { kind : "loading" , timestamp : Date . now ( ) } )
70
+ ) ;
71
+ }
72
+
73
+ // Define the GraphQL query and variables.
74
+ const query = `
75
+ query CoinsData($owner_address: String, $limit: Int, $offset: Int) {
76
+ current_fungible_asset_balances(
77
+ where: {owner_address: {_eq: $owner_address}}
78
+ limit: $limit
79
+ offset: $offset
80
+ ) {
81
+ amount
82
+ asset_type
83
+ metadata {
84
+ name
85
+ decimals
86
+ symbol
87
+ token_standard
88
+ }
89
+ }
90
+ }
91
+ ` ;
92
+ const variables = {
93
+ owner_address : address ,
94
+ limit : 100 ,
95
+ offset : 0 ,
96
+ } ;
97
+
98
+ // Set up the fetch options with appropriate headers.
99
+ const fetchOptions : RequestInit = {
100
+ method : "POST" ,
101
+ headers : {
102
+ "Content-Type" : "application/json" ,
103
+ "X-Indexer-Client" : "movement-explorer" ,
104
+ "X-Aptos-Client" : "aptos-typescript-sdk/1.35.0" ,
105
+ "X-Aptos-Typescript-Sdk-Origin-Method" : "queryIndexer" ,
106
+ } ,
107
+ body : JSON . stringify ( { query, variables } ) ,
108
+ } ;
109
+
110
+ try {
111
+ // Send the request to the Aptos indexer.
112
+ const response = await fetchJson ( "https://indexer.testnet.movementnetwork.xyz/v1/graphql" , fetchOptions ) ;
113
+ if ( response . isErr ( ) ) {
114
+ throw new Error ( response . error . message ) ;
115
+ }
116
+
117
+ const data = response . value . data ;
118
+ if ( ! data || ! data . current_fungible_asset_balances ) {
119
+ throw new Error ( "Invalid response data" ) ;
120
+ }
121
+
122
+ // Process each token balance from the response.
123
+ data . current_fungible_asset_balances . forEach ( ( token : any ) => {
124
+ // Here, asset_type can be used as the key for storing the balance.
125
+ const tokenKey = token . asset_type ;
126
+ const amount = token . amount ;
127
+ updateBalance ( chain . chain_id , tokenKey , { kind : "balance" , amount, timestamp : Date . now ( ) } ) ;
128
+ } ) ;
129
+ } catch ( error : any ) {
130
+ console . error ( "Error fetching Aptos balances" , error ) ;
131
+ // On error, update the balances for all tokens with an error state.
132
+ if ( chain . tokens && chain . tokens . length ) {
133
+ chain . tokens . forEach ( token =>
134
+ updateBalance ( chain . chain_id , token . denom , { kind : "error" , error : error . message , timestamp : Date . now ( ) } )
135
+ ) ;
136
+ }
137
+ }
138
+ }
139
+
140
+
64
141
65
142
export async function updateBalancesEvm ( chain : Chain , address : Address ) {
66
143
const denoms = chain . tokens . filter ( tokens => isAddress ( tokens . denom ) ) . map ( token => token . denom )
0 commit comments