contract permission{
struct transaction {
address payable sender;
address receiver;
string datatype;
Status status;
}
function getTransactionCount() public view returns(uint256 cnt) {
uint256 len = transactions.length;
return(len);
}
}
the above solidty code returns the count of transactions in a contract.
The data insertion(or transaction creation works fine). I get the transactionhash, as it verified but when I call the function to use the above function the contractInstance.methods.getTransactionCount().call();
returns null not default 0, even after inserting and using async/await procedure the problem still persists.
Please help
I have attached the test code for clarity
var Web3 = require('web3')
const web3 = new Web3('http://127.0.0.1:8545')
json = require('./../../../build/contracts/permission.json');
const interface = json['abi'];
const bytecode = json['bytecode'];
async function setup(){
var contract = new web3.eth.Contract(interface,'0xBcf2D22144C11C20B31A7Aa84875414B252a92DB');
const accounts = await web3.eth.getAccounts().then(res=>{return res;})
const admin = accounts[9];
var receiver = accounts[1];
var sender = accounts[3];
const xl = contract.deploy({ data: bytecode })
const bb = xl.send({from: admin,gas: 4712388,gasPrice: 100000000000},(error,transactionHash)=>{console.log('w',error,transactionHash);}).then(inst=>{return inst}).catch(err=>{console.log});
const bl = contract.methods.newTransact(receiver, 'dob').send({from: sender},(error,transactionHash)=>{console.log('e',error,transactionHash);});
const br = await contract.methods.getTransactionCount().call().then(rlt=>{console.log(rlt)})
const bt = contract.methods.newTransact(receiver, 'aadhar').send({from: sender},(error,transactionHash)=>{console.log('r',error,transactionHash)}).then(con=>{console.log});
const by = await contract.methods.getTransactionCount().call().then(rlt=>{console.log(rlt)});
console.log(br.cnt,by.cnt);
}
setup();
Still returns null,null
as the output.
If await is not used then the code just waits endlessly
the following is a sample code that has the same issue as my smart contract(it is just too long for publishing here) but here too the same issue occurs.
var Web3 = require('web3')
const web3 = new Web3('http://127.0.0.1:8545')
json = require('./build/contracts/test.json');
const abi = json['abi'];
const bytecode = json['bytecode'];
async function setup(){
const contract = new web3.eth.Contract(abi);
const accounts = await web3.eth.getAccounts();
const admin = accounts[9];
const receiver = accounts[1];
const sender = accounts[3];
const deployedContract = await contract.deploy({ data: bytecode }).send({from: admin,gas: 4712388,gasPrice: 100000000000});
const receipt = await deployedContract.methods.setData('dob').send({from: sender});
const data = await deployedContract.methods.getData.call();
console.log(data);
}
setup();
the solidity code the same is :
pragma solidity >=0.4.22 <0.6.0;
pragma experimental ABIEncoderV2;
contract test{
string data;
function getData() view external returns(string memory){
return data;
}
function setData(string calldata _data) external{
data = _data;
}
}
transactions
? 2. What iscnt
used for?await
and you can usethen
, but you should not be using both.then
.