Skip to content
Dave Cheli edited this page Jul 17, 2019 · 1 revision

Web3 How-to's This shows some how-to's with using Web3 to access your solidity contract.

Technologies used

Web3 1.0.0-beta.55
etherumjs-tx 1.3.7

Accessing public properties in a solidity contract

Properties that are declared as public have getters automatically generated for them in Solidity as a convenience. Consider the Solidity code sample below:

uint8 public version;
struct User {
    string name;
    bytes8 birthDate;
}

bytes[] public myArray;
mapping(bytes32=>uint256) public flatMap;
mapping(bytes32=>User) public mapToStruct;
mapping(bytes32=>uint256[]) public mapToArray;

To access the simple property version, you can use this code:

let v = await <yourcontract>.methods.version.call({ 
    from:web3.eth.defaultAccount,  
    gas: 4000000, 
    gasPrice: 50000000});
console.log(“contract version is: “, v); 

Solidity does not return dynamic arrays. To access elements of an array such as myArray, you need to access each item individually. In order to get the length of the array, the contract will need to provide a function for that. Here is how you can access the first element of an array. Notice the index 0 that is being passed.

let e = await <yourcontract>.methods.myArray(0).call( { 
    from:web3.eth.defaultAccount,  
    gas: 4000000, 
    gasPrice: 50000000}) ;

console.log(“The first element is: “, e); 

To access a map, you need to provide a key. If a key is not found, the getter will return a bunch of 0's like 0x0000...000. To access the flatMap property you can use the following code:

let key = “0x867fyr659..yrt655” 

let k = await <yourcontract>.methods.flatMap(key).call( { 
    from:web3.eth.defaultAccount,  
    gas: 4000000, 
    gasPrice: 50000000});

console.log(“the key returned the following item “, k); 

Maps sometimes point to structs as in mapToStruct. If a key is provided that matches an entry the individual values of the struct will be returned in a tuple. To access the return values, you can either use the actual name of the property in the struct or an index, such as [0] for the first element and [1] for the second. Here is some code you can use:

let key = “0x867fyr659..yrt655” 

let user= await <yourcontract>.methods.mapToStruct(key).call( { 
    from:web3.eth.defaultAccount, 
    gas: 4000000, 
    gasPrice: 50000000});

console.log(“Name is “, user.name, “ birth date is “, user.birthDate); 
console.log(“Name is “, user[0], “ birth date is “, user[1]); 

Often, maps contain arrays as in mapToArray. Keeping in mind that Solidity does not return arrays, you will need to access each element individually. Below is a way to access the first element for the provided key in the mapToArray property. Notice the index 0 provided after the key.

let key = “0x867fyr659..yrt655” 

let elem = await <yourcontract>.methods.mapToUser(key, 0).call( { 
    from:web3.eth.defaultAccount,  
    gas: 4000000, 
    gasPrice: 50000000}) 

console.log(“The first element is “, elem); 
Clone this wiki locally