Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vanilla Template Update #880

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions create-aleo-app/template-vanilla/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ window.key = () => {
worker.postMessage("key");
};

window.deploy = () => {
worker.postMessage("deploy");
};

document.querySelector("#app").innerHTML = `
<div>
<a href="https://vitejs.dev" target="_blank">
Expand All @@ -35,6 +39,7 @@ document.querySelector("#app").innerHTML = `
<div class="card">
<button onclick="window.execute()">Call Execute Function</button>
<button onclick="window.key()">Get Private Key</button>
<button onclick="window.deploy()">Deploy HelloWorld Program</button>
</div>
<p class="read-the-docs">
Click on the Aleo logo to learn more
Expand Down
56 changes: 52 additions & 4 deletions create-aleo-app/template-vanilla/worker.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {
Account,
initThreadPool,
PrivateKey,
ProgramManager,
Account,
ProgramManager,
PrivateKey,
initThreadPool,
AleoKeyProvider,
AleoNetworkClient,
NetworkRecordProvider,
} from "@aleohq/sdk";

await initThreadPool();

// A program name must be unique. To deploy the program, you should change the name from hello_hello.aleo to something else that is unique.
const hello_hello_program =`
program hello_hello.aleo;

Expand Down Expand Up @@ -36,12 +40,56 @@ function getPrivateKey() {
return new PrivateKey().to_string();
}

async function deployProgram(program) {
const keyProvider = new AleoKeyProvider();
keyProvider.useCache(true);

// Create a record provider that will be used to find records and transaction data for Aleo programs
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");

// Use existing account with funds
const account = new Account({
privateKey: "user1PrivateKey",
});

const recordProvider = new NetworkRecordProvider(account, networkClient);

// Initialize a program manager to talk to the Aleo network with the configured key and record providers
const programManager = new ProgramManager(
"https://api.explorer.aleo.org/v1",
keyProvider,
recordProvider,
);

programManager.setAccount(account);

// Define a fee to pay to deploy the program
const fee = 1.9; // 1.9 Aleo credits

// Deploy the program to the Aleo network
const tx_id = await programManager.deploy(program, fee);

// Optional: Pass in fee record manually to avoid long scan times
// const feeRecord = "{ owner: aleo1xxx...xxx.private, microcredits: 2000000u64.private, _nonce: 123...789group.public}";
// const tx_id = await programManager.deploy(program, fee, undefined, feeRecord);

return tx_id;
}

onmessage = async function (e) {
if (e.data === "execute") {
const result = await localProgramExecution(hello_hello_program, "hello", ["5u32", "5u32"]);
postMessage(result);
} else if (e.data === "key") {
const result = getPrivateKey();
postMessage(result);
} else if (e.data === "deploy") {
try {
const result = await deployProgram(hello_hello_program);
postMessage(result);
} catch (error) {
postMessage({ error: error.message });
}
}

};
Loading