Skip to content

Instantly share code, notes, and snippets.

@uzyn
Last active April 16, 2018 17:13
Show Gist options
  • Save uzyn/bdcf7c5881f6ddb590c4ed230187f0a2 to your computer and use it in GitHub Desktop.
Save uzyn/bdcf7c5881f6ddb590c4ed230187f0a2 to your computer and use it in GitHub Desktop.
# After deploying the contract
mkdir sgdt
cd sgdt
yarn add web3 # or npm install web3
node
# Now you can run web3-commands.js
pragma solidity ^0.4.2;
contract SGDT {
/* Public variables of the token */
string public name = 'Singapore Dollar Token';
string public symbol = 'SGDT';
uint8 public decimals = 2;
uint public totalSupply = 0;
address public authority;
/* This creates an array with all balances */
mapping (address => uint) public balanceOf;
mapping (address => uint) public issuedBy;
mapping (address => bool) public isIssuer;
/* This generates a public event on the blockchain that will notify clients */
event Transfer(address from, address to, uint value);
event TokenIssued(address by);
modifier onlyIssuer {
if (isIssuer[msg.sender] != true) {
throw;
}
_;
}
modifier onlyAuthority {
if (msg.sender != authority) {
throw;
}
_;
}
/* Constructor: initializes contract with initial supply tokens to the creator of the contract */
function SGDT() {
authority = msg.sender;
isIssuer[msg.sender] = true;
}
function addIssuer(address _newIssuer) public onlyAuthority {
isIssuer[_newIssuer] = true;
}
// TODO: removeIssuer()
/* Issue SGT */
function issue(uint _value) public onlyIssuer {
issuedBy[msg.sender] += _value;
balanceOf[msg.sender] += _value;
totalSupply += _value;
TokenIssued(msg.sender);
}
/** Remove SGDT from blockchain */
function redeem(uint _value) public onlyIssuer {
if (balanceOf[msg.sender] < _value || issuedBy[msg.sender] < _value) {
throw;
}
issuedBy[msg.sender] -= _value;
balanceOf[msg.sender] -= _value;
totalSupply -= _value;
TokenIssued(msg.sender);
}
/* Send coins */
function transfer(address _to, uint _value) public {
if (balanceOf[msg.sender] < _value) {
throw; // Check if the sender has enough
}
balanceOf[msg.sender] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
Transfer(msg.sender, _to, _value); // Notify anyone listening that this transfer took place
}
/* This unnamed function is called whenever someone tries to send ether to it */
function () {
throw; // Prevents accidental sending of ether
}
}
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
// If you would like to know how much gas it costs, without committing yet
SGDT.addIssuer.estimateGas(web3.eth.accounts[1], {
from: authority,
gas: 1000000
});
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