Ethereum / Quorum compatible library that makes smart contract compilation, deployment and interaction easy (both standard and as signed transactions)
Go to the project directory and type
npm install --save contract3
Tested with Node 8.0+ and web3 version 1.2.6
Below are example of using this library to interact with Smart Contracts
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
const Contract3 = require('contract3');
let contract3 = new Contract3(web3);
contract3.setSolc(require('solc'));
async function deploy(){
let Administered = 'contract Administered { .. }';
let Asset = 'contract Asset { .. }';
let input = {
'Administered.sol': {
content: Administered
},
'Asset.sol': {
content: Asset
}
}
const defaultAccount = await web3.eth.getCoinbase();
const contractInstances = await contract3.getInstances(input);
//Getting instance of the contract, by Contract name
const AssetInstance = contractInstances['Asset']();
try{
const ContractDeploymentResponse = await AssetInstance.deployContract(
//args to constuctor
["computerSystem",defaultAccount,100],
//Options
{
gas:4712388
});
console.log('Contract Address:',ContractDeploymentResponse.options.address)
const result = await AssetInstance.get('getValue',[]);
console.log('Value of Asset:', result)
console.log('Changing Asset Value')
const result2 = await AssetInstance.set('changeValue', [134],{gas:40000});
if (result2) {
const result = await AssetInstance.get('getValue',[]);
console.log('New Asset Value:', result)
}
} catch(e){
console.log(e)
}
}
deploy();This method is useful for consuming apis of services like infura to interact with ethereum blockchain
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
const Contract3 = require('contract3');
let contract3 = new Contract3(web3, "0x4E5feB13de0e29BC4aeA01........"/*Ethereum_Address*/, "dfae4457ef0fd39416597da2fab6739..........."/*Private_Key*/, true/*Automatically fetch current nonce flag*/, {chain: 'ropstein'});
const abi ={..};
const contractInstance = await contract3.getInstance(abi);
//Deploying contrcat
contractInstance.signedTxDeployContract(["arg1",true],
{
gas: 4712388,
value: '0x00'
})
//Calling a contract function
const data = await instance.signedTxFunctionCall('storeSellerInvoice', ["0xfa3a43c04170d3b35bae1a8d8e7c83bc5add4ed8a36f7e0586814344d1bef229"],
{
gas: 4712388,
value: '0x00'
});For calling contract methods as signed tx refer to "Contract Instance methods" section
Constructor takes web3 object as argument.
web3a intialized web3 instanceaddressis the ethereum account addressprivateKeyis the private key to be used for signing transaction.nonceFetchKeyis an optional flag to automatically fetch latest transaction count of the accountchainConfigis chainConfig chain config for etherumjs-tx module => eg. { chain: 'mainnet', hardfork: 'petersburg' } . Only required if signed transactions are used account
/*Pre Requisite*/
const Web3 = require('web3');
const provider = new Web3.providers.HttpProvider(YOUR_URL);
const web3 = new Web3(provider);
/*Pre-requistie End*/
//For signed
const contract3 = new Contract3(web3, "0xe3424234...","4de223424324324....", true);
//For non-singed tx
const contract3 = new Contract3(web3, "0xe3424234...");Constructor takes web3 object as argument.
solcis Solc instance if compiling is needed
const solc = require('solc');
...
contract3.setSolc(solc);Compiles and returns bytes code and other details as produced by solc
sourceis JSON object with Contract file name as key and Contract content as value
let Administered = 'contract Administered { .. }';
let Asset = 'contract Asset { .. }';
let input = {
'Administered.sol': {
content: Administered},
'Asset.sol': {contenet: Asset}
}
contract3.compile(input)Deploys a existing compiled smart contract. Returns promise containing the instance, the receipt and transaction hash of deployed smart contract
abiis the ABI of the smart contractcodeis the bytecode of the smart contractargsare the constuctor arguments for initializing the contractoptionscontains standard web3 options like gas, value, etc.
contract3.deploy([...], '0x45..', ['1'], {
gas:4712388,
//privateFor: [], /*This is for Quorum*/
//value:<Any Ether to send>
})Compiles and returns instances of the the contracts
sourceis JSON object with Contract file name as key and Contract content as value
let Administered = 'contract Administered { .. }';
let Asset = 'contract Asset { .. }';
let input = {
'Administered.sol': Administered,
'Asset.sol': Asset
}
const contractInstances = await contract3.getInstances(input);
//Getting a contract instance by name of the smart contract
const Asset = contractInstances['Asset'];Use this method to get instance of pre-compiled smart contract (Does not work if you want to deploy smart contract)
abiis the ABI of the smart contractaddressis the ethereum smart contract address
const abi ={..};
const address='0xad12313...';
const contractInstance = await contract3.getInstance(abi,address);Deploys the contract as a singed transaction. Deploys the contract and creates an instances using args passed as array.
argsare arguments to be passed to constructoroptionscontains standard web3 options like gas, value, etc.isTxHashOnlyOptional Flag to resolve once txHash is available instead of waiting till transaction is confirmed and receipts available
Returns transaction reciept if isTxHashOnly is false or undefined else gives back transaction hash
let Administered = 'contract Administered { .. }';
let Asset = 'contract Asset { .. }';
let input = {
'Administered.sol': Administered,
'Asset.sol': Asset
}
const contractInstances = await contract3.getInstances(input);
const AssetInstance = contractInstances['Asset'];
try{
const ContractObject = await AssetInstance.deployContract(
//args to constuctor
["computerSystem",defaultAccount,100],
//Other Parameters
{
value: 0,
gas:4712388
});
console.log('Contract Address: ',ContractObject.options.address)
}
catch (err) {
console.log(err)
}Interface to get encoded transaction data for contact deployment
argsare arguments to be passed to constructor
const abi ={..};
const contractInstance = await contract3.getInstance(abi);
console.log(contractInstance.deployContractEncoded([]));Deploys the contract as a singed transaction.
argsare arguments to be passed to constructoroptionscontains standard web3 options like gas, value, etc.isTxHashOnlyOptional Flag to resolve once txHash is available instead of waiting till transaction is confirmed and receipts available
Returns transaction reciept if isTxHashOnly is false or undefined else gives back transaction hash
const abi ={..};
const contractInstance = await contract3.getInstance(abi);
const txData = await contractInstance.signedTxDeployContract(["arg1",true],
{
gas: 4712388,
value: '0x00'
});A setter function that sets the address of the instance to call functions of an already deployed contracts
addressEtherum address of the deployed contract
const AssetInstance = contractInstances['Asset'];
AssetInstance.setAddress('0x234924...');A getter function that issue a call to function represented by functionName by passing the arguments given in array format.
functionNameSmart contract function nameargsare arguments to be passed to constructor. Its a array so multiple arguments can be passed dynamically
const result = await AssetInstance.get('getValue',[]);
console.log(result)Set is used to perform send transactions on functions of a deployed smart contract.
functionNameSmart contract function nameargsare arguments to be passed to constructor. Its a array so multiple arguments can be passed dynamicallyoptionscontains standard web3 options like gas, value, etc.isTxHashOnlyOptional Flag to resolve once txHash is available instead of waiting till transaction is confirmed and receipts available
Returns transaction reciept if isTxHashOnly is false or undefined else gives back transaction hash
const result2 = await contracts.set(ContractObject,'set', [134],{gas: 100000});
if (result2) {
const result = await contracts.get(ContractObject,'get',[]);
console.log(result)
}Deploys the contract as a singed transaction.
functionNameSmart contract function nameargsare arguments to be passed to constructoroptionscontains standard web3 options like gas, value, etc.isTxHashOnlyOptional Flag to resolve once txHash is available instead of waiting till transaction is confirmed and receipts available
Returns transaction reciept if isTxHashOnly is false or undefined else gives back transaction hash
const abi ={..};
const contractInstance = await contract3.getInstance(abi);
const data = await contractInstance.signedTxFunctionCall('storeSellerInvoice', ["0xfa3a43c04170d3b35bae1a8d8e7c83bc5add4ed8a36f7e0586814344d1bef229"],
{
gas: 4712388,
value: '0x00'
});Returns encoded value for a function to use for sending signed transaction
functionNameSmart contract function nameargsare arguments to be passed to constructor
const abi ={..};
const contractInstance = await contract3.getInstance(abi);
console.log(contractInstance.getFunctionEncoded("get",[]));Returns transaction receipt assosciated with contract deployment
Returns bytecode of the contract
Returns web3 contract instance of the instance
hashTransaction Hash
Returns receipt with decoded transaction log
hashTransaction Hash
Returns receipt with decoded transaction log
hashTransaction Hash
Returns transaction data with decoded input