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' tokens such as BTC or ETH.

info

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

Import statements

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

import { HttpTransport, PublicClient, generateWallet, topUp } from "@nilfoundation/niljs";

Make a top-up with default tokens

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

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

const wallet = await generateWallet({
shardId: 1,
rpcEndpoint: RPC_ENDPOINT,
faucetEndpoint: FAUCET_ENDPOINT,
});

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

console.log(resultBeforeTopUp);


await topUp({
address: wallet.address,
faucetEndpoint: FAUCET_ENDPOINT,
rpcEndpoint: RPC_ENDPOINT,
token: "NIL",
amount: 1_000_000,
});

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

console.log(result);

Make a top-up with custom 'mock' tokens

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

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

const wallet = await generateWallet({
shardId: 1,
rpcEndpoint: RPC_ENDPOINT,
faucetEndpoint: FAUCET_ENDPOINT,
});

const resultBeforeTopUp = await client.getTokens(wallet.address, "latest");

console.log(resultBeforeTopUp);


await topUp({
address: wallet.address,
faucetEndpoint: FAUCET_ENDPOINT,
rpcEndpoint: RPC_ENDPOINT,
token: "BTC",
amount: 1_000_000,
});

const result = await client.getTokens(wallet.address, "latest");

console.log(result);