Skip to content

Commit

Permalink
fix: switch json.stringify function (#931)
Browse files Browse the repository at this point in the history
* fix: switch json.stringify function

* fix: solved WS issue
  • Loading branch information
rodolfopietro97 authored May 31, 2024
1 parent effc6d2 commit 021a5c9
Show file tree
Hide file tree
Showing 51 changed files with 322 additions and 200 deletions.
3 changes: 2 additions & 1 deletion apps/sdk-hardhat-integration/scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers } from 'hardhat';
import { stringifyData } from '@vechain/sdk-errors';

async function main(): Promise<void> {
const signer = (await ethers.getSigners())[0];
Expand All @@ -12,7 +13,7 @@ async function main(): Promise<void> {

console.log(
'Contract deployment with the following transaction:',
JSON.stringify(txResponse)
stringifyData(txResponse)
);
}

Expand Down
37 changes: 0 additions & 37 deletions apps/sdk-hardhat-integration/scripts/temp_test_ethers.ts

This file was deleted.

2 changes: 1 addition & 1 deletion docs/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ After deploying a smart contract, interacting with its functions is the next ste

```typescript { name=contract-function-call, category=example }
// 1 - Init a simple contract ABI
const contractABI = JSON.stringify([
const contractABI = stringifyData([
{
constant: false,
inputs: [
Expand Down
2 changes: 1 addition & 1 deletion docs/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The contract interface is used to provide a higher level of abstraction to allow
```typescript { name=contract, category=example }
// 1 - Create a new function

const contractABI = JSON.stringify([
const contractABI = stringifyData([
{
constant: false,
inputs: [
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/contracts/contract-function-call.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { clauseBuilder, coder, type FunctionFragment } from '@vechain/sdk-core';
import { expect } from 'expect';
import { stringifyData } from '@vechain/sdk-errors';

// START_SNIPPET: ContractFunctionCallSnippet

// 1 - Init a simple contract ABI
const contractABI = JSON.stringify([
const contractABI = stringifyData([
{
constant: false,
inputs: [
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/encoding/contract.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { coder } from '@vechain/sdk-core';
import { expect } from 'expect';
import { stringifyData } from '@vechain/sdk-errors';

// START_SNIPPET: ContractSnippet

// 1 - Create a new function

const contractABI = JSON.stringify([
const contractABI = stringifyData([
{
constant: false,
inputs: [
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/encoding/rlp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RLP_CODER } from '@vechain/sdk-core';
import { expect } from 'expect';
import { stringifyData } from '@vechain/sdk-errors';

// START_SNIPPET: RlpSnippet

Expand Down Expand Up @@ -35,4 +36,4 @@ const obj = rlp.decodeObject(data);
expect(data.toString('hex')).toBe(
'd7947567d83b7b8d80addcb281a71d54fc7b3364ffed0a80'
);
expect(JSON.stringify(obj)).toBe(JSON.stringify(clause));
expect(stringifyData(obj)).toBe(stringifyData(clause));
9 changes: 5 additions & 4 deletions docs/examples/evm-extension/evm-extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { FunctionFragment, coder } from '@vechain/sdk-core';
import { ThorClient } from '@vechain/sdk-network'
import { coder, type FunctionFragment } from '@vechain/sdk-core';
import { ThorClient } from '@vechain/sdk-network';
import { expect } from 'expect';
import { stringifyData } from '@vechain/sdk-errors';

// ABI of the `TestingContract` smart contract
const TESTING_CONTRACT_ABI = JSON.stringify([
const TESTING_CONTRACT_ABI = stringifyData([
{
inputs: [
{
Expand Down Expand Up @@ -794,4 +795,4 @@ const totalSupply = await thorSoloClient.contracts.executeCall(
// END_SNIPPET: EVMExtensionSnippet

// Check the result
expect(totalSupply).toStrictEqual([10000000000000000000000000000n]);
expect(totalSupply).toStrictEqual([10000000000000000000000000000n]);
3 changes: 2 additions & 1 deletion docs/examples/transactions/simulation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'expect';
import { ThorClient } from '@vechain/sdk-network';
import { clauseBuilder, unitsUtils } from '@vechain/sdk-core';
import { stringifyData } from '@vechain/sdk-errors';

// START_SNIPPET: SimulationSnippet

Expand Down Expand Up @@ -107,4 +108,4 @@ const expected2 = [
];

// 3 - Check the result
expect(JSON.stringify(simulatedTx2)).toStrictEqual(JSON.stringify(expected2));
expect(stringifyData(simulatedTx2)).toStrictEqual(stringifyData(expected2));
9 changes: 7 additions & 2 deletions packages/core/src/contract/coder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Interface as EthersInterface, type InterfaceAbi } from 'ethers';
import { ABI, buildError, ERROR_CODES } from '@vechain/sdk-errors';
import {
ABI,
buildError,
ERROR_CODES,
stringifyData
} from '@vechain/sdk-errors';
import type { BytesLike, Interface, Log, Result } from '../abi';
import { abi } from '../abi';

Expand Down Expand Up @@ -36,7 +41,7 @@ function encodeFunctionInput(
ERROR_CODES.ABI.INVALID_DATA_TO_ENCODE,
`Method 'encodeFunctionInput' failed while encoding input for function '${functionName}'. ` +
`Input must match ABI specifications and be correctly formatted.\n` +
`Parameters: ${JSON.stringify(functionData)}.\n` +
`Parameters: ${stringifyData(functionData)}.\n` +
`Ethers' error message: ${(e as Error).message}.`,
{ functionName, functionData },
e
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/keystore/cryptography/ethers/keystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
*/
import { ethers } from 'ethers';
import { type Keystore, type KeystoreAccount } from '../../types';
import { assert, buildError, KEYSTORE } from '@vechain/sdk-errors';
import {
assert,
buildError,
KEYSTORE,
stringifyData
} from '@vechain/sdk-errors';
import { secp256k1 } from '../../../secp256k1';
import { addressUtils } from '../../../address';
import { Hex0x } from '../../../utils';
Expand Down Expand Up @@ -76,7 +81,7 @@ async function decrypt(

try {
return (await ethers.decryptKeystoreJson(
JSON.stringify(keystore),
stringifyData(keystore),
password
)) as KeystoreAccount;
} catch (e) {
Expand All @@ -100,7 +105,7 @@ async function decrypt(
* @returns A boolean indicating whether the keystore is valid or not.
*/
function isValid(keystore: Keystore): boolean {
return ethers.isKeystoreJson(JSON.stringify(keystore));
return ethers.isKeystoreJson(stringifyData(keystore));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* encryption, decryption, and validation functionality.
*/
import * as utils from '@noble/curves/abstract/utils';
import { assert, KEYSTORE } from '@vechain/sdk-errors';
import { assert, KEYSTORE, stringifyData } from '@vechain/sdk-errors';
import { ctr } from '@noble/ciphers/aes';
import { scrypt } from '@noble/hashes/scrypt';
import { type Keystore, type KeystoreAccount } from '../../types';
Expand Down Expand Up @@ -550,7 +550,7 @@ function decryptKeystore(
*/
function isValid(keystore: Keystore): boolean {
try {
const copy = JSON.parse(JSON.stringify(keystore)) as Keystore;
const copy = JSON.parse(stringifyData(keystore)) as Keystore;
if (
copy.crypto.cipher.toLowerCase() === KEYSTORE_CRYPTO_CIPHER &&
copy.crypto.kdf.toLowerCase() === KEYSTORE_CRYPTO_KDF &&
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/utils/const/abi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { stringifyData } from '@vechain/sdk-errors';

/**
* ABI of the Params built-in contract.
*
* @link see [params.sol](https://docs.vechain.org/developer-resources/built-in-contracts#params-sol)
*/
const PARAMS_ABI = JSON.stringify([
const PARAMS_ABI = stringifyData([
{
constant: false,
inputs: [
Expand Down Expand Up @@ -79,7 +81,7 @@ const PARAMS_ABI = JSON.stringify([
*
* @see [VIP 180](https://github.com/vechain/VIPs/blob/master/vips/VIP-180.md)
*/
const VIP180_ABI = JSON.stringify([
const VIP180_ABI = stringifyData([
{
constant: true,
inputs: [],
Expand Down Expand Up @@ -338,7 +340,7 @@ const VIP180_ABI = JSON.stringify([
}
]);

const ERC721_ABI = JSON.stringify([
const ERC721_ABI = stringifyData([
{
inputs: [],
stateMutability: 'nonpayable',
Expand Down
7 changes: 4 additions & 3 deletions packages/core/tests/abi/abi.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
InvalidAbiDataToEncodeError,
InvalidAbiEventError,
InvalidAbiFormatTypeError,
InvalidAbiFunctionError
InvalidAbiFunctionError,
stringifyData
} from '@vechain/sdk-errors';

/**
Expand Down Expand Up @@ -432,7 +433,7 @@ describe('Abi - Function & Event', () => {
topicsEventTestCases.forEach(
({ event, valuesToEncode, expectedTopics }) => {
test(`Encode Event topics - ${
typeof event === 'string' ? event : JSON.stringify(event)
typeof event === 'string' ? event : stringifyData(event)
}`, () => {
const ev = new abi.Event(event);

Expand All @@ -448,7 +449,7 @@ describe('Abi - Function & Event', () => {
*/
invalidTopicsEventTestCases.forEach(
({ event, valuesToEncode, expectedError }) => {
test(`Encode Event topics - ${JSON.stringify(event)}`, () => {
test(`Encode Event topics - ${stringifyData(event)}`, () => {
const ev = new abi.Event(event);

expect(() =>
Expand Down
11 changes: 7 additions & 4 deletions packages/core/tests/abi/fixture.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { addressUtils, Hex0x } from '../../src';
import { generateRandomValidAddress } from '../fixture';
import { InvalidAbiDataToEncodeError } from '@vechain/sdk-errors';
import {
InvalidAbiDataToEncodeError,
stringifyData
} from '@vechain/sdk-errors';

/**
* Simple functions fixtures
Expand Down Expand Up @@ -438,7 +441,7 @@ const encodedDecodedInvalidValues = [
}
];

const contractABI = JSON.stringify([
const contractABI = stringifyData([
{
constant: false,
inputs: [
Expand Down Expand Up @@ -469,7 +472,7 @@ const contractABI = JSON.stringify([
}
]);

const contractABIWithEvents = JSON.stringify([
const contractABIWithEvents = stringifyData([
{
constant: false,
inputs: [
Expand Down Expand Up @@ -517,7 +520,7 @@ const contractABIWithEvents = JSON.stringify([
}
]);

const contractStorageABI = JSON.stringify([
const contractStorageABI = stringifyData([
{
inputs: [],
name: 'getValue',
Expand Down
10 changes: 7 additions & 3 deletions packages/core/tests/bloom/bloom.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import * as utils from '@noble/curves/abstract/utils';
import { describe, expect, test } from '@jest/globals';
import { bloom, Hex } from '../../src';
import { bloomKTestCases } from './fixture';
import { InvalidBloomError, InvalidKError } from '../../../errors';
import {
InvalidBloomError,
InvalidKError,
stringifyData
} from '../../../errors';

/**
* Bloom filter tests
Expand Down Expand Up @@ -286,8 +290,8 @@ describe('Bloom Filter', () => {
expect(filter.bits.byteLength).toBe(25);
expect(filter.k).toBe(k);

expect(JSON.stringify(filter.bits)).toBe(
JSON.stringify(
expect(stringifyData(filter.bits)).toBe(
stringifyData(
utils.hexToBytes(
'a4d641159d68d829345f86f40d50676cf042f6265072075a94'
)
Expand Down
7 changes: 4 additions & 3 deletions packages/core/tests/keystore/keystore.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { encryptionPassword } from './fixture';
import {
InvalidKeystoreError,
InvalidKeystorePasswordError,
InvalidSecp256k1PrivateKeyError
InvalidSecp256k1PrivateKeyError,
stringifyData
} from '@vechain/sdk-errors';

/**
Expand Down Expand Up @@ -125,7 +126,7 @@ import {
);

// Verify keystore -> False
const invalidKeystore: string = JSON.stringify({
const invalidKeystore: string = stringifyData({
...myKeystore,
version: 4
});
Expand Down Expand Up @@ -157,7 +158,7 @@ import {
expect(keystore.isValid(myKeystore)).toBe(true);

// Verify keystore -> False
const invalidKeystore: string = JSON.stringify({
const invalidKeystore: string = stringifyData({
...myKeystore,
version: 4
});
Expand Down
Loading

1 comment on commit 021a5c9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 100%
100% (3383/3383) 100% (783/783) 100% (701/701)
Title Tests Skipped Failures Errors Time
core 484 0 💤 0 ❌ 0 🔥 1m 5s ⏱️
network 660 0 💤 0 ❌ 0 🔥 3m 50s ⏱️
errors 48 0 💤 0 ❌ 0 🔥 11.279s ⏱️

Please sign in to comment.