feat: remove batch readonly

This commit is contained in:
Kyle Fang
2025-02-23 02:20:11 +00:00
parent 246b0e44f6
commit c42c475c48
4 changed files with 83 additions and 184 deletions

View File

@@ -29,11 +29,7 @@ const result = await batchRead({
contract: contractPrincipalCV(...),
mapName: 'my-map',
mapKey: someCV
}]
});
// Batch readonly function calls
const readonlyResult = await batchReadonly({
}],
readonly: [{
contract: contractPrincipalCV(...),
functionName: 'my-function',

View File

@@ -9,7 +9,6 @@ import {
type ClarityValue,
type ContractPrincipalCV,
deserializeCV,
type OptionalCV,
serializeCV,
} from '@stacks/transactions';
@@ -134,55 +133,3 @@ export async function batchRead(
readonly: convertResults(rs.readonly),
};
}
export interface BatchReadonlyRequest {
readonly: {
contract: ContractPrincipalCV;
functionName: string;
functionArgs: ClarityValue[];
}[];
index_block_hash?: string;
}
export async function batchReadonly(
req: BatchReadonlyRequest,
options: BatchApiOptions = {}
): Promise<(ClarityValue | Error)[]> {
const payload = req.readonly.map((r) => [
serializeCV(r.contract),
r.functionName,
...r.functionArgs.map((arg) => serializeCV(arg)),
]);
const ibh =
req.index_block_hash == null
? null
: req.index_block_hash.startsWith('0x')
? req.index_block_hash.substring(2)
: req.index_block_hash;
const url = `${options.stxerApi ?? DEFAULT_STXER_API}/sidecar/v2/batch-readonly${ibh == null ? '' : `?tip=${ibh}`
}`;
const data = await fetch(url, {
method: 'POST',
body: JSON.stringify(payload),
});
const text = await data.text();
if (!text.includes('Ok') && !text.includes('Err')) {
throw new Error(
`Requesting batch readonly failed: ${text}, url: ${url}, payload: ${JSON.stringify(
payload
)}`
);
}
const results = JSON.parse(text) as [{ Ok: string } | { Err: string }];
const rs: (ClarityValue | Error)[] = [];
for (const result of results) {
if ('Ok' in result) {
rs.push(deserializeCV(result.Ok));
} else {
rs.push(new Error(result.Err));
}
}
return rs;
}

View File

@@ -4,10 +4,10 @@ import {
stringAsciiCV,
tupleCV,
uintCV,
} from '@stacks/transactions';
import { batchRead, batchReadonly } from '../batch-api';
} from '@stacks/transactions';
import { batchRead } from '../batch-api';
async function batchReadsExample() {
async function batchReadsExample() {
const rs = await batchRead({
// index_block_hash:
// 'ce04817b9c6d90814ff9c06228d3a07d64335b1d9b01a233456fc304e34f7c0e', // block 373499
@@ -70,58 +70,13 @@ import {
],
});
console.log(rs);
}
}
async function batchReadonlyExample() {
const rs = await batchReadonly({
index_block_hash:
'ce04817b9c6d90814ff9c06228d3a07d64335b1d9b01a233456fc304e34f7c0e',
readonly: [
{
contract: contractPrincipalCV(
'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4',
'sbtc-token'
),
functionName: 'get-total-supply',
functionArgs: [],
},
{
contract: contractPrincipalCV(
'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4',
'sbtc-token'
),
functionName: 'get-balance',
functionArgs: [
principalCV('SP1CT7J2RWBZD62QAX36A2PQ3HKH5NFDGVHB8J34V'),
],
},
{
contract: contractPrincipalCV(
'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4',
'sbtc-token'
),
functionName: 'function-not-exists',
functionArgs: [],
},
{
contract: contractPrincipalCV(
'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4',
'sbtc-token'
),
functionName: 'get-balance',
functionArgs: [stringAsciiCV('invalid-args')],
},
],
});
console.log(rs);
}
async function main() {
async function main() {
await batchReadsExample();
// await batchReadonlyExample();
}
}
if (require.main === module) {
if (require.main === module) {
main().catch(console.error);
}
}

View File

@@ -5,6 +5,7 @@
"module": "esnext",
"allowImportingTsExtensions": false,
"moduleResolution": "node",
"esModuleInterop": true
"esModuleInterop": true,
"noEmit": true
}
}