Skip to content

Commit

Permalink
fetchPrivateKeyWithGas() uses generateWalletWithGasAndTokens()
Browse files Browse the repository at this point in the history
  • Loading branch information
teogeb committed Dec 20, 2024
1 parent f4e0473 commit 72da6bc
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,31 +260,6 @@ export class KeyServer {
}
}

export async function fetchPrivateKeyWithGas(): Promise<string> {
let response
try {
response = await fetch(`http://127.0.0.1:${KeyServer.KEY_SERVER_PORT}/key`, {
signal: AbortSignal.timeout(5000)
})
} catch (_e) {
try {
await KeyServer.startIfNotRunning() // may throw if parallel attempts at starting server
} catch (_e2) {
// no-op
} finally {
response = await fetch(`http://127.0.0.1:${KeyServer.KEY_SERVER_PORT}/key`, {
signal: AbortSignal.timeout(5000)
})
}
}

if (!response.ok) {
throw new Error(`fetchPrivateKeyWithGas failed ${response.status} ${response.statusText}: ${await response.text()}`)
}

return response.text()
}

export class Queue<T> {

private readonly items: T[] = []
Expand Down Expand Up @@ -380,15 +355,18 @@ const getTestAdminWallet = (provider: Provider): Wallet => {
return new Wallet(TEST_CHAIN_CONFIG.adminPrivateKey).connect(provider)
}

export const generateWalletWithGasAndTokens = async (): Promise<Wallet & AbstractSigner<Provider>> => {
// TODO refactor method e.g. to createTestWallet({ gas: boolean, token: boolean })
export const generateWalletWithGasAndTokens = async (tokens = true): Promise<Wallet & AbstractSigner<Provider>> => {
const provider = getTestProvider()
const privateKey = crypto.randomBytes(32).toString('hex')
const newWallet = new Wallet(privateKey)
const adminWallet = getTestAdminWallet(provider)
const token = getTestTokenContract(adminWallet)
await retry(
async () => {
await (await token.mint(newWallet.address, parseEther('1000000'))).wait()
if (tokens) {
await (await token.mint(newWallet.address, parseEther('1000000'))).wait()
}
await (await adminWallet.sendTransaction({
to: newWallet.address,
value: parseEther('1')
Expand All @@ -403,3 +381,8 @@ export const generateWalletWithGasAndTokens = async (): Promise<Wallet & Abstrac
)
return newWallet.connect(provider) as (Wallet & AbstractSigner<Provider>)
}

export const fetchPrivateKeyWithGas = async (): Promise<string> => {
const wallet = await generateWalletWithGasAndTokens(false)
return wallet.privateKey
}

0 comments on commit 72da6bc

Please sign in to comment.