From f7fd9205a4a3bc7dd270d7e94146f3f70194024b Mon Sep 17 00:00:00 2001
From: Philippe ROSTAN <81040730+PhilippeR26@users.noreply.github.com>
Date: Tue, 11 Jul 2023 16:35:42 +0200
Subject: [PATCH] feat: meta-class accepts Calldata as input
---
__tests__/cairo1v2.test.ts | 19 ++++++++++++++++++-
src/contract/default.ts | 3 +--
src/utils/calldata/index.ts | 11 ++++++++++-
www/docs/guides/define_call_message.md | 4 ++--
www/docs/guides/use_ERC20.md | 6 +++---
5 files changed, 34 insertions(+), 9 deletions(-)
diff --git a/__tests__/cairo1v2.test.ts b/__tests__/cairo1v2.test.ts
index ade604b4d..19c5b0d71 100644
--- a/__tests__/cairo1v2.test.ts
+++ b/__tests__/cairo1v2.test.ts
@@ -141,6 +141,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 () => {
@@ -290,8 +307,8 @@ describe('Cairo 1 Devnet', () => {
[1, 2],
[3, 4],
]);
- const tx1 = await cairo1Contract.array2d_ex(cd);
await account.waitForTransaction(tx.transaction_hash);
+ const tx1 = await cairo1Contract.array2d_ex(cd);
await account.waitForTransaction(tx1.transaction_hash);
const result0 = await cairo1Contract.array2d_felt([
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/utils/calldata/index.ts b/src/utils/calldata/index.ts
index c32337f4e..947c939d4 100644
--- a/src/utils/calldata/index.ts
+++ b/src/utils/calldata/index.ts
@@ -110,11 +110,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...`);