1

contract has an function "addToWhiteList", only owner of the contract can access this function and add user in whitelist.

/**
* @dev Adds single address to whitelist.
* @param trusted Address to be added to the whitelist
*/
function addToWhiteList(address trusted) public onlyOwner {
   require(!whitelist[trusted]);
   whitelist[trusted] = true;

   emit UserWhitelist(true);
}

I am trying to add the user on whitelist from owner account :

// Send Transaction to the Blockchain
function sendTransaction() {
 const functionAbi = Contract.methods.addToWhiteList("0x9DA80947ACf5Fc16299311EdAabc37fF7d201C95").encodeABI()
 console.log(functionAbi);
 var nonce = web3.eth.getTransactionCount("0x61A44075419C4402f6DE631341d875Ece6A3922e");
 var gasPrice = "2";//or get with web3.eth.gasPrice
 var gasLimit = 3000000;
 var ownerPrivateKey = "0x5c9c7e00edc09d0df072c0de7911026a4ab241aa8a5e9f063fb450ba91fbc360";
 var formatedPrivteKey = ownerPrivateKey.substring(2);
 var tx = new ethereumjs.Tx({
   nonce: web3.utils.toHex(nonce),
   gasPrice: web3.utils.toHex(gasPrice * 1e9),
   gasLimit: web3.utils.toHex(gasLimit),
   to: ContractAddress,
   value: '0x00',
   data: functionAbi
 });

 tx.sign(ethereumjs.Buffer.Buffer.from(formatedPrivteKey, "hex"));

 var raw = "0x" + tx.serialize().toString("hex");

 web3.eth.sendSignedTransaction(raw)
  .on('receipt', function(receipt) {
    console.log(receipt);
  }).on("error", console.error);
}

Note : Add web3 : web3 1.0

<script language="javascript" type="text/javascript" src="web3.min.js"></script>
<script language="javascript" type="text/javascript" src="ethereumjs-wallet-0.6.0.min.js"></script>

Init. web3 :

web3 = new Web3(new Web3.providers.WebsocketProvider("wss://ropsten.infura.io/ws"));

.user is not adding in whitelist when i use console of the browser and call the function "sendTransaction" from console :

sendTransaction()
undefined

after few seconds :

web3.min.js:1 connection not open on send()
u.send @ web3.min.js:1
u.send @ web3.min.js:1
u.removeSubscription @ web3.min.js:1
a.unsubscribe @ web3.min.js:1
(anonymous) @ web3.min.js:1
Promise.catch (async)
g @ web3.min.js:1
(anonymous) @ web3.min.js:1
(anonymous) @ web3.min.js:1
(anonymous) @ web3.min.js:1
(anonymous) @ web3.min.js:1
(anonymous) @ web3.min.js:1
connection.onmessage @ web3.min.js:1
web3.min.js:1 Error: Failed to check for transaction receipt:
{}
    at Object._fireError (web3.min.js:1)
    at web3.min.js:1

when i check

Contract.methods.isAddressWhiteList("0x9DA80947ACf5Fc16299311EdAabc37fF7d201C95").call().then(console.log);

it's return false

  • Did you check the transaction receipt? It might have failed or being delayed if your gas price is low. – Ismael May 18 '18 at 14:22
  • yes , showing error : web3.min.js:1 connection not open on send() u.send @ web3.min.js:1 u.send @ web3.min.js:1 u.removeSubscription @ web3.min.js:1 a.unsubscribe @ web3.min.js:1 (anonymous) @ web3.min.js:1 Promise.catch (async) g @ web3.min.js:1 (anonymous) @ web3.min.js:1 (anonymous) @ web3.min.js:1 (anonymous) @ web3.min.js:1 (anonymous) @ web3.min.js:1 (anonymous) @ web3.min.js:1 connection.onmessage @ web3.min.js:1 web3.min.js:1 Error: Failed to check for transaction receipt: {} at Object._fireError (web3.min.js:1) at web3.min.js:1 – Chirag Maliwal May 18 '18 at 14:27
  • @Ismael, I updated the question. – Chirag Maliwal May 18 '18 at 14:28
  • 1
    It seems to be an open issue reported in both infura github.com/INFURA/infura/issues/95 and web3js github.com/ethereum/web3.js/issues/1255 (and a couple more) – Ismael May 18 '18 at 15:41
  • Yes ! I checked .. What is the other option ? – Chirag Maliwal May 18 '18 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.