subspace/packages/core/test/test3.js

129 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-08-19 19:34:22 +00:00
const { map, scan, last, distinctUntilChanged } = require('rxjs/operators');
2020-03-04 18:48:08 +00:00
const Web3 = require('web3');
const Subspace = require('../dist/index.js').default;
2019-08-19 19:34:22 +00:00
2020-03-04 18:48:08 +00:00
let web3 = new Web3("ws://localhost:8545");
2019-08-19 19:34:22 +00:00
let myscan = scan((acc, curr) => {
acc.push(curr);
return acc;
}, [])
let mymap = map(arr => arr.reduce((acc, current) => acc + current, 0) / arr.length)
async function deployContract() {
2020-03-04 18:48:08 +00:00
let accounts = await web3.eth.getAccounts();
2019-08-19 19:34:22 +00:00
// pragma solidity ^0.5.0;
// contract SimpleStorage {
// mapping(uint => uint) storeMap;
2019-08-19 19:34:22 +00:00
// event Test(uint indexed value);
// constructor() public {
2019-08-19 19:34:22 +00:00
// }
// function set(uint x, uint y) public {
// storeMap[x] = y;
2019-08-19 19:34:22 +00:00
// }
// function get(uint x) public view returns (uint, address) {
// return (storeMap[x], msg.sender);
2019-08-19 19:34:22 +00:00
// }
// }
let abi = [
{
"constant": false,
"inputs": [
{
"name": "x",
"type": "uint256"
},
{
"name": "y",
"type": "uint256"
2019-08-19 19:34:22 +00:00
}
],
"name": "set",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
2019-08-19 19:34:22 +00:00
{
"name": "x",
2019-08-19 19:34:22 +00:00
"type": "uint256"
}
],
"name": "get",
"outputs": [
2019-08-19 19:34:22 +00:00
{
"name": "",
2019-08-19 19:34:22 +00:00
"type": "uint256"
},
{
"name": "",
"type": "address"
2019-08-19 19:34:22 +00:00
}
],
"payable": false,
"stateMutability": "view",
2019-08-19 19:34:22 +00:00
"type": "function"
},
{
"inputs": [],
2019-08-19 19:34:22 +00:00
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "value",
"type": "uint256"
}
],
"name": "Test",
"type": "event"
}
]
2020-03-04 18:48:08 +00:00
var contract = new web3.eth.Contract(abi)
2019-08-19 19:34:22 +00:00
let instance = await contract.deploy({
data: '0x608060405234801561001057600080fd5b5061017c806100206000396000f3fe608060405260043610610046576000357c0100000000000000000000000000000000000000000000000000000000900480631ab06ee51461004b5780639507d39a14610090575b600080fd5b34801561005757600080fd5b5061008e6004803603604081101561006e57600080fd5b810190808035906020019092919080359060200190929190505050610112565b005b34801561009c57600080fd5b506100c9600480360360208110156100b357600080fd5b810190808035906020019092919050505061012d565b604051808381526020018273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019250505060405180910390f35b80600080848152602001908152602001600020819055505050565b60008060008084815260200190815260200160002054338090509150915091509156fea165627a7a7230582054f962728d2f5acbfde67516616e1b90e3aca5df27a213cbbabc16387d9af7d90029',
arguments: []
2019-08-19 19:34:22 +00:00
}).send({
from: accounts[0],
gas: '4700000'
})
return instance
}
async function run() {
2020-03-04 18:48:08 +00:00
let accounts = await web3.eth.getAccounts();
2019-08-19 19:34:22 +00:00
var SimpleStorageContract = await deployContract()
2020-03-04 18:48:08 +00:00
const subspace = new Subspace(web3);
await subspace.init();
await SimpleStorageContract.methods.set(2, 500).send({ from: accounts[0], gas: 4700000 })
2020-03-04 18:48:08 +00:00
subspace.trackProperty(SimpleStorageContract, 'get', [2], {from: "0x0000000000000000000000000000000000000012"} ).subscribe((v) => {
console.dir("value is ")
console.dir(v)
})
2019-08-19 19:34:22 +00:00
setTimeout(async () => {
await SimpleStorageContract.methods.set(2, 200).send({ from: accounts[0] })
await SimpleStorageContract.methods.set(1, 200).send({ from: accounts[0] })
await SimpleStorageContract.methods.set(0, 300).send({ from: accounts[0] })
await SimpleStorageContract.methods.set(2, 300).send({ from: accounts[0] })
await SimpleStorageContract.methods.set(0, 300).send({ from: accounts[0] })
}, 2000);
2019-08-19 19:34:22 +00:00
}
run()