0

So I have a Dapp and want to call a function from a contract deployed on Rinkeby. I use Metamask to interact with the blockchain.

When I use the functions in the console, they work, but when I want to call from the JS code, they don't work anymore. My Dapp works on port 3030.

Here's the JS code. Initializing web3:

import Web3 from 'web3';

export let web3;

if (typeof web3 !== 'undefined') {
  web3 = new Web3(web3.currentProvider);
} else {
  // set the provider you want from Web3.providers
  web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:3030'));
}

Here's the .js file with different functionalities:

import web3 from '../ethereum/web3.js';

var SapienTokenAddress = "0x9857D34e0a3Ea34518E6BC54eA32cfF544BA54b7"

var SapienTokenABI = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_stake","type":"address"}],"name":"enableTransferToContract","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_controller","type":"address"}],"name":"changeController","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"disableTransferToContract","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_amount","type":"uint256"}],"name":"increaseTotal","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_owned","type":"address"}],"name":"changeOwned","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"addToBalance","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_owned","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]

var SapienTokenBytecode = "60606040526040805190810160405280600b81526020017f53415049454e20434f494e00000000000000000000000000000000000000000081525060029080519060200190620000519291906200011b565b506040805190810160405280600381526020017f53504e0000000000000000000000000000000000000000000000000000000000815250600390805190602001906200009f9291906200011b565b50601260045560006005553415620000b657600080fd5b604051602080620011278339810160405280805190602001909190505080600660006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050620001ca565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106200015e57805160ff19168380011785556200018f565b828001600101855582156200018f579182015b828111156200018e57825182559160200191906001019062000171565b5b5090506200019e9190620001a2565b5090565b620001c791905b80821115620001c3576000816000905550600101620001a9565b5090565b90565b610f4d80620001da6000396000f3006060604052600436106100ba576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100bf57806318160ddd1461014d578063307820dc14610176578063313ce567146101af5780633cebb823146101d857806370a08231146102115780637c432ef21461025e57806381fa54311461027357806395d89b411461029657806397ee470014610324578063a9059cbb1461035d578063b08079a6146103b7575b600080fd5b34156100ca57600080fd5b6100d26103f9565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101125780820151818401526020810190506100f7565b50505050905090810190601f16801561013f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561015857600080fd5b610160610497565b6040518082815260200191505060405180910390f35b341561018157600080fd5b6101ad600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061049d565b005b34156101ba57600080fd5b6101c261058a565b6040518082815260200191505060405180910390f35b34156101e357600080fd5b61020f600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610590565b005b341561021c57600080fd5b610248600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506106b6565b6040518082815260200191505060405180910390f35b341561026957600080fd5b6102716106ff565b005b341561027e57600080fd5b61029460048080359060200190919050506107eb565b005b34156102a157600080fd5b6102a9610943565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156102e95780820151818401526020810190506102ce565b50505050905090810190601f1680156103165780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561032f57600080fd5b61035b600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506109e1565b005b341561036857600080fd5b61039d600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610b07565b604051808215151515815260200191505060405180910390f35b34156103c257600080fd5b6103f7600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610d17565b005b60028054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561048f5780601f106104645761010080835404028352916020019161048f565b820191906000526020600020905b81548152906001019060200180831161047257829003601f168201915b505050505081565b60005481565b600660009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663893d20e86000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561052b57600080fd5b6102c65a03f1151561053c57600080fd5b5050506040518051905073ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561057f57600080fd5b600160058190555050565b60045481565b600660009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663893d20e86000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561061e57600080fd5b6102c65a03f1151561062f57600080fd5b5050506040518051905073ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561067257600080fd5b80600760006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b600660009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663893d20e86000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561078d57600080fd5b6102c65a03f1151561079e57600080fd5b5050506040518051905073ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156107e157600080fd5b6000600581905550565b600660009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663893d20e86000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b151561087957600080fd5b6102c65a03f1151561088a57600080fd5b5050506040518051905073ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16148061091a57503373ffffffffffffffffffffffffffffffffffffffff16600760009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16145b151561092557600080fd5b61093a81600054610eea90919063ffffffff16565b60008190555050565b60038054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156109d95780601f106109ae576101008083540402835291602001916109d9565b820191906000526020600020905b8154815290600101906020018083116109bc57829003601f168201915b505050505081565b600660009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663893d20e86000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b1515610a6f57600080fd5b6102c65a03f11515610a8057600080fd5b5050506040518051905073ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141515610ac357600080fd5b80600660006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b60008073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1614151515610b4457600080fd5b3373ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff16141515610b7e57600080fd5b610bd082600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610f0890919063ffffffff16565b600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610c6582600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610eea90919063ffffffff16565b600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a36001905092915050565b600660009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663893d20e86000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b1515610da557600080fd5b6102c65a03f11515610db657600080fd5b5050506040518051905073ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161480610e4657503373ffffffffffffffffffffffffffffffffffffffff16600760009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16145b1515610e5157600080fd5b610ea381600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054610eea90919063ffffffff16565b600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055505050565b6000808284019050838110151515610efe57fe5b8091505092915050565b6000828211151515610f1657fe5b8183039050929150505600a165627a7a723058201a2c226e36e25d3037ffbea5afd48f008f5a65e4e0f3d334a02f995d1c81ef620029"

export function getCurrentPublicAddress() {

  web3.eth.getAccounts(function(error, result){
      if(!error)
        return result[0];
      else
        return error;
  });

}

export function getCurrentSPN() {

  var spnCoinContract = web3.eth.contract(SapienTokenABI).at(SapienTokenAddress);

  var notConnected = notWeb3Connected();

  if (!notConnected) {

  return spnCoinContract.balanceOf.call(String(getCurrentPublicAddress()), function(error, result){

    if (!error)
      console.log(result);

    else {
        console.log(error);
      }

    });

  }

}

export function notWeb3Connected() {

// Check the connection
if(!web3.isConnected()) {

  console.error("Not connected");

  return true;

}

return false;

}

And in another file I import the functions and console log their results. For example, I do this:

import { getCurrentPublicAddress, getCurrentSPN } from '/imports/lib/pudding/manageSPN.js';

And this:

console.log(getCurrentAddress());
console.log(getCurrentSPN());

When I call the functions, they throw errors:

enter image description here

enter image description here

P.S: I use meteor -p 3030 to initialize my app. P.P.S: With getCurrentPublicAddress I get the public key from Metamask. With the other function I get the amount of tokens that I minted for this specific account.

  • When initializing HttpProvider you should pass the address of your own instance of geth, or use a public provider like infura.io. – Ismael Nov 7 '17 at 16:53
0

It seems that in your getCurrentSPN() function, you are referencing a web3 object that is maybe not fully initialized.

I'm not clear what you're importing in that file, it just says import web3 from '../ethereum/web3.js';, which to me is a little strange, usually what you import is the Web3 class, not a web3 instance.

I assume you're trying to import it from export let web3;, but my understanding is that when you import a value, that is a read-only view into that module, you cannot mutate it and have that result propogate to the importing modules.

If you want to export a web3 instance from that first file, you should probably export an object, and have one of its properties update once web3 is detected, or just do web3 detection in each individual module.

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.