|
| 1 | +import { inject, injectable } from "tsyringe"; |
| 2 | +import { |
| 3 | + AccountUpdate, |
| 4 | + fetchAccount, |
| 5 | + fetchLastBlock, |
| 6 | + Lightnet, |
| 7 | + Mina, |
| 8 | + PrivateKey, |
| 9 | + PublicKey, |
| 10 | +} from "o1js"; |
| 11 | +import { log, noop, range, sleep } from "@proto-kit/common"; |
| 12 | + |
| 13 | +import type { |
| 14 | + LightnetMinaBaseLayerConfig, |
| 15 | + MinaBaseLayer, |
| 16 | + MinaBaseLayerConfig, |
| 17 | +} from "../MinaBaseLayer"; |
| 18 | +import { MinaTransactionSender } from "../../../settlement/transactions/MinaTransactionSender"; |
| 19 | + |
| 20 | +import { MinaNetworkUtils } from "./MinaNetworkUtils"; |
| 21 | + |
| 22 | +@injectable() |
| 23 | +export class LightnetUtils implements MinaNetworkUtils { |
| 24 | + public constructor( |
| 25 | + @inject("BaseLayer") |
| 26 | + private readonly baseLayer: MinaBaseLayer, |
| 27 | + @inject("TransactionSender") |
| 28 | + private readonly transactionSender: MinaTransactionSender |
| 29 | + ) {} |
| 30 | + |
| 31 | + public async waitForNetwork(): Promise<void> { |
| 32 | + const maxAttempts = 24; |
| 33 | + const delay = 5000; |
| 34 | + |
| 35 | + let lastBlock; |
| 36 | + let attempt = 0; |
| 37 | + |
| 38 | + const { config } = this.baseLayer; |
| 39 | + this.assertConfigLightnet(config); |
| 40 | + const graphqlEndpoint = config.network.graphql; |
| 41 | + |
| 42 | + log.info("Waiting for network to be ready..."); |
| 43 | + |
| 44 | + while (!lastBlock) { |
| 45 | + attempt += 1; |
| 46 | + if (attempt > maxAttempts) { |
| 47 | + throw new Error( |
| 48 | + `Network was still not ready after ${(delay / 1000) * (attempt - 1)}s` |
| 49 | + ); |
| 50 | + } |
| 51 | + try { |
| 52 | + // eslint-disable-next-line no-await-in-loop |
| 53 | + lastBlock = await fetchLastBlock(graphqlEndpoint); |
| 54 | + } catch (e) { |
| 55 | + // Ignore errors |
| 56 | + noop(); |
| 57 | + } |
| 58 | + // eslint-disable-next-line no-await-in-loop |
| 59 | + await sleep(delay); |
| 60 | + } |
| 61 | + |
| 62 | + log.provable.info("Network is ready", lastBlock); |
| 63 | + } |
| 64 | + |
| 65 | + private assertConfigLightnet( |
| 66 | + config: MinaBaseLayerConfig |
| 67 | + ): asserts config is { network: LightnetMinaBaseLayerConfig } { |
| 68 | + if (config.network.type !== "lightnet") { |
| 69 | + throw new Error("Config provided is not of type 'lightnet'"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public async faucet( |
| 74 | + receiver: PublicKey, |
| 75 | + fundingAmount = 1000 * 1e9, |
| 76 | + fee = 0.1 * 1e9 |
| 77 | + ) { |
| 78 | + const [faucetDonor] = await this.getFundedAccounts(1); |
| 79 | + |
| 80 | + const account = await fetchAccount({ publicKey: receiver }); |
| 81 | + |
| 82 | + log.provable.info( |
| 83 | + `Dripping ${fundingAmount / 1e9} MINA from ${faucetDonor.toBase58()} to ${receiver.toBase58()}` |
| 84 | + ); |
| 85 | + |
| 86 | + const faucetDonorPublicKey = faucetDonor.toPublicKey(); |
| 87 | + |
| 88 | + const tx = await Mina.transaction( |
| 89 | + { |
| 90 | + sender: faucetDonorPublicKey, |
| 91 | + fee, |
| 92 | + }, |
| 93 | + async () => { |
| 94 | + // if the destination account does not exist yet, pay the creation fee for it |
| 95 | + if (account.error) { |
| 96 | + AccountUpdate.fundNewAccount(faucetDonorPublicKey); |
| 97 | + } |
| 98 | + |
| 99 | + AccountUpdate.createSigned(faucetDonorPublicKey).balance.subInPlace( |
| 100 | + fundingAmount |
| 101 | + ); |
| 102 | + AccountUpdate.create(receiver).balance.addInPlace(fundingAmount); |
| 103 | + } |
| 104 | + ); |
| 105 | + |
| 106 | + tx.sign([faucetDonor]); |
| 107 | + |
| 108 | + await this.transactionSender.proveAndSendTransaction(tx, "included"); |
| 109 | + |
| 110 | + log.provable.info( |
| 111 | + `Funded account ${receiver.toBase58()} with ${fundingAmount / 1e9} MINA` |
| 112 | + ); |
| 113 | + |
| 114 | + await Lightnet.releaseKeyPair({ |
| 115 | + publicKey: faucetDonor.toPublicKey().toBase58(), |
| 116 | + lightnetAccountManagerEndpoint: this.getAccountManagerEndpoint(), |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + private getAccountManagerEndpoint() { |
| 121 | + const { |
| 122 | + baseLayer: { config }, |
| 123 | + } = this; |
| 124 | + |
| 125 | + this.assertConfigLightnet(config); |
| 126 | + |
| 127 | + if (config.network.accountManager === undefined) { |
| 128 | + throw new Error( |
| 129 | + "Wanted to retrieve funded keypairs, but accountManager endpoint is missing in config" |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + return config.network.accountManager; |
| 134 | + } |
| 135 | + |
| 136 | + public async getFundedAccounts(num: number = 1): Promise<PrivateKey[]> { |
| 137 | + const lightnetAccountManagerEndpoint = this.getAccountManagerEndpoint(); |
| 138 | + |
| 139 | + return await Promise.all( |
| 140 | + range(num).map(async (i) => { |
| 141 | + const pair = await Lightnet.acquireKeyPair({ |
| 142 | + isRegularAccount: true, |
| 143 | + lightnetAccountManagerEndpoint, |
| 144 | + }); |
| 145 | + return pair.privateKey; |
| 146 | + }) |
| 147 | + ); |
| 148 | + } |
| 149 | +} |
0 commit comments