Skip to content

Commit

Permalink
feat: felt252 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Apr 11, 2023
1 parent cf81d2b commit 3d20ea4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
12 changes: 12 additions & 0 deletions __tests__/cairo1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,17 @@ describeIfDevnetSequencer('Cairo 1', () => {

expect(toBigInt(balance[0])).toBe(100n);
});

test('Cairo 1 Contract Interaction - with validation', async () => {
const cairo1Contract = new Contract(compiledHelloSierra.abi, contractAddress, account);

const tx = await cairo1Contract.increase_balance(100);

await account.waitForTransaction(tx.transaction_hash);

const balance = await cairo1Contract.get_balance();

expect(toBigInt(balance[0])).toBe(200n);
});
});
});
12 changes: 11 additions & 1 deletion src/contract/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ function buildEstimate(contract: Contract, functionAbi: FunctionAbi): ContractFu
};
}

const detectCairoVersion = (abi: Abi) => {
if (!abi) return '0';
return abi.find((it) => 'state_mutability' in it) ? '1' : '0';
};

export class Contract implements ContractInterface {
abi: Abi;

Expand All @@ -110,23 +115,28 @@ export class Contract implements ContractInterface {

private callData: CallData;

private version: string = '0';

/**
* Contract class to handle contract methods
*
* @param abi - Abi of the contract object
* @param address (optional) - address to connect to
* @param providerOrAccount (optional) - Provider or Account to attach to
* @param cairoVersion (optional) - default '0', for Cairo 1 set '1'
*/
constructor(
abi: Abi,
address: string,
providerOrAccount: ProviderInterface | AccountInterface = defaultProvider
providerOrAccount: ProviderInterface | AccountInterface = defaultProvider,
cairoVersion: string = detectCairoVersion(abi)
) {
this.address = address && address.toLowerCase();
this.providerOrAccount = providerOrAccount;
this.callData = new CallData(abi);
this.structs = CallData.getAbiStruct(abi);
this.abi = abi;
this.version = cairoVersion;

const options = { enumerable: true, value: {}, writable: false };
Object.defineProperties(this, {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/calldata/cairo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { encodeShortString, isShortString, isText } from '../shortString';
import { UINT_128_MAX, Uint256, isUint256 } from '../uint256';

export const isLen = (name: string) => /_len$/.test(name);
export const isTypeFelt = (type: string) => type === 'felt';
export const isTypeFelt = (type: string) => type === 'felt' || type === 'core::felt252';
export const isTypeFeltArray = (type: string) => type === 'felt*';
export const isTypeArray = (type: string) => /\*/.test(type);
export const isTypeTuple = (type: string) => /^\(.*\)$/i.test(type);
Expand Down
9 changes: 5 additions & 4 deletions src/utils/calldata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ export class CallData {
const { outputs } = this.abi.find((abi) => abi.name === method) as FunctionAbi;
const responseIterator = response.flat()[Symbol.iterator]();

return outputs.flat().reduce((acc, output) => {
acc[output.name] = responseParser(responseIterator, output, this.structs, acc);
if (acc[output.name] && acc[`${output.name}_len`]) {
delete acc[`${output.name}_len`];
return outputs.flat().reduce((acc, output, idx) => {
const propName = output.name ?? idx;
acc[propName] = responseParser(responseIterator, output, this.structs, acc);
if (acc[propName] && acc[`${propName}_len`]) {
delete acc[`${propName}_len`];
}
return acc;
}, {} as Args);
Expand Down

0 comments on commit 3d20ea4

Please sign in to comment.