Skip to content

Commit 5a7c86a

Browse files
committed
chore(app): adding aptos balance fetcher wip
Signed-off-by: Kaan Caglan <[email protected]>
1 parent 49a6bd6 commit 5a7c86a

File tree

3 files changed

+80
-7
lines changed

3 files changed

+80
-7
lines changed

Diff for: app/src/lib/stores/balances.ts

+78-1
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,89 @@ export async function queryBalances(chain: Chain, address: string) {
5555
await updateBalancesCosmos(chain, address)
5656
break
5757
case "aptos":
58-
console.error("aptos balance fetching currently unsupported")
58+
await updateBalancesAptos(chain, address)
59+
// console.error("aptos balance fetching currently unsupported")
5960
break
6061
default:
6162
console.error("invalid rpc type in balance fetching")
6263
}
6364
}
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+
64141

65142
export async function updateBalancesEvm(chain: Chain, address: Address) {
66143
const denoms = chain.tokens.filter(tokens => isAddress(tokens.denom)).map(token => token.denom)

Diff for: typescript-sdk/playground/union-to-movement.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const cliArgs = parseArgs({
4848
})
4949

5050
const PRIVATE_KEY = cliArgs.values["private-key"]
51-
const MUNO_DENOM = "muno"
51+
const MUNO_DENOM = "0x6d756e6f"
5252
const AMOUNT = 15n
5353
const RECEIVER = "0x4d8a66ece11f6352224942bd1dabc456b4bb5316124f02b9a7b6292ad61f7777"
5454
const SOURCE_CHAIN_ID = "union-testnet-9"

Diff for: typescript-sdk/src/query/offchain/ucs03-channels.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export const getQuoteToken = async (
201201
// const functionCall =
202202
// Build the transaction payload.
203203

204-
const receiverVec = MoveVector.U8("0x6d756e6f")
204+
const receiverVec = MoveVector.U8(base_token)
205205
const output = await aptos.experimental.viewBinary({
206206
payload: {
207207
function: `${channel.destination_port_id}::ibc_app::predict_wrapped_token`,
@@ -223,10 +223,6 @@ export const getQuoteToken = async (
223223
const addressBytes = deserializer.deserializeFixedBytes(32)
224224
const wrappedAddressHex = "0x" + Buffer.from(addressBytes).toString("hex")
225225

226-
// // 2) The second return value is the salt (vector<u8>)
227-
// const saltBytes = deserializer.deserializeBytes()
228-
// const saltHex = "0x" + Buffer.from(saltBytes).toString("hex")
229-
230226
console.log("Wrapped address:", wrappedAddressHex)
231227
return ok({ type: "NEW_WRAPPED", quote_token: wrappedAddressHex })
232228
}

0 commit comments

Comments
 (0)