Skip to content

Instantly share code, notes, and snippets.

@uzyn
Created November 15, 2016 03:09
Show Gist options
  • Save uzyn/adddceffbd1441914a8677ad2613b7fd to your computer and use it in GitHub Desktop.
Save uzyn/adddceffbd1441914a8677ad2613b7fd to your computer and use it in GitHub Desktop.
const Web3 = require('web3');
const web3 = new Web3(
new Web3.providers.HttpProvider(
'http://localhost:8545'
)
);
web3;
web3.eth;
web3.eth.accounts;
// Other interesting commands
web3.eth.blockNumber;
web3.eth.getBalance(web3.eth.accounts[0]).toNumber();
web3.eth.getBalance(web3.eth.accounts[3]).toNumber(); // 1 ether = 1e18 wei
web3.fromWei(web3.eth.getBalance(web3.eth.accounts[3]), 'ether').toNumber();
web3.eth.gasPrice;
web3.fromWei(web3.eth.gasPrice, 'szabo').toNumber(); // 0.02 szabo or 20 Gwei (20e9 wei)
web3.fromWei(web3.eth.gasPrice, 'ether').toNumber(); // 2e-8 ether
let address = 'ADDRESS';
let abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_newIssuer","type":"address"}],"name":"addIssuer","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"issuedBy","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isIssuer","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"issue","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"redeem","outputs":[],"payable":false,"type":"function"},{"inputs":[],"type":"constructor"},{"payable":false,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"by","type":"address"}],"name":"TokenIssued","type":"event"}];
let SGDT = web3.eth.contract(abi).at(address);
SGDT;
SGDT.authority(); // Read some static values
SGDT.totalSupply(); // BigNumber.js Object
SGDT.totalSupply.toNumber();
let authority = SGDT.authority();
SGDT.isIssuer(web3.eth.accounts[1]); // false
SGDT.addIssuer(web3.eth.accounts[1], {
from: authority,
gas: 1000000
});
SGDT.isIssuer(web3.eth.accounts[1]); // true
SGDT.balanceOf(web3.eth.accounts[1]).toNumber(); // 0
SGDT.issue(1000, {
from: web3.eth.accounts[1],
gas: 1000000
});
SGDT.balanceOf(web3.eth.accounts[1]).toNumber(); // 1000 ($10.00)
SGDT.balanceOf(web3.eth.accounts[2]).toNumber(); // 0
SGDT.transfer(web3.eth.accounts[2], 499, {
from: web3.eth.accounts[1],
gas: 1000000,
});
SGDT.balanceOf(web3.eth.accounts[2]).toNumber(); // 499 ($4.99)
SGDT.balanceOf(web3.eth.accounts[1]).toNumber(); // 501 ($5.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment