contract3
v1.6.4
Published
A plugin making smart contract compilation, deployment and interaction easier
Downloads
103
Maintainers
Readme
Contract 3
Ethereum / Quorum compatible library that makes smart contract compilation, deployment and interaction easy (both standard and as signed transactions)
Installation
Go to the project directory and type
npm install --save contract3
Requirements
Tested with Node 8.0+ and web3 version 1.2.6
Examples
Below are example of using this library to interact with Smart Contracts
Standard Smart Contract Interaction
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();
Interacting with Smart Contract as Singed Transaction
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
Class functions
constructor(web3, address, privateKey, nonceFetchFlag, chainConfig)
Constructor takes web3 object as argument.
web3
a intialized web3 instanceaddress
is the ethereum account addressprivateKey
is the private key to be used for signing transaction.nonceFetchKey
is an optional flag to automatically fetch latest transaction count of the accountchainConfig
is 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...");
setSolc(solc)
Constructor takes web3 object as argument.
solc
is Solc instance if compiling is needed
const solc = require('solc');
...
contract3.setSolc(solc);
compile(source)
Compiles and returns bytes code and other details as produced by solc
source
is 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)
deploy(abi, code, args, options)
Deploys a existing compiled smart contract. Returns promise containing the instance, the receipt and transaction hash of deployed smart contract
abi
is the ABI of the smart contractcode
is the bytecode of the smart contractargs
are the constuctor arguments for initializing the contractoptions
contains standard web3 options like gas, value, etc.
contract3.deploy([...], '0x45..', ['1'], {
gas:4712388,
//privateFor: [], /*This is for Quorum*/
//value:<Any Ether to send>
})
getInstances(source)
Compiles and returns instances of the the contracts
source
is 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'];
getInstance(abi,address)
Use this method to get instance of pre-compiled smart contract (Does not work if you want to deploy smart contract)
abi
is the ABI of the smart contractaddress
is the ethereum smart contract address
const abi ={..};
const address='0xad12313...';
const contractInstance = await contract3.getInstance(abi,address);
Contract Instance methods
deployContract(args, options, isTxHashOnly)
Deploys the contract as a singed transaction. Deploys the contract and creates an instances using args passed as array.
args
are arguments to be passed to constructoroptions
contains standard web3 options like gas, value, etc.isTxHashOnly
Optional 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)
}
deployContractEncoded(args)
Interface to get encoded transaction data for contact deployment
args
are arguments to be passed to constructor
const abi ={..};
const contractInstance = await contract3.getInstance(abi);
console.log(contractInstance.deployContractEncoded([]));
signedTxDeployContract(args, options, isTxHashOnly)
Deploys the contract as a singed transaction.
args
are arguments to be passed to constructoroptions
contains standard web3 options like gas, value, etc.isTxHashOnly
Optional 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'
});
setAddress(address)
A setter function that sets the address of the instance to call functions of an already deployed contracts
address
Etherum address of the deployed contract
const AssetInstance = contractInstances['Asset'];
AssetInstance.setAddress('0x234924...');
get(functionName,args)
A getter function that issue a call to function represented by functionName by passing the arguments given in array format.
functionName
Smart contract function nameargs
are 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(functionName,args,options, isTxHashOnly)
Set is used to perform send transactions on functions of a deployed smart contract.
functionName
Smart contract function nameargs
are arguments to be passed to constructor. Its a array so multiple arguments can be passed dynamicallyoptions
contains standard web3 options like gas, value, etc.isTxHashOnly
Optional 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)
}
signedTxFunctionCall(functionName, args, options, isTxHashOnly)
Deploys the contract as a singed transaction.
functionName
Smart contract function nameargs
are arguments to be passed to constructoroptions
contains standard web3 options like gas, value, etc.isTxHashOnly
Optional 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'
});
getFunctionEncoded(functionName, args)
Returns encoded value for a function to use for sending signed transaction
functionName
Smart contract function nameargs
are arguments to be passed to constructor
const abi ={..};
const contractInstance = await contract3.getInstance(abi);
console.log(contractInstance.getFunctionEncoded("get",[]));
getTranactionReceipt()
Returns transaction receipt assosciated with contract deployment
getCode()
Returns bytecode of the contract
getInstance()
Returns web3 contract instance of the instance
getReceipt(hash)
hash
Transaction Hash
Returns receipt with decoded transaction log
getReceipt(hash)
hash
Transaction Hash
Returns receipt with decoded transaction log
getTransaction(hash)
hash
Transaction Hash
Returns transaction data with decoded input