user2447290
user2447290

Reputation:

Ways to get the value of mapping public?

I have this piece of code, but I dont have implemented a view returns function.

mapping (uint256 => TokenTimeLockInfo) public locks;
struct TokenTimeLockInfo {
    Token token;
    address beneficiary;
    uint256 amount;
    uint256 unlockTime;
}

I can access the value way web3js are possible?

Or do I need another contract to implement a view returns?

Upvotes: 1

Views: 3327

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43561

Since the property is public, you can access it using the getter function automatically generated by Solidity compiler.

const myContract = new web3.eth.Contract(abiJson, contractAddress);

// returns value of the mapping for the key `0`
const info = await myContract.methods.locks(0).call();

Docs:

Upvotes: 3

Related Questions