Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/0xs34n/starknet.js into …
Browse files Browse the repository at this point in the history
…fix/remove-always-parse-as-big
  • Loading branch information
dhruvkelawala committed Jun 21, 2022
2 parents f81a121 + d8c715e commit fcca1cf
Show file tree
Hide file tree
Showing 10 changed files with 1,238 additions and 1,581 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## [3.15.4](https://github.com/0xs34n/starknet.js/compare/v3.15.3...v3.15.4) (2022-06-20)

### Bug Fixes

- **parseResponse:** revert the changes from parseResponse ([d51996f](https://github.com/0xs34n/starknet.js/commit/d51996fa7635428ad4ffe271aa56f202ddcd4179))
- **provider:** allow the user to handle the contract errors ([5190f7a](https://github.com/0xs34n/starknet.js/commit/5190f7a20fb35382756d86cc67d3fab5c2d541ff))
- **test:** fix callContract() test ([b11c5da](https://github.com/0xs34n/starknet.js/commit/b11c5daf7fec6e8207dbc0be534aade8e00d5021))

## [3.15.3](https://github.com/0xs34n/starknet.js/compare/v3.15.2...v3.15.3) (2022-06-20)

### Bug Fixes

- **dev:** regenerated package-lock.json ([849cb1e](https://github.com/0xs34n/starknet.js/commit/849cb1ea3ffd7ba10b40b232d0ebc46b6599c7ea))
- use cross-fetch only for jest ([83be37a](https://github.com/0xs34n/starknet.js/commit/83be37a9e3328a44abd9583b8167c3cb8d882790))

## [3.15.2](https://github.com/0xs34n/starknet.js/compare/v3.15.1...v3.15.2) (2022-06-18)

### Bug Fixes
Expand Down
13 changes: 9 additions & 4 deletions __tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ const DEFAULT_TEST_ACCOUNT_ADDRESS = // run `starknet-devnet --seed 0` and this
'0x65d53c8ec4178096167b35a08e16e548d8075cb08ad7bc63d07966ca13569dc';
const DEFAULT_TEST_ACCOUNT_PRIVATE_KEY = '0xe3e70682c2094cac629f6fbed82c07cd';

export const getTestProvider = () => {
const baseUrl = process.env.TEST_PROVIDER_BASE_URL || DEFAULT_TEST_PROVIDER_BASE_URL;
const BASE_URL = process.env.TEST_PROVIDER_BASE_URL || DEFAULT_TEST_PROVIDER_BASE_URL;
export const IS_DEVNET = !BASE_URL.includes('starknet.io');

const provider = new Provider({ baseUrl });
export const getTestProvider = () => {
const provider = new Provider({ baseUrl: BASE_URL });

if (baseUrl.includes('localhost') || baseUrl.includes('127.0.0.1')) {
if (IS_DEVNET) {
// accelerate the tests when running locally
const originalWaitForTransaction = provider.waitForTransaction.bind(provider);
provider.waitForTransaction = (txHash, retryInterval) => {
Expand All @@ -43,3 +44,7 @@ export const getTestAccount = () => {

return new Account(provider, testAccountAddress, ec.getKeyPair(testAccountPrivateKey));
};

export const testIf = (condition: boolean) => (condition ? test : test.skip);
export const testIfDevnet = testIf(IS_DEVNET);
export const testIfNotDevnet = testIf(!IS_DEVNET);
3 changes: 3 additions & 0 deletions __tests__/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* eslint-disable no-console */
import fetch from 'cross-fetch';
import { register } from 'fetch-intercept';

global.fetch = fetch;

jest.setTimeout(50 * 60 * 1000);

if (process.env.DEBUG === 'true') {
Expand Down
34 changes: 30 additions & 4 deletions __tests__/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { BlockNumber, stark } from '../src';
import { toBN } from '../src/utils/number';
import { compiledErc20, compiledOpenZeppelinAccount, getTestProvider } from './fixtures';
import {
IS_DEVNET,
compiledErc20,
compiledOpenZeppelinAccount,
getTestProvider,
testIfNotDevnet,
} from './fixtures';

const { compileCalldata } = stark;

const provider = getTestProvider();

const testIf = (condition: boolean) => (condition ? test : test.skip);

describe('defaultProvider', () => {
let exampleTransactionHash: string;
let exampleContractAddress: string;
Expand All @@ -30,7 +34,7 @@ describe('defaultProvider', () => {
});

describe('feeder gateway endpoints', () => {
testIf(provider.baseUrl.includes('starknet.io'))('getContractAddresses()', async () => {
testIfNotDevnet('getContractAddresses()', async () => {
// not supported in starknet-devnet
const { GpsStatementVerifier, Starknet } = await provider.getContractAddresses();
expect(typeof GpsStatementVerifier).toBe('string');
Expand Down Expand Up @@ -111,6 +115,28 @@ describe('defaultProvider', () => {
).resolves.not.toThrow();
});

test('callContract() - gateway error', async () => {
const promise = provider.callContract({
contractAddress: exampleContractAddress,
entrypoint: 'non_existent_entrypoint',
calldata: compileCalldata({
user: '0xdeadbeef',
}),
});
expect(promise).rejects.toHaveProperty('errorCode');
expect(promise).rejects.toThrowErrorMatchingInlineSnapshot(
`"Entry point 0x23b0c8b3d98aa73d8a35f5303fe77d132c6d04279e63f6e1d6aac5946e04612 not found in contract with class hash 0x2864c45bd4ba3e66d8f7855adcadf07205c88f43806ffca664f1f624765207e."`
);

try {
await promise;
} catch (e) {
expect(e.errorCode).toMatchInlineSnapshot(
IS_DEVNET ? `500n` : `"StarknetErrorCode.ENTRY_POINT_NOT_FOUND_IN_CONTRACT"`
);
}
});

test('transaction trace', async () => {
const transactionTrace = await provider.getTransactionTrace(exampleTransactionHash);
expect(transactionTrace).toHaveProperty('function_invocation');
Expand Down
64 changes: 64 additions & 0 deletions __tests__/utils/typedData.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import typedDataExample from '../../__mocks__/typedDataExample.json';
import { number } from '../../src';
import { BigNumberish } from '../../src/utils/number';
import { encodeType, getMessageHash, getStructHash, getTypeHash } from '../../src/utils/typedData';

describe('typedData', () => {
Expand Down Expand Up @@ -34,4 +36,66 @@ describe('typedData', () => {
`"0x6fcff244f63e38b9d88b9e3378d44757710d1b244282b435cb472053c8d78d0"`
);
});

interface StringStruct {
len: BigNumberish;
data: BigNumberish[];
}
function stringToStringStruct(str: string): StringStruct {
const len = str.length;
const data = str.split('').map((char) => number.toHex(number.toBN(char.charCodeAt(0))));
return { len, data };
}

const typedDataStringExample = {
types: {
StarkNetDomain: [
{ name: 'name', type: 'felt' },
{ name: 'version', type: 'felt' },
{ name: 'chainId', type: 'felt' },
],
Person: [
{ name: 'name', type: 'felt' },
{ name: 'wallet', type: 'felt' },
],
String: [
{ name: 'len', type: 'felt' },
{ name: 'data', type: 'felt*' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'String' },
],
},
primaryType: 'Mail',
domain: {
name: 'StarkNet Mail',
version: '1',
chainId: 1,
},
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: stringToStringStruct(
'this is way longer than just 32 characters, to test if that is possible within a typedData struct.'
),
},
};

test('should transform strings correctly', () => {
const hash = getMessageHash(
typedDataStringExample,
'0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'
);
expect(hash).toMatchInlineSnapshot(
`"0x70338fb11b8f70b68b261de8a322bcb004bd85e88ac47d9147982c7f5ac66fd"`
);
});
});
Loading

0 comments on commit fcca1cf

Please sign in to comment.