Ethers.js: Intracting With Contract provider.resolveName is not A Function

Created on 31 Jan 2018  路  12Comments  路  Source: ethers-io/ethers.js

Trying to interact with contract token deployed on the Ethereum Node.
Most of the Functions seems to work fine except for my transfer() function which is supposed to send a token to a given address.
However, when I try to send using this code
```
var contract = new ether.Contract(contract_address, abi, provider);
var callPromise = contract.functions.transfer(receiver_address, "5000");

it gives
 ```

2018-01-31T05:21:09.159386+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: missing signer

2018-01-31T05:21:09.159465+00:00 app[web.1]: (node:4) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

So I tried to sign it using

         signer = new ether.Wallet(prvkey);
     provider = ether.providers.getDefaultProvider()
     var contract = new ether.Contract(add, abi, signer.address, provider);
     var callPromise = contract.functions.transfer(receiver_address, "5000");

it gives

              provider.resolveName is not a function
              at new Contract (C:\xampp\htdocs\EtherWallet\node_modules\ethers-contracts\contract.js:57:35)

So what am I doing wrong? please assit me get this right.

discussion

Most helpful comment

Heya!

You need to attach the provider to the signer, and then pass the signer into the contract:

var provider = ether.providers.getDefaultProvider();
var signer = new ether.Wallet(privateKey, provider);
var contract = new ether.Contract(contractAddress, abi, signer);

After that, everything should work fine. Let me know though if there are any issues.

All 12 comments

Heya!

You need to attach the provider to the signer, and then pass the signer into the contract:

var provider = ether.providers.getDefaultProvider();
var signer = new ether.Wallet(privateKey, provider);
var contract = new ether.Contract(contractAddress, abi, signer);

After that, everything should work fine. Let me know though if there are any issues.

Thanks, man it works like magic. Am grateful that you ricmoo is the one to answer me on time. should my contract address have ether on it? because I am getting insufficient funds for gas * price + value
I would like to add a link to my project for a user to use cause I have built it in an API Format so all users or developers.

The contract does not require funds.

The EOA (Externally Owned Account) which is the address associated with the prvKey does require funds to execute the transfer, though.

Ok but I think do, how do I reduce the gas limit I think the limit is above $14

There is certainly something wrong there. What gas price is it using? You can override the gas price using:

`javascript var options = { gasPrice: 1100000000, // 1.1Gwei gasLimit: 250000 // Should be enough for more transfer transactions }; contract.transfer(targetAddress, value, options).then(function(tx) { console.log(tx); });

The default gas price should be fine, since it comes from the node, but sometimes it isn't quite right; that said $14 seems very high.

Error: too many parameters my transfer function only accept two parameters. Using the gasPrice promise with the Provider the gas price is 13000000000

Do you have the latest version? This bug was addressed over the last couple days.

All contract transaction functions can take in 1 optional additional parameter to override gasPrice, gasLimit, nonce and value.

(the node_modules/ethers/package.json version should be 2.2.4)

2.2.4 is what am using now what am getting now is

code | -32000
-- | --
url | "https://mainnet.infura.io/"
body | "{\"method\":\"eth_sendRawTransaction\",\"params\":[\"0xf8a910844190ab008303d0909428abc25b4d4530720b99e051e79899642c3291e380b844a9059cbb00000000000000000000000006ae947db37c62f545dedf844d3c91721c38613a000000000000000000000000000000000000000000000000000000000000138825a07f6e7070a094b609f7b9460f35b5daf20192a9c1ac938471f23820cd67039420a055a7c0eb8f06b408309b386ad3f94ed339c01714c725d1df620b229870130798\"],\"id\":42,\"jsonrpc\":\"2.0\"}"
responseText | "{\"jsonrpc\":\"2.0\",\"id\":42,\"error\":{\"code\":-32000,\"message\":\"known transaction: 93957894c7db9472585213b771cf0d0f932268a7eabed5172187b4c35e62d812\"}}\n"

this is my full code

var abi = require('./abi.json'), 
            add = 'Contract_address', 
            provider = ether.providers.getDefaultProvider();
            signer = new ether.Wallet("prvkey", provider);
        var contract = new ether.Contract(add, abi, signer);
        var options = {
            gasPrice: 1100000000, // 1.1Gwei
            gasLimit: 250000 // Should be enough for more transfer transactions
        };
        var callPromise = contract.transfer("addres_toReceive", "5000", options);
        callPromise.then(function(obj){
            res.send(obj)
        })

        callPromise.catch(function(error){
            res.send(error)
        });

Please assist .

That error means your transaction is already on the network, and just hasn't been mined yet.

Here is your transaction: https://etherscan.io/tx/0x93957894c7db9472585213b771cf0d0f932268a7eabed5172187b4c35e62d812

The network is currently quite congested, so it may take some time to confirm. You can see the current gas prices required here: https://ethgasstation.info

You can try replacing your transaction with a new one by using the same nonce and a gas price at least 50% more than the current transaction on the network.

Wow Thanks that is nice to know execllent.

Was this page helpful?
0 / 5 - 0 ratings