Last active
June 20, 2022 21:40
-
-
Save namansharma34/edb00774718ebda0b6d77acb3421100a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 }; | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"text": "Hello" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment