From 96a449dd80fceedcdd0f3ddad59f7ba3b77da20e Mon Sep 17 00:00:00 2001 From: huianyang Date: Wed, 25 Oct 2023 00:08:12 -0700 Subject: [PATCH] test: replaced circular json.stringify with safe stringify --- .../contract-increase-paid-storage-operation.spec.ts | 3 ++- integration-tests/contract-lambda-view.spec.ts | 3 ++- integration-tests/contract-override-estimate.spec.ts | 3 ++- integration-tests/contract-permits.spec.ts | 7 ++++--- .../contract-update-consensus-key-operation.spec.ts | 3 ++- integration-tests/pack.spec.ts | 3 ++- integration-tests/package.json | 4 +++- integration-tests/prepare-operation.spec.ts | 3 ++- integration-tests/rpc-nodes.spec.ts | 3 ++- integration-tests/sandbox-drain-delegate-operation.spec.ts | 3 ++- integration-tests/tzip16-metadata-view.spec.ts | 7 ++++--- ...acts-with-metadata-on-itself-and-fetch-metadata.spec.ts | 3 ++- ...lets-with-metadata-on-itself-and-fetch-metadata.spec.ts | 3 ++- .../wallet-increase-paid-storage-operation.spec.ts | 3 ++- 14 files changed, 33 insertions(+), 18 deletions(-) diff --git a/integration-tests/contract-increase-paid-storage-operation.spec.ts b/integration-tests/contract-increase-paid-storage-operation.spec.ts index 3553d17b0d..9ae856860a 100644 --- a/integration-tests/contract-increase-paid-storage-operation.spec.ts +++ b/integration-tests/contract-increase-paid-storage-operation.spec.ts @@ -1,6 +1,7 @@ import { CONFIGS } from './config'; import { OpKind } from '@taquito/taquito'; import { ligoSample } from './data/ligo-simple-contract'; +import stringify from 'json-stringify-safe'; CONFIGS().forEach(({ lib, rpc, setup }) => { @@ -27,7 +28,7 @@ CONFIGS().forEach(({ lib, rpc, setup }) => { simpleContractAddress = op.contractAddress!; } catch (e) { - console.log(JSON.stringify(e)); + console.log(stringify(e)); } }); diff --git a/integration-tests/contract-lambda-view.spec.ts b/integration-tests/contract-lambda-view.spec.ts index 66111126fa..354c5e3028 100644 --- a/integration-tests/contract-lambda-view.spec.ts +++ b/integration-tests/contract-lambda-view.spec.ts @@ -3,10 +3,11 @@ import { CONFIGS } from './config'; import { tzip7Contract } from './data/tzip_7_contract'; import { testContract } from './data/test_lambda_view'; import { fa2Contract } from './data/fa2_contract'; +import stringify from 'json-stringify-safe'; CONFIGS().forEach(({ lib, rpc, setup }) => { const Tezos = lib; - const toJSON = (x: any) => JSON.parse(JSON.stringify(x)); + const toJSON = (x: any) => JSON.parse(stringify(x)); describe(`Test contract with lambda view trough contract api using: ${rpc}`, () => { beforeEach(async () => { diff --git a/integration-tests/contract-override-estimate.spec.ts b/integration-tests/contract-override-estimate.spec.ts index ec44c3ea81..97dc1e8f17 100644 --- a/integration-tests/contract-override-estimate.spec.ts +++ b/integration-tests/contract-override-estimate.spec.ts @@ -1,5 +1,6 @@ import { CONFIGS } from "./config"; import { InvalidEstimateValueError } from '@taquito/taquito'; +import stringify from 'json-stringify-safe'; CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => { const Tezos = lib; @@ -14,7 +15,7 @@ CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => { const account = await createAddress(); pkh = await account.signer.publicKeyHash(); } catch (e) { - console.log(JSON.stringify(e)); + console.log(stringify(e)); } }); diff --git a/integration-tests/contract-permits.spec.ts b/integration-tests/contract-permits.spec.ts index 30bccc3982..36e37aae05 100644 --- a/integration-tests/contract-permits.spec.ts +++ b/integration-tests/contract-permits.spec.ts @@ -6,6 +6,7 @@ import { permit_fa12_smartpy } from './data/permit_fa12_smartpy'; import { buf2hex, char2Bytes, hex2buf } from '@taquito/utils'; import { tzip16, Tzip16Module } from '@taquito/tzip16'; import { packDataBytes } from "@taquito/michel-codec" +import stringify from 'json-stringify-safe'; const blake = require('blakejs'); const bob_address = 'tz1Xk7HkSwHv6dTEgR7E2WC2yFj4cyyuj2Gh'; @@ -68,7 +69,7 @@ const create_bytes_to_sign = async (Tezos: TezosToolkit, contractAddress: string }; const sigParamPacked = packDataBytes(sigParamData, sigParamType); - // signs the hash + // signs the hash const signature = await Tezos.signer.sign(sigParamPacked.bytes); return signature.sig @@ -323,9 +324,9 @@ CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => { try { await fail_contract.methods.transfer(bootstrap3_address, bootstrap4_address, 1).send(); } catch (errors) { - let jsonStr: string = JSON.stringify(errors); + let jsonStr: string = stringify(errors); let jsonObj = JSON.parse(jsonStr); - let error_code = JSON.stringify(jsonObj.errors[1].with.int); + let error_code = stringify(jsonObj.errors[1].with.int); expect((error_code = '26')); } diff --git a/integration-tests/contract-update-consensus-key-operation.spec.ts b/integration-tests/contract-update-consensus-key-operation.spec.ts index db7a0d1a29..eba94f8073 100644 --- a/integration-tests/contract-update-consensus-key-operation.spec.ts +++ b/integration-tests/contract-update-consensus-key-operation.spec.ts @@ -1,4 +1,5 @@ import { CONFIGS } from './config'; +import stringify from 'json-stringify-safe'; CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => { @@ -21,7 +22,7 @@ CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => { await register.confirmation(); } catch (e) { - console.log(JSON.stringify(e)); + console.log(stringify(e)); } }); diff --git a/integration-tests/pack.spec.ts b/integration-tests/pack.spec.ts index 984b6d4a46..563b68acc9 100644 --- a/integration-tests/pack.spec.ts +++ b/integration-tests/pack.spec.ts @@ -4,6 +4,7 @@ import { MichelsonType, MichelsonData, ProtocolID, packDataBytes } from "@taquit import { MichelsonV1Expression } from "@taquito/rpc"; import fs from "fs"; import path from "path"; +import stringify from 'json-stringify-safe'; interface TypedTestData { type?: MichelsonType; @@ -29,7 +30,7 @@ CONFIGS().forEach(({ rpc, protocol }) => { describe("Test pack", () => { for (const s of src) { const def = (s.proto === undefined || s.proto === protocol) ? test : test.skip; - def(`Verify that .pack for local pack will return same result as for network pack: ${JSON.stringify(s.data)} (${rpc})`, async () => { + def(`Verify that .pack for local pack will return same result as for network pack: ${stringify(s.data)} (${rpc})`, async () => { const local = packDataBytes(s.data, s.type); const rpcResult = await Tezos.rpc.packData({ data: s.data, type: s.type as MichelsonV1Expression }, { block: "head" }) expect(local.bytes).toEqual(rpcResult.packed); diff --git a/integration-tests/package.json b/integration-tests/package.json index 1822ee2dd8..1d55355cf8 100644 --- a/integration-tests/package.json +++ b/integration-tests/package.json @@ -32,7 +32,8 @@ "@taquito/utils": "^17.3.2", "bignumber.js": "^9.1.2", "bip39": "3.1.0", - "blakejs": "^1.2.1" + "blakejs": "^1.2.1", + "json-stringify-safe": "^5.0.1" }, "private": true, "jest": { @@ -70,6 +71,7 @@ "jest-config": "^29.7.0", "jest-retries": "^1.0.1", "jest-stare": "2.5.1", + "@types/json-stringify-safe": "^5.0.1", "lint-staged": "^14.0.1", "ts-jest": "^29.1.1", "ts-node": "^10.9.1", diff --git a/integration-tests/prepare-operation.spec.ts b/integration-tests/prepare-operation.spec.ts index 0b08ea74f1..baf052415f 100644 --- a/integration-tests/prepare-operation.spec.ts +++ b/integration-tests/prepare-operation.spec.ts @@ -2,6 +2,7 @@ import { OperationContentsBallot, OperationContentsTransaction } from '@taquito/ import { OpKind } from '@taquito/taquito'; import { CONFIGS } from './config'; import { LocalForger } from '@taquito/local-forging'; +import stringify from 'json-stringify-safe'; CONFIGS().forEach(({ lib, setup, protocol, createAddress }) => { const Tezos = lib; @@ -26,7 +27,7 @@ CONFIGS().forEach(({ lib, setup, protocol, createAddress }) => { contractAddress = op.contractAddress!; } catch (e: any) { - console.log('Unable to originate contract: ', JSON.stringify(e)); + console.log('Unable to originate contract: ', stringify(e)); } }) diff --git a/integration-tests/rpc-nodes.spec.ts b/integration-tests/rpc-nodes.spec.ts index 5f8a370307..58f6bc1895 100644 --- a/integration-tests/rpc-nodes.spec.ts +++ b/integration-tests/rpc-nodes.spec.ts @@ -5,6 +5,7 @@ import { encodeExpr } from '@taquito/utils'; import { Schema } from '@taquito/michelson-encoder'; import { tokenBigmapCode, tokenBigmapStorage } from './data/token_bigmap'; import { ticketCode, ticketStorage } from './data/code_with_ticket'; +import stringify from 'json-stringify-safe'; CONFIGS().forEach( ({ @@ -38,7 +39,7 @@ CONFIGS().forEach( const ticketCallOp = await ticketContract.methods.auto_call(1).send(); await ticketCallOp.confirmation(); } catch (e) { - console.log('Failed to originate ticket contract', JSON.stringify(e)); + console.log('Failed to originate ticket contract', stringify(e)); } }); diff --git a/integration-tests/sandbox-drain-delegate-operation.spec.ts b/integration-tests/sandbox-drain-delegate-operation.spec.ts index 8e8b3f3338..ffaef5e8e5 100644 --- a/integration-tests/sandbox-drain-delegate-operation.spec.ts +++ b/integration-tests/sandbox-drain-delegate-operation.spec.ts @@ -1,5 +1,6 @@ import { TezosToolkit } from "@taquito/taquito"; import { CONFIGS, sleep } from "./config"; +import stringify from 'json-stringify-safe'; CONFIGS().forEach(({ lib, rpc, protocol, setup, createAddress }) => { const Tezos = lib; @@ -36,7 +37,7 @@ CONFIGS().forEach(({ lib, rpc, protocol, setup, createAddress }) => { await sleep(((constants.preserved_cycles + 2) * constants.blocks_per_cycle * (constants.minimal_block_delay!.toNumber())) * 1000); } catch (e) { - console.log(JSON.stringify(e)); + console.log(stringify(e)); } }) flextesanet('Should be able to inject drain_delegate operation', async () => { diff --git a/integration-tests/tzip16-metadata-view.spec.ts b/integration-tests/tzip16-metadata-view.spec.ts index 356f102fa8..8a2164d3f6 100644 --- a/integration-tests/tzip16-metadata-view.spec.ts +++ b/integration-tests/tzip16-metadata-view.spec.ts @@ -2,6 +2,7 @@ import { CONFIGS } from './config'; import { MichelsonMap } from '@taquito/taquito'; import { tzip16, Tzip16Module, char2Bytes } from '@taquito/tzip16'; import { contractCode, metadataViewsExample1, metadataViewsExample2 } from './data/metadataViews'; +import stringify from 'json-stringify-safe'; CONFIGS().forEach(({ lib, rpc, setup }) => { const Tezos = lib; @@ -16,7 +17,7 @@ CONFIGS().forEach(({ lib, rpc, setup }) => { const metadataBigMAp = new MichelsonMap(); metadataBigMAp.set("", char2Bytes('tezos-storage:here')); - metadataBigMAp.set("here", char2Bytes(JSON.stringify(metadataViewsExample1))) + metadataBigMAp.set("here", char2Bytes(stringify(metadataViewsExample1))) const op = await Tezos.contract.originate({ code: contractCode, @@ -60,7 +61,7 @@ CONFIGS().forEach(({ lib, rpc, setup }) => { const metadataBigMAp = new MichelsonMap(); metadataBigMAp.set("", char2Bytes('tezos-storage:here')); - metadataBigMAp.set("here", char2Bytes(JSON.stringify(metadataViewsExample2))) + metadataBigMAp.set("here", char2Bytes(stringify(metadataViewsExample2))) const op = await Tezos.contract.originate({ code: contractCode, @@ -90,7 +91,7 @@ CONFIGS().forEach(({ lib, rpc, setup }) => { expect(viewCallBalanceResult.toString()).toEqual('0'); const viewIdentityResult = await metadataViews['the-identity']().executeView(1, 'test', 200000); - expect(JSON.stringify(viewIdentityResult)).toEqual(`{"arg_zero":"1","arg_one_result":"test","arg_two":"200000"}`); + expect(stringify(viewIdentityResult)).toEqual(`{"arg_zero":"1","arg_one_result":"test","arg_two":"200000"}`); const viewContractAddressResult = await metadataViews['get-contract-address']().executeView(); expect(viewContractAddressResult.toString()).toEqual(contractAddress); diff --git a/integration-tests/tzip16-originate-contracts-with-metadata-on-itself-and-fetch-metadata.spec.ts b/integration-tests/tzip16-originate-contracts-with-metadata-on-itself-and-fetch-metadata.spec.ts index c09f5e028c..b95f4c6ed8 100644 --- a/integration-tests/tzip16-originate-contracts-with-metadata-on-itself-and-fetch-metadata.spec.ts +++ b/integration-tests/tzip16-originate-contracts-with-metadata-on-itself-and-fetch-metadata.spec.ts @@ -2,6 +2,7 @@ import { CONFIGS } from "./config"; import { tzip16, Tzip16Module, char2Bytes } from '@taquito/tzip16'; import { tacoContractTzip16 } from "./data/modified-taco-contract" import { MichelsonMap } from "@taquito/taquito"; +import stringify from 'json-stringify-safe'; CONFIGS().forEach(({ lib, rpc, setup }) => { const Tezos = lib; @@ -29,7 +30,7 @@ CONFIGS().forEach(({ lib, rpc, setup }) => { const metadataBigMap = new MichelsonMap(); metadataBigMap.set("", char2Bytes('tezos-storage:here')); - metadataBigMap.set("here", char2Bytes(JSON.stringify(metadataJSON))) + metadataBigMap.set("here", char2Bytes(stringify(metadataJSON))) // Ligo Taco shop contract modified to include metadata in storage // https://ide.ligolang.org/p/-uS469slzUlSm1zwNqHl1A diff --git a/integration-tests/tzip16-originate-wallets-with-metadata-on-itself-and-fetch-metadata.spec.ts b/integration-tests/tzip16-originate-wallets-with-metadata-on-itself-and-fetch-metadata.spec.ts index ff6c4d6a58..5692f01c27 100644 --- a/integration-tests/tzip16-originate-wallets-with-metadata-on-itself-and-fetch-metadata.spec.ts +++ b/integration-tests/tzip16-originate-wallets-with-metadata-on-itself-and-fetch-metadata.spec.ts @@ -2,6 +2,7 @@ import { CONFIGS } from "./config"; import { tzip16, Tzip16Module, char2Bytes } from '@taquito/tzip16'; import { tacoContractTzip16 } from "./data/modified-taco-contract" import { MichelsonMap } from "@taquito/taquito"; +import stringify from 'json-stringify-safe'; CONFIGS().forEach(({ lib, rpc, setup }) => { const Tezos = lib; @@ -29,7 +30,7 @@ CONFIGS().forEach(({ lib, rpc, setup }) => { const metadataBigMAp = new MichelsonMap(); metadataBigMAp.set("", char2Bytes('tezos-storage:here')); - metadataBigMAp.set("here", char2Bytes(JSON.stringify(metadataJSON))) + metadataBigMAp.set("here", char2Bytes(stringify(metadataJSON))) // Ligo Taco shop contract modified to include metadata in storage // https://ide.ligolang.org/p/-uS469slzUlSm1zwNqHl1A diff --git a/integration-tests/wallet-increase-paid-storage-operation.spec.ts b/integration-tests/wallet-increase-paid-storage-operation.spec.ts index 9271e38a16..3363332de2 100644 --- a/integration-tests/wallet-increase-paid-storage-operation.spec.ts +++ b/integration-tests/wallet-increase-paid-storage-operation.spec.ts @@ -1,4 +1,5 @@ import { CONFIGS } from './config'; +import stringify from 'json-stringify-safe'; CONFIGS().forEach(({ lib, rpc, setup }) => { @@ -25,7 +26,7 @@ CONFIGS().forEach(({ lib, rpc, setup }) => { simpleContractAddress = (await op.contract()).address } catch (e) { - console.log(JSON.stringify(e)); + console.log(stringify(e)); } });