Skip to content

Commit

Permalink
call sync not sync2
Browse files Browse the repository at this point in the history
  • Loading branch information
ec2 committed Oct 1, 2024
1 parent 5119121 commit ea6c42c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
17 changes: 17 additions & 0 deletions packages/demo-wallet/serve.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"headers": [
{
"source": "*/",
"headers": [
{
"key": "Cross-Origin-Opener-Policy",
"value": "same-origin"
},
{
"key": "Cross-Origin-Embedder-Policy",
"value": "require-corp"
}
]
}
]
}
68 changes: 34 additions & 34 deletions packages/demo-wallet/src/App/Actions.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
import initWasm, { initThreadPool, WebWallet } from "@webzjs/webz-core";
import initWasm, {initThreadPool, WebWallet} from "@webzjs/webz-core";

import { State, Action } from "./App";
import { MAINNET_LIGHTWALLETD_PROXY } from "./Constants";
import {Action, State} from "./App";
import {MAINNET_LIGHTWALLETD_PROXY} from "./Constants";

export async function init(dispatch: React.Dispatch<Action>) {
await initWasm();
await initThreadPool(10);
dispatch({
type: "set-web-wallet",
payload: new WebWallet("main", MAINNET_LIGHTWALLETD_PROXY, 1),
});
await initWasm();
await initThreadPool(10);
dispatch({
type: "set-web-wallet",
payload: new WebWallet("main", MAINNET_LIGHTWALLETD_PROXY, 1),
});
}

export async function addNewAccount(state: State, dispatch: React.Dispatch<Action>, seedPhrase: string, birthdayHeight: number) {
let account_id = await state.webWallet?.create_account(seedPhrase, 0, birthdayHeight) || 0;
dispatch({ type: "add-account-seed", payload: [account_id, seedPhrase] });
dispatch({type: "add-account-seed", payload: [account_id, seedPhrase]});
await syncStateWithWallet(state, dispatch);
}

export async function syncStateWithWallet(
state: State,
dispatch: React.Dispatch<Action>
state: State,
dispatch: React.Dispatch<Action>
) {
if (!state.webWallet) {
throw new Error("Wallet not initialized");
}
let summary = await state.webWallet?.get_wallet_summary();
if (summary) {
dispatch({ type: "set-summary", payload: summary });
}
let chainHeight = await state.webWallet?.get_latest_block();
if (chainHeight) {
dispatch({ type: "set-chain-height", payload: chainHeight });
}
dispatch({ type: "set-active-account", payload: summary?.account_balances[0][0] });
if (!state.webWallet) {
throw new Error("Wallet not initialized");
}
let summary = await state.webWallet?.get_wallet_summary();
if (summary) {
dispatch({type: "set-summary", payload: summary});
}
let chainHeight = await state.webWallet?.get_latest_block();
if (chainHeight) {
dispatch({type: "set-chain-height", payload: chainHeight});
}
dispatch({type: "set-active-account", payload: summary?.account_balances[0][0]});
}

export async function triggerRescan(
state: State,
dispatch: React.Dispatch<Action>
state: State,
dispatch: React.Dispatch<Action>
) {
if (!state.webWallet) {
throw new Error("Wallet not initialized");
}
await state.webWallet?.sync2();
await state.webWallet?.sync();
await syncStateWithWallet(state, dispatch);
}

export async function triggerTransfer(
state: State,
dispatch: React.Dispatch<Action>,
toAddress: string,
amount: bigint
state: State,
dispatch: React.Dispatch<Action>,
toAddress: string,
amount: bigint
) {
if (!state.webWallet) {
throw new Error("Wallet not initialized");
Expand All @@ -61,12 +61,12 @@ export async function triggerTransfer(
}

let activeAccountSeedPhrase = state.accountSeeds.get(state.activeAccount) || "";

let proposal = await state.webWallet?.propose_transfer(state.activeAccount, toAddress, amount);
console.log(JSON.stringify(proposal.describe(), null, 2));

let txids = await state.webWallet.create_proposed_transactions(proposal, activeAccountSeedPhrase);
console.log(JSON.stringify(txids, null, 2));

await state.webWallet.send_authorized_transactions(txids);
}

0 comments on commit ea6c42c

Please sign in to comment.