Here's a really simplistic contract to give you some ideas. I would suggest playing with it in Browser Solidity.
It records an order using orderNumber to identify them. orderNumber is presumed to come from order processing outside of the contract... it's a reference you can use to see how much Eth was sent and from whom.
The withdraw function let's the contract owner withdraw funds.
Since you're new, I'll point out that you must send Eth to the payable recordOrder() function, but you must not send Eth to the other non-payable functions. You'll probably find yourself missing that step and getting "Invalid Jump" errors. Order numbers are in hex format, e.g. "0x1". You'll do those conversions on the javascript side.
Hastily contrived example matching payments to order numbers. No warranty. :-)
pragma solidity ^0.4.6;
contract Order {
address public owner;
struct OrderStruct {
address sender;
uint amountReceived;
}
mapping(bytes32 => OrderStruct) orderStructs;
event LogWithdrawal(uint amount);
event LogOrder(address sender, bytes32 orderNumber, uint amount);
modifier onlyOwner {
if(msg.sender != owner) throw;
_;
}
function recordOrder(bytes32 orderNumber) payable returns(bool success) {
if(msg.value==0) throw;
orderStructs[orderNumber].sender = msg.sender;
orderStructs[orderNumber].amountReceived = msg.value;
LogOrder(msg.sender, orderNumber, msg.value);
return true;
}
function getOrder(bytes32 orderNumber) constant returns(address sender, uint amount) {
return(orderStructs[orderNumber].sender, orderStructs[orderNumber].amountReceived);
}
function withdrawFromContract(uint amount) onlyOwner returns(bool success) {
if(amount > this.balance) throw; // not enough money
if(msg.sender.send(amount)) {
LogWithdrawal(amount);
return true;
} else {
throw;
}
}
}
Hope it helps.
Or, blindly accept payments and just keep track of who sent Eth.
pragma solidity ^0.4.6;
contract Payments {
address public owner;
mapping(address => uint) amountReceived;
event LogWithdrawal(uint amount);
event LogPayment(address indexed sender, uint amount); // indexed means javascript can filter by user and get a history
modifier onlyOwner {
if(msg.sender != owner) throw;
_;
}
function Payments() {
owner = msg.sender; // whomever deploys this contract is the privileged "owner" address
}
function pay() payable returns(bool success) {
if(msg.value==0) throw; // can't pay "nothing"
amountReceived[msg.sender] += msg.value; // just a running total of receipts
LogPayment(msg.sender, msg.value); // transaction log of all receipts
return true;
}
function getReceived(address user) constant returns(uint received) {
return amountReceived[msg.sender]; // total sum of all-time receipts from this user
}
function withdrawFromContract(uint amount) onlyOwner returns(bool success) {
if(amount > this.balance) throw; // not enough money
if(msg.sender.send(amount)) {
LogWithdrawal(amount);
return true;
} else {
throw;
}
}
}
To send ERC20 compliant token you need to call method transfer
of contract. To do this with geth you correctly have chosen eth_sendTransaction
RPC Call.
In data
parameter you should specify ABI-encoded function transfer
with parameters encoded as described here (ABI-encoded).
To encode parameters you may want to use online tool https://abi.hashex.org. You enter name of function (transfer) and add parameters with types and values. Or you can just paste in token abi and pick function transfer
in dropdown list "Function type". Here is example of using this tool.
There is also a javascript library to encode parameters if you want to do this automatically.
After you got your ABI-encoded data, you set it to data
field in Geth RPC call, set other values and you are ready to call Geth and transfer tokens.
Pay attention to that account your are sending tokens from (from
parameter in RPC call) should be unlocked in Geth to sign transaction.
Best Answer
eth_sendTransaction
can be used for sending Ether, for creating contracts and calling contract function. It all depends on the parameters.For just sending Ether, you'd want to fill the
value
,to
andfrom
fields.For creating contracts, you'd leave the
to
empty but fill indata
.For calling a contract function, you'd fill in both
to
anddata
, and optionallyvalue
if you also want to transfer Ether to the contract.