1

I deployed a smart contract to an azure proof of authority network(ethereum). Following the instructions here https://docs.microsoft.com/en-us/azure/blockchain/templates/ethereum-poa-deployment#tutorials, I was also able to send a signed transaction to the contract which i believe succeeds since i get back a transaction hash. But I cannot successfully query state in the contract. Im not sure what I’m missing or doing wrong. But here’s sample code to describe better.

contract MyContract {
struct Account {
    address owner;
    boolean active;
}

mapping( address => Account) allAccounts;

function AddAccount(address newAddress) public returns (bool success ) {
    allAccounts[newAddress].owner = newAddress;
    allAccounts[newAddress].active = true;
    return true;
}

function GetAccount(address _address) public view returns( address owner, boolean active) {
    return (allAccounts[_address].owner, allAccounts[_address].active);
}
}

I executed the AddAccount method.above by sending a signed transaction as follows

web3.eth.getTransactionCount(accountAddress, (err, nonce) => {
const data = MyContract.methods.AddAccount(
  newAddress ).encodeABI();


const rawTx = {
  nonce,
  gasPrice: '0x00',
  gasLimit: '0x2FAF080',
  to: contractAddress,
  value: '0x00',
  data,
};

const tx = new ethereumjs(rawTx);
tx.sign(privateKey);

// var serializedTx = tx.serialize();
const raw = '0x' + tx.serialize().toString('hex');

web3.eth.sendSignedTransaction(raw, (txErr, transactionHash) => {
  console.log("TX Hash: " + transactionHash);
  console.log("Error: " + txErr);})

Then I try to query the account like so

MyContract.methods.GetAccount(newAddress).call({
    from: accountAddress,
  }, (error, result) => {
    if (error) {
      logger.error(`${errors}`);
    }
    logger.info(`${result}`);
  });

However the result of this query always returns wrong values. Like im not able to get the initial values I sent in the first call. Pls what im I doing wrong or how can I make this work?

5
  • Could you confirm that the transaction was successful, either using a block explorer or checking the transaction receipt.This would be easier if you are running a post-byzantinum node
    – Adibas03
    Oct 26, 2018 at 10:09
  • sorry i'm replying so late. so i tried to get the transaction receipt for the hash i got when the transaction was executed. it came back as null.
    – Oaks-view
    Nov 4, 2018 at 18:21
  • That could mean that the transaction was discarded, which could have been for a number of possible reasons. Did you try adding the chainId value for the rawTx?
    – Adibas03
    Nov 5, 2018 at 19:23
  • yes. ive tried that. still doesnt work. although i finally caught an error ive missed all this time coz it gets thrown minutes later. Exception has occurred: Error Error: Transaction was not mined within750 seconds, please make sure your transaction was properly sent. Be aware that it might still be mined!
    – Oaks-view
    Nov 5, 2018 at 21:16
  • Okay. Since transaction is not being mined, there are two possibilities, either the transaction is not valid or the chain is not moving forward because of the "miners". You might need to debug around that.
    – Adibas03
    Nov 7, 2018 at 16:56

2 Answers 2

0

I think you make a mistake on your code, the data should have correct property of nonce and data as below. Your code are missing the two properties, you only have value for it.

   var rawTx = {
     nonce: nonce,
     gasPrice: '0x00',
     gasLimit: '0x2FAF080',
     to: address,
     value: '0x00',
     data: data
   }

Change

const rawTx = {
  nonce,
  gasPrice: '0x00',
  gasLimit: '0x2FAF080',
  to: contractAddress,
  value: '0x00',
  data,
};

to

const rawTx = {
  nonce: nonce,
  gasPrice: '0x00',
  gasLimit: '0x2FAF080',
  to: contractAddress,
  value: '0x00',
  data: data,
};
0

Turns out the issue for me had to do with the account i was using to sign the transaction. Im not really sure what was wrong with the account though. But i created a new one and tried it out following the same steps, and transaction was success. Can now query and get the right value

Your Answer

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

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