diff --git a/package.json b/package.json index 0b98557b..5e20aeb7 100644 --- a/package.json +++ b/package.json @@ -125,7 +125,8 @@ "warp-contracts-plugin-deploy": "1.0.8", "warp-contracts-plugin-vm2": "1.0.1", "warp-contracts-plugin-vrf": "^1.0.3", - "ws": "^8.11.0" + "ws": "^8.11.0", + "warp-contracts": "file:lib/cjs" }, "resolutions": { "bn.js": "5.2.1", diff --git a/tools/send-interactions-to-sequencer.ts b/tools/send-interactions-to-sequencer.ts new file mode 100644 index 00000000..d1d30eeb --- /dev/null +++ b/tools/send-interactions-to-sequencer.ts @@ -0,0 +1,88 @@ +import ArLocal from "arlocal"; +import Arweave from "arweave"; +import { EvalStateResult, SortKeyCacheResult, SourceType, WarpFactory, defaultCacheOptions, defaultWarpGwOptions, sleep } from "../src"; +import fs from 'fs'; +import path from 'path'; +import { ArweaveSigner } from "warp-arbundles"; +import { DeployPlugin } from "warp-contracts-plugin-deploy"; + +interface ExampleContractState { + counter: number; +} + +async function main() { + const sequencerUrl = 'http://34.141.17.15:5666'; + // const sequencerUrl = 'http://localhost:5666'; + const arweavePort = 1983; + const arlocal = new ArLocal(arweavePort, false); + + try { + await arlocal.start(); + const arweave = Arweave.init({ + host: 'localhost', + port: arweavePort, + protocol: 'http' + }); + + const cacheOptions = { + ...defaultCacheOptions, + inMemory: true + } + const gatewayOptions = { ...defaultWarpGwOptions, source: SourceType.WARP_SEQUENCER, confirmationStatus: { notCorrupted: true } } + + const warp = WarpFactory + .custom(arweave, cacheOptions, 'custom') + .useWarpGateway(gatewayOptions, cacheOptions) + .build() + .use(new DeployPlugin()) + .useGwUrl(sequencerUrl) + + const contractSrc = fs.readFileSync(path.join(__dirname, '../src/__tests__/integration/data/example-contract.js'), 'utf8'); + const initialState = fs.readFileSync(path.join(__dirname, '../src/__tests__/integration/data/example-contract-state.json'), 'utf8'); + const wallet = (await warp.generateWallet()).jwk; + const { contractTxId } = await warp.deploy({ + wallet: new ArweaveSigner(wallet), + initState: initialState, + src: contractSrc + }); + const contract = warp.contract(contractTxId).setEvaluationOptions({ + sequencerUrl: sequencerUrl, + waitForConfirmation: true + }); + contract.connect(wallet); + + const initCounter = 555; + const numberOfInteractions = 1000; + let counter = initCounter; + let state: SortKeyCacheResult> | undefined; + let contractCounter: number; + while (counter < initCounter + numberOfInteractions) { + try { + const response = await contract.writeInteraction({ function: 'add' }) + console.log('interaction response', response); + counter++; + } catch(e) { + console.error("interaction failed", e); + } + + try { + state = await contract.readState(); + } catch(e) { + console.error("read state failed", e); + state = undefined; + } + if (state) { + contractCounter = state.cachedValue.state.counter + console.log('counter from contract', counter) + if (contractCounter !== counter) { + throw new Error("Invalid counter value. Expected: " + counter + " actual: " + contractCounter); + } + } + await sleep(1000); + } + } finally { + await arlocal.stop(); + } +} + +main().catch((e) => console.error(e)); diff --git a/yarn.lock b/yarn.lock index 9d1e2cc6..7a6cdb96 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7700,6 +7700,9 @@ warp-contracts-plugin-vrf@^1.0.3: "@idena/vrf-js" "^1.0.1" elliptic "^6.5.4" +"warp-contracts@file:lib/cjs": + version "0.0.0" + warp-isomorphic@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/warp-isomorphic/-/warp-isomorphic-1.0.0.tgz#dccccfc046bc6ac77919f8be1024ced1385c70ea"