var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var arguments = process.argv.splice(2);
if(!arguments || arguments.length != 2) {
console.log("Parameter error!");
return;
}
var _from = web3.eth.accounts[0];
var _to = "0x47fb1c31b3d1a2814b1f37ecb0655ba51fc34c93";
var _value = 500000000;
web3.eth.sendTransaction({
from:_from, to: _to, value: _value}, (err, res)=>{
if(err)
console.log("Error:", err);
else
console.log("Result:", res)
});
编译合约:
gexiao@gexiaovm:~/geth/0403/web3_test/contract$ cd ..
gexiao@gexiaovm:~/geth/0403/web3_test$ ls
contract node_modules package.json package-lock.json
gexiao@gexiaovm:~/geth/0403/web3_test$ vi transfer_script.js
gexiao@gexiaovm:~/geth/0403/web3_test$ node transfer_script.js
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var _from = web3.eth.accounts[0];
var _to = web3.eth.accounts[1];
var amount = 50000;
var abi = [{
"constant":true,"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{
"anonymous":false,"inputs":[{
"indexed":false,"internalType":"address","name":"from","type":"address"},{
"indexed":false,"internalType":"address","name":"to","type":"address"},{
"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Sent","type":"event"},{
"inputs":[{
"internalType":"address","name":"","type":"address"}],"name":"balances","outputs":[{
"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{
"inputs":[{
"internalType":"address","name":"receiver","type":"address"},{
"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{
"inputs":[],"name":"minter","outputs":[{
"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{
"inputs":[{
"internalType":"address","name":"receiver","type":"address"},{
"internalType":"uint256","name":"amount","type":"uint256"}],"name":"send","outputs":[],"stateMutability":"nonpayable","type":"function"}]
var CoinContract = web3.eth.contract(abi);
var contractAddress = "0x47Fb1C31B3D1A2814B1f37EcB0655BA51Fc34C93";
var contractInstance = CoinContract.at(contractAddress);
contractInstance.send(_to, amount, {
from:_from}, (err, res)=>{
if(err)
console.log("Error:", err);
else
console.log("Result:", res);
});
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var arguments = process.argv.slice(2);
if(!arguments || arguments.length!=2) {
console.log("Parameter length must be 2");
return;
}
var _from = web3.eth.accounts[0];
var _to = web3.eth.accounts[1];
var amount = 50000;
var abi = [{
"constant":true,"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{
"anonymous":false,"inputs":[{
"indexed":false,"internalType":"address","name":"from","type":"address"},{
"indexed":false,"internalType":"address","name":"to","type":"address"},{
"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Sent","type":"event"},{
"inputs":[{
"internalType":"address","name":"","type":"address"}],"name":"balances","outputs":[{
"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{
"inputs":[{
"internalType":"address","name":"receiver","type":"address"},{
"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{
"inputs":[],"name":"minter","outputs":[{
"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{
"inputs":[{
"internalType":"address","name":"receiver","type":"address"},{
"internalType":"uint256","name":"amount","type":"uint256"}],"name":"send","outputs":[],"stateMutability":"nonpayable","type":"function"}]
var CoinContract = web3.eth.contract(abi);
var contractAddress = "0x47Fb1C31B3D1A2814B1f37EcB0655BA51Fc34C93";
var contractInstance = CoinContract.at(contractAddress);
web3.personal.unlockAccount(_from, '自定义的密码', (err, res)=>{
if(err)
console.log("Error:", err);
else{
contractInstance.send(_to, amount, {
from:_from}, (err, res)=>{
if(err)
console.log("Error:", err);
else
console.log("Result:", res);
});
}
});
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var arguments = process.argv.slice(2);
if(!arguments || arguments.length!=1) {
console.log("Parameter length must be 1");
return;
}
var _addr = arguments[0];
var abi = [{
"constant":true,"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{
"anonymous":false,"inputs":[{
"indexed":false,"internalType":"address","name":"from","type":"address"},{
"indexed":false,"internalType":"address","name":"to","type":"address"},{
"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Sent","type":"event"},{
"inputs":[{
"internalType":"address","name":"","type":"address"}],"name":"balances","outputs":[{
"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{
"inputs":[{
"internalType":"address","name":"receiver","type":"address"},{
"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{
"inputs":[],"name":"minter","outputs":[{
"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{
"inputs":[{
"internalType":"address","name":"receiver","type":"address"},{
"internalType":"uint256","name":"amount","type":"uint256"}],"name":"send","outputs":[],"stateMutability":"nonpayable","type":"function"}]
var CoinContract = web3.eth.contract(abi);
var contractAddress = "0x47Fb1C31B3D1A2814B1f37EcB0655BA51Fc34C93";
var contractInstance = CoinContract.at(contractAddress);
contractInstance.balances(_addr, (err, res)=>{
if(err)
console.log("Error:", err);
else
console.log("Result:", res);
}) ;
var Web3 = require('web3');
var web3 = new Web3(new web3.providers.HttpProvider("http://localhost:8545"));
var abi = [{
"constant":true,"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{
"anonymous":false,"inputs":[{
"indexed":false,"internalType":"address","name":"from","type":"address"},{
"indexed":false,"internalType":"address","name":"to","type":"address"},{
"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Sent","type":"event"},{
"inputs":[{
"internalType":"address","name":"","type":"address"}],"name":"balances","outputs":[{
"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{
"inputs":[{
"internalType":"address","name":"receiver","type":"address"},{
"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{
"inputs":[],"name":"minter","outputs":[{
"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{
"inputs":[{
"internalType":"address","name":"receiver","type":"address"},{
"internalType":"uint256","name":"amount","type":"uint256"}],"name":"send","outputs":[],"stateMutability":"nonpayable","type":"function"}];
var contractAddr = "0x47Fb1C31B3D1A2814B1f37EcB0655BA51Fc34C93";
var CoinContract = web3.eth.contract(abi);
var contractInstance = CoinContract.at(contractAddr);
contractInstance.Sent("latest", (err, res)=>{
if(err)
console.log("Error:", err);
else
console.log("Sent Event occurs:", res);
});