Skip to content

Commit 8a7d8fc

Browse files
authored
Fetch transactions by account name (#33)
1 parent a695265 commit 8a7d8fc

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

packages/mobile-app/app/(tabs)/index.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import { StatusBar } from "expo-status-bar";
22
import { ScrollView, StyleSheet, Text, View } from "react-native";
3-
import { Assert } from "@ironfish/sdk";
43
import { useFacade } from "../../data/facades";
4+
import { useEffect, useState } from "react";
55

66
export default function Balances() {
77
const facade = useFacade();
88

9+
const [account, setAccount] = useState<string>("");
10+
911
const getTransactionsResult = facade.getTransactions.useQuery(
10-
{ accountName: "", hash: "" },
12+
{ accountName: account, hash: "" },
1113
{
1214
refetchInterval: 1000,
1315
},
1416
);
1517

16-
Assert.isEqual(50, 50);
18+
const getAccountsResult = facade.getAccounts.useQuery();
19+
20+
useEffect(() => {
21+
if (getAccountsResult.data) {
22+
setAccount(getAccountsResult.data[0].name);
23+
}
24+
}, [getAccountsResult.data]);
1725

1826
return (
1927
<View style={styles.container}>

packages/mobile-app/data/facades/wallet/demoHandlers.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,8 @@ export const walletDemoHandlers = f.facade<WalletHandlers>({
191191
getTransactions: f.handler.query(
192192
async ({
193193
accountName,
194-
hash,
195194
}: {
196195
accountName: string;
197-
hash: string;
198196
options?: {
199197
limit?: number;
200198
offset?: number;
@@ -205,7 +203,7 @@ export const walletDemoHandlers = f.facade<WalletHandlers>({
205203
}): Promise<Transaction[]> => {
206204
return [
207205
{
208-
hash: hash,
206+
hash: "4343291950e34661d8a89114dd77524ad95e7cb78fe50d29f1c9067adc1c2d4c",
209207
timestamp: new Date(new Date().setDate(new Date().getDate() - 1)),
210208
assetBalanceDeltas: [
211209
{

packages/mobile-app/data/facades/wallet/handlers.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,8 @@ export const walletHandlers = f.facade<WalletHandlers>({
200200
getTransactions: f.handler.query(
201201
async ({
202202
accountName,
203-
hash,
204203
}: {
205204
accountName: string;
206-
hash: string;
207205
options?: {
208206
limit?: number;
209207
offset?: number;
@@ -212,7 +210,7 @@ export const walletHandlers = f.facade<WalletHandlers>({
212210
address?: string;
213211
};
214212
}): Promise<Transaction[]> => {
215-
const txns = await wallet.getTransactions(Network.TESTNET);
213+
const txns = await wallet.getTransactions(accountName, Network.TESTNET);
216214

217215
// TODO: Make a better query for this
218216
const txnsWithNotes = await Promise.all(

packages/mobile-app/data/facades/wallet/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ export type WalletHandlers = {
116116
getTransactions: Query<
117117
(args: {
118118
accountName: string;
119-
hash: string;
120119
options?: {
121120
limit?: number;
122121
offset?: number;

packages/mobile-app/data/wallet/db.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as SecureStore from "expo-secure-store";
55
import { AccountFormat, encodeAccount, Note } from "@ironfish/sdk";
66
import { Network } from "../constants";
77
import * as Uint8ArrayUtils from "../../utils/uint8Array";
8-
98
interface AccountsTable {
109
id: Generated<number>;
1110
name: string;
@@ -462,12 +461,19 @@ export class WalletDb {
462461
.execute();
463462
}
464463

465-
async getTransactions(network: Network) {
464+
async getTransactions(accountId: number, network: Network) {
466465
return await this.db
467-
.selectFrom("transactions")
466+
.selectFrom("accountTransactions")
467+
.innerJoin(
468+
"transactions",
469+
"transactions.hash",
470+
"accountTransactions.transactionHash",
471+
)
468472
.selectAll()
469-
.where("network", "=", network)
470-
.orderBy("timestamp", "asc")
473+
.where((eb) =>
474+
eb.and([eb("accountId", "=", accountId), eb("network", "=", network)]),
475+
)
476+
.orderBy("transactions.timestamp", "asc")
471477
.execute();
472478
}
473479
}

packages/mobile-app/data/wallet/wallet.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,15 @@ class Wallet {
143143
return await this.state.db.getTransactionNotes(transactionHash);
144144
}
145145

146-
async getTransactions(network: Network) {
146+
async getTransactions(accountName: string, network: Network) {
147147
assertStarted(this.state);
148148

149-
return await this.state.db.getTransactions(network);
149+
const account = await this.getAccount(accountName);
150+
if (account == null) {
151+
throw new Error(`No account found with name ${accountName}`);
152+
}
153+
154+
return await this.state.db.getTransactions(account.id, network);
150155
}
151156

152157
private async connectBlock(

0 commit comments

Comments
 (0)