-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfaucet.ts
More file actions
70 lines (55 loc) · 2 KB
/
faucet.ts
File metadata and controls
70 lines (55 loc) · 2 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
import "reflect-metadata";
import { getRequiredEnv } from "../../utils/loadEnv";
export default async function (publicKey: string) {
const { AccountUpdate, fetchAccount, Lightnet, Mina, Provable, PublicKey } =
await import("o1js");
// configuration
const fee = 0.1 * 1e9;
const fundingAmount = 1000 * 1e9;
const net = Mina.Network({
mina: `${getRequiredEnv("MINA_NODE_GRAPHQL_HOST")}:${getRequiredEnv("MINA_NODE_GRAPHQL_PORT")}/graphql`,
archive: `${getRequiredEnv("MINA_ARCHIVE_GRAPHQL_HOST")}:${getRequiredEnv("MINA_ARCHIVE_GRAPHQL_PORT")}/graphql`,
lightnetAccountManager: `${getRequiredEnv("MINA_ACCOUNT_MANAGER_HOST")}:${getRequiredEnv("MINA_ACCOUNT_MANAGER_PORT")}`,
});
Mina.setActiveInstance(net);
// get the source account from the account manager
const pair = await Lightnet.acquireKeyPair({
isRegularAccount: true,
});
// which account to drip to
const keyArg = process.env[publicKey] ?? publicKey;
if (keyArg?.length === 0) {
throw new Error("No key provided");
}
const key = PublicKey.fromBase58(keyArg);
await fetchAccount({ publicKey: pair.publicKey });
Provable.log(
`Dripping ${fundingAmount / 1e9} MINA from ${pair.publicKey.toBase58()} to ${key.toBase58()}`
);
const tx = await Mina.transaction(
{
sender: pair.publicKey,
fee,
},
async () => {
const account = await fetchAccount({ publicKey: key });
// if the destination account does not exist yet, pay the creation fee for it
if (account.error) {
AccountUpdate.fundNewAccount(pair.publicKey);
}
AccountUpdate.createSigned(pair.publicKey).balance.subInPlace(
fundingAmount
);
AccountUpdate.create(key).balance.addInPlace(fundingAmount);
}
);
tx.sign([pair.privateKey]);
const sentTx = await tx.send();
await sentTx.wait();
Provable.log(
`Funded account ${key.toBase58()} with ${fundingAmount / 1e9} MINA`
);
await Lightnet.releaseKeyPair({
publicKey: pair.publicKey.toBase58(),
});
}