/
simple-demo.js
92 lines (79 loc) · 3.39 KB
/
simple-demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const fs = require('fs');
const path = require('path');
const { LoggerFactory, WarpFactory } = require('warp-contracts');
const { default: ArLocal } = require('arlocal');
(async () => {
let arLocal;
try {
// Set up ArLocal
arLocal = new ArLocal(1985, false);
await arLocal.start();
// Set up Warp client
LoggerFactory.INST.logLevel('error');
const warp = WarpFactory.forLocal(1985);
let wallet;
// note: warp.testing.generateWallet() automatically adds funds to the wallet
({ jwk: wallet } = await warp.testing.generateWallet());
// Deploying contract
const contractSrc = fs.readFileSync(path.join(__dirname, '../contracts/loot/contract.js'), 'utf8');
const initialState = fs.readFileSync(path.join(__dirname, '../contracts/loot/initial-state.json'), 'utf8');
const { contractTxId } = await warp.createContract.deploy({
wallet,
initState: initialState,
src: contractSrc,
});
// note: we need to mine block in ArLocal - so that contract deployment transaction was mined.
await warp.testing.mineBlock();
// Interacting with the contract
const contract = warp.contract(contractTxId).setEvaluationOptions({ allowBigInt: true }).connect(wallet);
// Read state
const { cachedValue } = await contract.readState();
console.log('State before any interactions');
console.dir(cachedValue.state, { depth: null });
// Write interaction
console.log("Sending 'generate' interaction...");
// note: if Warp instance is created with 'forLocal' - the writeInteraction method
// automatically mines a new block - so that you won't have to do it manually in your tests.
// if you want to switch off automatic mining - set evaluationOptions.mineArLocalBlocks to false, e.g.
// contract.setEvaluationOptions({ mineArLocalBlocks: false })
await contract.writeInteraction({ function: 'generate' });
console.log('Interaction has been sent');
// Read state after interaction
const stateAfterInteraction = await contract.readState();
console.log('State after 1 interaction');
console.dir(stateAfterInteraction.cachedValue.state, { depth: null });
// Using generatedAssets contract function
const { result: generatedAssets } = await contract.viewState({
function: 'generatedAssets',
});
const generatedAsset = generatedAssets[0];
console.log(`Generated asset: ${generatedAsset}`);
// Transferring the asset to another address
console.log("Sending 'transfer' interaction...");
await contract.writeInteraction({
function: 'transfer',
data: {
to: 'another-address',
asset: generatedAsset,
},
});
console.log('Interaction has been sent');
// Getting the new owner of the asset
const { result: newOwner } = await contract.viewState({
function: 'getOwner',
data: { asset: generatedAsset },
});
console.log(`New owner of the asset ${generatedAsset}: ${newOwner}`);
// Generating the new asset
console.log("Sending 'generate' interaction...");
await contract.writeInteraction({ function: 'generate' });
console.log('Interaction has been sent');
// Getting the final state
console.log(`Getting final state`);
const finalState = await contract.readState();
console.dir(finalState.cachedValue.state, { depth: null });
} finally {
// Shutting down ArLocal
await arLocal.stop();
}
})();