MapStorage

Interface
Persistent smart contract storage. Only usable as a SmartContract property. See the Properties and Storage chapter of the main guide for more information.
export interface MapStorage<K extends SerializableKey, V extends SerializableValue> extends Iterable<[K, V]> { 
  readonly [Symbol.iterator]: () => IterableIterator<[K, V]>; 
  readonly forEach: (callback: (value: V, key: K) => void) => void; 
  readonly get: (key: K) => V | undefined; 
  readonly has: (key: K) => boolean; 
  readonly delete: (key: K) => boolean; 
  readonly set: (key: K, value: V) => MapStorage<K, V>; 
  readonly at: K extends [SK, SK] 
    ? SKMapAtTwo<K, V> 
    : K extends [SK, SK, SK] 
    ? SKMapAtThree<K, V> 
    : K extends [SK, SK, SK, SK] 
    ? SKMapAtFour<K, V> 
    : never; 
  readonly [OpaqueTagSymbol0]: unique symbol; 
} 

Properties

at
K extends [SK, SK] ? SKMapAtTwo<K, V> : K extends [SK, SK, SK] ? SKMapAtThree<K, V> : K extends [SK, SK, SK, SK] ? SKMapAtFour<K, V> : never
Returns the elements from storage with the specified prefix.

Static Methods

for

Constructs a new MapStorage instance. Only usable as a SmartContract property.
for<K extends SerializableKey, V extends SerializableValue>(): MapStorage<K, V>; 

Methods

__@iterator

() => IterableIterator<[K, V]> 

forEach

Executes a provided function once per each key/value pair in storage.
(callback: (value: V, key: K) => void) => void 

get

Returns a specified element from storage.
(key: K) => V | undefined 

has

Returns a boolean indicating whether an element with the specified key exists or not.
(key: K) => boolean 

delete

Removes the specified element from storage.
(key: K) => boolean 

set

Adds or updates an element with a specified key and value in storage.
(key: K, value: V) => MapStorage<K, V> 

Example

class Token extends SmartContract { 
 private readonly balances = 
   MapStorage.for<Address, Fixed<8>>(); 
 public transfer( 
   from: Address, 
   to: Address, 
   amount: Fixed<8>, 
 ): boolean { 
   const fromBalance = this.balances.get(from); 
   const toBalance = this.balances.get(to); 
   this.balances.set(from, fromBalance - amount); 
   this.balances.set(to, toBalance + amount); 
   return true; 
 } 
} 
  • @neo-one/client
  • @neo-one/smart-contract
DOCS
InstallationMain ConceptsAdvanced GuidesAPI ReferenceContributing
CHANNELS
GitHubStack OverflowDiscord ChatTwitterYouTube
COPYRIGHT © 2021 NEO•ONE