Skip to content

Commit

Permalink
feat: meta-class accepts Calldata as input
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeR26 committed Jul 11, 2023
1 parent b2e6c29 commit f7fd920
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
19 changes: 18 additions & 1 deletion __tests__/cairo1v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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([
Expand Down
3 changes: 1 addition & 2 deletions src/contract/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion src/utils/calldata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions www/docs/guides/define_call_message.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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** | [] <br /> 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 <br /><br />(with 3 params, incl. calldata) | <br /><br /> ✔️ | <br /><br /> ✔️ | | ✔️ <br /><br /><br /> <br /> | | | <br /><br /> ✔️ |
Expand Down
6 changes: 3 additions & 3 deletions www/docs/guides/use_ERC20.md
Original file line number Diff line number Diff line change
Expand Up @@ -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...`);
Expand Down

0 comments on commit f7fd920

Please sign in to comment.