Skip to content

Commit

Permalink
feat: calldata result in populate
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit f7fd920
Author: Philippe ROSTAN <[email protected]>
Date:   Tue Jul 11 16:35:42 2023 +0200

    feat: meta-class accepts Calldata as input

commit b2e6c29
Author: Philippe ROSTAN <[email protected]>
Date:   Tue Jul 11 11:14:47 2023 +0200

    fix: calldata relocated

commit 045af8e
Author: CI <[email protected]>
Date:   Wed Jul 5 06:52:47 2023 +0000

    docs: generate documentation

commit 5f308d4
Author: Ivan Pavičić <[email protected]>
Date:   Wed Jul 5 08:48:29 2023 +0200

    docs: added waitForTransaction section and cleanups (#675)

    * docs: added waitForTransaction section and cleanups

    * docs: grammarly

    ---------

    Co-authored-by: Toni Tabak <[email protected]>

commit da64857
Author: semantic-release-bot <[email protected]>
Date:   Mon Jul 3 15:08:31 2023 +0000

    chore(release): 5.16.0 [skip ci]

    * cairo1 version2 support ([e564033](e564033))
    * extract parser from CallData and Cairo ([b7eba2a](b7eba2a))
    * parsers ([cce9029](cce9029))

commit ff35d5f
Author: Toni Tabak <[email protected]>
Date:   Mon Jul 3 16:32:11 2023 +0200

    chore: cleanup

commit 92b2be5
Author: Toni Tabak <[email protected]>
Date:   Mon Jul 3 14:07:45 2023 +0200

    chore: compiled files

commit 1f365f8
Author: Toni Tabak <[email protected]>
Date:   Mon Jul 3 14:05:52 2023 +0200

    feat: cairo1 version2 support

commit 5cdfdeb
Author: Toni Tabak <[email protected]>
Date:   Mon Jul 3 11:41:51 2023 +0200

    chore: refact

commit 3af53bd
Author: Toni Tabak <[email protected]>
Date:   Fri Jun 30 16:16:21 2023 +0200

    feat: parsers

commit 6e25aac
Author: Toni Tabak <[email protected]>
Date:   Fri Jun 30 16:13:14 2023 +0200

    feat: extract parser from CallData and Cairo

commit 2cb2848
Author: amanusk <[email protected]>
Date:   Thu Jun 29 21:23:28 2023 +0300

    Add example of new synatx cairo contract

commit 7e6e136
Merge: b2e7a40 e6b861a
Author: Philippe ROSTAN <[email protected]>
Date:   Fri Jun 30 12:36:18 2023 +0200

    Merge pull request #1 from 0xs34n/develop

    merge
  • Loading branch information
PhilippeR26 committed Jul 18, 2023
1 parent e097682 commit f4a7242
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 16 deletions.
17 changes: 17 additions & 0 deletions __tests__/cairo1v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
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
8 changes: 1 addition & 7 deletions src/types/contract.ts
Original file line number Diff line number Diff line change
@@ -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<T = any> = (...args: ArgsOrCalldataWithOptions) => Promise<T>;
export type ContractFunction = (...args: ArgsOrCalldataWithOptions) => any;
Expand All @@ -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 = {
Expand Down
8 changes: 7 additions & 1 deletion src/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down Expand Up @@ -90,7 +96,7 @@ export type DeclareContractTransaction = {

export type CallDetails = {
contractAddress: string;
calldata?: RawArgs;
calldata?: RawArgs | Calldata;
entrypoint?: string; // TODO: check if required
};

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 @@ -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;
}

/**
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 f4a7242

Please sign in to comment.