Skip to content

Commit

Permalink
feat: fill in sequencer class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
penovicp committed Nov 10, 2022
1 parent d26d98a commit 83c0a29
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ describe('defaultProvider', () => {
expect(classResponse).toHaveProperty('entry_points_by_type');
});

// TODO see if feasible to split
describe('getClassHashAt & GetClass', () => {
test('responses', async () => {
const classHash = await testProvider.getClassHashAt(exampleContractAddress);
expect(typeof classHash).toBe('string');

const classResponse = await testProvider.getClass(classHash);
expect(classResponse).toHaveProperty('program');
expect(classResponse).toHaveProperty('entry_points_by_type');
});
});

describe('getStorageAt', () => {
test('with "key" type of number', () => {
return expect(testProvider.getStorageAt(exampleContractAddress, 0)).resolves.not.toThrow();
Expand Down
14 changes: 14 additions & 0 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ export class Provider implements ProviderInterface {
return this.provider.getClassAt(contractAddress, blockIdentifier);
}

public async getClassHashAt(
contractAddress: string,
blockIdentifier: BlockIdentifier = 'pending'
): Promise<string> {
// TODO flip params in one of the provider implementations?
if (this.provider instanceof RpcProvider)
return this.provider.getClassHashAt(blockIdentifier, contractAddress);
return this.provider.getClassHashAt(contractAddress, blockIdentifier);
}

public getClass(classHash: string): Promise<ContractClass> {
return this.provider.getClass(classHash);
}

public async getEstimateFee(
invocationWithTxType: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
Expand Down
20 changes: 20 additions & 0 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ export abstract class ProviderInterface {
blockIdentifier?: BlockIdentifier
): Promise<ContractClass>;

/**
* Returns the class hash deployed under the given address.
*
* @param contractAddress - contract address
* @param blockIdentifier - block identifier
* @returns Class hash
*/
public abstract getClassHashAt(
contractAddress: string,
blockIdentifier?: BlockIdentifier
): Promise<string>;

/**
* Returns the contract class deployed under the given class hash.
*
* @param classHash - class hash
* @returns Contract class of compiled contract
*/
public abstract getClass(classHash: string): Promise<ContractClass>;

/**
* Gets the nonce of a contract with respect to a specific block
*
Expand Down
11 changes: 11 additions & 0 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,17 @@ export class SequencerProvider implements ProviderInterface {
);
}

public async getClassHashAt(
contractAddress: string,
blockIdentifier: BlockIdentifier = 'pending'
): Promise<string> {
return this.fetchEndpoint('get_class_hash_at', { blockIdentifier, contractAddress });
}

public async getClass(classHash: string): Promise<ContractClass> {
return this.fetchEndpoint('get_class_by_hash', { classHash }).then(parseContract);
}

public async invokeFunction(
functionInvocation: Invocation,
details: InvocationsDetailsWithNonce
Expand Down

0 comments on commit 83c0a29

Please sign in to comment.