-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(RREL-17): use rpc accounts for dev purposes only (#117)
* fix(RREL-17): use rpc accounts for dev purposes only * test: enable all tests * test: fix test descriptions
- Loading branch information
Showing
5 changed files
with
138 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { JsonRpcProvider } from '@ethersproject/providers'; | ||
import { BigNumber, Signer, utils } from 'ethers'; | ||
import log from 'loglevel'; | ||
|
||
export const REGTEST_CHAIN_ID = 33; | ||
|
||
export const findWealthyAccount = async ( | ||
rpcProvider: JsonRpcProvider, | ||
requiredBalance: BigNumber = utils.parseUnits('2', 'ether') | ||
): Promise<Signer> => { | ||
const { chainId } = await rpcProvider.getNetwork(); | ||
if (chainId !== REGTEST_CHAIN_ID) { | ||
throw new Error('Unlocked accounts are allowed for testing purposes only'); | ||
} | ||
|
||
let accounts: string[] = []; | ||
try { | ||
accounts = await rpcProvider.listAccounts(); | ||
for (let i = 0; i < accounts.length; i++) { | ||
const signer = rpcProvider.getSigner(i); | ||
const balance = await signer.getBalance(); | ||
if (balance.gte(requiredBalance)) { | ||
log.info(`Found funded account ${await signer.getAddress()}`); | ||
|
||
return signer; | ||
} | ||
} | ||
} catch (error) { | ||
log.error('Failed to retrieve accounts and balances:', error); | ||
} | ||
throw new Error( | ||
`could not find unlocked account with sufficient balance; all accounts:\n - ${accounts.join( | ||
'\n - ' | ||
)}` | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { expect, use } from 'chai'; | ||
import { utils, type providers } from 'ethers'; | ||
import { | ||
REGTEST_CHAIN_ID, | ||
findWealthyAccount, | ||
} from 'src/commands/findWealthyAccount'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
|
||
use(chaiAsPromised); | ||
|
||
const TESTNET_CHAIN_ID = 31; | ||
const MAINNET_CHAIN_ID = 30; | ||
|
||
describe('findWealthAccounts', function () { | ||
const expectedSigner = { | ||
getAddress: () => '0x123abc', | ||
getBalance: () => Promise.resolve(utils.parseUnits('2', 'ether')), | ||
}; | ||
|
||
const getMockedProvider = (expectedChainId: number) => | ||
({ | ||
getNetwork: () => ({ chainId: expectedChainId }), | ||
listAccounts: () => [1, 2, 3], | ||
getSigner: () => expectedSigner, | ||
} as unknown as providers.JsonRpcProvider); | ||
|
||
const expectFindWealthyAccountToFail = async (chainId: number) => { | ||
const mockRpcProvider = getMockedProvider(chainId); | ||
await expect(findWealthyAccount(mockRpcProvider)).to.rejectedWith( | ||
'Unlocked accounts are allowed for testing purposes only' | ||
); | ||
}; | ||
|
||
it('should not raise an error if it is used for dev purposes', async function () { | ||
const mockRpcProvider = getMockedProvider(REGTEST_CHAIN_ID); | ||
const account = await findWealthyAccount(mockRpcProvider); | ||
expect(account).to.be.eq(expectedSigner); | ||
}); | ||
|
||
it('should raise an error if it is used on Testnet', async function () { | ||
await expectFindWealthyAccountToFail(TESTNET_CHAIN_ID); | ||
}); | ||
|
||
it('should raise an error if it is used on Mainnet', async function () { | ||
await expectFindWealthyAccountToFail(MAINNET_CHAIN_ID); | ||
}); | ||
|
||
it('should raise an error if no accounts are found with enough balance', async function () { | ||
const mockRpcProvider = getMockedProvider(REGTEST_CHAIN_ID); | ||
await expect( | ||
findWealthyAccount(mockRpcProvider, utils.parseUnits('3', 'ether')) | ||
).to.rejectedWith( | ||
'could not find unlocked account with sufficient balance;' | ||
); | ||
}); | ||
}); |