Interface
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;
}
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;
}
}