Skip to content

Commit

Permalink
test: replaced circular json.stringify with safe stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang committed Oct 25, 2023
1 parent 719c9d8 commit 96a449d
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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 }) => {

Expand All @@ -27,7 +28,7 @@ CONFIGS().forEach(({ lib, rpc, setup }) => {

simpleContractAddress = op.contractAddress!;
} catch (e) {
console.log(JSON.stringify(e));
console.log(stringify(e));
}
});

Expand Down
3 changes: 2 additions & 1 deletion integration-tests/contract-lambda-view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/contract-override-estimate.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}

});
Expand Down
7 changes: 4 additions & 3 deletions integration-tests/contract-permits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CONFIGS } from './config';
import stringify from 'json-stringify-safe';

CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => {

Expand All @@ -21,7 +22,7 @@ CONFIGS().forEach(({ lib, rpc, setup, createAddress }) => {
await register.confirmation();

} catch (e) {
console.log(JSON.stringify(e));
console.log(stringify(e));
}

});
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/pack.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/prepare-operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}

})
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/rpc-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
({
Expand Down Expand Up @@ -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));
}

});
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/sandbox-drain-delegate-operation.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 () => {
Expand Down
7 changes: 4 additions & 3 deletions integration-tests/tzip16-metadata-view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CONFIGS } from './config';
import stringify from 'json-stringify-safe';

CONFIGS().forEach(({ lib, rpc, setup }) => {

Expand All @@ -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));
}
});

Expand Down

0 comments on commit 96a449d

Please sign in to comment.