feat: add read api

This commit is contained in:
Kyle Fang
2025-02-23 15:03:45 +00:00
parent f1e38b18ba
commit 9c33de794d
3 changed files with 38 additions and 94 deletions

View File

@@ -83,55 +83,35 @@ const processor = new BatchProcessor({
// Queue multiple operations that will be batched together // Queue multiple operations that will be batched together
const [resultA, resultB] = await Promise.all([ const [resultA, resultB] = await Promise.all([
new Promise((resolve, reject) => { processor.read({
processor.enqueue({ mode: 'variable',
request: { contractAddress: 'SP...',
mode: 'variable', contractName: 'my-contract',
contractAddress: 'SP...', variableName: 'variable-a'
contractName: 'my-contract',
variableName: 'variable-a'
},
resolve,
reject
});
}), }),
new Promise((resolve, reject) => { processor.read({
processor.enqueue({ mode: 'variable',
request: { contractAddress: 'SP...',
mode: 'variable', contractName: 'my-contract',
contractAddress: 'SP...', variableName: 'variable-b'
contractName: 'my-contract',
variableName: 'variable-b'
},
resolve,
reject
});
}) })
]); ]);
// You can also queue different types of operations // You can also queue different types of operations
processor.enqueue({ processor.read({
request: { mode: 'readonly',
mode: 'readonly', contractAddress: 'SP...',
contractAddress: 'SP...', contractName: 'my-contract',
contractName: 'my-contract', functionName: 'get-value',
functionName: 'get-value', functionArgs: []
functionArgs: []
},
resolve: (value) => console.log('Function result:', value),
reject: (error) => console.error('Error:', error)
}); });
processor.enqueue({ processor.read({
request: { mode: 'mapEntry',
mode: 'mapEntry', contractAddress: 'SP...',
contractAddress: 'SP...', contractName: 'my-contract',
contractName: 'my-contract', mapName: 'my-map',
mapName: 'my-map', mapKey: someKey
mapKey: someKey
},
resolve: (value) => console.log('Map entry:', value),
reject: (error) => console.error('Error:', error)
}); });
``` ```

View File

@@ -60,6 +60,12 @@ export class BatchProcessor {
return tip ?? '_undefined'; return tip ?? '_undefined';
} }
read(request: BatchRequest): Promise<ClarityValue | OptionalCV> {
return new Promise((resolve, reject) => {
this.enqueue({ request, resolve, reject });
});
}
enqueue(request: QueuedRequest): void { enqueue(request: QueuedRequest): void {
const queueKey = this.getQueueKey(request.tip); const queueKey = this.getQueueKey(request.tip);

View File

@@ -82,66 +82,24 @@ async function batchQueueProcessorExample() {
}); });
const promiseA = new Promise((resolve, reject) => { const promiseA = processor.read({
processor.enqueue({ mode: 'variable',
request: { contractAddress: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275',
mode: 'variable', contractName: 'liquidity-token-v5kbe3oqvac',
contractAddress: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275', variableName: 'balance-x',
contractName: 'liquidity-token-v5kbe3oqvac',
variableName: 'balance-x',
},
resolve: resolve,
reject: reject,
});
}); });
const promiseB = new Promise((resolve, reject) => { const promiseB = processor.read({
processor.enqueue({ mode: 'variable',
request: { contractAddress: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275',
mode: 'variable', contractName: 'liquidity-token-v5kbe3oqvac',
contractAddress: 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275', variableName: 'balance-y',
contractName: 'liquidity-token-v5kbe3oqvac',
variableName: 'balance-y',
},
resolve: resolve,
reject: reject,
});
}); });
const result = await Promise.all([promiseA, promiseB]); const result = await Promise.all([promiseA, promiseB]);
console.log(result); console.log(result);
} }
// https://explorer.hiro.so/txid/SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9.trait-sip-010
const sip010 = {
functions: [
{
name: 'get-balance',
access: 'read_only',
args: [
{
name: 'who',
type: 'principal',
},
],
outputs: {
type: {
response: {
ok: 'uint128',
error: 'none',
},
},
},
},
],
variables: [],
maps: [],
fungible_tokens: [],
non_fungible_tokens: [],
epoch: 'Epoch2_05',
clarity_version: 'Clarity1',
} as const;
async function batchSip010Example() { async function batchSip010Example() {
const supply = callReadonly({ const supply = callReadonly({
abi: SIP010TraitABI.functions, abi: SIP010TraitABI.functions,