Skip to content

test: modify estimation of tip for Devnet #1449

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

Merged
merged 1 commit into from
Jul 24, 2025
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
3 changes: 2 additions & 1 deletion __tests__/WebSocketChannel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { Provider, Subscription, WebSocketChannel } from '../src';
import { logger } from '../src/global/logger';
import { StarknetChainId } from '../src/global/constants';
import { getTestAccount, getTestProvider, STRKtokenAddress, TEST_WS_URL } from './config/fixtures';
import { getTestProvider, TEST_WS_URL } from './config/fixtures';
import { getTestAccount, STRKtokenAddress } from './config/fixturesInit';

const describeIfWs = TEST_WS_URL ? describe : describe.skip;
const NODE_URL = TEST_WS_URL!;
Expand Down
20 changes: 14 additions & 6 deletions __tests__/account.outsideExecution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ import {
} from '../src';
import { getSelectorFromName } from '../src/utils/hash';
import { getDecimalString } from '../src/utils/num';
import { contracts, createTestProvider, getTestAccount, STRKtokenAddress } from './config/fixtures';
import { contracts } from './config/fixtures';
import {
adaptAccountIfDevnet,
createTestProvider,
getTestAccount,
STRKtokenAddress,
} from './config/fixturesInit';
import { initializeMatcher } from './config/schema';

describe('Account and OutsideExecution', () => {
Expand Down Expand Up @@ -92,11 +98,13 @@ describe('Account and OutsideExecution', () => {
constructorCalldata: constructorAXCallData,
});
const targetAddress = response.deploy.contract_address;
signerAccount = new Account({
provider,
address: targetAddress,
signer: targetPK,
});
signerAccount = adaptAccountIfDevnet(
new Account({
provider,
address: targetAddress,
signer: targetPK,
})
);

// Transfer dust of STRK token to the signer account
const transferCall = {
Expand Down
3 changes: 2 additions & 1 deletion __tests__/account.starknetId.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Account, Provider, num, shortString } from '../src';
import { contracts, createTestProvider, getTestAccount, STRKtokenAddress } from './config/fixtures';
import { contracts } from './config/fixtures';
import { createTestProvider, getTestAccount, STRKtokenAddress } from './config/fixturesInit';

const { hexToDecimalString } = num;

Expand Down
50 changes: 27 additions & 23 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ import {
type Calldata,
type InvokeTransactionReceiptResponse,
} from '../src';
import { C1v2ClassHash, contracts, describeIfDevnet, erc20ClassHash } from './config/fixtures';
import {
C1v2ClassHash,
TEST_TX_VERSION,
contracts,
createTestProvider,
describeIfDevnet,
devnetFeeTokenAddress,
erc20ClassHash,
getTestAccount,
} from './config/fixtures';
devnetFeeTokenAddress,
adaptAccountIfDevnet,
TEST_TX_VERSION,
} from './config/fixturesInit';
import { initializeMatcher } from './config/schema';

const { cleanHex, hexToDecimalString, toBigInt } = num;
Expand Down Expand Up @@ -124,12 +122,14 @@ describe('deploy and test Account', () => {
await account.waitForTransaction(transaction_hash);

// deploy account
const accountOZ = new Account({
provider,
address: toBeAccountAddress,
signer: privKey,
transactionVersion: TEST_TX_VERSION,
});
const accountOZ = adaptAccountIfDevnet(
new Account({
provider,
address: toBeAccountAddress,
signer: privKey,
transactionVersion: TEST_TX_VERSION,
})
);
const deployed = await accountOZ.deploySelf({
classHash: accountClassHash,
constructorCalldata: calldata,
Expand Down Expand Up @@ -299,11 +299,13 @@ describe('deploy and test Account', () => {
{ publicKey: starkKeyPub },
0
);
const newAccount = new Account({
provider,
address: precalculatedAddress,
signer: privateKey,
});
const newAccount = adaptAccountIfDevnet(
new Account({
provider,
address: precalculatedAddress,
signer: privateKey,
})
);

const res = await newAccount.simulateTransaction([
{
Expand Down Expand Up @@ -549,11 +551,13 @@ describe('deploy and test Account', () => {
{ publicKey: starkKeyPub },
0
);
newAccount = new Account({
provider,
address: precalculatedAddress,
signer: privateKey,
});
newAccount = adaptAccountIfDevnet(
new Account({
provider,
address: precalculatedAddress,
signer: privateKey,
})
);
});

test('estimateAccountDeployFee Cairo 1', async () => {
Expand Down
3 changes: 2 additions & 1 deletion __tests__/cairo1v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import {
selector,
shortString,
} from '../src';
import { contracts, createTestProvider, getTestAccount } from './config/fixtures';
import { contracts } from './config/fixtures';
import { initializeMatcher } from './config/schema';
import { createTestProvider, getTestAccount } from './config/fixturesInit';

const { uint256, tuple, isCairo1Abi } = cairo;
const { toHex } = num;
Expand Down
19 changes: 11 additions & 8 deletions __tests__/cairo1v2_typed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ import {
import { hexToDecimalString } from '../src/utils/num';
import { encodeShortString } from '../src/utils/shortString';
import { isString } from '../src/utils/typed';
import { contracts } from './config/fixtures';
import {
contracts,
createTestProvider,
getTestAccount,
STRKtokenAddress,
adaptAccountIfDevnet,
TEST_TX_VERSION,
} from './config/fixtures';
} from './config/fixturesInit';
import { initializeMatcher } from './config/schema';

const { uint256, tuple, isCairo1Abi } = cairo;
Expand Down Expand Up @@ -743,12 +744,14 @@ describe('Cairo 1', () => {
await account.waitForTransaction(transaction_hash);

// deploy account
accountC1 = new Account({
provider,
address: toBeAccountAddress,
signer: priKey,
transactionVersion: TEST_TX_VERSION,
});
accountC1 = adaptAccountIfDevnet(
new Account({
provider,
address: toBeAccountAddress,
signer: priKey,
transactionVersion: TEST_TX_VERSION,
})
);
const deployed = await accountC1.deploySelf({
classHash: accountClassHash,
constructorCalldata: calldata,
Expand Down
3 changes: 2 additions & 1 deletion __tests__/cairov24onward.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
} from '../src';
import { hexToDecimalString } from '../src/utils/num';
import { encodeShortString } from '../src/utils/shortString';
import { contracts, createTestProvider, getTestAccount } from './config/fixtures';
import { contracts } from './config/fixtures';
import { createTestProvider, getTestAccount } from './config/fixturesInit';

describe('Cairo v2.4 onwards', () => {
let provider: ProviderInterface;
Expand Down
46 changes: 2 additions & 44 deletions __tests__/config/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import fs from 'node:fs';
import path from 'node:path';

import { Account, Provider, ProviderInterface, RpcProvider, config, hash, json } from '../../src';
import { Provider, ProviderInterface, RpcProvider, config, hash, json } from '../../src';
import {
CompiledSierra,
CompiledSierraCasm,
LegacyCompiledContract,
RpcProviderOptions,
} from '../../src/types';
import { toHex } from '../../src/utils/num';
import { wait } from '../../src/utils/provider';
import { isString } from '../../src/utils/typed';
import './customMatchers'; // ensures TS traversal
import { SupportedRpcVersion, SupportedTransactionVersion } from '../../src/global/constants';
import { SupportedRpcVersion } from '../../src/global/constants';

const readFile = (subpath: string) => fs.readFileSync(path.resolve(__dirname, subpath));

Expand Down Expand Up @@ -101,45 +100,8 @@ export function getTestProvider(
return isProvider ? new Provider(providerOptions) : new RpcProvider(providerOptions);
}

export async function createTestProvider(
isProvider?: true,
setProviderOptions?: RpcProviderOptions
): Promise<ProviderInterface>;
export async function createTestProvider(
isProvider?: false,
setProviderOptions?: RpcProviderOptions
): Promise<RpcProvider>;
export async function createTestProvider(
isProvider: boolean = true,
setProviderOptions?: RpcProviderOptions
): Promise<ProviderInterface | RpcProvider> {
const isDevnet = process.env.IS_DEVNET === 'true';

const providerOptions: RpcProviderOptions = {
...setProviderOptions,
nodeUrl: process.env.TEST_RPC_URL,
specVersion: process.env.RPC_SPEC_VERSION as SupportedRpcVersion,
// accelerate the tests when running locally
...(isDevnet && { transactionRetryIntervalFallback: 1000 }),
};
return isProvider ? Provider.create(providerOptions) : RpcProvider.create(providerOptions);
}

export const TEST_TX_VERSION = process.env.TX_VERSION as SupportedTransactionVersion;
export const { TEST_WS_URL } = process.env;

export const getTestAccount = (
provider: ProviderInterface,
txVersion?: SupportedTransactionVersion
) => {
return new Account({
provider,
address: toHex(process.env.TEST_ACCOUNT_ADDRESS || ''),
signer: process.env.TEST_ACCOUNT_PRIVATE_KEY || '',
transactionVersion: txVersion ?? TEST_TX_VERSION,
});
};

export const createBlockForDevnet = async (): Promise<void> => {
if (!(process.env.IS_DEVNET === 'true')) return;
const response = await fetch(new URL('/create_block', process.env.TEST_RPC_URL), {
Expand Down Expand Up @@ -180,7 +142,3 @@ export const describeIfRpc09 = describeIf(process.env.RPC_SPEC_VERSION === '0.9.
export const erc20ClassHash: string = hash.computeContractClassHash(contracts.Erc20OZ.sierra); // Cairo 1
export const C1v2ClassHash: string = hash.computeContractClassHash(contracts.C1v2.sierra); // Cairo 1
export const wrongClassHash = '0x000000000000000000000000000000000000000000000000000000000000000';
export const ETHtokenAddress = '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7';
export const STRKtokenAddress =
'0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d';
export const devnetFeeTokenAddress = TEST_TX_VERSION === '0x3' ? STRKtokenAddress : ETHtokenAddress;
76 changes: 76 additions & 0 deletions __tests__/config/fixturesInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {
Account,
Provider,
ProviderInterface,
RpcProvider,
config,
getTipStatsFromBlocks,
type TipAnalysisOptions,
} from '../../src';
import { RpcProviderOptions, type BlockIdentifier } from '../../src/types';
import { toHex } from '../../src/utils/num';
import './customMatchers'; // ensures TS traversal
import { SupportedRpcVersion, SupportedTransactionVersion } from '../../src/global/constants';

config.set('logLevel', 'ERROR');

export async function createTestProvider(
isProvider?: true,
setProviderOptions?: RpcProviderOptions
): Promise<ProviderInterface>;
export async function createTestProvider(
isProvider?: false,
setProviderOptions?: RpcProviderOptions
): Promise<RpcProvider>;
export async function createTestProvider(
isProvider: boolean = true,
setProviderOptions?: RpcProviderOptions
): Promise<ProviderInterface | RpcProvider> {
const isDevnet = process.env.IS_DEVNET === 'true';
const providerOptions: RpcProviderOptions = {
...setProviderOptions,
nodeUrl: process.env.TEST_RPC_URL,
specVersion: process.env.RPC_SPEC_VERSION as SupportedRpcVersion,
// accelerate the tests when running locally
...(isDevnet && { transactionRetryIntervalFallback: 1000 }),
};
return isProvider ? Provider.create(providerOptions) : RpcProvider.create(providerOptions);
}

export const TEST_TX_VERSION = process.env.TX_VERSION as SupportedTransactionVersion;
export function adaptAccountIfDevnet(account: Account): Account {
const isDevnet = process.env.IS_DEVNET === 'true';
if (isDevnet) {
// eslint-disable-next-line no-param-reassign
account.getEstimateTip = function async(
blockIdentifier?: BlockIdentifier,
options: TipAnalysisOptions = {}
) {
return getTipStatsFromBlocks(this, blockIdentifier, {
...options,
minTxsNecessary: options.minTxsNecessary ?? 3,
maxBlocks: options.maxBlocks ?? 10,
});
};
}
return account;
}

export const getTestAccount = (
provider: ProviderInterface,
txVersion?: SupportedTransactionVersion
) => {
return adaptAccountIfDevnet(
new Account({
provider,
address: toHex(process.env.TEST_ACCOUNT_ADDRESS || ''),
signer: process.env.TEST_ACCOUNT_PRIVATE_KEY || '',
transactionVersion: txVersion ?? TEST_TX_VERSION,
})
);
};

export const ETHtokenAddress = '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7';
export const STRKtokenAddress =
'0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d';
export const devnetFeeTokenAddress = TEST_TX_VERSION === '0x3' ? STRKtokenAddress : ETHtokenAddress;
35 changes: 35 additions & 0 deletions __tests__/config/helpers/initDevnetHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable no-await-in-loop */
import { cairo } from '../../../src';
import { createTestProvider, devnetFeeTokenAddress, getTestAccount } from '../fixturesInit';

/** Create in Devnet 3 transactions to initiate tip history */
export async function InitDevnetHistory() {
// It has been checked previously that we are using Devnet
const provider = await createTestProvider();
const account = getTestAccount(provider);
const nbBlocks = await provider.getBlockNumber();
if (nbBlocks < 3) {
// eslint-disable-next-line no-console
console.log('Init Devnet...');
// eslint-disable-next-line no-plusplus
for (let i = 1n; i <= 3n; i++) {
try {
const { transaction_hash } = await account.execute(
{
contractAddress: devnetFeeTokenAddress,
entrypoint: 'transfer',
calldata: {
recipient: account.address,
amount: cairo.uint256(1n * 10n ** 3n),
},
},
{ tip: i * 10n ** 6n }
);
await account.waitForTransaction(transaction_hash);
} catch (error: any) {
// eslint-disable-next-line no-console
console.error('Error in Devnet initialization.', error);
}
}
}
}
Loading
Loading