diff --git a/__tests__/cairo1v2.test.ts b/__tests__/cairo1v2.test.ts index 9e8a30cd3..d1e55a6e0 100644 --- a/__tests__/cairo1v2.test.ts +++ b/__tests__/cairo1v2.test.ts @@ -143,6 +143,23 @@ describe('Cairo 1 Devnet', () => { // defined as struct const result1 = await cairo1Contract.test_u256(uint256(2n ** 256n - 2n)); expect(result1).toBe(2n ** 256n - 1n); + + // using Contract.populate result in meta-class + const functionParameters: RawArgsObject = { p1: cairo.uint256(15) }; + const myCall0 = cairo1Contract.populate('test_u256', functionParameters); + const res0 = await cairo1Contract.test_u256(myCall0.calldata); + expect(res0).toBe(16n); + + // using myCallData.compile result in meta-class + const contractCallData: CallData = new CallData(cairo1Contract.abi); + const myCalldata: Calldata = contractCallData.compile('test_u256', functionParameters); + const res1 = await cairo1Contract.test_u256(myCalldata); + expect(res1).toBe(16n); + + // using CallData.compile result in meta-class + const contractCallData2: Calldata = CallData.compile(functionParameters); + const res2 = await cairo1Contract.test_u256(contractCallData2); + expect(res2).toBe(16n); }); test('Cairo 1 Contract Interaction - bool', async () => { diff --git a/src/contract/default.ts b/src/contract/default.ts index 07f31a16e..27a6342ec 100644 --- a/src/contract/default.ts +++ b/src/contract/default.ts @@ -315,8 +315,7 @@ export class Contract implements ContractInterface { } public populate(method: string, args: RawArgs = []): Call { - const calldata = getCalldata(args, () => this.callData.compile(method, args)); - + const calldata: Calldata = getCalldata(args, () => this.callData.compile(method, args)); return { contractAddress: this.address, entrypoint: method, diff --git a/src/types/contract.ts b/src/types/contract.ts index 914d76e01..603b70310 100644 --- a/src/types/contract.ts +++ b/src/types/contract.ts @@ -1,5 +1,5 @@ import { CairoEnum } from './cairoEnum'; -import { BigNumberish, BlockIdentifier, RawArgsArray, Signature } from './lib'; +import { BigNumberish, BlockIdentifier, Calldata, RawArgsArray, Signature } from './lib'; export type AsyncContractFunction = (...args: ArgsOrCalldataWithOptions) => Promise; export type ContractFunction = (...args: ArgsOrCalldataWithOptions) => any; @@ -14,12 +14,6 @@ export type Result = | boolean | CairoEnum; -/** - * Compiled calldata ready to be sent - * decimal-string array - */ -export type Calldata = string[] & { readonly __compiled__?: boolean }; - export type ArgsOrCalldata = RawArgsArray | [Calldata] | Calldata; export type ArgsOrCalldataWithOptions = ArgsOrCalldata & ContractOptions; export type ContractOptions = { diff --git a/src/types/lib/index.ts b/src/types/lib/index.ts index dcef835f9..930affd46 100644 --- a/src/types/lib/index.ts +++ b/src/types/lib/index.ts @@ -8,6 +8,12 @@ export type Signature = ArraySignatureType | WeierstrassSignatureType; export type BigNumberish = string | number | bigint; +/** + * Compiled calldata ready to be sent + * decimal-string array + */ +export type Calldata = string[] & { readonly __compiled__?: boolean }; + /** * Represents an integer in the range [0, 2^256) */ @@ -90,7 +96,7 @@ export type DeclareContractTransaction = { export type CallDetails = { contractAddress: string; - calldata?: RawArgs; + calldata?: RawArgs | Calldata; entrypoint?: string; // TODO: check if required }; diff --git a/src/utils/calldata/index.ts b/src/utils/calldata/index.ts index 8bbf8241f..6c3674ea6 100644 --- a/src/utils/calldata/index.ts +++ b/src/utils/calldata/index.ts @@ -122,11 +122,20 @@ export class CallData { } const argsIterator = args[Symbol.iterator](); - return abiMethod.inputs.reduce( + + const callArray = abiMethod.inputs.reduce( (acc, input) => isLen(input.name) ? acc : acc.concat(parseCalldataField(argsIterator, input, this.structs)), [] as Calldata ); + + // add compiled property to array object + Object.defineProperty(callArray, '__compiled__', { + enumerable: false, + writable: false, + value: true, + }); + return callArray; } /** diff --git a/www/docs/guides/define_call_message.md b/www/docs/guides/define_call_message.md index efc6a876e..c7d149a96 100644 --- a/www/docs/guides/define_call_message.md +++ b/www/docs/guides/define_call_message.md @@ -318,7 +318,7 @@ const myCall: Call = myContract.populate("setup_elements", functionParameters); const tx = await account0.execute(myCall); // or const myCall: Call = myContract.populate("get_elements", functionParameters); -const res = await myContract.get_elements(...myCall.calldata); +const res = await myContract.get_elements(myCall.calldata); ``` It can be used only with methods that know the abi: `Contract.populate, myCallData.compile`. @@ -392,7 +392,7 @@ These types of arguments can't be used at your convenience everywhere. Here is a | Function | array of parameters | ordered object | non ordered object | Call & MultiCall | list of parameters | array of strings (\*) | array of strings (\*\*) | | ----------------------------------------------------------: | :-----------------: | :-------------: | :----------------: | :--------------------------: | :----------------: | :-------------------: | :---------------------: | -| **Typescript type** | []
Calldata | {} RawArgsArray | {} RawArgsObject | Call & Call[] | ...Calldata | string[] | string[] | +| **Typescript type** | N/A | {} RawArgsArray | {} RawArgsObject | Call & Call[] | ...[] | string[] | string[] | | contract.metaClass() contract\[metaclass]() | | | | | ✔️ | ✔️ | ✔️ | | contract.call / contract.invoke | ✔️ | | | | | ✔️ | ✔️ | | account.execute

(with 3 params, incl. calldata) |

✔️ |

✔️ | | ✔️



| | |

✔️ | diff --git a/www/docs/guides/use_ERC20.md b/www/docs/guides/use_ERC20.md index ca39177d7..0aff5a4b4 100644 --- a/www/docs/guides/use_ERC20.md +++ b/www/docs/guides/use_ERC20.md @@ -115,10 +115,10 @@ console.log("account0 has a balance of:", uint256.uint256ToBN(balanceBeforeTrans console.log(`Invoke Tx - Transfer 10 tokens back to erc20 contract...`); const toTransferTk: Uint256 = cairo.uint256(10); const transferCallData: Call = erc20.populate("transfer", { - recipient: erc20Address, - amount: toTransferTk // with Cairo 1 contract, 'toTransferTk' can be replaced by '10n' + recipient: erc20Address, + amount: toTransferTk // with Cairo 1 contract, 'toTransferTk' can be replaced by '10n' }); - const { transaction_hash: transferTxHash } = await erc20.transfer( ...transferCallData.calldata); + const { transaction_hash: transferTxHash } = await erc20.transfer( transferCallData.calldata); // Wait for the invoke transaction to be accepted on Starknet console.log(`Waiting for Tx to be Accepted on Starknet - Transfer...`);