-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathfundedWallet.ts
More file actions
71 lines (62 loc) · 2.02 KB
/
fundedWallet.ts
File metadata and controls
71 lines (62 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { ArkNote, InMemoryContractRepository, InMemoryWalletRepository, SingleKey, Wallet } from '@arkade-os/sdk'
import { exec } from 'child_process'
import { EventSource } from 'eventsource'
import { promisify } from 'util'
const execAsync = promisify(exec)
const waitForBalance = async (
getBalance: () => Promise<{ available: number }>,
minAmount = 100,
timeout = 5_000,
): Promise<void> => {
await new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
clearInterval(intervalId)
reject(new Error('Timed out waiting for balance'))
}, timeout)
const intervalId = setInterval(async () => {
try {
const balance = await getBalance()
if (balance.available >= minAmount) {
clearTimeout(timeoutId)
clearInterval(intervalId)
resolve(true)
}
} catch (err) {
clearTimeout(timeoutId)
clearInterval(intervalId)
reject(err)
}
}, 500)
})
}
const createNote = async (amount: number): Promise<string> => {
const { stdout } = await execAsync(`docker exec -t ark arkd note --amount ${amount}`)
return stdout.trim()
}
const getFundedWallet = async (arkUrl: string): Promise<Wallet> => {
const amount = 1_000_000
globalThis.EventSource ??= EventSource as typeof globalThis.EventSource
const wallet = await Wallet.create({
identity: SingleKey.fromRandomBytes(),
arkServerUrl: arkUrl,
storage: {
walletRepository: new InMemoryWalletRepository(),
contractRepository: new InMemoryContractRepository(),
},
})
await wallet.settle({
inputs: [ArkNote.fromString(await createNote(amount))],
outputs: [
{
address: await wallet.getAddress(),
amount: BigInt(amount),
},
],
})
await waitForBalance(() => wallet.getBalance())
return wallet
}
export async function faucetOffchain(address: string, amount: number): Promise<void> {
const fundedWallet = await getFundedWallet('http://localhost:7070')
await fundedWallet.send({ address, amount })
}