I have a contract for a simple bidding scenario as below:
pragma solidity ^0.4.9;
/* Contract accepting biddings during 30 minutes */
contract SimpleBidding {
address contractOwnersAccount;
uint deadline;
struct Vendor {
string name;
address account;
uint assetBarcode;
uint stockCount;
}
//Vendor[] vendors;
// request information
address requester;
uint requestedAssetBarcode;
// bidding information
uint expectedProposals;
address bestVendor;
uint bestPrice;
//event VendorRegistered(string name, address account, uint assetBarcode, uint stockCount);
event AssetRequested(address requester, uint barcode);
event PriceProposed(address vendor, uint barcode, uint price);
event BiddingFinished(uint barcode, uint price);
event PaymentReceived(address sender, uint amount, uint barcode);
event AssetShipped(uint barcode, uint trackingNumber);
/* Constructor */
function SimpleBidding() {
// set the deadline of the contract as 10 minutes
deadline = now + 30 * 1 minutes;
}
/*
function registerVendor(string name, uint assetBarcode, uint stockCount) {
vendors[vendorsCount++] = Vendor(name, msg.sender, assetBarcode, stockCount);
VendorRegistered(name, msg.sender, assetBarcode, stockCount);
}*/
/* Used by clients to request assets and start bidding. */
function requestAsset(uint barcode) {
// save the requester
requester = msg.sender;
// save the asset barcode
requestedAssetBarcode = barcode;
// create the event
AssetRequested(requester, requestedAssetBarcode);
// initialize the bidding process
expectedProposals = 2;
bestPrice = 999999;
}
/* Used by vendors to propose a price for an asset. */
function proposePrice(uint barcode, uint price) {
// process the proposal
PriceProposed(msg.sender, barcode, price);
if (price < bestPrice) {
bestPrice = price;
bestVendor = msg.sender;
}
// update bidding condition
expectedProposals--;
if (expectedProposals == 0) {
BiddingFinished(requestedAssetBarcode, bestPrice);
}
}
/* The function without name is the default function that is called whenever
anyone sends funds to a contract */
function () public payable {
if (msg.sender == requester) {
uint amount = msg.value;
if (amount == bestPrice) {
// transfer the payment to the vendor
bestVendor.send(amount);
// reduce the asset from the vendor's stock
// ...to be implemented...
// create the event
PaymentReceived(msg.sender, msg.value, requestedAssetBarcode);
// ship the asset
uint trackingNumber = 23235235;
AssetShipped(requestedAssetBarcode, trackingNumber);
}
}
}
/* function getVendor(uint id) constant returns (string name, address acc, uint barcode, uint numAssets) {
var vendor = vendors[id];
name = vendor.name;
acc = vendor.account;
barcode = vendor.assetBarcode;
numAssets = vendor.stockCount;
}*/
modifier afterDeadline() { if (now >= deadline) _; }
/* checks if the time limit has been reached and ends the contract */
function dispose() afterDeadline {
selfdestruct(contractOwnersAccount);
}
}
And I deploy it as below:
loadScript("UnlockAccounts.js")
var simplebidding_sol_simplebiddingContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"barcode","type":"uint256"}],"name":"requestAsset","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"dispose","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"barcode","type":"uint256"},{"name":"price","type":"uint256"}],"name":"proposePrice","outputs":[],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"requester","type":"address"},{"indexed":false,"name":"barcode","type":"uint256"}],"name":"AssetRequested","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"vendor","type":"address"},{"indexed":false,"name":"barcode","type":"uint256"},{"indexed":false,"name":"price","type":"uint256"}],"name":"PriceProposed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"barcode","type":"uint256"},{"indexed":false,"name":"price","type":"uint256"}],"name":"BiddingFinished","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"barcode","type":"uint256"}],"name":"PaymentReceived","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"barcode","type":"uint256"},{"indexed":false,"name":"trackingNumber","type":"uint256"}],"name":"AssetShipped","type":"event"}]);
var simplebidding = simplebidding_sol_simplebiddingContract.new(
{
from: web3.eth.accounts[0],
data: '0x6060604052341561000c57fe5b5b61070842016001555b5b61030e806100266000396000f3006060604052361561003b5763ffffffff60e060020a6000350416632b25d4fd811461012b5780634c86659e14610140578063e161926914610152575b6101295b600254600090819033600160a060020a03908116911614156101235734915060065482141561012357600554604051600160a060020a039091169083156108fc029084906000818181858888f1505060035460408051600160a060020a033316815234602082015280820192909252517f5677b5d4cf976ac32defbd95a6a5aaf0d1fee450a11fc26f3c11aae6e6c33d0694509081900360600192509050a150600354604080519182526301628aa360208301819052815190927fb392290afa7f5b07c58b82f2a4f36a13e77050183671211ed5b5f27ca86b135e92908290030190a15b5b5b5050565b005b341561013357fe5b61012960043561016a565b005b341561014857fe5b6101296101eb565b005b341561015a57fe5b610129600435602435610206565b005b6002805473ffffffffffffffffffffffffffffffffffffffff191633600160a060020a0390811691909117918290556003839055604080519290911682526020820183905280517f182330bdae199ac3ad3e13c76eb719f687f880653f8dee040adc74327eaf3a669281900390910190a16002600455620f423f6006555b50565b600154421061020257600054600160a060020a0316ff5b5b5b565b60408051600160a060020a03331681526020810184905280820183905290517f753ea8d69d7d6b1f4d50c3fad4b7198d77bc061bcda3d58b777be0b28dc9b6189181900360600190a16006548110156102875760068190556005805473ffffffffffffffffffffffffffffffffffffffff191633600160a060020a03161790555b6004805460001901908190551515610123577fc17eabdf34dbdda04d4e5bc7f639984fff1d8a51ba4bb7f728e59744c3320e07600354600654604051808381526020018281526020019250505060405180910390a15b5b50505600a165627a7a72305820888b9c31e7518cdc3c92065dd42acf70adfa453a4456fce887ef823ea467b3c70029',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
simplebidding.AssetRequested().watch(function(error, result){
if (error) {
console.log(error);
return;
}
console.log("[Asset " + result.args.barcode + " requested by " + result.args.requester + "]");
});
}
})
Then I interact with it using geth.
> loadScript("contracts/SimpleBidding2.js")
null [object Object]
true
> null [object Object]
Contract mined! address: 0x11645eb08f8304f707fbc46f43abcc146a80c767 transactionHash: 0xdcc627ebd504f6d928f9e2916109e7404861deab1871011b200b58e20a4c7e98
> simplebidding.requestAsset.sendTransaction(1234, {from: eth.accounts[0]})
"0x2dc33bcee737ab1a821dc1c707db864aef9d645abe2143f2db817b90f59b3a92"
>
Normally, I would expect a log message here like [Asset 1234 requested by 0x?????]. However, I waited really long time and nothing happened. No error message or another thing to understand what is the problem. Any ideas? I am stuck here...
requester = msg.sender;, it works. No idea why it is like that. Any ideas? – Önder Gürcan Mar 22 '17 at 14:13msg.senderis undefined. However, I am calling it as: `> simplebidding_sol_simplebidding.requestAsset.sendTransaction(9876, {from: eth.accounts[0]}) "0xd6fb82fc8ba155ad884c6ec5ebd1687c6425c3c3b731f6c8f3be3c506f9713bd" – Önder Gürcan Mar 22 '17 at 14:28