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:
| To | Write | Refused 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:
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.";
| State | When | What to show |
|---|---|---|
sent | at once | The person's part is done. It usually lands in about 10 seconds. Show the change on your page as on its way, not done. |
slow | after 30 seconds | It's taking longer than usual and should still go through. They can leave the page. |
late | after 5 minutes | It hasn't landed yet and may still. Offer "Check again" (sent.checkNow()), and let them try again if they want. |
| landed | wait() resolves | Done. 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
- While it's sent, slow or late, don't send it again on your own. It may still land, and a second copy would be refused or would apply twice.
- After
Lost, read the chain again: the person may not want the write any more. Build a new one from what the chain now holds. - After
NotRecorded, the write most likely didn't go through. Read the chain first; if it's still needed, the person can send it again.
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 ↗