Skip to content

Commit ca6203f

Browse files
committed
feat: support contract declaration API introduced at SN 0.9.0
1 parent 496dfb6 commit ca6203f

File tree

7 files changed

+76
-5
lines changed

7 files changed

+76
-5
lines changed

__tests__/provider.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defaultProvider, stark } from '../src';
22
import { toBN } from '../src/utils/number';
3-
import { compiledArgentAccount } from './fixtures';
3+
import { compiledArgentAccount, compiledErc20 } from './fixtures';
44

55
const { compileCalldata } = stark;
66

@@ -123,6 +123,16 @@ describe('defaultProvider', () => {
123123
});
124124

125125
describe('addTransaction()', () => {
126+
test('declareContract()', async () => {
127+
const response = await defaultProvider.declareContract({
128+
contract: compiledErc20,
129+
});
130+
131+
expect(response.code).toBe('TRANSACTION_RECEIVED');
132+
expect(response.transaction_hash).toBeDefined();
133+
expect(response.class_hash).toBeDefined();
134+
});
135+
126136
test('deployContract()', async () => {
127137
const response = await defaultProvider.deployContract({
128138
contract: compiledArgentAccount,

src/account/default.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export class Account extends Provider implements AccountInterface {
143143
*/
144144
public async LEGACY_addTransaction(transaction: Transaction): Promise<AddTransactionResponse> {
145145
if (transaction.type === 'DEPLOY') throw new Error('No DEPLOYS');
146+
if (transaction.type === 'DECLARE') throw new Error('No DECLARES');
146147

147148
assert(
148149
!transaction.signature,

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export enum StarknetChainId {
1313
TESTNET = '0x534e5f474f45524c49', // encodeShortString('SN_GOERLI'),
1414
}
1515
export enum TransactionHashPrefix {
16+
DECLARE = '0x6465636c617265', // encodeShortString('declare'),
1617
DEPLOY = '0x6465706c6f79', // encodeShortString('deploy'),
1718
INVOKE = '0x696e766f6b65', // encodeShortString('invoke'),
1819
L1_HANDLER = '0x6c315f68616e646c6572', // encodeShortString('l1_handler'),

src/provider/default.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import fetch from 'cross-fetch';
22
import urljoin from 'url-join';
33

4-
import { StarknetChainId } from '../constants';
4+
import { ONE, StarknetChainId, ZERO } from '../constants';
55
import {
66
Abi,
77
AddTransactionResponse,
88
Call,
99
CallContractResponse,
1010
CompiledContract,
11+
DeclareContractPayload,
1112
DeployContractPayload,
1213
Endpoints,
1314
GetBlockResponse,
@@ -309,6 +310,34 @@ export class Provider implements ProviderInterface {
309310
return this.fetchEndpoint('get_transaction_trace', { transactionHash: txHashHex });
310311
}
311312

313+
/**
314+
* Declare a given compiled contract (json) on starknet
315+
*
316+
* @param contract - a json object containing the compiled contract
317+
* @returns a confirmation of sending a transaction on the starknet contract
318+
*/
319+
public declareContract(
320+
payload: DeclareContractPayload,
321+
_abi?: Abi
322+
): Promise<AddTransactionResponse> {
323+
const parsedContract =
324+
typeof payload.contract === 'string'
325+
? (parse(payload.contract) as CompiledContract)
326+
: payload.contract;
327+
const contractDefinition = {
328+
...parsedContract,
329+
program: compressProgram(parsedContract.program),
330+
};
331+
332+
return this.fetchEndpoint('add_transaction', undefined, {
333+
type: 'DECLARE',
334+
contract_class: contractDefinition,
335+
nonce: toHex(ZERO),
336+
signature: [],
337+
sender_address: toHex(ONE),
338+
});
339+
}
340+
312341
/**
313342
* Deploys a given compiled contract (json) to starknet
314343
*

src/types/api.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ export type GetContractAddressesResponse = {
9797
GpsStatementVerifier: string;
9898
};
9999

100+
export type DeclareTransaction = {
101+
type: 'DECLARE';
102+
contract_class: CompressedCompiledContract;
103+
nonce: BigNumberish;
104+
sender_address: BigNumberish;
105+
signature: Signature;
106+
};
107+
100108
export type DeployTransaction = {
101109
type: 'DEPLOY';
102110
contract_definition: CompressedCompiledContract;
@@ -148,7 +156,7 @@ export type CallContractTransaction = Omit<
148156
'type' | 'entry_point_type' | 'nonce'
149157
>;
150158

151-
export type Transaction = DeployTransaction | InvokeFunctionTransaction;
159+
export type Transaction = DeclareTransaction | DeployTransaction | InvokeFunctionTransaction;
152160

153161
export type CallContractResponse = {
154162
result: string[];
@@ -224,6 +232,7 @@ export type AddTransactionResponse = {
224232
code: TransactionStatus;
225233
transaction_hash: string;
226234
address?: string;
235+
class_hash?: string;
227236
};
228237

229238
export type TransactionReceiptResponse = {

src/types/lib.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export type DeployContractPayload = {
1212
addressSalt?: BigNumberish;
1313
};
1414

15+
export type DeclareContractPayload = {
16+
contract: CompiledContract | string;
17+
};
18+
1519
export type Invocation = {
1620
contractAddress: string;
1721
entrypoint: string;
@@ -35,7 +39,7 @@ export type Status =
3539
| 'ACCEPTED_ON_L1'
3640
| 'REJECTED';
3741
export type TransactionStatus = 'TRANSACTION_RECEIVED';
38-
export type Type = 'DEPLOY' | 'INVOKE_FUNCTION';
42+
export type Type = 'DECLARE' | 'DEPLOY' | 'INVOKE_FUNCTION';
3943
export type EntryPointType = 'EXTERNAL';
4044
export type CompressedProgram = string;
4145

www/docs/API/provider.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,26 @@ Gets the transaction trace from a tx hash.
198198

199199
<hr/>
200200

201+
provider.**declareContract**(payload [ , abi ]) => _Promise < AddTransactionResponse >_
202+
203+
Declares a contract on Starknet
204+
205+
###### _AddTransactionResponse_
206+
207+
```
208+
{
209+
code: 'TRANSACTION_RECEIVED';
210+
transaction_hash: string;
211+
class_hash: string;
212+
};
213+
214+
<hr/>
215+
216+
```
217+
201218
provider.**deployContract**(payload [ , abi ]) => _Promise < AddTransactionResponse >_
202219

203-
Gets the transaction trace from a tx hash.
220+
Deploys a contract on Starknet
204221

205222
###### _AddTransactionResponse_
206223

0 commit comments

Comments
 (0)