Skip to content

Commit

Permalink
chore(app): adding aptos balance fetcher wip
Browse files Browse the repository at this point in the history
Signed-off-by: Kaan Caglan <[email protected]>
  • Loading branch information
Caglankaan committed Feb 27, 2025
1 parent 49a6bd6 commit 1952a43
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
79 changes: 78 additions & 1 deletion app/src/lib/stores/balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,89 @@ export async function queryBalances(chain: Chain, address: string) {
await updateBalancesCosmos(chain, address)
break
case "aptos":
console.error("aptos balance fetching currently unsupported")
await updateBalancesAptos(chain, address)
// console.error("aptos balance fetching currently unsupported")
break
default:
console.error("invalid rpc type in balance fetching")
}
}
export async function updateBalancesAptos(chain: Chain, address: string) {
// Optionally mark expected tokens as "loading" (if chain.tokens exists)
if (chain.tokens && chain.tokens.length) {
chain.tokens.forEach(token =>
updateBalance(chain.chain_id, token.denom, { kind: "loading", timestamp: Date.now() })
);
}

// Define the GraphQL query and variables.
const query = `
query CoinsData($owner_address: String, $limit: Int, $offset: Int) {
current_fungible_asset_balances(
where: {owner_address: {_eq: $owner_address}}
limit: $limit
offset: $offset
) {
amount
asset_type
metadata {
name
decimals
symbol
token_standard
}
}
}
`;
const variables = {
owner_address: address,
limit: 100,
offset: 0,
};

// Set up the fetch options with appropriate headers.
const fetchOptions: RequestInit = {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Indexer-Client": "movement-explorer",
"X-Aptos-Client": "aptos-typescript-sdk/1.35.0",
"X-Aptos-Typescript-Sdk-Origin-Method": "queryIndexer",
},
body: JSON.stringify({ query, variables }),
};

try {
// Send the request to the Aptos indexer.
const response = await fetchJson("https://indexer.testnet.movementnetwork.xyz/v1/graphql", fetchOptions);
if (response.isErr()) {
throw new Error(response.error.message);
}

const data = response.value.data;
if (!data || !data.current_fungible_asset_balances) {
throw new Error("Invalid response data");
}

// Process each token balance from the response.
data.current_fungible_asset_balances.forEach((token: any) => {
// Here, asset_type can be used as the key for storing the balance.
const tokenKey = token.asset_type;
const amount = token.amount;
updateBalance(chain.chain_id, tokenKey, { kind: "balance", amount, timestamp: Date.now() });
});
} catch (error: any) {
console.error("Error fetching Aptos balances", error);
// On error, update the balances for all tokens with an error state.
if (chain.tokens && chain.tokens.length) {
chain.tokens.forEach(token =>
updateBalance(chain.chain_id, token.denom, { kind: "error", error: error.message, timestamp: Date.now() })
);
}
}
}



export async function updateBalancesEvm(chain: Chain, address: Address) {
const denoms = chain.tokens.filter(tokens => isAddress(tokens.denom)).map(token => token.denom)
Expand Down
2 changes: 1 addition & 1 deletion typescript-sdk/playground/union-to-movement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const cliArgs = parseArgs({
})

const PRIVATE_KEY = cliArgs.values["private-key"]
const MUNO_DENOM = "muno"
const MUNO_DENOM = "0x6d756e6f"
const AMOUNT = 15n
const RECEIVER = "0x4d8a66ece11f6352224942bd1dabc456b4bb5316124f02b9a7b6292ad61f7777"
const SOURCE_CHAIN_ID = "union-testnet-9"
Expand Down
22 changes: 17 additions & 5 deletions typescript-sdk/src/query/offchain/ucs03-channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,21 @@ export const getQuoteToken = async (
// const functionCall =
// Build the transaction payload.

const receiverVec = MoveVector.U8("0x6d756e6f")
const receiverVec = MoveVector.U8(base_token)
const sending_Data = {
payload: {
function: `${channel.destination_port_id}::ibc_app::predict_wrapped_token`,
typeArguments: [],
// Adjust functionArguments as needed.
functionArguments: [
0, // path
channel.destination_channel_id, // channel
receiverVec
]
}
}
console.info("sending_Data:", sending_Data)
console.info("receiverVec:", receiverVec)
const output = await aptos.experimental.viewBinary({
payload: {
function: `${channel.destination_port_id}::ibc_app::predict_wrapped_token`,
Expand All @@ -217,16 +231,14 @@ export const getQuoteToken = async (

console.info("base_token:", base_token)
console.info("transaction:", output)
console.info("output.length::", output.length)
console.info("channel.destination_channel_id:", channel.destination_channel_id)
console.info("channel.destination_port_id:", channel.destination_port_id)
const deserializer = new Deserializer(output.slice(1))
console.info("deserializer.remaining(): ", deserializer.remaining())
const addressBytes = deserializer.deserializeFixedBytes(32)
const wrappedAddressHex = "0x" + Buffer.from(addressBytes).toString("hex")

// // 2) The second return value is the salt (vector<u8>)
// const saltBytes = deserializer.deserializeBytes()
// const saltHex = "0x" + Buffer.from(saltBytes).toString("hex")

console.log("Wrapped address:", wrappedAddressHex)
return ok({ type: "NEW_WRAPPED", quote_token: wrappedAddressHex })
}
Expand Down

0 comments on commit 1952a43

Please sign in to comment.