0

I have created a solidity contract where I can store application

pragma solidity ^0.4.17;

contract Applications {

  address applicant;

  mapping(uint => string) Application;

  function Applications() internal {
    applicant = msg.sender;
  } 

  modifier ifApplicant() {
    if (applicant != msg.sender) {
      revert();
    } else {
      _;
    }
  }

  function setApplication(uint id, string data) public {
    Application[id] = data;
  }

  function getApplication(uint id) ifApplicant public view returns (string) {
     return Application[id];
  }
}

When I try to access the setApplication method via web3 I get the above error Attempting to run transaction which calls a contract function, but recipient address is not a contract address

const Web3 = require('web3');
const contract = require('truffle-contract');

const web3 = new Web3(
    new Web3.providers.HttpProvider('http://localhost:8545')
);
const ApplicationContract = require('./../blockchain/build/contracts/Applications.json');
const application = contract(ApplicationContract);
//web3 has deprecated sendAsync func whereas truffle-contract relies on it - this is patch hack
web3.currentProvider.sendAsync = web3.currentProvider.send;
application.setProvider(web3.currentProvider);

web3.eth.getAccounts().then(accounts => {
        application.new(
            {
                from: accounts[0],
                data:
                    '0x6060604052341561000f57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555061037a8061005e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631f2f4bfd146100515780632ee68716146100ed575b600080fd5b341561005c57600080fd5b6100726004808035906020019091905050610153565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b2578082015181840152602081019050610097565b50505050905090810190601f1680156100df5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156100f857600080fd5b610151600480803590602001909190803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091905050610269565b005b61015b610295565b3373ffffffffffffffffffffffffffffffffffffffff166000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415156101b657600080fd5b600160008381526020019081526020016000208054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561025d5780601f106102325761010080835404028352916020019161025d565b820191906000526020600020905b81548152906001019060200180831161024057829003601f168201915b50505050509050919050565b806001600084815260200190815260200160002090805190602001906102909291906102a9565b505050565b602060405190810160405280600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102ea57805160ff1916838001178555610318565b82800160010185558215610318579182015b828111156103175782518255916020019190600101906102fc565b5b5090506103259190610329565b5090565b61034b91905b8082111561034757600081600090555060010161032f565b5090565b905600a165627a7a723058200b7685150ea101e930185efc1657a5e630782e6b70be37899e6bf66e523478d10029',
                gas: '4700000',
            }   
        ).then((contract) => {if (typeof contract.address !== 'undefined') {
                    const ApplicationContract = new web3.eth.Contract(
                        application.abi,
                        contract.address
                    );
                    ApplicationContract.methods
                        .setApplication(1, 'form')
                        .call({ from: accounts[0] })
                        .then(data => console.log(data))
                        .catch(err => console.log(err));
                    console.log(
                        'Contract mined! address: ' +
                            contract.address +
                            ' transactionHash: ' +
                            contract.transactionHash
                    );
                }
            });

I am running ganache-cli for local development.

PS: I am very new to Solidity

  • .call().send(...) looks suspicious. Do one or the other. (You probably want send here, so methods.setApplication(1, 'from').send(...).) – smarx Mar 11 '18 at 7:47
  • But also, probably ApplicationContract.methods.setApplication..., right? Or what is methods? – smarx Mar 11 '18 at 7:47
  • @smarx Corrected. Yes, you are right. I get the same error when I do setApplication.call({from: ''}) and I invoke send I get error VM4711:1 Error: Returned error: sender account not recognized – Ajain Vivek Mar 11 '18 at 7:59
  • It looks like you're trying to send the transaction from the contract's address. You can't do that. Use web3.eth.accounts to see what accounts ganache knows about. You can send transactions from those. – smarx Mar 11 '18 at 8:01
  • 2
    Please answer my questions. They were designed to help you to be more specific about how you obtained the address. – smarx Mar 11 '18 at 10:08
4

Run this from your command line

truffle networks --clean

this will clean your local network of previously built network...

1

I think you are using call for a write operation, where it should be used to just read values from Blockchain.

ApplicationContract.methods .setApplication(1, 'form') .call

Replace the call with a sendTransaction and it should work.

Please read web3.eth.call vs web3.eth.sendTransaction

For your reference What is the difference between a transaction and a call?

1

I remove build/contracts folder and re-run

  1. truffle build
  2. truffle migrate

The error message disappears.

0

This answer explains what is causing this error. Running truffle migrate --reset should fix it.

-1

Try it.

var adoptionInstance;

web3.eth.getAccounts(function(error, accounts) {

    if (error) {
        console.log(error);
    }

    var account = accounts[0];
    App.contracts.ApplicationContract.deployed().then(function(instance) {
    contractInstance = instance;
    //Execute adopt as a transaction by sending account
    console.log("Pedding Function");
    return contractInstance.setApplication(1,'form', {from: account, value: web3.toWei(prices, "ether")});
}).then(function(result) {
    console.log("Success Adopt");
}).catch(function(err) {
    console.log("Error Adopt " + err.message);
});
});

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.