1
1
import {
2
2
Account ,
3
- initThreadPool ,
4
- PrivateKey ,
5
3
ProgramManager ,
6
- } from "@aleohq/sdk" ;
7
-
8
- await initThreadPool ( ) ;
9
-
10
- const hello_hello_program = `
11
- program hello_hello.aleo;
12
-
13
- function hello:
14
- input r0 as u32.public;
15
- input r1 as u32.private;
16
- add r0 r1 into r2;
17
- output r2 as u32.private;`
18
-
19
- async function localProgramExecution ( program , aleoFunction , inputs ) {
20
- const programManager = new ProgramManager ( ) ;
21
-
22
- // Create a temporary account for the execution of the program
23
- const account = new Account ( ) ;
24
- programManager . setAccount ( account ) ;
25
-
26
- const executionResponse = await programManager . run (
27
- program ,
28
- aleoFunction ,
29
- inputs ,
30
- false ,
4
+ PrivateKey ,
5
+ initThreadPool ,
6
+ AleoKeyProvider ,
7
+ AleoNetworkClient ,
8
+ NetworkRecordProvider ,
9
+ } from "@aleohq/sdk" ;
10
+
11
+ await initThreadPool ( ) ;
12
+
13
+ const hello_hello_program =
14
+ "program hello_hello_123.aleo;\n" +
15
+ "\n" +
16
+ "function hello:\n" +
17
+ " input r0 as u32.public;\n" +
18
+ " input r1 as u32.private;\n" +
19
+ " add r0 r1 into r2;\n" +
20
+ " output r2 as u32.private;\n" ;
21
+
22
+ async function localProgramExecution ( program , aleoFunction , inputs ) {
23
+ const programManager = new ProgramManager ( ) ;
24
+
25
+ // Create a temporary account for the execution of the program
26
+ const account = new Account ( ) ;
27
+ programManager . setAccount ( account ) ;
28
+
29
+ const executionResponse = await programManager . run (
30
+ hello_hello_program ,
31
+ "hello" ,
32
+ [ "5u32" , "5u32" ] ,
33
+ false ,
34
+ ) ;
35
+ return executionResponse . getOutputs ( ) ;
36
+ }
37
+
38
+ function getPrivateKey ( ) {
39
+ return new PrivateKey ( ) . to_string ( ) ;
40
+ }
41
+
42
+ async function deployProgram ( program ) {
43
+ const keyProvider = new AleoKeyProvider ( ) ;
44
+ keyProvider . useCache ( true ) ;
45
+
46
+ // Create a record provider that will be used to find records and transaction data for Aleo programs
47
+ const networkClient = new AleoNetworkClient ( "https://api.explorer.aleo.org/v1" ) ;
48
+
49
+ // Use existing account with funds
50
+ const account = new Account ( {
51
+ privateKey : "user1PrivateKey" ,
52
+ } ) ;
53
+
54
+ const recordProvider = new NetworkRecordProvider ( account , networkClient ) ;
55
+
56
+ // Initialize a program manager to talk to the Aleo network with the configured key and record providers
57
+ const programManager = new ProgramManager (
58
+ "https://api.explorer.aleo.org/v1" ,
59
+ keyProvider ,
60
+ recordProvider ,
31
61
) ;
32
- return executionResponse . getOutputs ( ) ;
33
- }
34
-
35
- function getPrivateKey ( ) {
36
- return new PrivateKey ( ) . to_string ( ) ;
37
- }
38
-
39
- onmessage = async function ( e ) {
40
- if ( e . data === "execute" ) {
41
- const result = await localProgramExecution ( hello_hello_program , "hello" , [ "5u32" , "5u32" ] ) ;
42
- postMessage ( result ) ;
43
- } else if ( e . data === "key" ) {
44
- const result = getPrivateKey ( ) ;
45
- postMessage ( result ) ;
62
+
63
+ programManager . setAccount ( account ) ;
64
+
65
+ // Define a fee to pay to deploy the program
66
+ const fee = 1.9 ; // 1.9 Aleo credits
67
+
68
+ // Deploy the program to the Aleo network
69
+ const tx_id = await programManager . deploy ( program , fee ) ;
70
+
71
+ // Optional: Pass in fee record manually to avoid long scan times
72
+ // const feeRecord = "{ owner: aleo1xxx...xxx.private, microcredits: 2000000u64.private, _nonce: 123...789group.public}";
73
+ // const tx_id = await programManager.deploy(program, fee, undefined, feeRecord);
74
+
75
+ return tx_id ;
76
+ }
77
+
78
+ onmessage = async function ( e ) {
79
+ if ( e . data === "execute" ) {
80
+ const result = await localProgramExecution ( ) ;
81
+ postMessage ( result ) ;
82
+ } else if ( e . data === "key" ) {
83
+ const result = getPrivateKey ( ) ;
84
+ postMessage ( result ) ;
85
+ } else if ( e . data === "deploy" ) {
86
+ deployProgram ( hello_hello_program ) . then ( result => {
87
+ postMessage ( result ) ;
88
+ } ) . catch ( error => {
89
+ postMessage ( { error : error . message } ) ;
90
+ } ) ;
46
91
}
47
- } ;
92
+
93
+ } ;
94
+
0 commit comments