I have implemented my personal blockchain using node js for IoT uses case so I now want to deploy smart contract code into this blockchain without using geth or tuffle or ganache OR Remix or web3.js so my goal is to use EVM API
just to compile and execute a smart contract.
So, how?
2 Answers
You can look at Contract ABI Specification
Follow here
The Contract Application Binary Interface (ABI) is the standard way to interact with contracts in the Ethereum ecosystem, both from outside the blockchain and for contract-to-contract interaction. Data is encoded according to its type, as described in this specification. The encoding is not self describing and thus requires a schema in order to decode.
Also,
To integrate with Ethereum, requires an Ethereum client node like Geth, parity
, quorum
etc with RPC / IPC enabled.
If you are just interested in retrieving existing data from contracts, or sending offline signed transactions from the Ethereum public chain, you can just use a public node like Infura
without the need to install a local client like Parity or Geth.
For smart contract development you can also install Test RPC
which will create a network in memory without truffle or ganache.
testrpc is a Node.js based Ethereum client for testing and development. It uses ethereumjs to simulate full client behavior and make developing Ethereum applications much faster. It also includes all popular RPC functions and features (like events) and can be run deterministically to make development a breeze.
for more details on testrpc , follow here
To install testrpc
npm install -g ethereumjs-testrpc
Note
Ganache CLI is the latest version of TestRPC: a fast and customizable blockchain emulator. It allows you to make calls to the blockchain without the overheads of running an actual Ethereum node.
To install ganache-cli
npm install -g ganache-cli
Steps:-
- Create smart contract for iot application.
- Compile the contract using solc solidity compiler for nodejs
From the compiled JS contract object get the interface object
The bytecode string, which is the binary representation of the contract is basically a set of (kind of) assembly instructions for the Ethereum Virtual Machine to execute
Deploy the contract to the testrpc network
here is the detailed solution without using truffle or ganache
To deploy only using Bytecode
See Also to deploy using only bytecode : here and here and the Yellow paper
Compile your contract (result file is /build/contracts/YourContract.json) and then deploy without truffle:
// Dependencies
const Web3 = require('web3');
const fs = require("fs");
// Load Parameter
let PROVIDER = ...
let FROM_ACCOUNT = ...
let FROM_PRIVATE_KEY = ...
// Create web3 instance
const web3 = new Web3(new Web3.providers.HttpProvider(PROVIDER), null, {transactionConfirmationBlocks: 1});
// Deploy
web3.eth.getTransactionCount(FROM_ACCOUNT)
.catch(e => {
console.log(e);
})
.then(txCount => {
// construct the transaction data
const txData = {
nonce: web3.utils.toHex(txCount),
from: FROM_ACCOUNT,
data: JSON.parse(fs.readFileSync('./build/contracts/YourContract.json', 'utf8')).bytecode,
gasLimit: web3.utils.toHex(7900000),
gasPrice: web3.utils.toHex(web3.utils.toWei('12', 'gwei')),
};
console.log('nonce : ' + txData.nonce);
console.log('gasPrice : ' + txData.gasPrice);
// contract creation
web3.eth.accounts.signTransaction(txData, FROM_PRIVATE_KEY).then(signed => {
console.log('sign transaction : OK');
web3.eth.sendSignedTransaction(signed.rawTransaction)
.on("error", error => {
console.log('send transaction : ' + error);
throw "DEPLOY FAILED";
})
.on("receipt", (receipt) => {
console.log('send transaction : OK');
let contractAddress = (receipt.contractAddress === null) ? 'n.a.' : receipt.contractAddress;
console.log("CONTRACT_ADDRESS : " + contractAddress);
});
});
});
});
Dependencies web3 v1.2.1 and fs v0.0.2