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 transaction 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, generateSmartAccount, topUp } from "@nilfoundation/niljs";
Make a top-up with default tokens
To create a smart account and top it up with 'default' tokens:
const client = new PublicClient({
transport: new HttpTransport({
endpoint: RPC_ENDPOINT,
}),
shardId: 1,
});
const smartAccount = await generateSmartAccount({
shardId: 1,
rpcEndpoint: RPC_ENDPOINT,
faucetEndpoint: FAUCET_ENDPOINT,
});
const resultBeforeTopUp = await client.getBalance(smartAccount.address);
console.log(resultBeforeTopUp);
await topUp({
address: smartAccount.address,
faucetEndpoint: FAUCET_ENDPOINT,
rpcEndpoint: RPC_ENDPOINT,
token: "NIL",
amount: 1_000_000,
});
const result = await client.getBalance(smartAccount.address);
console.log(result);
Make a top-up with custom 'mock' tokens
To create a smart account and top it up with a custom 'mock' token (BTC):
const client = new PublicClient({
transport: new HttpTransport({
endpoint: RPC_ENDPOINT,
}),
shardId: 1,
});
const smartAccount = await generateSmartAccount({
shardId: 1,
rpcEndpoint: RPC_ENDPOINT,
faucetEndpoint: FAUCET_ENDPOINT,
});
const resultBeforeTopUp = await client.getTokens(smartAccount.address, "latest");
console.log(resultBeforeTopUp);
await topUp({
address: smartAccount.address,
faucetEndpoint: FAUCET_ENDPOINT,
rpcEndpoint: RPC_ENDPOINT,
token: "BTC",
amount: 1_000_000,
});
const result = await client.getTokens(smartAccount.address, "latest");
console.log(result);