@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.
- package/cli/avm-node.js +9 -0
- package/cli/avm-run.js +21 -0
- package/cli/avm-sdk.js +34 -0
- package/hello/index.js +2 -0
- package/hello/package.json +9 -0
- package/index.js +11 -0
- package/package.json +11 -0
- package/programs/example.js +7 -0
- package/rpc.js +17 -0
- package/state.js +6 -0
- package/vm.js +42 -0
package/cli/avm-node.js
ADDED
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
package/index.js
ADDED
package/package.json
ADDED
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
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;
|