From ca6203f93471d7fb421d580e07d6de7c183e40f3 Mon Sep 17 00:00:00 2001 From: Yoav Gaziel Date: Thu, 9 Jun 2022 13:04:34 +0300 Subject: [PATCH] feat: support contract declaration API introduced at SN 0.9.0 --- __tests__/provider.test.ts | 12 +++++++++++- src/account/default.ts | 1 + src/constants.ts | 1 + src/provider/default.ts | 31 ++++++++++++++++++++++++++++++- src/types/api.ts | 11 ++++++++++- src/types/lib.ts | 6 +++++- www/docs/API/provider.md | 19 ++++++++++++++++++- 7 files changed, 76 insertions(+), 5 deletions(-) diff --git a/__tests__/provider.test.ts b/__tests__/provider.test.ts index c3e405db6..c4d34ada4 100644 --- a/__tests__/provider.test.ts +++ b/__tests__/provider.test.ts @@ -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; @@ -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, diff --git a/src/account/default.ts b/src/account/default.ts index 910f703ff..3fd68ea89 100644 --- a/src/account/default.ts +++ b/src/account/default.ts @@ -143,6 +143,7 @@ export class Account extends Provider implements AccountInterface { */ public async LEGACY_addTransaction(transaction: Transaction): Promise { if (transaction.type === 'DEPLOY') throw new Error('No DEPLOYS'); + if (transaction.type === 'DECLARE') throw new Error('No DECLARES'); assert( !transaction.signature, diff --git a/src/constants.ts b/src/constants.ts index 0fd6b7ae2..8cac92921 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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'), diff --git a/src/provider/default.ts b/src/provider/default.ts index 4be99135b..902acbca2 100644 --- a/src/provider/default.ts +++ b/src/provider/default.ts @@ -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, @@ -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 { + 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 * diff --git a/src/types/api.ts b/src/types/api.ts index b6ca01591..9271774cb 100644 --- a/src/types/api.ts +++ b/src/types/api.ts @@ -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; @@ -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[]; @@ -224,6 +232,7 @@ export type AddTransactionResponse = { code: TransactionStatus; transaction_hash: string; address?: string; + class_hash?: string; }; export type TransactionReceiptResponse = { diff --git a/src/types/lib.ts b/src/types/lib.ts index 9e692be20..9091c1e29 100644 --- a/src/types/lib.ts +++ b/src/types/lib.ts @@ -12,6 +12,10 @@ export type DeployContractPayload = { addressSalt?: BigNumberish; }; +export type DeclareContractPayload = { + contract: CompiledContract | string; +}; + export type Invocation = { contractAddress: string; entrypoint: string; @@ -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; diff --git a/www/docs/API/provider.md b/www/docs/API/provider.md index 1b3b856c8..95360b8f6 100644 --- a/www/docs/API/provider.md +++ b/www/docs/API/provider.md @@ -198,9 +198,26 @@ Gets the transaction trace from a tx hash.
+provider.**declareContract**(payload [ , abi ]) => _Promise < AddTransactionResponse >_ + +Declares a contract on Starknet + +###### _AddTransactionResponse_ + +``` +{ + code: 'TRANSACTION_RECEIVED'; + transaction_hash: string; + class_hash: string; +}; + +
+ +``` + provider.**deployContract**(payload [ , abi ]) => _Promise < AddTransactionResponse >_ -Gets the transaction trace from a tx hash. +Deploys a contract on Starknet ###### _AddTransactionResponse_