@avm-sdk/avmnet-cli 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ const startRPC = require("../avm/rpc");
3
+
4
+ const chainId = 1337;
5
+ const { rpcUrl } = startRPC(chainId);
6
+
7
+ console.log("Local blockchain started!");
8
+ console.log("RPC URL:", rpcUrl);
9
+ console.log("CHAIN ID:", chainId);
package/cli/avm-run.js ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env node
2
+ const path = require("path");
3
+ const AVM = require("../avm/vm");
4
+ const accounts = require("../avm/state");
5
+ const startRPC = require("../avm/rpc");
6
+
7
+ const args = process.argv.slice(2);
8
+ if(args[0] === "run") {
9
+ const file = args[1];
10
+ if(!file) return console.log("Usage: npx avm-run run <program.js>");
11
+
12
+ const programPath = path.resolve(file);
13
+ const program = require(programPath);
14
+
15
+ const rpc = startRPC(1337);
16
+ const vm = new AVM(accounts, rpc);
17
+
18
+ program(vm).then(() => console.log("Program executed!", accounts));
19
+ } else {
20
+ console.log("Usage: npx avm-run run <program.js>");
21
+ }
package/cli/avm-sdk.js ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env node
2
+ const fs = require("fs");
3
+ const path = require("path");
4
+
5
+ const args = process.argv.slice(2);
6
+ if (args[0] === "init") {
7
+ const projectName = args[1] || "my-avm-project";
8
+ const projectPath = path.join(process.cwd(), projectName);
9
+
10
+ if (!fs.existsSync(projectPath)) {
11
+ fs.mkdirSync(projectPath);
12
+ fs.mkdirSync(path.join(projectPath, "avm"));
13
+ fs.mkdirSync(path.join(projectPath, "programs"));
14
+
15
+ fs.writeFileSync(
16
+ path.join(projectPath, "package.json"),
17
+ JSON.stringify({
18
+ name: projectName,
19
+ version: "1.0.0",
20
+ main: "index.js",
21
+ scripts: { start: "node index.js" },
22
+ dependencies: {}
23
+ }, null, 2)
24
+ );
25
+
26
+ fs.writeFileSync(
27
+ path.join(projectPath, "index.js"),
28
+ `const { AVM, accounts, rpc } = require("avm-sdk");
29
+ console.log("Project initialized. Start coding your AVM programs in JS!");`
30
+ );
31
+
32
+ console.log(`Project ${projectName} created!`);
33
+ } else console.log(`Directory ${projectName} already exists.`);
34
+ } else console.log("Usage: npx avm-sdk init <project-name>");
package/hello/index.js ADDED
@@ -0,0 +1,2 @@
1
+ const { AVM, accounts, rpc } = require("avm-sdk");
2
+ console.log("Project initialized. Start coding your AVM programs in JS!");
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "hello",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "start": "node index.js"
7
+ },
8
+ "dependencies": {}
9
+ }
package/index.js ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env node
2
+
3
+ const args = process.argv.slice(2);
4
+
5
+ if (args[0] === "node") {
6
+ require("./rpc"); // runs your rpc server
7
+ } else {
8
+ console.log("AVM CLI");
9
+ console.log("Use:");
10
+ console.log(" avm node");
11
+ }
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@avm-sdk/avmnet-cli",
3
+ "version": "1.0.0",
4
+ "description": "AVMNet Blockchain CLI",
5
+ "main": "vm.js",
6
+ "bin": {
7
+ "avmnet": "./index.js"
8
+ },
9
+ "type": "commonjs",
10
+ "license": "MIT"
11
+ }
@@ -0,0 +1,7 @@
1
+ module.exports = async (vm) => {
2
+ await vm.push(100);
3
+ await vm.store("gold", "ALICE");
4
+ await vm.load("gold", "ALICE");
5
+ console.log("ALICE gold:", vm.stack.pop());
6
+ await vm.transfer("ALICE", "BOB", 50);
7
+ };
package/rpc.js ADDED
@@ -0,0 +1,17 @@
1
+ const express = require("express");
2
+
3
+ function startRPC(chainId = 1337) {
4
+ const app = express();
5
+ const port = 8545;
6
+ const rpcUrl = `http://127.0.0.1:${port}`;
7
+
8
+ app.get("/", (req, res) => {
9
+ res.json({ chainId, rpcUrl });
10
+ });
11
+
12
+ app.listen(port, () => console.log(`RPC running at ${rpcUrl}, Chain ID: ${chainId}`));
13
+
14
+ return { chainId, rpcUrl };
15
+ }
16
+
17
+ module.exports = startRPC;
package/state.js ADDED
@@ -0,0 +1,6 @@
1
+ const accounts = {
2
+ ALICE: { balance: 1000, storage: {} },
3
+ BOB: { balance: 500, storage: {} },
4
+ };
5
+
6
+ module.exports = accounts;
package/vm.js ADDED
@@ -0,0 +1,42 @@
1
+ class AVM {
2
+ constructor(accounts = {}, rpc = {}) {
3
+ this.stack = [];
4
+ this.memory = {};
5
+ this.accounts = accounts;
6
+ this.rpc = rpc;
7
+ }
8
+
9
+ async push(value) {
10
+ this.stack.push(value);
11
+ }
12
+
13
+ async pop() {
14
+ return this.stack.pop();
15
+ }
16
+
17
+ async add() {
18
+ const a = this.stack.pop();
19
+ const b = this.stack.pop();
20
+ this.stack.push(a + b);
21
+ }
22
+
23
+ async store(key, account) {
24
+ if (!this.accounts[account]) throw new Error("Account not found");
25
+ this.accounts[account].storage[key] = this.stack.pop();
26
+ }
27
+
28
+ async load(key, account) {
29
+ if (!this.accounts[account]) throw new Error("Account not found");
30
+ this.stack.push(this.accounts[account].storage[key]);
31
+ return this.accounts[account].storage[key];
32
+ }
33
+
34
+ async transfer(from, to, amount) {
35
+ if (!this.accounts[from] || !this.accounts[to])
36
+ throw new Error("Account not found");
37
+ this.accounts[from].balance -= amount;
38
+ this.accounts[to].balance += amount;
39
+ }
40
+ }
41
+
42
+ module.exports = AVM;