0

I created a contract, and deployed it into my private blockchain, as you can see I use mapping and dynamically allocated array. When I am trying to add a domain by 'addDomain' method with remix web ide it works fine, by when I am trying to use 'addDomain' method with nodejs its return an address(Big Number) as return value, but actually, when I use 'getDoamin' its seems that the transaction is not approved or something like this.

Reputation.sol

pragma solidity ^0.4.0;


contract addressRecord {
    struct  Record {
        address owner;
        uint score;
    }

    mapping (string => Record) records;
    string[] records_arr;

    function addDomain(string _domain, uint _score) {
        if (records[_domain].owner != address(0x0)
        && records[_domain].owner != msg.sender) { return; }

        records[_domain] = Record(msg.sender, _score);
        records_arr.length += 1;
        records_arr[records_arr.length-1] = _domain;
    }

    function getDomain(string _domain) constant returns(uint) {
        return records[_domain].score;
    }

    function changeScore(string _domain, address _to) {
        require(records[_domain].owner == msg.sender);
        records[_domain].owner = _to;
    }    

    function domainsCount() constant returns (uint) {
        return records_arr.length;
    }
}

reputation.js

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

const abi = require('./abi.json');
const contractAbi = web3.eth.contract(abi);

const contractAddr = "0x0014b44b785769e28b5cb097c1329784c96333f6"; // replace this
const walletAddr = "0xca1c9203f10f9b918d8630a540d85456dfb8b760";   // replace this

const myContract = contractAbi.at(contractAddr);

web3.personal.unlockAccount(walletAddr, "12345678");
var myArgs = process.argv.slice(2);
console.log(myArgs[0]);
console.log(parseInt(myArgs[1], 10));
console.log(myContract.addDomain(myArgs[0], parseInt(myArgs[1], 10), { from: walletAddr, to: contractAddr }));
console.log(myContract.getDomain(myArgs[0], {from: walletAddr, to: contractAddr}));

Nodejs output

testdomain.com
50
0x11229d6545a7002cbda9f102d91e010f9d990a149072382fe86dc63b1f6b639d
{ [String: '0'] s: 1, e: 0, c: [ 0 ] }

Geth log

INFO [09-03|00:50:32] Submitted transaction                    fullhash=0x73b720ccfb3811db2339a060655341bf07ad778e7dd9a6e160b868c647b0120a recipient=0x0014b44b785769e28b5cb097c1329784c96333f6
INFO [09-03|00:50:36] Successfully sealed new block            number=4851 hash=3bbc5a…d972af
INFO [09-03|00:50:36] 🔗 block reached canonical chain          number=4846 hash=4d78df…ca4938
INFO [09-03|00:50:36] 🔨 mined potential block                  number=4851 hash=3bbc5a…d972af
INFO [09-03|00:50:36] Commit new mining work                   number=4852 txs=1 uncles=0 elapsed=486.898µs

1 Answer 1

0

Although it's returning something that looks suspiciously like the 10 that we're expecting, this might help it wait for the transaction to be mined: https://gist.github.com/xavierlepretre/88682e871f4ad07be4534ae560692ee6

When you're sure it's mined, carry on like:

  return myContract.getDomain(myArgs[....); // return a promise
})
.then(function(score) { // your function returns the score
  console.log(score.toString(10); // make it human-readable
});

Hope it helps.

2
  • Actually, getDomain works well. The problem is that, addDomain dont writing into the blockchain. Sep 3, 2017 at 16:10
  • Make sure it the same address that deployed the contract because add Domain is designed to reject txns from everyone else. Then, be sure to wait for the txn to be mined. The first callback will give you a txn receipt indicating the txn has been sent, not mined. You use that receipt to wait for it. Sep 3, 2017 at 17:31

Your Answer

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

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