0
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 endlesslysample output

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;
    }
}
9
  • Your solidity code looks messy. 1. What is transactions? 2. What is cnt used for? May 13, 2019 at 7:00
  • Your Javascript code also looks messy. You can use await and you can use then, but you should not be using both. May 13, 2019 at 7:02
  • ` struct transaction { address payable sender; address receiver; string datatype; Status status; } ` this is to count the number of transactions in the network. without the await the code just waits endlessly.
    – vaishakh k
    May 13, 2019 at 9:45
  • So get rid of the then. May 13, 2019 at 10:09
  • @goodvibration, without then the value returned is null.
    – vaishakh k
    May 13, 2019 at 11:07

1 Answer 1

1

I get the transactionhash, as it verified

I believe you are mistaken about this. The returned value from ...newTransact() is a transaction hash. This is an acknowledgment that the transaction was submitted, not a confirmation that it was mined. That transaction will hang around in the transaction pool for a little while before it is included in a block.

The code suggests that you do not wait for the transaction to be mined. Right here:

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)})

Have a look over here to get a sense of what has to happen between those steps: Get Transaction Hash And Mined Receipt

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.