Skip to content

Commit

Permalink
fix: errors when testing in Testnet with a Cairo 1 account
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeR26 committed Nov 15, 2023
1 parent cbebfce commit 243074e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 64 deletions.
134 changes: 74 additions & 60 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
compiledOpenZeppelinAccount,
compiledStarknetId,
compiledTestDapp,
describeIfDevnet,
describeIfDevnetSequencer,
erc20ClassHash,
getTestAccount,
Expand Down Expand Up @@ -130,7 +131,7 @@ describe('deploy and test Wallet', () => {
entrypoint: 'transfer',
calldata: {
recipient: tobeAccountAddress,
amount: uint256(1_000_000_000_000_000),
amount: uint256(5 * 10 ** 15),
},
});
await account.waitForTransaction(transaction_hash);
Expand Down Expand Up @@ -228,15 +229,19 @@ describe('deploy and test Wallet', () => {
]);
expect(res).toMatchSchemaRef('SimulateTransactionResponse');
});
test('simulate DECLARE - Cairo 0 Contract', async () => {
const res = await account.simulateTransaction([
{
type: TransactionType.DECLARE,
contract: compiledErc20,
},
]);
expect(res).toMatchSchemaRef('SimulateTransactionResponse');

describeIfDevnet('declare tests only on devnet', () => {
test('simulate DECLARE - Cairo 0 Contract', async () => {
const res = await account.simulateTransaction([
{
type: TransactionType.DECLARE,
contract: compiledErc20,
},
]);
expect(res).toMatchSchemaRef('SimulateTransactionResponse');
});
});

test('simulate DECLARE - Cairo 1 Contract - test if not already declared', async () => {
const declareContractPayload = extractContractHashes({
contract: compiledHelloSierra,
Expand Down Expand Up @@ -297,11 +302,13 @@ describe('deploy and test Wallet', () => {
expect(res).toMatchSchemaRef('SimulateTransactionResponse');
});
test('simulate DEPLOY_ACCOUNT - Cairo 0 Account', async () => {
const declareAccount = await account.declare({
const declareAccount = await account.declareIfNot({
contract: compiledOpenZeppelinAccount,
});
const accountClassHash = declareAccount.class_hash;
await provider.waitForTransaction(declareAccount.transaction_hash);
if (declareAccount.transaction_hash) {
await provider.waitForTransaction(declareAccount.transaction_hash);
}
const privateKey = stark.randomAddress();
const starkKeyPub = ec.starkCurve.getStarkKey(privateKey);
const precalculatedAddress = hash.calculateContractAddressFromHash(
Expand Down Expand Up @@ -427,11 +434,13 @@ describe('deploy and test Wallet', () => {
});

test('Declare ERC20 contract', async () => {
const declareTx = await account.declare({
const declareTx = await account.declareIfNot({
contract: compiledErc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
});
await provider.waitForTransaction(declareTx.transaction_hash);
if (declareTx.transaction_hash) {
await provider.waitForTransaction(declareTx.transaction_hash);
}
expect(declareTx).toMatchSchemaRef('DeclareContractResponse');
});

Expand Down Expand Up @@ -510,11 +519,13 @@ describe('deploy and test Wallet', () => {

describe('Declare and UDC Deploy Flow', () => {
test('ERC20 Declare', async () => {
const declareTx = await account.declare({
const declareTx = await account.declareIfNot({
contract: compiledErc20,
});

await provider.waitForTransaction(declareTx.transaction_hash);
if (declareTx.transaction_hash) {
await provider.waitForTransaction(declareTx.transaction_hash);
}
expect(declareTx).toMatchSchemaRef('DeclareContractResponse');
expect(hexToDecimalString(declareTx.class_hash)).toEqual(hexToDecimalString(erc20ClassHash));
});
Expand Down Expand Up @@ -600,12 +611,13 @@ describe('deploy and test Wallet', () => {
let newAccount: Account;

beforeAll(async () => {
const declareAccount = await account.declare({
const declareAccount = await account.declareIfNot({
contract: compiledOpenZeppelinAccount,
});
accountClassHash = declareAccount.class_hash;
await provider.waitForTransaction(declareAccount.transaction_hash);

if (declareAccount.transaction_hash) {
await provider.waitForTransaction(declareAccount.transaction_hash);
}
const privateKey = stark.randomAddress();
starkKeyPub = ec.starkCurve.getStarkKey(privateKey);
precalculatedAddress = hash.calculateContractAddressFromHash(
Expand Down Expand Up @@ -705,51 +717,53 @@ describe('deploy and test Wallet', () => {
});
});

test('declare, deploy & multi invoke functions', async () => {
const res = await account.estimateFeeBulk([
/* {
// Cairo 1.1.0, if declared estimate error with can't redeclare same contract
type: TransactionType.DECLARE,
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
}, */
{
// Cairo 0
type: TransactionType.DECLARE,
payload: {
contract: compiledErc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
describeIfDevnet('declare tests only on devnet', () => {
test('declare, deploy & multi invoke functions', async () => {
const res = await account.estimateFeeBulk([
/* {
// Cairo 1.1.0, if declared estimate error with can't redeclare same contract
type: TransactionType.DECLARE,
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
}, */
{
// Cairo 0
type: TransactionType.DECLARE,
payload: {
contract: compiledErc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
},
},
},
{
type: TransactionType.DEPLOY,
payload: {
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
constructorCalldata: ['Token', 'ERC20', account.address],
{
type: TransactionType.DEPLOY,
payload: {
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
constructorCalldata: ['Token', 'ERC20', account.address],
},
},
},
{
type: TransactionType.INVOKE,
payload: [
{
contractAddress: erc20Address,
entrypoint: 'approve',
calldata: {
address: erc20Address,
amount: uint256(10),
{
type: TransactionType.INVOKE,
payload: [
{
contractAddress: erc20Address,
entrypoint: 'approve',
calldata: {
address: erc20Address,
amount: uint256(10),
},
},
},
{
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [erc20.address, '10', '0'],
},
],
},
]);
expect(res).toHaveLength(3);
res.forEach((value) => {
expect(value).toMatchSchemaRef('EstimateFee');
{
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [erc20.address, '10', '0'],
},
],
},
]);
expect(res).toHaveLength(3);
res.forEach((value) => {
expect(value).toMatchSchemaRef('EstimateFee');
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion __tests__/cairo1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ describeIfDevnet('Cairo 1 Devnet', () => {
entrypoint: 'transfer',
calldata: {
recipient: toBeAccountAddress,
amount: uint256(1_000_000_000_000_000),
amount: uint256(5 * 10 ** 15),
},
});
await account.waitForTransaction(transaction_hash);
Expand Down
2 changes: 1 addition & 1 deletion __tests__/cairo1v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ describe('Cairo 1', () => {
entrypoint: 'transfer',
calldata: {
recipient: toBeAccountAddress,
amount: uint256(1_000_000_000_000_000),
amount: uint256(5 * 10 ** 15),
},
});
await account.waitForTransaction(transaction_hash);
Expand Down
15 changes: 13 additions & 2 deletions src/utils/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { BigNumberish, CairoVersion, Call, CallStruct, Calldata, ParsedStruct } from '../types';
import {
BigNumberish,
CairoVersion,
Call,
CallStruct,
Calldata,
ParsedStruct,
RawArgs,
} from '../types';
import { CallData } from './calldata';
import { getSelectorFromName } from './hash';
import { toBigInt } from './num';
Expand Down Expand Up @@ -66,7 +74,10 @@ export const fromCallsToExecuteCalldata_cairo1 = (calls: Call[]) => {
const orderCalls = calls.map((call) => ({
contractAddress: call.contractAddress,
entrypoint: call.entrypoint,
calldata: call.calldata,
calldata:
Array.isArray(call.calldata) && '__compiled' in call.calldata
? call.calldata // Calldata type
: CallData.compile(call.calldata as RawArgs), // RawArgsObject | RawArgsArray type
}));

return CallData.compile({ orderCalls });
Expand Down

0 comments on commit 243074e

Please sign in to comment.