Example exchange
One exchange, its stock pages: the code below is the code this page runs on its server, and the example beneath it shows the stock pages the example exchange lists, the one badge beside each stock whose company carries benefits, and why a stock wears none.
How this is built
The printed call is the one this page's read makes when it reads: it connects the SDK's client and asks the availability read for every stock the example exchange lists, one request for the whole list. The ask names the market the example asks in, the two-letter country the demo world keeps its offers in. The printed code carries a placeholder where the key stands; this page's own key never reaches it or your browser.
import { connectDistributorApi } from "@yutai/sdk-web/client";
const client = connectDistributorApi({ base: "https://api.yutai.com", key: "ytk_your-key", country: "PT" });
const answer = await client.askAvailability(["US0378331005", "US5801351017", "US6541061031", "US8552441094", "US0090661010", "US2546871060", "US67066G1040", "US88160R1014", "US5949181045", "US0231351067", "US02079K3059", "US30303M1027", "US19260Q1076"]);GET /v1/availabilitythe benefits each stock the example lists carries, one batched read for the whole listGET /v1/eligibilitythe benefits the example's own customer's wallet reaches, the read the holder's view drawsPOST /v1/claims/challengethe words the customer's wallet signs, before the claim is askedPOST /v1/claimsthe claim, recorded on the proof the wallet signedGET /v1/claims/{id}the claim's own state, in the words the read answersGET /v1/fulfilmentsthe benefits waiting on the exchange's own workerPOST /v1/fulfilments/{id}/confirmthe exchange's worker confirming the credit with its own reference
The example
The live example is not wired yet: its key is not set, so nothing was read.
The claim flow
A benefit the example lists is one its holder can claim. The claim flow is the exchange's own: its customer's wallet signs the one message Yutai asks for, Yutai records the claim on that proof, and the benefit's credit waits on the exchange until its own worker confirms it, so the claim reads waiting on the venue until then and credited by the venue after.
The flow's own code, the order the presses run it in: the two asks the SDK's client carries, then the read and the worker pass, the calls the panel above names.
import { connectDistributorApi, refusedSentenceOf } from "@yutai/sdk-web/client";
const key = "ytk_your-key";
const client = connectDistributorApi({ base: "https://api.yutai.com", key, country: "PT" });
const headers = { Authorization: `Bearer ${key}`, Accept: "application/json" };
// The words the customer's wallet signs, before the claim is asked.
const challenge = await client.askClaimChallenge(benefitId, [wallet]);
const signature = await wallet.signMessage(challenge.message);
// The claim, recorded on that proof.
const answer = await client.askClaim({ benefitId, wallet, nonce: challenge.nonce, signature });
if (answer.status !== "claimed") throw new Error(refusedSentenceOf(answer.reason));
// The claim's own state, in the words the read answers.
const claim = await fetch(`https://api.yutai.com/v1/claims/${answer.claimId}`, { headers }).then((read) => read.json());
// When the benefit waits on the exchange's own worker: the poll, then the confirm with its own reference.
if (claim.state === "waiting on the venue") {
const pending = await fetch(`https://api.yutai.com/v1/fulfilments`, { headers }).then((poll) => poll.json());
const row = pending.fulfilments.find((row) => row.claimId === answer.claimId);
await fetch(`https://api.yutai.com/v1/fulfilments/${row.fulfilmentId}/confirm`, {
method: "POST",
headers: { ...headers, "Content-Type": "application/json" },
body: JSON.stringify({ venueReference: `Exchange credit for claim ${answer.claimId}` }),
});
}