Skip to content

Commit

Permalink
feat: declareIfNot, update declareAndDeploy with it, cleanup cairo1 test
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed May 8, 2023
1 parent 5688611 commit 494dbb8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 43 deletions.
62 changes: 25 additions & 37 deletions __tests__/cairo1.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { CallData, Contract, SequencerProvider } from '../src';
import { tuple } from '../src/utils/calldata/cairo';
import { toBigInt } from '../src/utils/num';
import { decodeShortString } from '../src/utils/shortString';
import { bnToUint256 } from '../src/utils/uint256';
import {
CallData,
Contract,
DeclareDeployUDCResponse,
SequencerProvider,
cairo,
num,
shortString,
} from '../src';
import {
compiledHelloSierra,
compiledHelloSierraCasm,
Expand All @@ -13,67 +17,51 @@ import {
} from './fixtures';
import { initializeMatcher } from './schema';

// Testnet will not accept declare v2 with same compiledClassHash,
// aka. we can't redeclare same contract
describeIfDevnetSequencer('Cairo 1 Devnet', () => {
describe('Sequencer API', () => {
const provider = getTestProvider() as SequencerProvider;
const account = getTestAccount(provider);
let classHash: any; // = '0x2daf503eec96f469b6069b3294be67ef7ffc1dce89c09535d400550d884e82e';
let contractAddress: any;
let declareV2Tx: any;
let dd: DeclareDeployUDCResponse;
let cairo1Contract: Contract;
initializeMatcher(expect);

beforeAll(async () => {
declareV2Tx = await account.declare({
dd = await account.declareAndDeploy({
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
});
classHash = declareV2Tx.class_hash;
await provider.waitForTransaction(declareV2Tx.transaction_hash);

const { transaction_hash, contract_address } = await account.deploy({ classHash });
[contractAddress] = contract_address;
await provider.waitForTransaction(transaction_hash);

cairo1Contract = new Contract(compiledHelloSierra.abi, contractAddress, account);
cairo1Contract = new Contract(compiledHelloSierra.abi, dd.deploy.contract_address, account);
});

test('Declare v2 - Hello Cairo 1 contract', async () => {
expect(declareV2Tx).toMatchSchemaRef('DeclareContractResponse');
});

xtest('declareAndDeploy Cairo 1', async () => {
// Can't test due to redeployment not allowed
await account.declareAndDeploy({
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
});
test('Declare & deploy v2 - Hello Cairo 1 contract', async () => {
expect(dd.declare).toMatchSchemaRef('DeclareContractResponse');
expect(dd.deploy).toMatchSchemaRef('DeployContractUDCResponse');
expect(cairo1Contract).toBeInstanceOf(Contract);
});

test('deployContract Cairo 1 with false cairoVersion UDC parameter', async () => {
const deploy = await account.deployContract(
{
classHash,
classHash: dd.deploy.classHash,
},
{ cairoVersion: '1' }
);
expect(deploy).toHaveProperty('address');
});

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

test('GetClassByHash', async () => {
const classResponse = await provider.getClassByHash(classHash);
const classResponse = await provider.getClassByHash(dd.deploy.classHash);
expect(classResponse).toMatchSchemaRef('SierraContractClass');
});

test('GetClassAt', async () => {
const classResponse = await provider.getClassAt(contractAddress);
const classResponse = await provider.getClassAt(dd.deploy.contract_address);
expect(classResponse).toMatchSchemaRef('SierraContractClass');
});

Expand All @@ -89,7 +77,7 @@ describeIfDevnetSequencer('Cairo 1 Devnet', () => {
parseResponse: false,
});

expect(toBigInt(balance[0])).toBe(100n);
expect(num.toBigInt(balance[0])).toBe(100n);
});

test('Cairo 1 Contract Interaction - felt252', async () => {
Expand Down Expand Up @@ -152,7 +140,7 @@ describeIfDevnetSequencer('Cairo 1 Devnet', () => {
});

test('Cairo 1 Contract Interaction - echo flat un-named un-nested tuple', async () => {
const status = await cairo1Contract.echo_un_tuple(tuple(77, 123));
const status = await cairo1Contract.echo_un_tuple(cairo.tuple(77, 123));
expect(Object.values(status)).toEqual([77n, 123n]);
});

Expand All @@ -171,7 +159,7 @@ describeIfDevnetSequencer('Cairo 1 Devnet', () => {
const status = await cairo1Contract.echo_struct({
val: 'simple',
});
expect(decodeShortString(status.val)).toBe('simple');
expect(shortString.decodeShortString(status.val)).toBe('simple');
});

test('Cairo 1 more complex structs', async () => {
Expand Down Expand Up @@ -207,7 +195,7 @@ describeIfDevnetSequencer('Cairo 1 Devnet', () => {
});

describeIfSequencerTestnet2('Cairo1 Testnet2', () => {
describe('Sequencer API', () => {
describe('Sequencer API - C1 T2 C:0x771bbe2ba64f...', () => {
const provider = getTestProvider() as SequencerProvider;
const account = getTestAccount(provider);
const classHash: any = '0x028b6f2ee9ae00d55a32072d939a55a6eb522974a283880f3c73a64c2f9fd6d6';
Expand Down Expand Up @@ -255,7 +243,7 @@ describeIfSequencerTestnet2('Cairo1 Testnet2', () => {
});

test('Cairo 1 - uint256 struct', async () => {
const myUint256 = bnToUint256(2n ** 256n - 2n);
const myUint256 = cairo.uint256(2n ** 256n - 2n);
const result = await cairo1Contract.test_u256(myUint256);
expect(result).toBe(2n ** 256n - 1n);
});
Expand Down
39 changes: 33 additions & 6 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,30 @@ export class Account extends Provider implements AccountInterface {
);
}

/**
* First check if contract is already declared, if not declare it
* If contract already declared returned transaction_hash is ''.
* Method will pass even if contract is already declared
* @param payload DeclareContractPayload
* @param transactionsDetail (optional) InvocationsDetails = {}
* @returns DeclareContractResponse
*/
public async declareIfNot(
payload: DeclareContractPayload,
transactionsDetail: InvocationsDetails = {}
): Promise<DeclareContractResponse> {
const declareContractPayload = extractContractHashes(payload);
try {
await this.getClassByHash(declareContractPayload.classHash);
} catch (error) {
return this.declare(payload, transactionsDetail);
}
return {
transaction_hash: '',
class_hash: declareContractPayload.classHash,
};
}

public async declare(
payload: DeclareContractPayload,
transactionsDetail: InvocationsDetails = {}
Expand Down Expand Up @@ -421,15 +445,18 @@ export class Account extends Provider implements AccountInterface {
details?: InvocationsDetails | undefined
): Promise<DeclareDeployUDCResponse> {
const { constructorCalldata, salt, unique } = payload;
const { transaction_hash, class_hash } = await this.declare(payload, details);
const declare = await this.waitForTransaction(transaction_hash, {
successStates: [TransactionStatus.ACCEPTED_ON_L2],
});
let declare = await this.declareIfNot(payload, details);
if (declare.transaction_hash !== '') {
const tx = await this.waitForTransaction(declare.transaction_hash, {
successStates: [TransactionStatus.ACCEPTED_ON_L2],
});
declare = { ...declare, ...tx };
}
const deploy = await this.deployContract(
{ classHash: class_hash, salt, unique, constructorCalldata },
{ classHash: declare.class_hash, salt, unique, constructorCalldata },
details
);
return { declare: { ...declare, class_hash }, deploy };
return { declare: { ...declare }, deploy };
}

public async deployAccount(
Expand Down
1 change: 1 addition & 0 deletions src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export abstract class AccountInterface extends ProviderInterface {
/**
* Declares and Deploy a given compiled contract (json) to starknet using UDC
* Internal wait for L2 transaction, do not support multicall
* Method will pass even if contract is already declared (internal using DeclareIfNot)
*
* @param containing
* - contract: compiled contract code
Expand Down

0 comments on commit 494dbb8

Please sign in to comment.