Skip to content

Commit b59952e

Browse files
authored
fix: contract withOptions Fix/1252 (#1255)
* fix: contract class splitArgsAndOptions * fix: type ArgsOrCalldataWithOptions * fix: options in method on contract class, BREAKING * chore: clenup and options docs update * test: fix * chore: develop * chore: fix typed test
1 parent fabca27 commit b59952e

9 files changed

+181
-109
lines changed

__tests__/WebSocketChannel.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { WebSocket } from 'isows';
22

33
import { Provider, WSSubscriptions, WebSocketChannel } from '../src';
4-
import { StarknetChainId } from '../src/constants';
4+
import { StarknetChainId } from '../src/global/constants';
55
import { getTestAccount, getTestProvider } from './config/fixtures';
66

77
const nodeUrl = 'wss://sepolia-pathfinder-rpc.spaceshard.io/rpc/v0_8';

__tests__/cairo1.test.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ describeIfDevnet('Cairo 1 Devnet', () => {
143143
);
144144
await account.waitForTransaction(tx.transaction_hash);
145145

146-
const balance = await cairo1Contract.get_balance({
147-
parseResponse: false,
148-
});
146+
const balance = await cairo1Contract
147+
.withOptions({
148+
parseResponse: false,
149+
})
150+
.get_balance();
149151

150152
expect(num.toBigInt(balance[0])).toBe(100n);
151153
});
@@ -291,9 +293,11 @@ describeIfDevnet('Cairo 1 Devnet', () => {
291293
test('Cairo 1 more complex structs', async () => {
292294
const tx = await cairo1Contract.set_bet();
293295
await account.waitForTransaction(tx.transaction_hash);
294-
const status = await cairo1Contract.get_bet(1, {
295-
formatResponse: { name: 'string', description: 'string' },
296-
});
296+
const status = await cairo1Contract
297+
.withOptions({
298+
formatResponse: { name: 'string', description: 'string' },
299+
})
300+
.get_bet(1);
297301

298302
const expected = {
299303
name: 'test',

__tests__/cairo1v2.test.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,11 @@ describe('Cairo 1', () => {
137137
);
138138
await account.waitForTransaction(tx.transaction_hash);
139139

140-
const balance = await cairo1Contract.get_balance({
141-
parseResponse: false,
142-
});
140+
const balance = await cairo1Contract
141+
.withOptions({
142+
parseResponse: false,
143+
})
144+
.get_balance();
143145

144146
expect(num.toBigInt(balance[0])).toBe(100n);
145147
});
@@ -335,9 +337,11 @@ describe('Cairo 1', () => {
335337
test('Cairo 1 more complex structs', async () => {
336338
const tx = await cairo1Contract.set_bet();
337339
await account.waitForTransaction(tx.transaction_hash);
338-
const status = await cairo1Contract.get_bet(1, {
339-
formatResponse: { name: 'string', description: 'string' },
340-
});
340+
const status = await cairo1Contract
341+
.withOptions({
342+
formatResponse: { name: 'string', description: 'string' },
343+
})
344+
.get_bet(1);
341345

342346
const expected = {
343347
name: 'test',

__tests__/cairo1v2_typed.test.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ describe('Cairo 1', () => {
143143
);
144144
await account.waitForTransaction(tx.transaction_hash);
145145

146-
const balance = await cairo1Contract.get_balance({
147-
parseResponse: false,
148-
});
146+
const balance = await cairo1Contract
147+
.withOptions({
148+
parseResponse: false,
149+
})
150+
.get_balance();
149151

150152
// TODO: handle parseResponse correctly, get_balance should return a list here !?
151153
expect(num.toBigInt(balance)).toBe(100n);
@@ -348,9 +350,11 @@ describe('Cairo 1', () => {
348350
test('Cairo 1 more complex structs', async () => {
349351
const tx = await cairo1Contract.set_bet();
350352
await account.waitForTransaction(tx.transaction_hash);
351-
const status = await cairo1Contract.get_bet(1, {
352-
formatResponse: { name: 'string', description: 'string' },
353-
});
353+
const status = await cairo1Contract
354+
.withOptions({
355+
formatResponse: { name: 'string', description: 'string' },
356+
})
357+
.get_bet(1);
354358

355359
const expected = {
356360
name: 'test',

__tests__/contract.test.ts

+61-45
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ describe('contract module', () => {
8383
});
8484

8585
test('read initial balance of that account', async () => {
86-
const { balance } = await erc20Contract.balanceOf(wallet, {
87-
formatResponse: { balance: uint256ToBN },
88-
});
86+
const { balance } = await erc20Contract
87+
.withOptions({
88+
formatResponse: { balance: uint256ToBN },
89+
})
90+
.balanceOf(wallet);
8991
expect(balance).toStrictEqual(BigInt(1000));
9092
});
9193

@@ -573,18 +575,24 @@ describe('Complex interaction', () => {
573575
const calldata = CallData.compile(request);
574576
const args = Object.values(request);
575577

576-
const result = await erc20Echo20Contract.echo(calldata, {
577-
parseRequest: true,
578-
parseResponse: true,
579-
formatResponse,
580-
});
578+
const result = await erc20Echo20Contract
579+
.withOptions({
580+
parseRequest: true,
581+
parseResponse: true,
582+
formatResponse,
583+
})
584+
.echo(calldata);
585+
586+
const result2 = await erc20Echo20Contract
587+
.withOptions({
588+
formatResponse,
589+
})
590+
.echo(...args);
581591

582-
const result2 = await erc20Echo20Contract.echo(...args, {
583-
formatResponse,
584-
});
585592
const result3 = await erc20Echo20Contract.call('echo', calldata, {
586593
formatResponse,
587594
});
595+
588596
const result4 = await erc20Echo20Contract.call('echo', args, {
589597
formatResponse,
590598
});
@@ -851,26 +859,27 @@ describe('Complex interaction', () => {
851859

852860
describe('speedup live tests', () => {
853861
test('call parameterized data', async () => {
854-
const result = await erc20Echo20Contract.echo(
855-
request.t1,
856-
request.n1,
857-
request.tl2,
858-
request.k1,
859-
request.k2,
860-
request.u1,
861-
request.s1,
862-
request.s2,
863-
request.af1,
864-
request.au1,
865-
request.as1,
866-
request.atmk,
867-
request.atmku,
868-
{
862+
const result = await erc20Echo20Contract
863+
.withOptions({
869864
parseRequest: true,
870865
parseResponse: true,
871866
formatResponse,
872-
}
873-
);
867+
})
868+
.echo(
869+
request.t1,
870+
request.n1,
871+
request.tl2,
872+
request.k1,
873+
request.k2,
874+
request.u1,
875+
request.s1,
876+
request.s2,
877+
request.af1,
878+
request.au1,
879+
request.as1,
880+
request.atmk,
881+
request.atmku
882+
);
874883

875884
// Convert request uint256 to match response
876885
const compareRequest = {
@@ -883,22 +892,25 @@ describe('Complex interaction', () => {
883892
});
884893

885894
test('invoke parameterized data', async () => {
886-
const result = await erc20Echo20Contract.iecho(
887-
request.t1,
888-
request.n1,
889-
request.tl2,
890-
request.k1,
891-
request.k2,
892-
request.u1,
893-
request.s1,
894-
request.s2,
895-
request.af1,
896-
request.au1,
897-
request.as1,
898-
request.atmk,
899-
request.atmku,
900-
{ formatResponse }
901-
);
895+
const result = await erc20Echo20Contract
896+
.withOptions({
897+
formatResponse,
898+
})
899+
.iecho(
900+
request.t1,
901+
request.n1,
902+
request.tl2,
903+
request.k1,
904+
request.k2,
905+
request.u1,
906+
request.s1,
907+
request.s2,
908+
request.af1,
909+
request.au1,
910+
request.as1,
911+
request.atmk,
912+
request.atmku
913+
);
902914
const transaction = await provider.waitForTransaction(result.transaction_hash);
903915
expect(
904916
(transaction as unknown as SuccessfulTransactionReceiptResponse).execution_status
@@ -930,7 +942,11 @@ describe('Complex interaction', () => {
930942

931943
// mark data as compiled (it can be also done manually check defineProperty compiled in CallData.compile)
932944
const compiledCallData = CallData.compile(populated4.calldata);
933-
const result = await erc20Echo20Contract.echo(compiledCallData, { formatResponse });
945+
const result = await erc20Echo20Contract
946+
.withOptions({
947+
formatResponse,
948+
})
949+
.echo(compiledCallData);
934950

935951
// Convert request uint256 to match response
936952
const compareRequest = {

__tests__/utils/ethSigner.test.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,9 @@ describe('Ethereum signer', () => {
169169

170170
test('ETH account transaction V2', async () => {
171171
const ethContract2 = new Contract(contracts.Erc20.abi, devnetETHtokenAddress, ethAccount);
172-
const respTransfer = await ethContract2.transfer(
173-
account.address,
174-
cairo.uint256(1 * 10 ** 4),
175-
{ maxFee: 1 * 10 ** 16 }
176-
);
172+
const respTransfer = await ethContract2
173+
.withOptions({ maxFee: 1 * 10 ** 16 })
174+
.transfer(account.address, cairo.uint256(1 * 10 ** 4));
177175
const txR = await provider.waitForTransaction(respTransfer.transaction_hash);
178176
if (txR.isSuccess()) {
179177
// TODO: @PhilippeR26 Why this is not working, fix 'as any' hotfix

src/contract/contractFactory.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { AccountInterface } from '../account';
22
import { logger } from '../global/logger';
33
import {
44
Abi,
5-
ArgsOrCalldataWithOptions,
5+
ArgsOrCalldata,
66
CairoAssembly,
77
CompiledContract,
8+
ContractOptions,
89
ValidateType,
910
} from '../types';
1011
import assert from '../utils/assert';
1112
import { CallData } from '../utils/calldata';
12-
import { Contract, getCalldata, splitArgsAndOptions } from './default';
13+
import { Contract, getCalldata } from './default';
1314

1415
export type ContractFactoryParams = {
1516
compiledContract: CompiledContract;
@@ -18,6 +19,7 @@ export type ContractFactoryParams = {
1819
classHash?: string;
1920
compiledClassHash?: string;
2021
abi?: Abi;
22+
contractOptions?: ContractOptions;
2123
};
2224

2325
export class ContractFactory {
@@ -35,6 +37,8 @@ export class ContractFactory {
3537

3638
private CallData: CallData;
3739

40+
public contractOptions?: ContractOptions;
41+
3842
/**
3943
* @param params CFParams
4044
* - compiledContract: CompiledContract;
@@ -52,23 +56,24 @@ export class ContractFactory {
5256
this.classHash = params.classHash;
5357
this.compiledClassHash = params.compiledClassHash;
5458
this.CallData = new CallData(this.abi);
59+
this.contractOptions = params.contractOptions;
5560
}
5661

5762
/**
5863
* Deploys contract and returns new instance of the Contract
5964
*
6065
* If contract is not declared it will first declare it, and then deploy
6166
*/
62-
public async deploy(...args: ArgsOrCalldataWithOptions): Promise<Contract> {
63-
const { args: param, options = { parseRequest: true } } = splitArgsAndOptions(args);
67+
public async deploy(...args: ArgsOrCalldata): Promise<Contract> {
68+
// const { args: param, options = { parseRequest: true } } = args; // splitArgsAndOptions(args);
6469

65-
const constructorCalldata = getCalldata(param, () => {
66-
if (options.parseRequest) {
67-
this.CallData.validate(ValidateType.DEPLOY, 'constructor', param);
68-
return this.CallData.compile('constructor', param);
70+
const constructorCalldata = getCalldata(args, () => {
71+
if (this.contractOptions?.parseRequest) {
72+
this.CallData.validate(ValidateType.DEPLOY, 'constructor', args);
73+
return this.CallData.compile('constructor', args);
6974
}
7075
logger.warn('Call skipped parsing but provided rawArgs, possible malfunction request');
71-
return param;
76+
return args;
7277
});
7378

7479
const {
@@ -79,7 +84,7 @@ export class ContractFactory {
7984
classHash: this.classHash,
8085
compiledClassHash: this.compiledClassHash,
8186
constructorCalldata,
82-
salt: options.addressSalt,
87+
salt: this.contractOptions?.addressSalt,
8388
});
8489
assert(Boolean(contract_address), 'Deployment of the contract failed');
8590

0 commit comments

Comments
 (0)