Skip to main content
Universal constructor for creating BrandedStorageKey from various input types.

Signature

    Parameters

    • input (StorageKeyLike) - Input value to convert to storage key

    Returns

    BrandedStorageKey - Storage key instance

    Type Definition

    type StorageKeyLike =
      | BrandedStorageKey
      | {
          address: AddressType;
          slot: bigint;
        };
    

    Examples

    From Object

      From Existing StorageKey

      import * as State from 'tevm/State';
      
      const originalKey = State.StorageKey(contractAddr, 42n);
      
      // Identity operation (returns same structure)
      const key = State(originalKey);
      
      console.log(State.StorageKey.equals(originalKey, key)); // true
      

      Validation

      import * as State from 'tevm/State';
      
      try {
        // Invalid: missing slot
        const key = State({
          address: contractAddr
          // TypeScript error: Property 'slot' is missing
        });
      } catch (error) {
        console.error('Invalid storage key input');
      }
      
      try {
        // Invalid: slot must be bigint
        const key = State({
          address: contractAddr,
          slot: 42 // TypeScript error: Type 'number' is not assignable to type 'bigint'
        });
      } catch (error) {
        console.error('Slot must be bigint');
      }
      

      Type Safety

      from() provides compile-time type checking:
      import * as State from 'tevm/State';
      import * as Address from 'tevm/Address';
      
      // ✅ Valid inputs
      State({ address: contractAddr, slot: 0n });
      State(existingStorageKey);
      
      // ❌ Invalid inputs (compile-time errors)
      State({ address: "0x...", slot: 0n }); // address must be AddressType
      State({ address: contractAddr });       // missing slot
      State({ address: contractAddr, slot: 0 }); // slot must be bigint
      State({ slot: 0n });                    // missing address
      State("string");                        // not StorageKeyLike
      State(123);                             // not StorageKeyLike
      

      Use Cases

      Generic Storage Operations

      import * as State from 'tevm/State';
      
      // Function accepting various input types
      function getStorageValue(
        keyInput: StorageKeyLike,
        storage: Map<string, bigint>
      ): bigint | undefined {
        const key = State(keyInput);
        const keyStr = State.StorageKey.toString(key);
        return storage.get(keyStr);
      }
      
      // Works with object
      const value1 = getStorageValue({
        address: contractAddr,
        slot: 0n
      }, storage);
      
      // Works with StorageKey
      const storageKey = State.StorageKey(contractAddr, 0n);
      const value2 = getStorageValue(storageKey, storage);
      

      API Wrapper

      import * as State from 'tevm/State';
      
      class StateManager {
        private storage = new Map<string, bigint>();
      
        set(keyInput: StorageKeyLike, value: bigint): void {
          const key = State(keyInput);
          this.storage.set(State.StorageKey.toString(key), value);
        }
      
        get(keyInput: StorageKeyLike): bigint | undefined {
          const key = State(keyInput);
          return this.storage.get(State.StorageKey.toString(key));
        }
      }
      
      const manager = new StateManager();
      
      // Set using object
      manager.set({ address: contractAddr, slot: 0n }, 1000n);
      
      // Get using StorageKey
      const key = State.StorageKey(contractAddr, 0n);
      const value = manager.get(key); // 1000n