mirror of
https://github.com/stxer/stxer-sdk.git
synced 2026-01-12 07:23:57 +08:00
feat: remove batch readonly
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
tupleCV,
|
||||
uintCV,
|
||||
} from '@stacks/transactions';
|
||||
import { batchRead, batchReadonly } from '../batch-api';
|
||||
import { batchRead } from '../batch-api';
|
||||
|
||||
async function batchReadsExample() {
|
||||
const rs = await batchRead({
|
||||
@@ -72,50 +72,6 @@ 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() {
|
||||
await batchReadsExample();
|
||||
// await batchReadonlyExample();
|
||||
@@ -124,4 +80,3 @@ import {
|
||||
if (require.main === module) {
|
||||
main().catch(console.error);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
"module": "esnext",
|
||||
"allowImportingTsExtensions": false,
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true
|
||||
"esModuleInterop": true,
|
||||
"noEmit": true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user