Smart contracts are code segments, which are placed and executed on Blockchain nodes.
This article is about data storage options within smart contracts written in Solidity for TRON. We'll look at some of the Value and Reference Types (Arrays & Structs) available.
TRON IDE currently uses Solidity v.0.8.6. The most actual Solidity version is v.0.8.12. Be aware that this is a fast-evolving space, which is exciting and requires us to keep learning!
Value Types
bool - true or false
pragma solidity >=0.7.0 <0.9.0;
contract Samples {
bool private trueorfalse;
function get() public view returns (bool) {
return trueorfalse;
}
function setTrue() public {
trueorfalse = true;
}
function setFalse() public {
trueorfalse = false;
}
}
int / uint - integer values like we know them from any other programming language
pragma solidity >=0.7.0 <0.9.0;
contract Samples {
uint public uintvalue;
int public intvalue;
function getIntValue() public view returns (int) {
return intvalue;
}
function getUIntValue() public view returns (uint) {
return uintvalue;
}
function setuintvalueMax() public {
uintvalue = type(uint).max;
}
function setintvalueMin() public {
intvalue = type(int).min;
}
}
fixed / ufixed - those are fixed-point values/numbers, but be aware that they are not yet fully supported as of Solidity v0.8.12.
address - address of a 20-byte size, which equals an Ethereum address. But TRON has 21-byte addresses.
The following is a sample from the TRON Developer Tutorial illustrating address conversion :
/**
* @dev convert uint256 (HexString add 0x at beginning) tron address to solidity address type
* @param tronAddress uint256 tronAddress, begin with 0x, followed by HexString
* @return Solidity address type
*/
function convertFromTronInt(uint256 tronAddress) public view returns(address) {
return address(tronAddress);
}
pragma solidity >=0.7.0 <0.9.0;
contract Samples {
fixed public fixValue;
ufixed public ufixvalue;
address public adresse;
uint256 public tronAddress;
}
bytes1 ... bytes32 - fixed-sized byte arrays with size 1 to 32
string - Strings, comparable to similar functionality in Java, but with less functionality attached to it. Keep in mind that TRON still uses Solidity v0.8.6 (at beginning of March), hence some newer functionality like concat is not yet available.
pragma solidity >=0.7.0 <0.9.0;
contract Samples {
bytes1 public byteA = 0xa5;
bytes1 public byteB = 0x24;
string name = "Hannu";
}
Arrays
Arrays, as we know them from other programming languages. They can have fixed or dynamic sizes. bytes and string are arrays as well, but index access is not possible for string.
pragma solidity >=0.7.0 <0.9.0;
contract Samples {
int[] public dynamicSizeArray;
int[10] public fixSizeArray;
// add an element to the array using a member
function push(int i) public {
arr.push(i);
}
// access an element - might cause some issues if we don't have a long enough array!
function retrieveElement(int i) public view returns (int) {
return arr[i];
}
}
Some array members are:
- length - retrieves the length of the array.
- push - as used above adds an element at the end of an array.
- pop - removes the element at the end of an array.
Struct Types
Structures are groupings of variables without any functionality. They cannot be recursive.
The following is an INCORRECT example and will return a "TypeError: Recursive struct definition."
contract Samples {
struct Person {
string firstname;
string lastname;
Person[] children;
Person spouse;
}
}
Enum Types
Enums enable you to create your own type with your own defined values.
pragma solidity >=0.7.0 <0.9.0;
contract Samples {
enum Coworker { Lazy, Smart, Diligent, Boring, Funny }
}
Discussion (0)