Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V7 Breaking] Fix/1252 #1255

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions __tests__/cairo1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ describeIfDevnet('Cairo 1 Devnet', () => {
);
await account.waitForTransaction(tx.transaction_hash);

const balance = await cairo1Contract.get_balance({
parseResponse: false,
});
const balance = await cairo1Contract
.withOptions({
parseResponse: false,
})
.get_balance();

expect(num.toBigInt(balance[0])).toBe(100n);
});
Expand Down Expand Up @@ -285,9 +287,11 @@ describeIfDevnet('Cairo 1 Devnet', () => {
test('Cairo 1 more complex structs', async () => {
const tx = await cairo1Contract.set_bet();
await account.waitForTransaction(tx.transaction_hash);
const status = await cairo1Contract.get_bet(1, {
formatResponse: { name: 'string', description: 'string' },
});
const status = await cairo1Contract
.withOptions({
formatResponse: { name: 'string', description: 'string' },
})
.get_bet(1);

const expected = {
name: 'test',
Expand Down
17 changes: 11 additions & 6 deletions __tests__/cairo1v2.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs';
import path from 'node:path';

import {
Account,
BigNumberish,
Expand Down Expand Up @@ -123,9 +124,11 @@ describe('Cairo 1', () => {
);
await account.waitForTransaction(tx.transaction_hash);

const balance = await cairo1Contract.get_balance({
parseResponse: false,
});
const balance = await cairo1Contract
.withOptions({
parseResponse: false,
})
.get_balance();

expect(num.toBigInt(balance[0])).toBe(100n);
});
Expand Down Expand Up @@ -321,9 +324,11 @@ describe('Cairo 1', () => {
test('Cairo 1 more complex structs', async () => {
const tx = await cairo1Contract.set_bet();
await account.waitForTransaction(tx.transaction_hash);
const status = await cairo1Contract.get_bet(1, {
formatResponse: { name: 'string', description: 'string' },
});
const status = await cairo1Contract
.withOptions({
formatResponse: { name: 'string', description: 'string' },
})
.get_bet(1);

const expected = {
name: 'test',
Expand Down
16 changes: 10 additions & 6 deletions __tests__/cairo1v2_typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ describe('Cairo 1', () => {
);
await account.waitForTransaction(tx.transaction_hash);

const balance = await cairo1Contract.get_balance({
parseResponse: false,
});
const balance = await cairo1Contract
.withOptions({
parseResponse: false,
})
.get_balance();

// TODO: handle parseResponse correctly, get_balance should return a list here !?
expect(num.toBigInt(balance)).toBe(100n);
Expand Down Expand Up @@ -335,9 +337,11 @@ describe('Cairo 1', () => {
test('Cairo 1 more complex structs', async () => {
const tx = await cairo1Contract.set_bet();
await account.waitForTransaction(tx.transaction_hash);
const status = await cairo1Contract.get_bet(1, {
formatResponse: { name: 'string', description: 'string' },
});
const status = await cairo1Contract
.withOptions({
formatResponse: { name: 'string', description: 'string' },
})
.get_bet(1);

const expected = {
name: 'test',
Expand Down
108 changes: 62 additions & 46 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
ContractFactory,
ParsedEvents,
RawArgs,
SuccessfulTransactionReceiptResponse,
json,
shortString,
stark,
SuccessfulTransactionReceiptResponse,
} from '../src';
import { CallData } from '../src/utils/calldata';
import { felt, isCairo1Abi, tuple, uint256 } from '../src/utils/calldata/cairo';
Expand Down Expand Up @@ -76,9 +76,11 @@ describe('contract module', () => {
});

test('read initial balance of that account', async () => {
const { balance } = await erc20Contract.balanceOf(wallet, {
formatResponse: { balance: uint256ToBN },
});
const { balance } = await erc20Contract
.withOptions({
formatResponse: { balance: uint256ToBN },
})
.balanceOf(wallet);
expect(balance).toStrictEqual(BigInt(1000));
});

Expand Down Expand Up @@ -563,18 +565,24 @@ describe('Complex interaction', () => {
const calldata = CallData.compile(request);
const args = Object.values(request);

const result = await erc20Echo20Contract.echo(calldata, {
parseRequest: true,
parseResponse: true,
formatResponse,
});
const result = await erc20Echo20Contract
.withOptions({
parseRequest: true,
parseResponse: true,
formatResponse,
})
.echo(calldata);

const result2 = await erc20Echo20Contract
.withOptions({
formatResponse,
})
.echo(...args);

const result2 = await erc20Echo20Contract.echo(...args, {
formatResponse,
});
const result3 = await erc20Echo20Contract.call('echo', calldata, {
formatResponse,
});

const result4 = await erc20Echo20Contract.call('echo', args, {
formatResponse,
});
Expand Down Expand Up @@ -830,26 +838,27 @@ describe('Complex interaction', () => {

describe('speedup live tests', () => {
test('call parameterized data', async () => {
const result = await erc20Echo20Contract.echo(
request.t1,
request.n1,
request.tl2,
request.k1,
request.k2,
request.u1,
request.s1,
request.s2,
request.af1,
request.au1,
request.as1,
request.atmk,
request.atmku,
{
const result = await erc20Echo20Contract
.withOptions({
parseRequest: true,
parseResponse: true,
formatResponse,
}
);
})
.echo(
request.t1,
request.n1,
request.tl2,
request.k1,
request.k2,
request.u1,
request.s1,
request.s2,
request.af1,
request.au1,
request.as1,
request.atmk,
request.atmku
);

// Convert request uint256 to match response
const compareRequest = {
Expand All @@ -862,22 +871,25 @@ describe('Complex interaction', () => {
});

test('invoke parameterized data', async () => {
const result = await erc20Echo20Contract.iecho(
request.t1,
request.n1,
request.tl2,
request.k1,
request.k2,
request.u1,
request.s1,
request.s2,
request.af1,
request.au1,
request.as1,
request.atmk,
request.atmku,
{ formatResponse }
);
const result = await erc20Echo20Contract
.withOptions({
formatResponse,
})
.iecho(
request.t1,
request.n1,
request.tl2,
request.k1,
request.k2,
request.u1,
request.s1,
request.s2,
request.af1,
request.au1,
request.as1,
request.atmk,
request.atmku
);
const transaction = await provider.waitForTransaction(result.transaction_hash);
expect(
(transaction as SuccessfulTransactionReceiptResponse).execution_status
Expand Down Expand Up @@ -909,7 +921,11 @@ describe('Complex interaction', () => {

// mark data as compiled (it can be also done manually check defineProperty compiled in CallData.compile)
const compiledCallData = CallData.compile(populated4.calldata);
const result = await erc20Echo20Contract.echo(compiledCallData, { formatResponse });
const result = await erc20Echo20Contract
.withOptions({
formatResponse,
})
.echo(compiledCallData);

// Convert request uint256 to match response
const compareRequest = {
Expand Down
8 changes: 3 additions & 5 deletions __tests__/utils/ethSigner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,9 @@ describe('Ethereum signer', () => {

test('ETH account transaction V2', async () => {
const ethContract2 = new Contract(contracts.Erc20.abi, devnetETHtokenAddress, ethAccount);
const respTransfer = await ethContract2.transfer(
account.address,
cairo.uint256(1 * 10 ** 4),
{ maxFee: 1 * 10 ** 16 }
);
const respTransfer = await ethContract2
.withOptions({ maxFee: 1 * 10 ** 16 })
.transfer(account.address, cairo.uint256(1 * 10 ** 4));
const txR = await provider.waitForTransaction(respTransfer.transaction_hash);
if (txR.isSuccess()) {
expect(txR.execution_status).toBe('SUCCEEDED');
Expand Down
25 changes: 15 additions & 10 deletions src/contract/contractFactory.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { AccountInterface } from '../account';
import {
Abi,
ArgsOrCalldataWithOptions,
ArgsOrCalldata,
CairoAssembly,
CompiledContract,
ContractOptions,
ValidateType,
} from '../types';
import assert from '../utils/assert';
import { CallData } from '../utils/calldata';
import { Contract, getCalldata, splitArgsAndOptions } from './default';
import { Contract, getCalldata } from './default';

export type ContractFactoryParams = {
compiledContract: CompiledContract;
Expand All @@ -17,6 +18,7 @@ export type ContractFactoryParams = {
classHash?: string;
compiledClassHash?: string;
abi?: Abi;
contractOptions?: ContractOptions;
};

export class ContractFactory {
Expand All @@ -34,6 +36,8 @@ export class ContractFactory {

private CallData: CallData;

public contractOptions?: ContractOptions;

/**
* @param params CFParams
* - compiledContract: CompiledContract;
Expand All @@ -51,24 +55,25 @@ export class ContractFactory {
this.classHash = params.classHash;
this.compiledClassHash = params.compiledClassHash;
this.CallData = new CallData(this.abi);
this.contractOptions = params.contractOptions;
}

/**
* Deploys contract and returns new instance of the Contract
*
* If contract is not declared it will first declare it, and then deploy
*/
public async deploy(...args: ArgsOrCalldataWithOptions): Promise<Contract> {
const { args: param, options = { parseRequest: true } } = splitArgsAndOptions(args);
public async deploy(...args: ArgsOrCalldata): Promise<Contract> {
// const { args: param, options = { parseRequest: true } } = args; // splitArgsAndOptions(args);

const constructorCalldata = getCalldata(param, () => {
if (options.parseRequest) {
this.CallData.validate(ValidateType.DEPLOY, 'constructor', param);
return this.CallData.compile('constructor', param);
const constructorCalldata = getCalldata(args, () => {
if (this.contractOptions?.parseRequest) {
this.CallData.validate(ValidateType.DEPLOY, 'constructor', args);
return this.CallData.compile('constructor', args);
}
// eslint-disable-next-line no-console
console.warn('Call skipped parsing but provided rawArgs, possible malfunction request');
return param;
return args;
});

const {
Expand All @@ -79,7 +84,7 @@ export class ContractFactory {
classHash: this.classHash,
compiledClassHash: this.compiledClassHash,
constructorCalldata,
salt: options.addressSalt,
salt: this.contractOptions?.addressSalt,
});
assert(Boolean(contract_address), 'Deployment of the contract failed');

Expand Down
Loading