selfhold

SELFHOLD / FOR DEVELOPERS Guide pages ↓

Write records and wait for them to land

A write changes records on one chain, and can pay addresses in the same transaction. The person approves it in Selfhold; then the chain records it, usually within seconds.

Send a write

button.onclick = async () => {
  const sent = await wallet.writeRecords({
    chainPath: ["Nexus", "your-chain"],
    actions: [{ key: "profile/bio", newValue: "Hello" }],
  }, { account: address });
  if (!sent) return; // nothing to send: see below
  await sent.wait();
};

Actions

Each action is a compare-and-swap on one key. The chain refuses the whole write if any action doesn't match what it holds:

ToWriteRefused if
Add a key{ key, newValue }the key exists
Change a key{ key, oldValue, newValue }the key doesn't hold oldValue
Delete a key{ key, oldValue }the key doesn't hold oldValue

So read the current values just before you build the write. A chain may also have its own rules for which records it accepts.

Payments

Add payments: [{ to, amount }] to pay addresses from the account in the same transaction. Amounts are strings of whole coins, such as "100". Selfhold shows the person every payment before they approve, and warns about any address that no record in the write names.

Read first, without losing the click

If you need to read the chain to build the write, pass a promise. The window opens at once, in the click, and waits for it:

button.onclick = () => wallet.writeRecords(readState().then(buildWrite), { account: address });

Resolve it to null when there's nothing to change: the window closes and writeRecords() resolves to null.

Progress

Selfhold's window tells you what it's doing, such as paying the fee with this device's work. Show it:

await wallet.writeRecords(request, { progress: (text) => status.textContent = text });

Wait for it to land

writeRecords() resolves once the write is sent: signed, and taken by a node. It lands when a block records it. sent.wait() follows it there:

A sent write's states Sent, then slow after 30 seconds, then late after 5 minutes. From any of them it can land. After 30 minutes it ends as not recorded, and if another write took its place it ends as lost. sentslowlatelandedNotRecorded 30 s5 minit lands30 min
From any state, the write can land, and wait() resolves. After 30 minutes it rejects with NotRecorded. If your check says another write took its place, it rejects with Lost.
const words = {
  sent: "Sent. Your device's part is done. The network usually records it in about 10 seconds.",
  slow: "Taking longer than usual. It's sent and should still go through.",
  late: "The network hasn't recorded it yet. It may still go through.",
};
await sent.wait({ onChange: (state) => status.textContent = words[state] });
status.textContent = "Saved.";
StateWhenWhat to show
sentat onceThe person's part is done. It usually lands in about 10 seconds. Show the change on your page as on its way, not done.
slowafter 30 secondsIt's taking longer than usual and should still go through. They can leave the page.
lateafter 5 minutesIt hasn't landed yet and may still. Offer "Check again" (sent.checkNow()), and let them try again if they want.
landedwait() resolvesDone. Draw your page from what the chain now holds.

Decide "landed" from your own state

By default, wait() reads the account's nonce at the host the write went to and at your hosts, and calls the write landed once the nonce has passed it. That can't tell this write from another the same account signed elsewhere at the same nonce. Where that matters, pass a check:

// previous: the bio as you read it when you built the write.
await sent.wait({
  check: async () => {
    const bio = await readRecord("profile/bio");
    if (bio === "Hello") return true;           // landed
    if (bio !== previous) return "Your bio was changed somewhere else."; // another write took its place
    return false;                               // not yet
  },
});

Return a string when another write landed in this one's place. A host that lags can mislead one read, so wait() rejects with Lost only after three reads in a row agree.

Trying again safely

To stop waiting, for example when the person leaves the page, pass an AbortSignal as signal.

Something wrong or missing here? Report it on GitHub ↗