Skip to content

Commit 209ee4b

Browse files
authored
Merge pull request #9 from ProvableHQ/feature/transaction-sign-react-example
Handles ExecuteTransaction
2 parents e9aea54 + 97f3761 commit 209ee4b

8 files changed

Lines changed: 251 additions & 53 deletions

File tree

examples/react-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
},
1212
"dependencies": {
1313
"@provablehq/aleo-wallet-adaptor-core": "workspace:*",
14+
"@provablehq/aleo-wallet-adaptor-leo": "workspace:*",
1415
"@provablehq/aleo-wallet-adaptor-puzzle": "workspace:*",
1516
"@provablehq/aleo-wallet-adaptor-react": "workspace:*",
1617
"@provablehq/aleo-wallet-adaptor-react-ui": "workspace:*",
17-
"@provablehq/aleo-wallet-adaptor-leo": "workspace:*",
1818
"@puzzlehq/sdk": "^0.2.0",
1919
"react": "^18.2.0",
2020
"react-dom": "^18.2.0"

examples/react-app/src/App.tsx

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useMemo } from 'react';
1+
import { useMemo, useState } from 'react';
22
import { AleoWalletProvider } from '@provablehq/aleo-wallet-adaptor-react';
33
import { WalletConnectButton } from '@provablehq/aleo-wallet-adaptor-react-ui';
44
import { PuzzleWalletAdapter } from '@provablehq/aleo-wallet-adaptor-puzzle';
@@ -29,53 +29,51 @@ const WalletInfo = () => {
2929
};
3030

3131
// Example transaction component
32-
// const ExecuteTransaction = () => {
33-
// const { executeTransaction, connected } = useWallet();
34-
// const [txId, setTxId] = useState<string | null>(null);
35-
// const [loading, setLoading] = useState(false);
32+
const ExecuteTransaction = () => {
33+
const { executeTransaction, connected } = useWallet();
34+
const [txId, setTxId] = useState<string | null>(null);
35+
const [loading, setLoading] = useState(false);
3636

37-
// const handleExecute = async () => {
38-
// if (!connected) return;
37+
const handleExecute = async () => {
38+
if (!connected) return;
3939

40-
// try {
41-
// setLoading(true);
40+
try {
41+
setLoading(true);
4242

43-
// // This is just an example - you would need a real program and function to call
44-
// const tx = await executeTransaction({
45-
// program: 'hello_world.aleo',
46-
// function: 'main',
47-
// inputs: [],
48-
// });
43+
// This is just an example - you would need a real program and function to call
44+
const tx = await executeTransaction({
45+
program: 'hello_world.aleo',
46+
function: 'main',
47+
inputs: ['1u32', '1u32'],
48+
fee: 100000,
49+
});
4950

50-
// setTxId(tx.id);
51-
// } catch (error) {
52-
// console.error('Transaction failed', error);
53-
// } finally {
54-
// setLoading(false);
55-
// }
56-
// };
51+
setTxId(tx.id);
52+
} catch (error) {
53+
console.error('Transaction failed', error);
54+
} finally {
55+
setLoading(false);
56+
}
57+
};
5758

58-
// if (!connected) {
59-
// return null;
60-
// }
59+
if (!connected) {
60+
return null;
61+
}
6162

62-
// return (
63-
// <div className="transaction">
64-
// <button
65-
// onClick={handleExecute}
66-
// disabled={loading}
67-
// >
68-
// {loading ? 'Executing...' : 'Execute Transaction'}
69-
// </button>
63+
return (
64+
<div className="transaction">
65+
<button onClick={handleExecute} disabled={loading}>
66+
{loading ? 'Executing...' : 'Execute Transaction'}
67+
</button>
7068

71-
// {txId && (
72-
// <div className="tx-result">
73-
// <p>Transaction ID: {txId}</p>
74-
// </div>
75-
// )}
76-
// </div>
77-
// );
78-
// };
69+
{txId && (
70+
<div className="tx-result">
71+
<p>Transaction ID: {txId}</p>
72+
</div>
73+
)}
74+
</div>
75+
);
76+
};
7977

8078
export function App() {
8179
// memoize to avoid re‑instantiating adapters on each render
@@ -97,13 +95,14 @@ export function App() {
9795
);
9896

9997
return (
100-
<AleoWalletProvider wallets={wallets} autoConnect network={Network.MAINNET}>
98+
<AleoWalletProvider wallets={wallets} autoConnect network={Network.TESTNET3}>
10199
<header>
102100
<div className="app">
103101
<h1>Aleo Wallet Example</h1>
104102
<WalletConnectButton />
105103

106104
<WalletInfo />
105+
<ExecuteTransaction />
107106
</div>
108107
</header>
109108
<main>{/* your DApp's components */}</main>

packages/aleo-types/src/transaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface Transaction {
2929
/**
3030
* The transaction fee
3131
*/
32-
fee?: string;
32+
fee?: number;
3333

3434
/**
3535
* The transaction data
@@ -59,7 +59,7 @@ export interface TransactionOptions {
5959
/**
6060
* The transaction fee to pay
6161
*/
62-
fee?: string;
62+
fee?: number;
6363

6464
/**
6565
* Record indices to use

packages/aleo-wallet-adaptor-leo/src/LeoWalletAdapter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export class LeoWalletAdapter extends BaseAleoWalletAdapter {
140140
// Call connect and extract address safely
141141
try {
142142
await this._leoWallet?.connect(DecryptPermission.NoDecrypt, network);
143+
this._network = network;
143144
} catch (error: unknown) {
144145
if (
145146
error instanceof Object &&
@@ -235,8 +236,8 @@ export class LeoWalletAdapter extends BaseAleoWalletAdapter {
235236
const requestData = {
236237
address: this._publicKey,
237238
chainId: this._network,
238-
fee: options.fee ? parseFloat(options.fee) : 0.001,
239-
feePrivate: true,
239+
fee: options.fee ? options.fee : 0.001,
240+
feePrivate: false,
240241
transitions: [
241242
{
242243
program: options.program,

packages/aleo-wallet-adaptor-puzzle/src/PuzzleWalletAdapter.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class PuzzleWalletAdapter extends BaseAleoWalletAdapter {
105105
/**
106106
* Current network
107107
*/
108-
private _network: Network | undefined;
108+
private _network: PuzzleNetwork | undefined;
109109

110110
/**
111111
* Public key
@@ -173,7 +173,8 @@ export class PuzzleWalletAdapter extends BaseAleoWalletAdapter {
173173
throw new WalletConnectionError('Invalid response from wallet');
174174
}
175175

176-
this._network = network;
176+
this._network =
177+
network === Network.MAINNET ? PuzzleNetwork.AleoMainnet : PuzzleNetwork.AleoTestnet;
177178

178179
const address = (response as { connection: { address: string } }).connection?.address;
179180

@@ -252,7 +253,7 @@ export class PuzzleWalletAdapter extends BaseAleoWalletAdapter {
252253
}
253254

254255
try {
255-
const fee = options.fee ? parseFloat(options.fee) / 1000000 : 0.001;
256+
const fee = options.fee ? options.fee / 1000000 : 0.001;
256257

257258
const requestData = {
258259
type: EventType.Execute,
@@ -261,7 +262,7 @@ export class PuzzleWalletAdapter extends BaseAleoWalletAdapter {
261262
fee,
262263
inputs: options.inputs,
263264
address: this._publicKey,
264-
network: this._network as unknown as PuzzleNetwork,
265+
network: this._network,
265266
};
266267

267268
const result = await requestCreateEvent(requestData);

packages/aleo-wallet-adaptor-react/src/WalletProvider.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useEffect, useState, ReactNode } from 'react';
22
import type { WalletAdapter } from '@provablehq/aleo-wallet-standard';
3-
import type { Account, Network } from '@provablehq/aleo-types';
3+
import type { Account, Network, TransactionOptions } from '@provablehq/aleo-types';
44
import { WalletReadyState } from '@provablehq/aleo-wallet-standard';
55
import { WalletContext } from './context';
66

@@ -88,6 +88,12 @@ export const AleoWalletProvider: React.FC<{
8888
setWallet(null);
8989
};
9090

91+
const executeTransaction = async (options: TransactionOptions) => {
92+
if (!wallet) throw new Error('No wallet selected');
93+
if (!connected) throw new Error('No wallet connected');
94+
return await wallet.executeTransaction(options);
95+
};
96+
9197
return (
9298
<WalletContext.Provider
9399
value={{
@@ -99,6 +105,7 @@ export const AleoWalletProvider: React.FC<{
99105
selectWallet,
100106
connect,
101107
disconnect,
108+
executeTransaction,
102109
}}
103110
>
104111
{children}

packages/aleo-wallet-adaptor-react/src/context.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createContext, useContext } from 'react';
22
import { WalletAdapter } from '@provablehq/aleo-wallet-standard';
3-
import { Account } from '@provablehq/aleo-types';
3+
import { Account, Transaction, TransactionOptions } from '@provablehq/aleo-types';
44

55
/**
66
* Wallet context state
@@ -46,6 +46,11 @@ export interface WalletContextState {
4646
* Disconnect from the connected wallet
4747
*/
4848
disconnect: () => Promise<void>;
49+
50+
/**
51+
* Execute a transaction
52+
*/
53+
executeTransaction: (options: TransactionOptions) => Promise<Transaction>;
4954
}
5055

5156
/**

0 commit comments

Comments
 (0)