Skip to content

Instantly share code, notes, and snippets.

@lingxiao
Created January 28, 2018 20:46
Show Gist options
  • Save lingxiao/63789a67e2dcc2c65c5779b55b8636d7 to your computer and use it in GitHub Desktop.
Save lingxiao/63789a67e2dcc2c65c5779b55b8636d7 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.2;
contract Hello{
mapping (address => uint) public coinBalanceOf;
event CoinTransfer(address sender, address receiver, uint amount);
function hello(uint supply){
coinBalanceOf[msg.sender] = supply;
}
function sendCoin(address receiver, uint amount) returns (bool sufficient)
{
if (coinBalanceOf[msg.sender] < amount) return false;
coinBalanceOf[msg.sender] -= amount;
coinBalanceOf[receiver] += amount;
CoinTransfer(msg.sender, receiver, amount);
return true;
}
}
/***************** in javascript ******************************
web3.eth.getAccounts().then(accounts => {
var coinbase = accounts[0];
var receiver = accounts[1];
// create contract
var myContract = new web3.eth.Contract(abi_, coinbase,
{
from : coinbase,
gasPrice: "2000000"
});
var deployedContract = myContract.deploy({
data: '0x' + bytecode,
}).send({
from: coinbase,
gas : 1500000 ,
gasPrice: '30000000000000'
}, (err, hash) => {
if (err) { console.log("error on deployment: ", err) }
console.log("Hash: ", hash)
})
// send contract fn to network to be executed
// it seems like I cannot access the value
// now see if I can mutate the value
myContract.methods.sendCoin(receiver, 70000000000).send({ from: coinbase },
(err,val) => {
if (err) { console.log(err) }
else {
console.log("sent coin: ", val)
}
})
.then(console.log)
}
*************************************** return value **************************************
{ blockHash: '0x7b3530ed33700bf48249799474a54801a1881ebf791d10eb6089ef092e59c229',
blockNumber: 4465,
contractAddress: null,
cumulativeGasUsed: 282997,
from: '0x449021b2219186745762b49cac0158107ad3e5ce',
gasUsed: 23000,
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
root: '0x379cd0b315c40d801c02684d304676735805a3aa42ee800a86d9ea09f358965f',
to: '0x449021b2219186745762b49cac0158107ad3e5ce',
transactionHash: '0xd7f0e2c15f079442ce614bd182b48cc855fee588cd0c989044b701cad820a2d9',
transactionIndex: 1,
events: {} }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment