Skip to content

[V7 Breaking] Fix/1252 #1255

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

Merged
merged 9 commits into from
Mar 6, 2025
Merged
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
2 changes: 1 addition & 1 deletion __tests__/WebSocketChannel.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WebSocket } from 'isows';

import { Provider, WSSubscriptions, WebSocketChannel } from '../src';
import { StarknetChainId } from '../src/constants';
import { StarknetChainId } from '../src/global/constants';
import { getTestAccount, getTestProvider } from './config/fixtures';

const nodeUrl = 'wss://sepolia-pathfinder-rpc.spaceshard.io/rpc/v0_8';
Expand All @@ -20,7 +20,7 @@
try {
await webSocketChannel.waitForConnection();
} catch (error: any) {
console.log(error.message);

Check warning on line 23 in __tests__/WebSocketChannel.test.ts

View workflow job for this annotation

GitHub Actions / Run test on rpc-devnet / Run tests

Unexpected console statement
}
expect(webSocketChannel.isConnected()).toBe(true);
});
Expand Down
16 changes: 10 additions & 6 deletions __tests__/cairo1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,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 @@ -291,9 +293,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
16 changes: 10 additions & 6 deletions __tests__/cairo1v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,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 @@ -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
16 changes: 10 additions & 6 deletions __tests__/cairo1v2_typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,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 @@ -348,9 +350,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
106 changes: 61 additions & 45 deletions __tests__/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,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 @@ -573,18 +575,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 @@ -851,26 +859,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 @@ -883,22 +892,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 unknown as SuccessfulTransactionReceiptResponse).execution_status
Expand Down Expand Up @@ -930,7 +942,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 @@ -169,11 +169,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()) {
// TODO: @PhilippeR26 Why this is not working, fix 'as any' hotfix
Expand Down
25 changes: 15 additions & 10 deletions src/contract/contractFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { AccountInterface } from '../account';
import { logger } from '../global/logger';
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 @@ -18,6 +19,7 @@ export type ContractFactoryParams = {
classHash?: string;
compiledClassHash?: string;
abi?: Abi;
contractOptions?: ContractOptions;
};

export class ContractFactory {
Expand All @@ -35,6 +37,8 @@ export class ContractFactory {

private CallData: CallData;

public contractOptions?: ContractOptions;

/**
* @param params CFParams
* - compiledContract: CompiledContract;
Expand All @@ -52,23 +56,24 @@ 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);
}
logger.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
Loading