Skip to content

Creating an Entity

In this example, we'll show you how to add a new entity to a running Golem-base Op-Geth node.

Tip: We'll be using a live, test node!

Here we're assuming you:

  1. Have a private.key file that contains an Ethereum address. If not, read here on how to create one.

  2. Add funds. If you need to do so but aren't sure how, check out this page.

Now, at the command prompt, add in the golem-base-sdk, along with two helper packages:

pip install golem_base_sdk asyncio anyio

Next, create a file called main.py and add the following code to it; adjust the /path/to to point to wherever you have your private.key stored. (If you want to use XDG so it's in a common config location, check out this example, and add such code here.)

import asyncio
import anyio
from golem_base_sdk import ( Annotation, GolemBaseClient, GolemBaseCreate )

async def main() -> None:
    with open('/path/to/private.key', 'rb') as private_key_file:
        key_bytes = private_key_file.read(32)

    client = await GolemBaseClient.create_rw_client(
        rpc_url='https://kaolin.holesky.golem-base.io/rpc',
        ws_url='wss://kaolin.holesky.golem-base.io/rpc/ws',
        private_key=key_bytes,
    )

    create_receipt = await client.create_entities(
        [GolemBaseCreate(b"hello", 60, [Annotation("app", "demo")], [])]
    )

    print(f"Entity hash: {create_receipt[0].entity_key.as_hex_string()}")
    print(f"Expires at: {create_receipt[0].expiration_block}")

    await client.disconnect()

if __name__ == "__main__":
    asyncio.run(main())

The above code demonstrates how to store a single entity. But with Golem-Base, you can store multiple entities. Here's an example where we store two entities:

Example 2: Creating multiple entities

import asyncio
import anyio
from golem_base_sdk import ( Annotation, GolemBaseClient, GolemBaseCreate )

async def main() -> None:
    with open('/path/to/private.key', 'rb') as private_key_file:
        key_bytes = private_key_file.read(32)

    client = await GolemBaseClient.create_rw_client(
        rpc_url='https://kaolin.holesky.golem-base.io/rpc',
        ws_url='wss://kaolin.holesky.golem-base.io/rpc/ws',
        private_key=key_bytes,
    )

    create_receipts = await client.create_entities([
        GolemBaseCreate(b"hello", 60, [Annotation("app", "demo")], []),
        GolemBaseCreate(b"Greetings!", 60,
            [Annotation("name", "greeting"), Annotation("type","app")],  # String annotations
            [Annotation("version",20)] # Numeric annotations
        )
    ])

    for receipt in create_receipts:
        print(f"Entity hash: {receipt.entity_key.as_hex_string()}")
        print(f"Expires at: {receipt.expiration_block}")
        print()

    await client.disconnect()

if __name__ == "__main__":
    asyncio.run(main())