diff --git a/packages/demo-wallet/src/App/Actions.tsx b/packages/demo-wallet/src/App/Actions.tsx index cef521b..3cb6786 100644 --- a/packages/demo-wallet/src/App/Actions.tsx +++ b/packages/demo-wallet/src/App/Actions.tsx @@ -1,6 +1,6 @@ import initWasm, { initThreadPool, WebWallet } from "@webzjs/webz-core"; -import { Action } from "./App"; +import { State, Action } from "./App"; import { MAINNET_LIGHTWALLETD_PROXY } from "./constants"; export async function init(dispatch: React.Dispatch) { @@ -12,18 +12,24 @@ export async function init(dispatch: React.Dispatch) { }); } +export async function addNewAccount(state: State, dispatch: React.Dispatch, seedPhrase: string, birthdayHeight: number) { + await state.webWallet?.create_account(seedPhrase, 0, birthdayHeight); + dispatch({ type: "append-account-seed", payload: seedPhrase }); + await syncStateWithWallet(state, dispatch); +} + export async function syncStateWithWallet( - webWallet: WebWallet | undefined, + state: State, dispatch: React.Dispatch ) { - if (!webWallet) { - return; + if (!state.webWallet) { + throw new Error("Wallet not initialized"); } - let summary = await webWallet?.get_wallet_summary(); + let summary = await state.webWallet?.get_wallet_summary(); if (summary) { dispatch({ type: "set-summary", payload: summary }); } - let chainHeight = await webWallet?.get_latest_block(); + let chainHeight = await state.webWallet?.get_latest_block(); if (chainHeight) { dispatch({ type: "set-chain-height", payload: chainHeight }); } @@ -31,12 +37,29 @@ export async function syncStateWithWallet( } export async function triggerRescan( - webWallet: WebWallet | undefined, + state: State, dispatch: React.Dispatch ) { - if (!webWallet) { - return; + if (!state.webWallet) { + throw new Error("Wallet not initialized"); + } + await state.webWallet?.sync2(); + await syncStateWithWallet(state, dispatch); +} + +export async function triggerTransfer( + state: State, + dispatch: React.Dispatch, + toAddress: string, + amount: bigint +) { + if (!state.webWallet) { + throw new Error("Wallet not initialized"); } - await webWallet?.sync2(); - await syncStateWithWallet(webWallet, dispatch); + if (state.activeAccount == null) { + throw new Error("No active account"); + } + + await state.webWallet?.transfer(state.accountSeeds[state.activeAccount], state.activeAccount, toAddress, amount); + await syncStateWithWallet(state, dispatch); } diff --git a/packages/demo-wallet/src/App/App.tsx b/packages/demo-wallet/src/App/App.tsx index cdf69ad..d650a94 100644 --- a/packages/demo-wallet/src/App/App.tsx +++ b/packages/demo-wallet/src/App/App.tsx @@ -18,21 +18,24 @@ import { SendFunds } from "./components/SendFunds"; import { ReceiveFunds } from "./components/ReceiveFunds"; import { Summary } from "./components/Summary"; -type State = { +export type State = { webWallet?: WebWallet; activeAccount?: number; summary?: WalletSummary; chainHeight?: bigint; + accountSeeds: string[]; }; const initialState: State = { activeAccount: undefined, summary: undefined, chainHeight: undefined, + accountSeeds: [], }; export type Action = | { type: "set-active-account"; payload: number } + | { type: "append-account-seed"; payload: string } | { type: "set-web-wallet"; payload: WebWallet } | { type: "set-summary"; payload: WalletSummary } | { type: "set-chain-height"; payload: bigint }; @@ -42,6 +45,9 @@ const reducer = (state: State, action: Action): State => { case "set-active-account": { return { ...state, activeAccount: action.payload }; } + case "append-account-seed": { + return { ...state, accountSeeds: [...state.accountSeeds, action.payload] }; + } case "set-web-wallet": { return { ...state, webWallet: action.payload }; } @@ -68,16 +74,6 @@ export function App() { init(dispatch); }, [dispatch]); - const triggerRescan = () => { - if (!state.webWallet) { - return; - } - console.log("rescanning"); - state.webWallet.sync2().then(() => { - console.log("rescan complete"); - }); - }; - return (
diff --git a/packages/demo-wallet/src/App/components/Header.tsx b/packages/demo-wallet/src/App/components/Header.tsx index 312e39b..0eba07c 100644 --- a/packages/demo-wallet/src/App/components/Header.tsx +++ b/packages/demo-wallet/src/App/components/Header.tsx @@ -17,7 +17,6 @@ export function Header() { state.summary?.account_balances.find( ([id]) => id === state.activeAccount ); - console.log(activeBalanceReport); let totalBalance = activeBalanceReport ? activeBalanceReport[1].sapling_balance + activeBalanceReport[1].orchard_balance : 0 return ( @@ -55,12 +54,12 @@ export function Header() { - diff --git a/packages/demo-wallet/src/App/components/ImportAccount.tsx b/packages/demo-wallet/src/App/components/ImportAccount.tsx index beee9d5..8588efc 100644 --- a/packages/demo-wallet/src/App/components/ImportAccount.tsx +++ b/packages/demo-wallet/src/App/components/ImportAccount.tsx @@ -5,7 +5,7 @@ import Form from "react-bootstrap/Form"; import { ToastContainer, toast } from "react-toastify"; import { WalletContext } from "../App"; -import { syncStateWithWallet } from "../Actions"; +import { addNewAccount } from "../Actions"; export function ImportAccount() { let {state, dispatch} = useContext(WalletContext); @@ -13,13 +13,12 @@ export function ImportAccount() { let [birthdayHeight, setBirthdayHeight] = useState(2657762); let [seedPhrase, setSeedPhrase] = useState("mix sample clay sweet planet lava giraffe hand fashion switch away pool rookie earth purity truly square trumpet goose move actor save jaguar volume"); - const handleSubmit = async (event: FormEvent) => { - event.preventDefault(); - await state.webWallet?.create_account(seedPhrase, 0, birthdayHeight); + const handleSubmit = async (e: FormEvent) => { + e.preventDefault(); + await addNewAccount(state, dispatch, seedPhrase, birthdayHeight); toast.success("Account imported successfully", { position: "top-center", }); - await syncStateWithWallet(state.webWallet, dispatch); setBirthdayHeight(0); setSeedPhrase(""); }; diff --git a/packages/demo-wallet/src/App/components/SendFunds.tsx b/packages/demo-wallet/src/App/components/SendFunds.tsx index 71fc98e..3b0531f 100644 --- a/packages/demo-wallet/src/App/components/SendFunds.tsx +++ b/packages/demo-wallet/src/App/components/SendFunds.tsx @@ -1,22 +1,44 @@ -import React from "react"; +import React, { FormEvent, useContext, useState } from "react"; import Button from "react-bootstrap/Button"; import Form from "react-bootstrap/Form"; +import { WalletContext } from "../App"; +import { triggerTransfer } from "../Actions"; + export function SendFunds() { + let { state, dispatch } = useContext(WalletContext); + + let [toAddress, setToAddress] = useState(""); + let [amount, setAmount] = useState(BigInt(0)); + + const handleSubmit = async (e: FormEvent) => { + e.preventDefault(); + console.log("Sending", amount, "to", toAddress); + await triggerTransfer(state, dispatch, toAddress, amount); + console.log("Send complete"); + }; + return ( -
+ - From Account: - - - To: - + setToAddress(e.target.value)} + /> Amount: - + setAmount(BigInt(e.target.value))} + /> Memo (optional): - +