From c42c475c48f5c9631971297a39f4bd216e722f5b Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Sun, 23 Feb 2025 02:20:11 +0000 Subject: [PATCH] feat: remove batch readonly --- README.md | 6 +- src/batch-api.ts | 55 +----------- src/sample/read.ts | 203 ++++++++++++++++++--------------------------- tsconfig.json | 3 +- 4 files changed, 83 insertions(+), 184 deletions(-) diff --git a/README.md b/README.md index 842e267..770c8b8 100644 --- a/README.md +++ b/README.md @@ -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', diff --git a/src/batch-api.ts b/src/batch-api.ts index 220ec09..29fdc19 100644 --- a/src/batch-api.ts +++ b/src/batch-api.ts @@ -9,7 +9,6 @@ import { type ClarityValue, type ContractPrincipalCV, deserializeCV, - type OptionalCV, serializeCV, } from '@stacks/transactions'; @@ -133,56 +132,4 @@ export async function batchRead( maps: convertResults(rs.maps), 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; -} +} \ No newline at end of file diff --git a/src/sample/read.ts b/src/sample/read.ts index 7348981..8f5718e 100644 --- a/src/sample/read.ts +++ b/src/sample/read.ts @@ -1,127 +1,82 @@ import { - contractPrincipalCV, - principalCV, - stringAsciiCV, - tupleCV, - uintCV, - } from '@stacks/transactions'; - import { batchRead, batchReadonly } from '../batch-api'; - - async function batchReadsExample() { - const rs = await batchRead({ - // index_block_hash: - // 'ce04817b9c6d90814ff9c06228d3a07d64335b1d9b01a233456fc304e34f7c0e', // block 373499 - variables: [ - { - contract: contractPrincipalCV( - 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275', - 'liquidity-token-v5kbe3oqvac' + contractPrincipalCV, + principalCV, + stringAsciiCV, + tupleCV, + uintCV, +} from '@stacks/transactions'; +import { batchRead } from '../batch-api'; + +async function batchReadsExample() { + const rs = await batchRead({ + // index_block_hash: + // 'ce04817b9c6d90814ff9c06228d3a07d64335b1d9b01a233456fc304e34f7c0e', // block 373499 + variables: [ + { + contract: contractPrincipalCV( + 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275', + 'liquidity-token-v5kbe3oqvac' + ), + variableName: 'balance-x', + }, + { + contract: contractPrincipalCV( + 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275', + 'liquidity-token-v5kbe3oqvac' + ), + variableName: 'balance-y', + }, + { + contract: contractPrincipalCV( + 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275', + 'liquidity-token-v5kbe3oqvac' + ), + variableName: 'something-not-exists', + }, + ], + maps: [ + { + contract: contractPrincipalCV( + 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM', + 'amm-registry-v2-01' + ), + mapName: 'pools-data-map', + mapKey: tupleCV({ + 'token-x': principalCV( + 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-wstx-v2' ), - variableName: 'balance-x', - }, - { - contract: contractPrincipalCV( - 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275', - 'liquidity-token-v5kbe3oqvac' + 'token-y': principalCV( + 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex' ), - variableName: 'balance-y', - }, - { - contract: contractPrincipalCV( - 'SP1Z92MPDQEWZXW36VX71Q25HKF5K2EPCJ304F275', - 'liquidity-token-v5kbe3oqvac' - ), - variableName: 'something-not-exists', - }, - ], - maps: [ - { - contract: contractPrincipalCV( - 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM', - 'amm-registry-v2-01' - ), - mapName: 'pools-data-map', - mapKey: tupleCV({ - 'token-x': principalCV( - 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-wstx-v2' - ), - 'token-y': principalCV( - 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-alex' - ), - factor: uintCV(1e8), - }), - }, - { - contract: contractPrincipalCV( - 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1', - 'univ2-core' - ), - mapName: 'pools', - mapKey: uintCV(1), - }, - { - contract: contractPrincipalCV( - 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1', - 'contract-not-exists' - ), - mapName: 'pools', - mapKey: uintCV(1), - }, - ], - }); - 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(); - } - - if (require.main === module) { - main().catch(console.error); - } - \ No newline at end of file + factor: uintCV(1e8), + }), + }, + { + contract: contractPrincipalCV( + 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1', + 'univ2-core' + ), + mapName: 'pools', + mapKey: uintCV(1), + }, + { + contract: contractPrincipalCV( + 'SP1Y5YSTAHZ88XYK1VPDH24GY0HPX5J4JECTMY4A1', + 'contract-not-exists' + ), + mapName: 'pools', + mapKey: uintCV(1), + }, + ], + }); + console.log(rs); +} + +async function main() { + await batchReadsExample(); + // await batchReadonlyExample(); +} + +if (require.main === module) { + main().catch(console.error); +} diff --git a/tsconfig.json b/tsconfig.json index 87b844c..4b9a7cc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "module": "esnext", "allowImportingTsExtensions": false, "moduleResolution": "node", - "esModuleInterop": true + "esModuleInterop": true, + "noEmit": true } }