Making Transactions using node js!!!! - HemantMadnani/MyContracts GitHub Wiki

module.exports = { info: function(info) { console.log('Info: ' + info); }, warning: function(warning) { console.log('Warning: ' + warning); }, error: function(error) { console.log('Error: ' + error); } };

var express = require('express'); var app = express(); var fs = require("fs");

var bodyparser = require("body-parser"); var urlencode = bodyparser.urlencoded({extended : false}); const Web3 = require('web3') const Tx = require('ethereumjs-tx') app.use(bodyparser.json());

let web3 = new Web3( new Web3.providers.HttpProvider( "https://rinkeby.infura.io/v3/28b280b7d36643269d7c050c9dc873af" ) )

const account = '0xBedd987995c770F0426C2e439d59aFEdcEfA7b6f'; //Your account address

const contractAddress = '0x48b48ad7508d63b401deb982d536420a509439c3'; // Deployed manually const abi =[ { "constant": true, "inputs": [], "name": "mintingFinished", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "name", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "approve", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "totalSupply", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_from", "type": "address" }, { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transferFrom", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [], "name": "unpause", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_amount", "type": "uint256" } ], "name": "mint", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "paused", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_subtractedValue", "type": "uint256" } ], "name": "decreaseApproval", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "_owner", "type": "address" } ], "name": "balanceOf", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [], "name": "renounceOwnership", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [], "name": "finishMinting", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [], "name": "pause", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "owner", "outputs": [ { "name": "", "type": "address" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_spender", "type": "address" }, { "name": "_addedValue", "type": "uint256" } ], "name": "increaseApproval", "outputs": [ { "name": "success", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "name": "_owner", "type": "address" }, { "name": "_spender", "type": "address" } ], "name": "allowance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "name": "_newOwner", "type": "address" } ], "name": "transferOwnership", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "amount", "type": "uint256" } ], "name": "Mint", "type": "event" }, { "anonymous": false, "inputs": [], "name": "MintFinished", "type": "event" }, { "anonymous": false, "inputs": [], "name": "Pause", "type": "event" }, { "anonymous": false, "inputs": [], "name": "Unpause", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "previousOwner", "type": "address" } ], "name": "OwnershipRenounced", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "previousOwner", "type": "address" }, { "indexed": true, "name": "newOwner", "type": "address" } ], "name": "OwnershipTransferred", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "owner", "type": "address" }, { "indexed": true, "name": "spender", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Approval", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" } ] const contract = new web3.eth.Contract(abi, contractAddress, { from: account, gasLimit: 3000000, });

app.post("/mine",function(req, res) { //const privateKey = Buffer.from('d11d72add2c2e7ceee4796bcdf60cb5e43011c9b0a1833df20c3f2e3e35dd02b', 'hex');

response = {
    to : req.body.to    };
//--------------------------------------------------------

console.log(req.body.pk); console.log(req.body.to); const privateKey = Buffer.from(req.body.pk, 'hex'); const contractFunction = contract.methods.mint(req.body.to,req.body.amount); // Here you can call your contract functions

const functionAbi = contractFunction.encodeABI();

let estimatedGas; let nonce; var err; console.log("Getting gas estimate");

contractFunction.estimateGas({from: account}).then((gasAmount) => { estimatedGas = gasAmount.toString(16);

console.log("Estimated gas: " + estimatedGas);

web3.eth.getTransactionCount(account).then(_nonce => { nonce = _nonce.toString(16);

console.log("Nonce: " + nonce);
const txParams = {
  gasPrice: 400000000000,
  gasLimit: 3000000,
  to: contractAddress,
  data: functionAbi,
  from: account,
  nonce: '0x' + nonce
};

const tx = new Tx(txParams);
 tx.sign(privateKey); // Transaction Signing here
const serializedTx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'),function(errr){ if (errr) return res.send(JSON.stringify(errr.message.toString()));

}).on('receipt', receipt => { console.log(receipt); err = receipt.status; //---------------------------------------------------------- res.send("TransactionStatus: "+err+"\t Transaction Completed...");

})

}); });

// var result = "Transferred to " + req.body.to; //res.send(JSON.stringify(result));

});

app.get("/",function(req,res){ res.send("Hello!!!, You are connected to server");

});

app.get("/index",function(req,res){ res.sendFile(__dirname+"/index.html");

});

app.get("/mineTokens",function(req,res){ res.sendFile(__dirname+"/mineTokens.html"); });

//used to check whether address is ethereum address or not app.post("/verify",function(req,res){

var result = web3.utils.isAddress(req.body.addr); return res.send(JSON.stringify(result));

});

app.post("/transfer",function(req, res) { //const privateKey = Buffer.from('d11d72add2c2e7ceee4796bcdf60cb5e43011c9b0a1833df20c3f2e3e35dd02b', 'hex');

response = {
    to : req.body.to    };
//--------------------------------------------------------

console.log(req.body.pk); console.log(req.body.to); const privateKey = Buffer.from(req.body.pk, 'hex'); const contractFunction = contract.methods.transfer(req.body.to,req.body.amount); // Here you can call your contract functions

const functionAbi = contractFunction.encodeABI();

let estimatedGas; let nonce; var err; console.log("Getting gas estimate");

contractFunction.estimateGas({from: account}).then((gasAmount) => { estimatedGas = gasAmount.toString(16);

console.log("Estimated gas: " + estimatedGas);

web3.eth.getTransactionCount(account).then(_nonce => { nonce = _nonce.toString(16);

console.log("Nonce: " + nonce);
const txParams = {
  gasPrice: 400000000000,
  gasLimit: 3000000,
  to: contractAddress,
  data: functionAbi,
  from: account,
  nonce: '0x' + nonce
};

const tx = new Tx(txParams);
 tx.sign(privateKey); // Transaction Signing here
const serializedTx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'),function(errr){ if (errr) return res.send(JSON.stringify(errr.message.toString()));

}).on('receipt', receipt => { console.log(receipt); err = receipt.status; //---------------------------------------------------------- res.send("TransactionStatus: "+err+"\t Transaction Completed...");

})

}); });

// var result = "Transferred to " + req.body.to; //res.send(JSON.stringify(result));

});

var server = app.listen(8081, function () {

var host = server.address().address var port = server.address().port

console.log("Example app listening at http://%s:%s", host, port)

})