Skip to main content

Use the faucet service via Nil.js

Faucets form a special service for 'topping up' addresses and existing smart contracts. Existing faucets can distribute the 'default' token used to pay for message execution as well as several pre-defined 'mock' currencies such as BTC or ETH.

info

'Mock' currencies from faucets are identical to custom currencies and cannot be used to pay for execution.

Import statements

The example imports the following components from @nilfoundation/niljs:

import {
Faucet,
FaucetClient,
generateRandomPrivateKey,
HttpTransport,
LocalECDSAKeySigner,
PublicClient,
WalletV1,
} from "@nilfoundation/niljs";

import {} from "viem";

Make a top-up with default tokens

To create a wallet and top it up with 'default' tokens:

const SALT = BigInt(Math.floor(Math.random() * 10000));

const client = new PublicClient({
transport: new HttpTransport({
endpoint: RPC_ENDPOINT,
}),
shardId: 1,
});

const faucet = new Faucet(client);

const pkey = generateRandomPrivateKey();

const signer = new LocalECDSAKeySigner({
privateKey: pkey,
});

const pubkey = signer.getPublicKey();

const wallet = new WalletV1({
pubkey: pubkey,
client: client,
signer: signer,
shardId: 1,
salt: SALT,
});

const walletAddress = wallet.address;

await faucet.withdrawToWithRetry(walletAddress, 80_000_000n);

await wallet.selfDeploy(true);

const resultBeforeTopUp = await client.getBalance(wallet.address);

console.log(resultBeforeTopUp);


const faucetClient = new FaucetClient({
transport: new HttpTransport({
endpoint: RPC_ENDPOINT,
}),
});

const faucets = await faucetClient.getAllFaucets();

const defaultFaucet = faucets.NIL;

const tx = await faucetClient.topUpAndWaitUntilCompletion(
{
faucetAddress: defaultFaucet,
walletAddress,
amount: 1_000_000,
},
client,
);

const result = await client.getBalance(wallet.address);

console.log(result);
console.log(tx);

Make a top-up with custom 'mock' currencies

To create a wallet and top it up with a custom 'mock' currency (BTC):

const SALT = BigInt(Math.floor(Math.random() * 10000));

const client = new PublicClient({
transport: new HttpTransport({
endpoint: RPC_ENDPOINT,
}),
shardId: 1,
});

const faucet = new Faucet(client);

const pkey = generateRandomPrivateKey();

const signer = new LocalECDSAKeySigner({
privateKey: pkey,
});

const pubkey = signer.getPublicKey();

const wallet = new WalletV1({
pubkey: pubkey,
client: client,
signer: signer,
shardId: 1,
salt: SALT,
});

const walletAddress = wallet.address;

await faucet.withdrawToWithRetry(walletAddress, 80_000_000n);

await wallet.selfDeploy(true);

const resultBeforeTopUp = await client.getBalance(wallet.address);

console.log(resultBeforeTopUp);

const faucetClient = new FaucetClient({
transport: new HttpTransport({
endpoint: RPC_ENDPOINT,
}),
});

const faucets = await faucetClient.getAllFaucets();

const faucetBTC = faucets.BTC;

const tx = await faucetClient.topUpAndWaitUntilCompletion(
{
faucetAddress: faucetBTC,
walletAddress,
amount: 1_000_000,
},
client,
);

const result = await client.getCurrencies(walletAddress, "latest");

console.log(result);