Use web3.js - second-state/oasis-ssvm-runtime GitHub Wiki
index.js
const Web3 = require('web3');
const HDWalletProvider = require('truffle-hdwallet-provider');
// 0xB8b3666d8fEa887D97Ab54f571B8E5020c5c8b58
const MNEMONIC = 'range drive remove bleak mule satisfy mandate east lion minimum unfold ready';
const URL = 'http://localhost:8545';
const provider = new HDWalletProvider(MNEMONIC, URL);
const web3 = new Web3(provider);
web3.eth.net.isListening()
.then(() => console.log('Web3 is connected.'))
.catch(e => console.log('Something went wrong.'));
let accounts = [];
web3.eth.getAccounts()
.then((r) => {
accounts = r;
console.log(`accounts: ${JSON.stringify(accounts)}`);
return web3.eth.getBalance(accounts[0]);
})
.then((balance) => {
console.log(`web3.eth.getBalance(${accounts[0]}) = ${balance}`);
let tx = {
from: accounts[0],
to: '0x0000000000000000000000000000000000000001',
value: 1,
};
return web3.eth.sendTransaction(tx);
})
.then((receipt) => {
console.log(`web3.eth.sendTransaction(...) = ${JSON.stringify(receipt, null, 2)}`);
return web3.eth.getBalance(accounts[0]);
})
.then((balance) => {
console.log(`web3.eth.getBalance(${accounts[0]}) = ${balance}`);
})
.catch((err) => {
console.log(`Error:`);
console.log(err);
});
- Command
$ oasis chain
$ npm install web3 truffle-hdwallet-provider
$ node index.js
accounts: ["0xB8b3666d8fEa887D97Ab54f571B8E5020c5c8b58"]
web3.eth.getBalance(0xB8b3666d8fEa887D97Ab54f571B8E5020c5c8b58) = 100000000000000000000
Web3 is connected.
web3.eth.sendTransaction(...) = {
"blockHash": "0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6",
"blockNumber": 1,
"contractAddress": null,
"cumulativeGasUsed": 5100,
"gasUsed": 5100,
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"root": null,
"status": true,
"transactionHash": "0xc93f37cbdcde888c59478b47ecbc8e22dfb8da83f5d31c1218878825a62ad388",
"transactionIndex": 0
}
web3.eth.getBalance(0xB8b3666d8fEa887D97Ab54f571B8E5020c5c8b58) = 99999994899999999999
const Web3 = require("web3");
const URL = 'http://localhost:8545';
const HDWalletProvider = require('truffle-hdwallet-provider');
const privateKeys = ['c61675c22aee77da8f6e19444ece45557dc80e1482aa848f541e94e3e5d91179', 'b5144c6bda090723de712e52b92b4c758d78348ddce9aa80ca8ef51125bfb308', '7ec6102f6a2786c03b3daf6ac4772491f33925902326a0d2d83521b964a87402'];
const provider = new HDWalletProvider(privateKeys, URL, 0, 3);
let abi = [
{
"inputs": [],
"name": "retreive",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "num",
"type": "uint256"
}
],
"name": "store",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
let bytecode = "0x608060405234801561001057600080fd5b5060c78061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060325760003560e01c80636057361d146037578063b05784b8146062575b600080fd5b606060048036036020811015604b57600080fd5b8101908080359060200190929190505050607e565b005b60686088565b6040518082815260200191505060405180910390f35b8060008190555050565b6000805490509056fea26469706673582212208dea039245bf78c381278382d7056eef5083f7d243d8958817ef447e0a403bd064736f6c63430006060033"
const web3 = new Web3(provider);
(async () => {
await web3.eth.net.isListening();
console.log('Web3 is connected.');
let accounts = await web3.eth.getAccounts();
console.log(`accounts: ${JSON.stringify(accounts)}`);
let balance = await web3.eth.getBalance(accounts[0]);
console.log(`web3.eth.getBalance(${accounts[0]}) = ${balance}`);
let tx = {
from: accounts[0],
to: '0x0000000000000000000000000000000000000000',
value: 1,
};
let receipt = await web3.eth.sendTransaction(tx);
console.log(`web3.eth.sendTransaction(...) = ${JSON.stringify(receipt, null, 2)}`);
balance = await web3.eth.getBalance(accounts[0]);
console.log(`web3.eth.getBalance(${accounts[0]}) = ${balance}`);
const contract = new web3.eth.Contract(abi);
const deployment = await contract.deploy({
data: bytecode,
}).send({
from: accounts[0],
}).on('error', (err) => {
console.log(`Deploy failed with error: ${err}`)
});
contract.options.address = deployment.options.address;
console.log(`Factory deployed successfully, at address: ${deployment.options.address}`);
await contract.methods.store(98).send({ from: accounts[0] });
let r = await contract.methods.retreive().call();
console.log(`r = ${JSON.stringify(r, null, 2)}`);
await provider.engine.stop();
})();
上面用的 sol 是:
pragma solidity >=0.4.22 <0.7.0;
/**
* @title Storage
* @dev Store & retreive value in a variable
*/
contract Storage {
uint256 number;
/**
* @dev Store value in variable
* @param num value to store
*/
function store(uint256 num) public {
number = num;
}
/**
* @dev Return value
* @return value of 'number'
*/
function retreive() public view returns (uint256){
return number;
}
}