0

I'm following a guide that uses some web3 code written 3 years ago.

I am trying to use the TimeLockedWallet.info() call to get information about a wallet.

Below are my two smart contracts.

TimeLockedWalletFactory.sol

pragma solidity ^0.4.18;

import "./TimeLockedWallet.sol";

contract TimeLockedWalletFactory {

    mapping(address => address[]) wallets;

    function getWallets(address _user)
        public
        view
        returns(address[])
    {
        return wallets[_user];
    }

    function newTimeLockedWallet(uint256 _unlockDate)
        payable
        public
        returns(address wallet)
    {
        // Create new wallet.
        wallet = new TimeLockedWallet(msg.sender, _unlockDate);

        // Add wallet to sender's wallets.
        wallets[msg.sender].push(wallet);

        // Send ether from this transaction to the created contract.
        wallet.transfer(msg.value);

        // Emit event.
        Created(wallet, msg.sender, now, _unlockDate, msg.value);
    }

    // Prevents accidental sending of ether to the factory
    function () public {
        revert();
    }

    event Created(address wallet, address to, uint256 createdAt, uint256 unlockDate, uint256 amount);
}

TimeLockedWallet.sol

pragma solidity ^0.4.18;

contract TimeLockedWallet {
    address public owner;
    uint256 public unlockDate;
    uint256 public createdAt;

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    function TimeLockedWallet(address _owner, uint256 _unlockDate) public {
        owner = _owner;
        unlockDate = _unlockDate;
        createdAt = now;
    }

    // keep all the ether sent to this address
    function() public payable {
        Received(msg.sender, msg.value);
    }

    // callable by owner only, after specified time
    function withdraw() public onlyOwner {
        require(now >= unlockDate);
        //now send all the balance
        msg.sender.transfer(this.balance);
        Withdrew(msg.sender, this.balance);
    }

    function info()
        public
        view
        returns (
            uint256,
            uint256,
            uint256
        )
    {
        return (unlockDate, createdAt, this.balance);
    }

    event Received(address from, uint256 amount);
    event Withdrew(address to, uint256 amount);
    event WithdrewTokens(address tokenContract, address to, uint256 amount);
}

And my frontend code tries to get information about the wallet from TimeLockedWallet.sol (unlockDate, owner, createdAt) using the contract.at() function. However, I get an error

VM3368:1 Uncaught TypeError: c_timeLockedWallet.at is not a function

App.js

import TimeLockedWallet from "./contracts/TimeLockedWallet.json";
import TimeLockedWalletFactory from "./contracts/TimeLockedWalletFactory.json";

const c_timeLockedWalletFactory = new web3.eth.Contract(
  TimeLockedWalletFactory.abi,
  deployedNetwork && deployedNetwork.address,
);
const c_timeLockedWallet = new web3.eth.Contract(
  TimeLockedWallet.abi,
);

const accounts = await web3.eth.getAccounts();

await c_timeLockedWalletFactory.methods.newTimeLockedWallet(unixTime).send({ from: accounts[0], value: web3.utils.toWei("1", "ether") });
const wallets = await c_timeLockedWalletFactory.methods.getWallets(accounts[0]).call();


c_timeLockedWallet.at(wallets[0]).then(function(walletInstance){return walletInstance.info();}).then(function(info){console.log(info)})

Is there another method to use to make this call? I've tried reading the web3 docs, but have come up short.

2

I figured it out!

The API changed from

var contract = web3.eth.contract(ABI).at(contractAddress);

to

var contract = new web3.eth.Contract(abi, contractAddress)

So instead of my code

const c_timeLockedWallet = new web3.eth.Contract(
  TimeLockedWallet.abi,
);
c_timeLockedWallet.at(wallets[0]).then(function(walletInstance){return walletInstance.info();}).then(function(info){console.log(info)})

it should be

const c_timeLockedWallet = new web3.eth.Contract(
  TimeLockedWallet.abi, wallets[0]
);
c_timeLockedWallet.methods.info().call().then(console.log)

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.