geth --mine --datadir eth-data --networkid 123 --nodiscover --maxpeers 0 console 2>>geth.log
To verify that it's mining,
execute this command
to see the current block. Then wait 30 seconds and
execute it again.
eth.blockNumber
The block number should increase,
as shown below.
primary = eth.accounts[0]
personal.unlockAccount(primary)
Enter your password.
After a pause of several seconds, the
response is "true",
as shown below.
The owner owns the object being auctioned.
The placeBid() method is called with an amount of Ether. It does nothing unless the bid is larger than the leading bid; if it is, that bid becomes the leader, and the previous bid's Ether is returned.
Only the owner can cann endAuction(), and then the owner collects the Ether from the winning bid.
contract Auction {
event newBid();
address owner;
address public leader;
address public winner;
string public item;
uint public leadingBid;
function Auction(string name, uint price) {
owner = msg.sender;
item = name;
leadingBid = price;
}
function placeBid() {
if (msg.value > leadingBid) {
returnPrevBid();
leader = msg.sender;
leadingBid = msg.value;
newBid();
}
}
function returnPrevBid() {
if (leader != 0) {
leader.send(leadingBid);
}
}
function endAuction() {
if (msg.sender == owner) {
winner = leader;
owner.send(leadingBid);
}
}
}
var auctionSource = 'contract Auction { event newBid(); address owner; address public leader; address public winner; string public item; uint public leadingBid; function Auction(string name, uint price) { owner = msg.sender; item = name; leadingBid = price; } function placeBid() { if (msg.value > leadingBid) { returnPrevBid(); leader = msg.sender; leadingBid = msg.value; newBid(); } } function returnPrevBid() { if (leader != 0) { leader.send(leadingBid); } } function endAuction() { if (msg.sender == owner) { winner = leader; owner.send(leadingBid); } }}'
Only the end of the code is visible, and the reply
is "undefined",
as shown below.
To see the source, execute this command:
auctionSource
The source code appears,
as shown below.
var auctionCompiled = eth.compile.solidity(auctionSource)
The reply
is "undefined" again,
as shown below.
To see the compiled code, execute this command:
auctionCompiled
The "auctionCompiled" object prints
out, as shown below. The attribute
auctionCompiled.Auction.code contains a
large blob of hexadecimal data, and the
other sections like auctionCompiled.info
and auctionCompiled.info.source are more
readable.
var auctionContract = web3.eth.contract(auctionCompiled.Auction.info.abiDefinition);
function Auction(name, price) {
return auctionContract.new(
name,
price,
{
from:web3.eth.accounts[0],
data:auctionCompiled.Auction.code,
gas: 3000000
}, function(e, contract){
console.log(e, contract);
if (typeof contract.address != 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
}
The reply
is "undefined" again,
as shown below.
To see the autionContract, execute this command:
auctionContract
The contract is 2 or 3 screens long, ending
in a long list of functions,
as shown below.
var myAuction = Auction("cat", web3.toWei(0.5, "ether"))
After 30 seconds, the contract should
be mined,
as shown below.
myAuction
You see the structure of the myAuction
object, including functions such as
"leader" and "item", as
shown below.
To see the current status of the auction, execute these commands:
myAuction.item()
myAuction.leadingBid()
myAuction.leader()
The item is "cat", and the leader is all zeroes,
as shown below.
The leading bid is 500,000,000,000,000,000 in "wei" units, which is 0.5 Ether. Ethereum uses far too many units, as shown below.
To see the leading bid in Ether, execute this command:
web3.fromWei(myAuction.leadingBid())
The bid appears in a more readable
form,
as shown below.
In the geth console, execute this command.
var event = myAuction.newBid(function(error, result){
if (!error)
console.log("New bid placed for " + web3.fromWei(myAuction.leadingBid(),"ether") + " Ether from " + myAuction.leader());
});
The reply is "undefined",
as shown below.
myAuction.placeBid({from: personal.listAccounts[0], value: web3.toWei(0.6, "ether")})
The transaction ID appears, and
within 30 seconds, the watcher
prints out the new bid amount and
account number,
as shown below.
personal.newAccount()
Enter a password twice. Your new account's
address appears, as shown below.
personal.listAccounts
You now have two or more accounts,
as shown below.
eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value: web3.toWei(5, "ether")})
A transaction ID appears,
as shown below.
personal.unlockAccount(eth.accounts[1])
Enter your password.
After a pause of several seconds, the
response is "true",
as shown below.
web3.fromWei(eth.getBalance(personal.listAccounts[1]), "ether")
myAuction.placeBid({from: personal.listAccounts[1], value: web3.toWei(2, "ether")})
web3.fromWei(eth.getBalance(personal.listAccounts[1]), "ether")
The transaction ID appears,
but the account balance does not fall
immediately, as shown below.
After the transaction is mined, repeat this command to see the reduced balance:
web3.fromWei(eth.getBalance(personal.listAccounts[1]), "ether")
personal.unlockAccount(eth.accounts[0])
Enter your password.
After a pause of several seconds, the
response is "true",
as shown below.
myAuction.endAuction({from: personal.listAccounts[0]})
myAuction.winner()