|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import accountResolver from './accountResolver'; |
| 3 | +import { GS_DEFAULT_TEST_PROVIDER_URL, LOCAL_DEVNET_NOT_RUNNING_MESSAGE } from '../constants'; |
| 4 | +import { setIfNullish } from './env'; |
| 5 | +import { BaseUrl } from '../../../src/constants'; |
| 6 | + |
| 7 | +class StrategyResolver { |
| 8 | + private isDevnet = false; |
| 9 | + |
| 10 | + private isRpcNode = false; |
| 11 | + |
| 12 | + get isRpcDevnet() { |
| 13 | + return this.isDevnet || !!process.env.TEST_RPC_URL; |
| 14 | + } |
| 15 | + |
| 16 | + get isTestnet() { |
| 17 | + return process.env.TEST_RPC_URL?.includes(BaseUrl.SN_SEPOLIA); |
| 18 | + } |
| 19 | + |
| 20 | + get hasAllAccountEnvs() { |
| 21 | + const { TEST_ACCOUNT_ADDRESS, TEST_ACCOUNT_PRIVATE_KEY } = process.env; |
| 22 | + return !!(TEST_ACCOUNT_PRIVATE_KEY && TEST_ACCOUNT_ADDRESS); |
| 23 | + } |
| 24 | + |
| 25 | + private async isRsDevnet(): Promise<boolean> { |
| 26 | + const response = await fetch(GS_DEFAULT_TEST_PROVIDER_URL, { |
| 27 | + method: 'POST', |
| 28 | + headers: { Accept: 'application/json', 'Content-Type': 'application/json' }, |
| 29 | + body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'starknet_syncing' }), |
| 30 | + }); |
| 31 | + const { jsonrpc } = await response.json(); |
| 32 | + return jsonrpc === '2.0'; |
| 33 | + } |
| 34 | + |
| 35 | + async detectDevnet(): Promise<void> { |
| 36 | + // if on base url RPC endpoint work it is devnet-rs else it devnet-py |
| 37 | + try { |
| 38 | + this.isDevnet = await this.isRsDevnet(); |
| 39 | + if (this.isDevnet) console.log('Detected Devnet-RS'); |
| 40 | + } catch (error) { |
| 41 | + console.log('\x1b[36m%s\x1b[0m', LOCAL_DEVNET_NOT_RUNNING_MESSAGE); |
| 42 | + throw new Error( |
| 43 | + 'Local RS devnet is not Running. Please follow the devnet setup instructions.' |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + setIfNullish('IS_DEVNET', this.isRpcDevnet); |
| 48 | + } |
| 49 | + |
| 50 | + resolveRpc(): void { |
| 51 | + const hasRpcUrl = !!process.env.TEST_RPC_URL; |
| 52 | + |
| 53 | + this.isRpcNode = hasRpcUrl || this.isDevnet; |
| 54 | + |
| 55 | + if (!hasRpcUrl && this.isDevnet) { |
| 56 | + process.env.TEST_RPC_URL = GS_DEFAULT_TEST_PROVIDER_URL; |
| 57 | + } |
| 58 | + |
| 59 | + setIfNullish('IS_RPC', this.isRpcNode); |
| 60 | + setIfNullish('IS_TESTNET', this.isTestnet); |
| 61 | + |
| 62 | + console.log('Detected RPC'); |
| 63 | + } |
| 64 | + |
| 65 | + private logConfigInfo(): void { |
| 66 | + console.table({ |
| 67 | + TEST_ACCOUNT_ADDRESS: process.env.TEST_ACCOUNT_ADDRESS, |
| 68 | + TEST_ACCOUNT_PRIVATE_KEY: '****', |
| 69 | + INITIAL_BALANCE: process.env.INITIAL_BALANCE, |
| 70 | + TEST_RPC_URL: process.env.TEST_RPC_URL, |
| 71 | + TX_VERSION: process.env.TX_VERSION === 'v3' ? 'v3' : 'v2', |
| 72 | + }); |
| 73 | + |
| 74 | + console.table({ |
| 75 | + IS_DEVNET: process.env.IS_DEVNET, |
| 76 | + IS_RPC: process.env.IS_RPC, |
| 77 | + IS_TESTNET: process.env.IS_TESTNET, |
| 78 | + }); |
| 79 | + |
| 80 | + console.log('Global Test Environment is Ready'); |
| 81 | + } |
| 82 | + |
| 83 | + private verifyAccountData(shouldThrow?: boolean): void { |
| 84 | + const { TEST_ACCOUNT_ADDRESS, TEST_ACCOUNT_PRIVATE_KEY } = process.env; |
| 85 | + if (!TEST_ACCOUNT_ADDRESS) { |
| 86 | + if (shouldThrow) throw new Error('TEST_ACCOUNT_ADDRESS env is not provided'); |
| 87 | + console.log('\x1b[33m', 'TEST_ACCOUNT_ADDRESS env is not provided!'); |
| 88 | + delete process.env.TEST_ACCOUNT_ADDRESS; |
| 89 | + } |
| 90 | + if (!TEST_ACCOUNT_PRIVATE_KEY) { |
| 91 | + if (shouldThrow) throw new Error('TEST_ACCOUNT_PRIVATE_KEY env is not provided'); |
| 92 | + console.log('\x1b[33m', 'TEST_ACCOUNT_PRIVATE_KEY env is not provided!', '\x1b[0m'); |
| 93 | + delete process.env.TEST_ACCOUNT_PRIVATE_KEY; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private useProvidedSetup(): void { |
| 98 | + setIfNullish('IS_DEVNET', false); |
| 99 | + setIfNullish('IS_RPC', !!process.env.TEST_RPC_URL); |
| 100 | + setIfNullish('IS_TESTNET', this.isTestnet); |
| 101 | + |
| 102 | + this.logConfigInfo(); |
| 103 | + |
| 104 | + console.log('Using Provided Test Setup'); |
| 105 | + } |
| 106 | + |
| 107 | + async execute(): Promise<void> { |
| 108 | + // 1. Assume setup is provided and ready; |
| 109 | + console.log('Global Test Setup Started'); |
| 110 | + this.verifyAccountData(); |
| 111 | + |
| 112 | + if (this.hasAllAccountEnvs) { |
| 113 | + this.useProvidedSetup(); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + // 2. Try to detect devnet setup |
| 118 | + console.log('Basic test parameters are missing, Auto Setup Started'); |
| 119 | + |
| 120 | + await this.detectDevnet(); |
| 121 | + this.resolveRpc(); |
| 122 | + await accountResolver.execute(this.isDevnet); |
| 123 | + |
| 124 | + this.verifyAccountData(true); |
| 125 | + if (!this.hasAllAccountEnvs) console.error('Test Setup Environment is NOT Ready'); |
| 126 | + |
| 127 | + this.logConfigInfo(); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +export default new StrategyResolver(); |
0 commit comments