Skip to content

Commit ca814cc

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

File tree

3 files changed

+86
-25
lines changed

3 files changed

+86
-25
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

+7-23
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@ export const getQuoteToken = async (
184184
if (aptosChainId.includes(channel.destination_chain_id)) {
185185
let network: Network
186186
let rpcUrl = ""
187-
if (channel.destination_chain_id === "126") {
188-
network = Network.MAINNET
189-
} else if (channel.destination_chain_id === "250") {
187+
if (channel.destination_chain_id === "250") {
190188
network = Network.TESTNET
191189
rpcUrl = "https://aptos.testnet.bardock.movementlabs.xyz/v1"
192190
} else {
@@ -196,38 +194,24 @@ export const getQuoteToken = async (
196194
const config = new AptosConfig({ network: network, fullnode: rpcUrl })
197195
const aptos = new Aptos(config)
198196

199-
// Define the Move function call.
200-
// Replace <MODULE_ADDRESS> and <MODULE_NAME> with your contract's module address and name.
201-
// const functionCall =
202-
// Build the transaction payload.
203-
204-
const receiverVec = MoveVector.U8("0x6d756e6f")
205-
const output = await aptos.experimental.viewBinary({
197+
const output = await aptos.view({
206198
payload: {
207199
function: `${channel.destination_port_id}::ibc_app::predict_wrapped_token`,
208200
typeArguments: [],
209201
// Adjust functionArguments as needed.
210202
functionArguments: [
211203
0, // path
212204
channel.destination_channel_id, // channel
213-
receiverVec
205+
MoveVector.U8(base_token)
214206
]
215207
}
216208
})
217209

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")
225-
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-
210+
const wrappedAddressHex = output[0]?.toString()
230211
console.log("Wrapped address:", wrappedAddressHex)
212+
if (!wrappedAddressHex) {
213+
return err(new Error("failed to get wrapped address from aptos"))
214+
}
231215
return ok({ type: "NEW_WRAPPED", quote_token: wrappedAddressHex })
232216
}
233217

0 commit comments

Comments
 (0)