Skip to content

Commit

Permalink
feat: response parsing and types
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Apr 3, 2023
1 parent 78b1f81 commit 09a46af
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 17 deletions.
26 changes: 23 additions & 3 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import typedDataExample from '../__mocks__/typedDataExample.json';
import { Account, Contract, Provider, TransactionStatus, ec, stark } from '../src';
import {
Account,
Contract,
Provider,
SequencerProvider,
TransactionStatus,
ec,
stark,
} from '../src';
import { uint256 } from '../src/utils/calldata/cairo';
import { parseUDCEvent } from '../src/utils/events';
import { calculateContractAddressFromHash, feeTransactionVersion } from '../src/utils/hash';
Expand Down Expand Up @@ -499,18 +507,30 @@ describe('deploy and test Wallet', () => {
describeIfDevnetSequencer('not implemented for RPC', () => {
// Testnet will not accept declare v2 with same compiledClassHash,
// aka. we can't redeclare same contract
describe('Test Cairo 1', () => {
const provider = getTestProvider();
describe('Cairo 1', () => {
const provider = getTestProvider() as SequencerProvider;
const account = getTestAccount(provider);
let classHash;
initializeMatcher(expect);

test('Declare v2 - Hello Cairo 1 contract', async () => {
const declareTx = await account.declare({
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
});
classHash = declareTx.class_hash;
await provider.waitForTransaction(declareTx.transaction_hash);
expect(declareTx).toMatchSchemaRef('DeclareContractResponse');
});

test('getCompiledClassByClassHash', async () => {
const compiledClass = await provider.getCompiledClassByClassHash(classHash);
expect(compiledClass).toMatchSchemaRef('CompiledClass');
});

test('GetClassByHash', async () => {
const classResponse = await provider.getClassByHash(classHash);
expect(classResponse).toMatchSchemaRef('SierraContractClass');
});
});
});
4 changes: 2 additions & 2 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ describe('defaultProvider', () => {

test('getClassAt(contractAddress, blockNumber="latest")', async () => {
const classResponse = await testProvider.getClassAt(erc20ContractAddress);
expect(classResponse).toMatchSchemaRef('ContractClass');
expect(classResponse).toMatchSchemaRef('LegacyContractClass');
});

test('GetClassByHash', async () => {
const classResponse = await testProvider.getClassByHash(erc20ClassHash);
expect(classResponse).toMatchSchemaRef('ContractClass');
expect(classResponse).toMatchSchemaRef('LegacyContractClass');
});

describe('getStorageAt', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describeIfRpc('RPCProvider', () => {
const contractClass = await rpcProvider.getClass(
'0x058d97f7d76e78f44905cc30cb65b91ea49a4b908a76703c54197bca90f81773'
);
expect(contractClass).toMatchSchemaRef('ContractClass');
expect(contractClass).toMatchSchemaRef('LegacyContractClass');
});
});
});
31 changes: 31 additions & 0 deletions __tests__/schemas/account.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,37 @@
},
"required": ["transaction_hash", "class_hash"]
},
"CompiledClass": {
"type": "object",
"properties": {
"prime": {
"type": "string"
},
"compiler_version": {
"type": "string"
},
"bytecode": {
"type": "array"
},
"hints": {
"type": "array"
},
"pythonic_hints": {
"type": "array"
},
"entry_points_by_type": {
"type": "object"
}
},
"required": [
"prime",
"compiler_version",
"bytecode",
"hints",
"pythonic_hints",
"entry_points_by_type"
]
},
"TransactionSimulation": {
"type": "object",
"properties": {
Expand Down
23 changes: 22 additions & 1 deletion __tests__/schemas/lib.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$id": "libSchemas",
"definitions": {
"ContractClass": {
"LegacyContractClass": {
"type": "object",
"properties": {
"program": {
Expand All @@ -15,6 +15,27 @@
}
},
"required": ["program", "entry_points_by_type"]
},
"SierraContractClass": {
"type": "object",
"properties": {
"sierra_program": {
"type": "array"
},
"sierra_program_debug_info": {
"type": "object"
},
"contract_class_version": {
"type": "string"
},
"entry_points_by_type": {
"type": "object"
},
"abi": {
"type": "array"
}
},
"required": ["sierra_program", "contract_class_version", "entry_points_by_type", "abi"]
}
}
}
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,12 @@ export class SequencerProvider implements ProviderInterface {
classHash: string,
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<ContractClass> {
return this.fetchEndpoint('get_class_by_hash', { classHash, blockIdentifier }).then(
parseContract
);
return this.fetchEndpoint('get_class_by_hash', { classHash, blockIdentifier }).then((res) => {
if (isSierra(res)) {
return this.responseParser.parseGetClassByHashResponse(res);
}
return parseContract(res);
});
}

public async getCompiledClassByClassHash(
Expand Down
5 changes: 3 additions & 2 deletions src/types/lib/contract/sierra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export type CairoAssembly = {
prime: string;
compiler_version: string;
bytecode: ByteCode;
hints: Hints;
hints: any[];
pythonic_hints: PythonicHints;
entry_points_by_type: EntryPointsByType;
};

Expand All @@ -24,7 +25,7 @@ export type CompiledSierraCasm = CairoAssembly;

/** SUBTYPES */
export type ByteCode = string[];
export type Hints = [number, string[]][];
export type PythonicHints = [number, string[]][];

export type SierraProgramDebugInfo = {
type_names: [number, string][];
Expand Down
9 changes: 9 additions & 0 deletions src/utils/responseParser/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
GetTransactionResponse,
InvokeFunctionResponse,
Sequencer,
SierraContractClass,
StateUpdateResponse,
TransactionSimulationResponse,
} from '../../types';
Expand Down Expand Up @@ -213,4 +214,12 @@ export class SequencerAPIResponseParser extends ResponseParser {
},
};
}

// TODO: Define response as new type as it diff from ContractClass
public parseGetClassByHashResponse(res: any): SierraContractClass {
return {
...res,
abi: JSON.parse(res.abi),
};
}
}

0 comments on commit 09a46af

Please sign in to comment.