Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: reorganize compiled contract handling #1240

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
31 changes: 13 additions & 18 deletions __tests__/account.outsideExecution.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
// We test here the most common case: an account compatible with ERC-165 and SNIP-9 (v2).
// To limit test duration, these cases are not tested: non ERC165 account, non SNIP-9 account, SNIP9-v1 account.
import {
Provider,
Account,
cairo,
ec,
stark,
CairoCustomEnum,
CairoOption,
CairoOptionVariant,
CallData,
OutsideExecutionVersion,
type OutsideExecutionOptions,
type OutsideTransaction,
constants,
type Call,
Contract,
ec,
outsideExecution,
type TypedData,
type Calldata,
OutsideExecutionVersion,
Provider,
src5,
stark,
type Call,
type Calldata,
type OutsideExecutionOptions,
type OutsideTransaction,
type TypedData,
} from '../src';
import { getSelectorFromName } from '../src/utils/hash';
import { getDecimalString } from '../src/utils/num';
import {
compiledArgentX4Account,
compiledErc20OZ,
getTestAccount,
getTestProvider,
} from './config/fixtures';
import { contracts, getTestAccount, getTestProvider } from './config/fixtures';

describe('Account and OutsideExecution', () => {
const ethAddress = '0x49D36570D4E46F48E99674BD3FCC84644DDD6B96F7C741B1562B82F9E004DC7';
Expand All @@ -39,19 +34,19 @@ describe('Account and OutsideExecution', () => {
const targetPubK = ec.starkCurve.getStarkKey(targetPK);
// For ERC20 transfer outside call
const recipientAccount = executorAccount;
const ethContract = new Contract(compiledErc20OZ.abi, ethAddress, provider);
const ethContract = new Contract(contracts.Erc20OZ.sierra.abi, ethAddress, provider);

beforeAll(async () => {
// Deploy the SNIP-9 signer account (ArgentX v 0.4.0, using SNIP-9 v2):
const calldataAX = new CallData(compiledArgentX4Account.abi);
const calldataAX = new CallData(contracts.ArgentX4Account.sierra.abi);
const axSigner = new CairoCustomEnum({ Starknet: { pubkey: targetPubK } });
const axGuardian = new CairoOption<unknown>(CairoOptionVariant.None);
const constructorAXCallData = calldataAX.compile('constructor', {
owner: axSigner,
guardian: axGuardian,
});
const response = await executorAccount.declareAndDeploy({
contract: compiledArgentX4Account,
contract: contracts.ArgentX4Account.sierra,
classHash: '0x36078334509b514626504edc9fb252328d1a240e4e948bef8d0c08dff45927f',
compiledClassHash: '0x7a663375245780bd307f56fde688e33e5c260ab02b76741a57711c5b60d47f6',
constructorCalldata: constructorAXCallData,
Expand Down
29 changes: 9 additions & 20 deletions __tests__/account.starknetId.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { Provider, num, shortString } from '../src';
import {
compiledNaming,
compiledNamingCasm,
compiledPricing,
compiledPricingCasm,
compiledSidMulticall,
compiledSidMulticallCasm,
compiledStarknetId,
compiledStarknetIdCasm,
getTestAccount,
getTestProvider,
} from './config/fixtures';
import { contracts, getTestAccount, getTestProvider } from './config/fixtures';

const { hexToDecimalString } = num;

Expand All @@ -25,32 +14,32 @@ describe('deploy and test Wallet', () => {
beforeAll(async () => {
// Deploy Starknet id contract
const idResponse = await account.declareAndDeploy({
contract: compiledStarknetId,
casm: compiledStarknetIdCasm,
contract: contracts.starknetId.StarknetId.sierra,
casm: contracts.starknetId.StarknetId.casm,
constructorCalldata: [account.address, 0],
});
identityAddress = idResponse.deploy.contract_address;

// Deploy pricing contract
const pricingResponse = await account.declareAndDeploy({
contract: compiledPricing,
casm: compiledPricingCasm,
contract: contracts.starknetId.Pricing.sierra,
casm: contracts.starknetId.Pricing.casm,
constructorCalldata: [devnetERC20Address],
});
const pricingAddress = pricingResponse.deploy.contract_address;

// Deploy naming contract
const namingResponse = await account.declareAndDeploy({
contract: compiledNaming,
casm: compiledNamingCasm,
contract: contracts.starknetId.Naming.sierra,
casm: contracts.starknetId.Naming.casm,
constructorCalldata: [identityAddress, pricingAddress, 0, account.address],
});
namingAddress = namingResponse.deploy.contract_address;

// Deploy multicall contract
const multicallResponse = await account.declareAndDeploy({
contract: compiledSidMulticall,
casm: compiledSidMulticallCasm,
contract: contracts.starknetId.SidMulticall.sierra,
casm: contracts.starknetId.SidMulticall.casm,
});
multicallAddress = multicallResponse.deploy.contract_address;

Expand Down
62 changes: 29 additions & 33 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ import {
} from '../src';
import {
TEST_TX_VERSION,
compiledErc20,
compiledHelloSierra,
compiledHelloSierraCasm,
compiledOpenZeppelinAccount,
compiledTestDapp,
contracts,
describeIfDevnet,
erc20ClassHash,
getTestAccount,
Expand All @@ -51,7 +47,7 @@ describe('deploy and test Wallet', () => {
expect(account).toBeInstanceOf(Account);

dd = await account.declareAndDeploy({
contract: compiledErc20,
contract: contracts.Erc20,
constructorCalldata: [
encodeShortString('Token'),
encodeShortString('ERC20'),
Expand All @@ -60,17 +56,17 @@ describe('deploy and test Wallet', () => {
});

erc20Address = dd.deploy.contract_address;
erc20 = new Contract(compiledErc20.abi, erc20Address, provider);
erc20 = new Contract(contracts.Erc20.abi, erc20Address, provider);

const { balance } = await erc20.balanceOf(account.address);
expect(BigInt(balance.low).toString()).toStrictEqual(BigInt(1000).toString());

const dappResponse = await account.declareAndDeploy({
contract: compiledTestDapp,
contract: contracts.TestDapp,
classHash: '0x04367b26fbb92235e8d1137d19c080e6e650a6889ded726d00658411cc1046f5',
});

dapp = new Contract(compiledTestDapp.abi, dappResponse.deploy.contract_address!, provider);
dapp = new Contract(contracts.TestDapp.abi, dappResponse.deploy.contract_address!, provider);
});

xtest('validate TS for redeclare - skip testing', async () => {
Expand Down Expand Up @@ -121,7 +117,7 @@ describe('deploy and test Wallet', () => {

// declare account
const declareAccount = await account.declareIfNot({
contract: compiledOpenZeppelinAccount,
contract: contracts.OpenZeppelinAccount,
});
const accountClassHash = declareAccount.class_hash;

Expand Down Expand Up @@ -250,7 +246,7 @@ describe('deploy and test Wallet', () => {
const invocation = await provider.prepareInvocations([
{
type: TransactionType.DECLARE,
contract: compiledErc20,
contract: contracts.Erc20,
},
]);
if (invocation.length) {
Expand All @@ -264,8 +260,8 @@ describe('deploy and test Wallet', () => {
const invocation = await provider.prepareInvocations([
{
type: TransactionType.DECLARE,
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
contract: contracts.HelloSierra.sierra,
casm: contracts.HelloSierra.casm,
},
]);

Expand Down Expand Up @@ -311,7 +307,7 @@ describe('deploy and test Wallet', () => {
});
test('simulate DEPLOY_ACCOUNT - Cairo 0 Account', async () => {
const declareAccount = await account.declareIfNot({
contract: compiledOpenZeppelinAccount,
contract: contracts.OpenZeppelinAccount,
});
const accountClassHash = declareAccount.class_hash;
if (declareAccount.transaction_hash) {
Expand Down Expand Up @@ -480,7 +476,7 @@ describe('deploy and test Wallet', () => {

test('Declare ERC20 contract', async () => {
const declareTx = await account.declareIfNot({
contract: compiledErc20,
contract: contracts.Erc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
});
if (declareTx.transaction_hash) {
Expand All @@ -493,7 +489,7 @@ describe('deploy and test Wallet', () => {
describe('Declare and UDC Deploy Flow', () => {
test('ERC20 Declare', async () => {
const declareTx = await account.declareIfNot({
contract: compiledErc20,
contract: contracts.Erc20,
});

if (declareTx.transaction_hash) {
Expand Down Expand Up @@ -585,7 +581,7 @@ describe('deploy and test Wallet', () => {

beforeAll(async () => {
const declareAccount = await account.declareIfNot({
contract: compiledOpenZeppelinAccount,
contract: contracts.OpenZeppelinAccount,
});
accountClassHash = declareAccount.class_hash;
if (declareAccount.transaction_hash) {
Expand Down Expand Up @@ -705,8 +701,8 @@ describe('deploy and test Wallet', () => {
});

const hashes = extractContractHashes({
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
contract: contracts.HelloSierra.sierra,
casm: contracts.HelloSierra.casm,
});

const isDeclaredCairo1 = await account.isClassDeclared({ classHash: hashes.classHash });
Expand Down Expand Up @@ -743,7 +739,7 @@ describe('deploy and test Wallet', () => {
// Cairo 0
type: TransactionType.DECLARE,
payload: {
contract: compiledErc20,
contract: contracts.Erc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
},
},
Expand All @@ -754,8 +750,8 @@ describe('deploy and test Wallet', () => {
{
// Cairo 1.1.0, if declared estimate error with can't redeclare same contract
type: TransactionType.DECLARE,
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
contract: contracts.HelloSierra.sierra,
casm: contracts.HelloSierra.casm,
},
]
: []),
Expand Down Expand Up @@ -798,15 +794,15 @@ describe('deploy and test Wallet', () => {
// Cairo 0
type: TransactionType.DECLARE,
payload: {
contract: compiledErc20,
contract: contracts.Erc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
},
},
{
// Cairo 1.1.0, if declared estimate error with can't redeclare same contract
type: TransactionType.DECLARE,
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
contract: contracts.HelloSierra.sierra,
casm: contracts.HelloSierra.casm,
},
]);

Expand All @@ -823,8 +819,8 @@ describe('deploy and test Wallet', () => {
// TODO @dhruvkelawala check expectation for feeTransactionVersion
// Cairo 1 contract
const ddc1: DeclareDeployUDCResponse = await account.declareAndDeploy({
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
contract: contracts.HelloSierra.sierra,
casm: contracts.HelloSierra.casm,
});

// const innerInvokeEstFeeSpy = jest.spyOn(account.signer, 'signTransaction');
Expand All @@ -849,21 +845,21 @@ describe('unit', () => {

test('declareIfNot', async () => {
const declare = await account.declareIfNot({
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
contract: contracts.HelloSierra.sierra,
casm: contracts.HelloSierra.casm,
});
expect(declare).toMatchSchemaRef('DeclareContractResponse');

await expect(
account.declare({
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
contract: contracts.HelloSierra.sierra,
casm: contracts.HelloSierra.casm,
})
).rejects.toThrow();

const redeclare = await account.declareIfNot({
contract: compiledHelloSierra,
casm: compiledHelloSierraCasm,
contract: contracts.HelloSierra.sierra,
casm: contracts.HelloSierra.casm,
});
expect(redeclare.class_hash).toBe(declare.class_hash);
});
Expand Down
Loading