Skip to content

Commit c4ed99f

Browse files
committedFeb 28, 2025··
chore(app): fetching movement balances
Signed-off-by: Kaan Caglan <[email protected]>
1 parent f9a3661 commit c4ed99f

File tree

4 files changed

+56
-36
lines changed

4 files changed

+56
-36
lines changed
 

‎app/src/lib/components/TransferCube/components/Cube/faces/Transfer.svelte

+10-5
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,14 @@ const transfer = async () => {
8888
// @ts-ignore
8989
transferState.set({ kind: "SWITCHING_TO_CHAIN" })
9090
91-
const rpcUrl = sourceChain.rpcs.find(rpc => rpc.type === "rpc")?.url
92-
if (!rpcUrl) return toast.error(`no rpc available for ${sourceChain.display_name}`)
91+
let rpcUrl = sourceChain.rpcs.find((rpc) => rpc.type === "rpc")?.url;
92+
if (!rpcUrl)
93+
return toast.error(`no rpc available for ${sourceChain.display_name}`);
94+
95+
if (!rpcUrl.endsWith("/v1", rpcUrl.length - 3)) {
96+
rpcUrl = rpcUrl + "/v1";
97+
}
98+
rpcUrl = "https://aptos.testnet.bardock.movementlabs.xyz/v1"; //TODO: Remove this later its for test
9399
94100
if (stepBefore($transferState, "CONFIRMING_TRANSFER")) {
95101
const chainInfo = await wallet.getNetwork()
@@ -111,13 +117,12 @@ const transfer = async () => {
111117
const client = createUnionClient({
112118
chainId: sourceChain.chain_id as AptosChainId,
113119
account: await wallet?.getAccount(),
114-
transport: wallet as AptosBrowserWallet
120+
transport: http(`${rpcUrl}`),
115121
})
116122
117123
let realArgs = {
118124
...transferArgs,
119-
receiver: toHex(transferArgs.receiver),
120-
baseToken: fromHex(transferArgs.baseToken, "string")
125+
receiver: toHex(transferArgs.receiver)
121126
}
122127
123128
const transfer = await client.transferAsset(realArgs)

‎app/src/lib/components/TransferCube/index.svelte

+13-4
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,19 @@ onMount(() => {
179179
const unsubscribe = transfer.subscribe(trans => {
180180
const chain = trans.intents.sourceChain
181181
if (chain) {
182-
const userAddr =
183-
chain.rpc_type === "evm"
184-
? $userAddress.evm?.canonical.toLowerCase()
185-
: $userAddrCosmos?.canonical
182+
let userAddr;
183+
if (chain.rpc_type === "evm") {
184+
userAddr = $userAddress.evm?.canonical.toLowerCase();
185+
} else if (
186+
chain.rpc_type === "aptos" ||
187+
chain.chain_id === "movement"
188+
) {
189+
// Use the aptos field for Movement
190+
userAddr = $userAddress.aptos?.canonical.toLowerCase();
191+
} else {
192+
// Fallback to cosmos conversion if needed
193+
userAddr = $userAddrCosmos?.canonical;
194+
}
186195
if (userAddr && (previousSourceChain !== chain.chain_id || previousUserAddr !== userAddr)) {
187196
previousUserAddr = userAddr
188197
previousSourceChain = chain.chain_id

‎app/src/lib/stores/balances.ts

+31-25
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ export async function queryBalances(chain: Chain, address: string) {
5555
await updateBalancesCosmos(chain, address)
5656
break
5757
case "aptos":
58-
await updateBalancesAptos(chain, address)
58+
await updateBalancesAptos(chain, address as Address)
5959
// console.error("aptos balance fetching currently unsupported")
6060
break
6161
default:
6262
console.error("invalid rpc type in balance fetching")
6363
}
6464
}
65-
export async function updateBalancesAptos(chain: Chain, address: string) {
65+
export async function updateBalancesAptos(chain: Chain, address: Address) {
6666
// Optionally mark expected tokens as "loading" (if chain.tokens exists)
6767
if (chain.tokens && chain.tokens.length) {
6868
chain.tokens.forEach(token =>
@@ -91,54 +91,60 @@ export async function updateBalancesAptos(chain: Chain, address: string) {
9191
`;
9292
const variables = {
9393
owner_address: address,
94-
limit: 100,
95-
offset: 0,
94+
limit: 200,
95+
offset: 0
9696
};
9797

9898
// Set up the fetch options with appropriate headers.
9999
const fetchOptions: RequestInit = {
100100
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-
},
107101
body: JSON.stringify({ query, variables }),
108102
};
109103

110104
try {
111105
// Send the request to the Aptos indexer.
112-
const response = await fetchJson("https://indexer.testnet.movementnetwork.xyz/v1/graphql", fetchOptions);
106+
const response = await fetchJson(
107+
"https://indexer.testnet.movementnetwork.xyz/v1/graphql",
108+
fetchOptions
109+
)
113110
if (response.isErr()) {
114-
throw new Error(response.error.message);
111+
throw new Error(response.error.message)
115112
}
116-
117113
const data = response.value.data;
118114
if (!data || !data.current_fungible_asset_balances) {
119115
throw new Error("Invalid response data");
120116
}
121117

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-
});
118+
const aptosBalances = data.current_fungible_asset_balances
119+
.filter((token: any) => token.metadata.token_standard === "v2")
120+
.map((token: any) => ({
121+
denom: token.asset_type,
122+
amount: token.amount
123+
}))
124+
125+
console.info("aptosBalances: ", aptosBalances)
126+
aptosBalances.forEach(token => {
127+
updateBalance(chain.chain_id, token.denom, {
128+
kind: "balance",
129+
amount: token.amount,
130+
timestamp: Date.now()
131+
})
132+
})
129133
} catch (error: any) {
130-
console.error("Error fetching Aptos balances", error);
134+
console.error("Error fetching Aptos balances", error)
131135
// On error, update the balances for all tokens with an error state.
132136
if (chain.tokens && chain.tokens.length) {
133137
chain.tokens.forEach(token =>
134-
updateBalance(chain.chain_id, token.denom, { kind: "error", error: error.message, timestamp: Date.now() })
135-
);
138+
updateBalance(chain.chain_id, token.denom, {
139+
kind: "error",
140+
error: error.message,
141+
timestamp: Date.now()
142+
})
143+
)
136144
}
137145
}
138146
}
139147

140-
141-
142148
export async function updateBalancesEvm(chain: Chain, address: Address) {
143149
const denoms = chain.tokens.filter(tokens => isAddress(tokens.denom)).map(token => token.denom)
144150
balances.update(val => {

‎app/src/lib/utilities/neverthrow.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { ResultAsync } from "neverthrow"
22

3-
export const fetchJson = (url: string) =>
3+
export const fetchJson = (url: string, options?: RequestInit) =>
44
ResultAsync.fromPromise(
5-
fetch(url).then(response => {
5+
fetch(url, options).then(response => {
66
if (!response.ok) {
77
throw new Error(`HTTP error! Status: ${response.status}`)
88
}

0 commit comments

Comments
 (0)