You can implement simple arrays in Tact by using the map
type.
To create an array, define a map with an Int
type as the key. This key will represent the index in the array. Additionally, include a variable to keep track of the number of items in the array.
The example contract records the last five timestamps when the timer
message was received. These timestamps are stored in a cyclical array, implemented as a map.
Maps are designed to hold a limited number of items. Only use a map if you know the maximum number of items it will contain. It's also a good idea to write a test to populate the map with its maximum number of elements and observe how gas consumption behaves under stress.
If the number of items is unbounded and could potentially grow into the billions, you will need to architect your contract differently. We will discuss unbounded arrays later, under the topic of contract sharding.
All Examplesimport "@stdlib/deploy"; contract CurrentTime with Deployable { deployTime: Int as uint32; init() { self.deployTime = now(); // returns unix time, the number of seconds since the epoch } receive("wait 10s") { require(now() - self.deployTime > 10, "Did not wait long enough"); dump("thanks for waiting 10 seconds"); } receive("wait 10d") { require(now() - self.deployTime > 10*24*60*60, "Did not wait long enough"); dump("thanks for waiting 10 days"); } get fun unixTime(): Int { return now(); } get fun stringTime(): String { let sb: StringBuilder = beginString(); sb.append(now().toString()); sb.append(" seconds elapsed since 1 January 1970"); return sb.toString(); } }