Skip to content

Commit

Permalink
feat: support contract declaration API introduced at SN 0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yoga-braavos committed Jun 15, 2022
1 parent 496dfb6 commit ca6203f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 5 deletions.
12 changes: 11 additions & 1 deletion __tests__/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defaultProvider, stark } from '../src';
import { toBN } from '../src/utils/number';
import { compiledArgentAccount } from './fixtures';
import { compiledArgentAccount, compiledErc20 } from './fixtures';

const { compileCalldata } = stark;

Expand Down Expand Up @@ -123,6 +123,16 @@ describe('defaultProvider', () => {
});

describe('addTransaction()', () => {
test('declareContract()', async () => {
const response = await defaultProvider.declareContract({
contract: compiledErc20,
});

expect(response.code).toBe('TRANSACTION_RECEIVED');
expect(response.transaction_hash).toBeDefined();
expect(response.class_hash).toBeDefined();
});

test('deployContract()', async () => {
const response = await defaultProvider.deployContract({
contract: compiledArgentAccount,
Expand Down
1 change: 1 addition & 0 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export class Account extends Provider implements AccountInterface {
*/
public async LEGACY_addTransaction(transaction: Transaction): Promise<AddTransactionResponse> {
if (transaction.type === 'DEPLOY') throw new Error('No DEPLOYS');
if (transaction.type === 'DECLARE') throw new Error('No DECLARES');

assert(
!transaction.signature,
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum StarknetChainId {
TESTNET = '0x534e5f474f45524c49', // encodeShortString('SN_GOERLI'),
}
export enum TransactionHashPrefix {
DECLARE = '0x6465636c617265', // encodeShortString('declare'),
DEPLOY = '0x6465706c6f79', // encodeShortString('deploy'),
INVOKE = '0x696e766f6b65', // encodeShortString('invoke'),
L1_HANDLER = '0x6c315f68616e646c6572', // encodeShortString('l1_handler'),
Expand Down
31 changes: 30 additions & 1 deletion src/provider/default.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import fetch from 'cross-fetch';
import urljoin from 'url-join';

import { StarknetChainId } from '../constants';
import { ONE, StarknetChainId, ZERO } from '../constants';
import {
Abi,
AddTransactionResponse,
Call,
CallContractResponse,
CompiledContract,
DeclareContractPayload,
DeployContractPayload,
Endpoints,
GetBlockResponse,
Expand Down Expand Up @@ -309,6 +310,34 @@ export class Provider implements ProviderInterface {
return this.fetchEndpoint('get_transaction_trace', { transactionHash: txHashHex });
}

/**
* Declare a given compiled contract (json) on starknet
*
* @param contract - a json object containing the compiled contract
* @returns a confirmation of sending a transaction on the starknet contract
*/
public declareContract(
payload: DeclareContractPayload,
_abi?: Abi
): Promise<AddTransactionResponse> {
const parsedContract =
typeof payload.contract === 'string'
? (parse(payload.contract) as CompiledContract)
: payload.contract;
const contractDefinition = {
...parsedContract,
program: compressProgram(parsedContract.program),
};

return this.fetchEndpoint('add_transaction', undefined, {
type: 'DECLARE',
contract_class: contractDefinition,
nonce: toHex(ZERO),
signature: [],
sender_address: toHex(ONE),
});
}

/**
* Deploys a given compiled contract (json) to starknet
*
Expand Down
11 changes: 10 additions & 1 deletion src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ export type GetContractAddressesResponse = {
GpsStatementVerifier: string;
};

export type DeclareTransaction = {
type: 'DECLARE';
contract_class: CompressedCompiledContract;
nonce: BigNumberish;
sender_address: BigNumberish;
signature: Signature;
};

export type DeployTransaction = {
type: 'DEPLOY';
contract_definition: CompressedCompiledContract;
Expand Down Expand Up @@ -148,7 +156,7 @@ export type CallContractTransaction = Omit<
'type' | 'entry_point_type' | 'nonce'
>;

export type Transaction = DeployTransaction | InvokeFunctionTransaction;
export type Transaction = DeclareTransaction | DeployTransaction | InvokeFunctionTransaction;

export type CallContractResponse = {
result: string[];
Expand Down Expand Up @@ -224,6 +232,7 @@ export type AddTransactionResponse = {
code: TransactionStatus;
transaction_hash: string;
address?: string;
class_hash?: string;
};

export type TransactionReceiptResponse = {
Expand Down
6 changes: 5 additions & 1 deletion src/types/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export type DeployContractPayload = {
addressSalt?: BigNumberish;
};

export type DeclareContractPayload = {
contract: CompiledContract | string;
};

export type Invocation = {
contractAddress: string;
entrypoint: string;
Expand All @@ -35,7 +39,7 @@ export type Status =
| 'ACCEPTED_ON_L1'
| 'REJECTED';
export type TransactionStatus = 'TRANSACTION_RECEIVED';
export type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
export type Type = 'DECLARE' | 'DEPLOY' | 'INVOKE_FUNCTION';
export type EntryPointType = 'EXTERNAL';
export type CompressedProgram = string;

Expand Down
19 changes: 18 additions & 1 deletion www/docs/API/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,26 @@ Gets the transaction trace from a tx hash.

<hr/>

provider.**declareContract**(payload [ , abi ]) => _Promise < AddTransactionResponse >_

Declares a contract on Starknet

###### _AddTransactionResponse_

```
{
code: 'TRANSACTION_RECEIVED';
transaction_hash: string;
class_hash: string;
};
<hr/>
```

provider.**deployContract**(payload [ , abi ]) => _Promise < AddTransactionResponse >_

Gets the transaction trace from a tx hash.
Deploys a contract on Starknet

###### _AddTransactionResponse_

Expand Down

0 comments on commit ca6203f

Please sign in to comment.