const RPC_URL = process.env.SIGIL_RPC_URL ?? 'https://testnet-rpc.sigil.ml/rpc';
async function rpc<T>(method: string, params: unknown): Promise<T> {
const res = await fetch(RPC_URL, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }),
});
const body = await res.json() as { result?: T; error?: { code: number; message: string } };
if (body.error) throw new Error(`${body.error.code}: ${body.error.message}`);
if (body.result === undefined) throw new Error('missing result');
return body.result;
}
async function nativeBalance(did: string) {
return rpc<{ did: string; balance: number; nonce: number }>('sigil_getBalance', { did });
}
async function tokenBalance(did: string, tokenId: string) {
return rpc<{ did: string; token_id: string; balance: string }>(
'sigil_getAccount',
{ did, token_id: tokenId },
);
}
const did = process.env.QUERY_DID ?? 'did:oas:sigil:agent:alice...';
console.log(await nativeBalance(did));
console.log(await tokenBalance(did, 'tok:demo:points'));