Skip to content

Instantly share code, notes, and snippets.

@namansharma34
Last active June 20, 2022 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save namansharma34/edb00774718ebda0b6d77acb3421100a to your computer and use it in GitHub Desktop.
Save namansharma34/edb00774718ebda0b6d77acb3421100a to your computer and use it in GitHub Desktop.
import Arweave from "arweave";
import { JWKInterface } from "arweave/node/lib/wallet";
import { LoggerFactory, WarpNodeFactory } from "warp-contracts";
import fs from "fs";
import path from "path";
import ArLocal from "arlocal";
(async () => {
const arlocal = new ArLocal(8080);
await arlocal.start();
const arweave = Arweave.init({
host: "localhost",
port: 8080,
protocol: "http",
});
LoggerFactory.INST.logLevel("error");
const warp = WarpNodeFactory.forTesting(arweave);
const wallet = await arweave.wallets.generate();
addFunds(arweave, wallet);
const contractSrc = fs.readFileSync(
path.join(__dirname + "/../dist/hello.js"),
"utf8"
);
const stateFile = JSON.parse(
fs.readFileSync(path.join(__dirname + "/../dist/hello.json"), "utf8")
);
const walletAddress = await arweave.wallets.jwkToAddress(wallet);
const state = {
...stateFile,
...{
owner: walletAddress,
},
};
const contractTxId = await warp.createContract.deploy({
wallet,
initState: JSON.stringify(state),
src: contractSrc,
});
const contract = warp.contract(contractTxId).connect(wallet);
await contract.writeInteraction({
function: "set",
text: "linux",
});
await mineBlock(arweave);
//Here it fails. It shows "ReferenceError: handle is not defined"
const stat = (await contract.readState()).state;
console.log(stat);
await arlocal.stop();
})();
async function addFunds(arweave: Arweave, wallet: JWKInterface) {
const walletAddress = await arweave.wallets.getAddress(wallet);
await arweave.api.get(`/mint/${walletAddress}/1000000000000000`);
}
async function mineBlock(arweave: Arweave) {
await arweave.api.get("mine");
}
//hello.js
"use strict";
(() => {
// contract/hello.ts
function handle(state, action) {
if (action.input.function == "set") {
state.text = action.input.text;
} else {
throw new ContractError("Wrong function called");
}
return { state };
}
})();
{
"text": "Hello"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment