Skip to content

Commit fa6ed66

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

File tree

3 files changed

+106
-17
lines changed

3 files changed

+106
-17
lines changed

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)

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"

typescript-sdk/src/query/offchain/ucs03-channels.ts

+27-15
Original file line numberDiff line numberDiff line change
@@ -201,32 +201,44 @@ export const getQuoteToken = async (
201201
// const functionCall =
202202
// Build the transaction payload.
203203

204-
const receiverVec = MoveVector.U8("0x6d756e6f")
205-
const output = await aptos.experimental.viewBinary({
204+
const sending_Data = {
206205
payload: {
207206
function: `${channel.destination_port_id}::ibc_app::predict_wrapped_token`,
208207
typeArguments: [],
209208
// Adjust functionArguments as needed.
210209
functionArguments: [
211210
0, // path
212211
channel.destination_channel_id, // channel
213-
receiverVec
212+
MoveVector.U8(base_token)
214213
]
215214
}
216-
})
217-
218-
console.info("base_token:", base_token)
219-
console.info("transaction:", output)
220-
console.info("channel.destination_channel_id:", channel.destination_channel_id)
221-
console.info("channel.destination_port_id:", channel.destination_port_id)
222-
const deserializer = new Deserializer(output.slice(1))
223-
const addressBytes = deserializer.deserializeFixedBytes(32)
224-
const wrappedAddressHex = "0x" + Buffer.from(addressBytes).toString("hex")
215+
}
216+
console.info("sending_Data:", sending_Data)
217+
console.info("MoveVector.U8(base_token):", MoveVector.U8(base_token))
225218

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")
219+
const output = await aptos.view({
220+
payload: {
221+
function: `${channel.destination_port_id}::ibc_app::predict_wrapped_token`,
222+
typeArguments: [],
223+
// Adjust functionArguments as needed.
224+
functionArguments: [
225+
0, // path
226+
channel.destination_channel_id, // channel
227+
MoveVector.U8(base_token)
228+
]
229+
}
230+
})
229231

232+
// console.info("base_token:", base_token)
233+
// console.info("transaction:", output[0])
234+
// console.info("output.length::", output.length)
235+
// console.info("channel.destination_channel_id:", channel.destination_channel_id)
236+
// console.info("channel.destination_port_id:", channel.destination_port_id)
237+
// const deserializer = new Deserializer(output.slice(1))
238+
// console.info("deserializer.remaining(): ", deserializer.remaining())
239+
// const addressBytes = deserializer.deserializeFixedBytes(32)
240+
// const wrappedAddressHex = "0x" + Buffer.from(addressBytes).toString("hex")
241+
const wrappedAddressHex = output[0]
230242
console.log("Wrapped address:", wrappedAddressHex)
231243
return ok({ type: "NEW_WRAPPED", quote_token: wrappedAddressHex })
232244
}

0 commit comments

Comments
 (0)