First of all, You must import the modules you want to use in your source code before using them. For example, if you want to use calculateExpectedSeig
function, you must import it just like the example code below.
const { calculateExpectedSeig } = require("tokamak-staking-lib");
This function sets network information. You must call this function before calling Layer2 Query
and Staking Query
functions. This function does not create a connection. The first query request will create the connection to Ethereum node for the RPC communication.
function setNetwork(provider: provider, net: string = "mainnet")
provider: provider
- The web3 provider which includes the host information you want to connect to. You can pass string or provider instance to this parameter.net: string (optional)
- The network type you want to connect to. You can choosemainnet
orrinkeby
. The default ismainnet
.
- Nothing
setNetwork("https://api.infura.io/v1/jsonrpc/mainnet", "mainnet");
// or
setNetwork(new Web3.providers.HttpProvider("https://api.infura.io/v1/jsonrpc/mainnet"), "mainnet");
This function calculates the expected seigniorate.
function calculateExpectedSeig(
fromBlockNumber: BN,
toBlockNumber: BN,
userStakedAmount: BN,
totalStakedAmount: BN,
totalSupplyOfTON: BN,
pseigRate: BN
): BN
fromBlockNumber: BN
- The latest commited block number. You can get this using seigManager.lastCommitBlock (layer2).toBlockNumber: BN
- The target block number in which you want to calculate seigniorage.userStakedAmount: BN
- The staked WTON amount of user. You can get this using coinage.balanceOf(user).totalStakedAmount: BN
- The staked WTON amount in SeigManager. You can get this using tot.totalSupply().totalSupplyOfTON: BN
- The current total supply of TON in RAY. You can get this using ton.totalSupply() - ton.balanceOf(WTON) + tot.totalSupply().pseigRate: BN
- The pseig rate in RAY. The current value is 0.4. You can get this using seigManager.relativeSeigRate().
expectedSeig: BN
- The expected seigniorage (decimals: 27).
const RAY = toBN("1000000000000000000000000000"); // 1e+27
const fromBlockNumber = toBN("1");
const toBlockNumber = toBN("10000");
const userStakedAmount = toBN("1000").mul(RAY);
const totalStakedAmount = toBN("2000000").mul(RAY);
const totalSupplyOfTON = toBN("50000000").mul(RAY);
const pseigRate = toBN("4").mul(RAY).div(toBN("10"));
const expectedSeig = calculateExpectedSeig(
fromBlockNumber,
toBlockNumber,
userStakedAmount,
totalStakedAmount,
totalSupplyOfTON,
pseigRate
);
console.log(expectedSeig); // 8309568960000000000000000000
This function calculates the expected seigniorage with commission.
function calculateExpectedSeigWithCommission(
fromBlockNumber: BN,
toBlockNumber: BN,
userStakedAmount: BN,
totalStakedAmount: BN,
totalSupplyOfTON: BN,
pseigRate: BN,
commissionRate: BN,
isCommissionRateNegative: boolean,
operatorStakedAmount: BN,
totalStakedAmountOnLayer2: BN,
isOperator: boolean
): BN
fromBlockNumber: BN
- The latest commited block number. You can get this using seigManager.lastCommitBlock (layer2).toBlockNumber: BN
- The target block number in which you want to calculate seigniorage.userStakedAmount: BN
- The staked WTON amount of user. You can get this using coinage.balanceOf(user).totalStakedAmount: BN
- The staked WTON amount in SeigManager. You can get this using tot.totalSupply().totalSupplyOfTON: BN
- The current total supply of TON in RAY. You can get this using ton.totalSupply() - ton.balanceOf(WTON) + tot.totalSupply().pseigRate: BN
- The pseig rate in RAY. The current value is 0.4. You can get this using seigManager.relativeSeigRate().commissionRate: BN
- The commission rate of the current layer2. You can get this using seigManager.commissionRates(layer2).isCommissionRateNegative: boolean
- Whether the commission rate of the current layer2 is negative. You can get this using seigManager.isCommissionRateNegative(layer2).operatorStakedAmount: BN
- The operator's staked amount. You can get this using coinage.balanceOf(operator).totalStakedAmountOnLayer2: BN
- The total amount in the current layer2. You can get this using coinage.totalSupply().isOperator: boolean
- Whether the user you want to calculate seigniorate for is operator.
expectedSeigWithCommission: BN
- The expected seigniorage with commission (decimals: 27).
const RAY = toBN("1000000000000000000000000000"); // 1e+27
const fromBlockNumber = toBN("1");
const toBlockNumber = toBN("10000");
const userStakedAmount = toBN("5000").mul(RAY);
const totalStakedAmount = toBN("2000000").mul(RAY);
const totalSupplyOfTON = toBN("50000000").mul(RAY);
const pseigRate = toBN("4").mul(RAY).div(toBN("10"));
const commissionRate = toBN("7").mul(RAY).div(toBN("10"));
const isCommissionRateNegative = true;
const operatorStakedAmount = toBN("500000").mul(RAY);
const totalStakedAmountOnLayer2 = toBN("1000000").mul(RAY);
const isOperator = false;
const expectedSeigWithCommission = calculateExpectedSeigWithCommission(
fromBlockNumber,
toBlockNumber,
userStakedAmount,
totalStakedAmount,
totalSupplyOfTON,
pseigRate,
commissionRate,
isCommissionRateNegative,
operatorStakedAmount,
totalStakedAmountOnLayer2,
isOperator
);
console.log(expectedSeigWithCommission);
This class calculates the expected seigniorate.
class Calculator {
constructor();
public setSeigPerBlock(seig: BN);
public setPseigRate(rate: BN);
public setTotalSupplyOfTON(totalSupply: BN);
public setTotalStakedAmount(amount: BN);
public getExpectedSeig(fromBlockNumber: BN, toBlockNumber: BN, userStakedAmount: BN): BN;
}
let calculator = new Calculator();
calculator.setTotalStakedAmount(toBN("2000000").mul(RAY));
const fromBlockNumber = new BN("1");
const toBlockNumber = new BN("10000");
const userStakedAmount = toBN("1000").mul(RAY);
const expectedSeig = calculator.getExpectedSeig(fromBlockNumber, toBlockNumber, userStakedAmount);
console.log(expectedSeig); // 8309568960000000000000000000
This function gets the number of layer2.
function getNumLayer2(): Promise<number>
- Nothing
numLayer2: Promise<number>
- The number of layer2.
const numLayer2 = await getNumLayer2();
console.log(numLayer2);
This function gets the layer2 address by index.
function getLayer2ByIndex(index: number): Promise<string>
index: number
- The layer2 index.
layer2: Promise<string>
- The layer2 address.
const layer2 = await getLayer2ByIndex(0);
console.log(layer2);
This function checks whether the given address is a layer2 address.
function isLayer2(layer2: string): Promise<boolean>
layer2: string
- The address to check whether it's a layer2 address.
isLayer2: Promise<boolean>
- Whether the given address is a layer2 address.
const result = await isLayer2("0x39A13a796A3Cd9f480C28259230D2EF0a7026033");
console.log(result);
This function gets operator address.
function getOperator(layer2: string): Promise<string>
layer2: string
- The layer2 address.
operator: Promise<string>
- The operator address.
const layer2 = "0x39A13a796A3Cd9f480C28259230D2EF0a7026033";
const operator = await tokamak.getOperator(layer2);
console.log(operator);
This function checks whether the given account is a submitter.
function isSubmitter(layer2: string, account: string): Promise<boolean>
layer2: string
- The layer2 address.account: string
- The account address to check whether it's a submitter.
isSubmitter: Promise<boolean>
- Whether the given account address is a submitter.
const result = await isSubmitter("0xEA8e2eC08dCf4971bdcdfFFe21439995378B44F3");
console.log(result);
This function gets the staked amount by layer2 and account.
async function getStakedAmount(layer2: string, account: string, blockNumber?: BN): Promise<BN>
layer2: string
- The layer2 address.account: string
- The account address.blockNumber: BN (optional)
- The block number. If you don't specifyblockNumber
, it will belatest
by default.
stakedAmount: Promise<BN>
- The staked amount of the specified block (decimals: 27).
const layer2 = "0x39A13a796A3Cd9f480C28259230D2EF0a7026033";
const account = "0xEA8e2eC08dCf4971bdcdfFFe21439995378B44F3"; // tokamak1 operator
const blockNumber = new BN(11072614);
// 1. It gets the staked amount of the latest block.
const amount1 = await getStakedAmount(layer2, account);
console.log(amount1);
// 2. It gets the staked amount of the specified block.
const amount2 = await getStakedAmount(layer2, account, blockNumber);
console.log(amount2);
This function gets the staked amount difference by layer2 and account.
async function getStakedAmountDiff(layer2: string, account: string, fromBlockNumber: BN, toBlockNumber?: BN): Promise<BN>
layer2: string
- The layer2 address.account: string
- The account address.fromBlockNumber: BN
- The starting block number of the block period.toBlockNumber: BN (optional)
- The ending block number of the block period. If you don't specifytoBlockNumber
, it will belatest
by default.
stakedAmountDiff: Promise<BN>
- The staked amount difference of the specified block period (decimals: 27).
const layer2 = "0x39A13a796A3Cd9f480C28259230D2EF0a7026033";
const account = "0xEA8e2eC08dCf4971bdcdfFFe21439995378B44F3"; // tokamak1 operator
const fromBlockNumber = new BN(11072614);
const toBlockNumber = fromBlockNumber.add(new BN(1));
// 1. It gets the staked amount difference of the specified block period.
const amount1 = await getStakedAmountDiff(layer2, account, fromBlockNumber, toBlockNumber);
console.log(amount1);
// 2. It gets the staked amount difference of the specified block period (until the latest block).
const amount2 = await getStakedAmountDiff(layer2, account, fromBlockNumber);
console.log(amount2);
This function gets the total staked amount of all layer2s by account.
async function getTotalStakedAmount(account: string, blockNumber?: BN): Promise<BN>
account: string
- The account address.blockNumber: BN (optional)
- The block number. If you don't specifyblockNumber
, it will belatest
by default.
totalStakedAmount: Promise<BN>
- The total staked amount of the specified block (decimals: 27).
const account = "0xEA8e2eC08dCf4971bdcdfFFe21439995378B44F3"; // tokamak1 operator
const blockNumber = new BN(11072614);
// 1. It gets the total staked amount of the latest block.
const amount1 = await getTotalStakedAmount(account);
console.log(amount1);
// 2. It gets the total staked amount of the specified block.
const amount2 = await getTotalStakedAmount(account, blockNumber);
console.log(amount2);
This function gets the total staked amount difference of all layer2s by account.
async function getTotalStakedAmountDiff(account: string, fromBlockNumber: BN, toBlockNumber?: BN): Promise<BN>
account: string
- The account address.fromBlockNumber: BN
- The starting block number of the block period.toBlockNumber: BN (optional)
- The ending block number of the block period. If you don't specifytoBlockNumber
, it will belatest
by default.
totalStakedAmountDiff: Promise<BN>
- The total staked amount difference of the specified block period (decimals: 27).
const account = "0xEA8e2eC08dCf4971bdcdfFFe21439995378B44F3"; // tokamak1 operator
const fromBlockNumber = new BN(11072614);
const toBlockNumber = fromBlockNumber.add(new BN(1));
// 1. It gets the total staked amount difference of the specified block period.
const amount1 = await getTotalStakedAmountDiff(account, fromBlockNumber, toBlockNumber);
console.log(amount1);
// 2. It gets the total staked amount difference of the specified block period (until the latest block).
const amount2 = await getTotalStakedAmountDiff(account, fromBlockNumber);
console.log(amount2);
This function gets the total supply of TON.
function getTotalSupplyOfTON(): Promise<BN>
- Nothing
totalSupplyOfTON: Promise<BN>
- the total supply of TON (decimals: 18).
const totalSupply = await getTotalSupplyOfTON();
console.log(totalSupply);
This function gets the total supply of TON with seigniorage.
async function getTotalSupplyOfTONWithSeig(): Promise<BN>
- Nothing
totalSupplyOfTONWithSeig: Promise<BN>
- the total supply of TON with seigniorage (decimals: 18).
const totalSupply = await getTotalSupplyOfTONWithSeig();
console.log(totalSupply);
This function gets the total supply of WTON.
function getTotalSupplyOfWTON(): Promise<BN>
- Nothing
totalSupplyOfWTON: Promise<BN>
- the total supply of WTON (decimals: 27).
const totalSupply= await getTotalSupplyOfWTON();
console.log(totalSupply);
This function commits dummy data.
function commitDummy(layer2: string, privkey: string): Promise<any>
layer2: string
: The layer2 address.privkey: string
: The submitter's private key. Generally, the operator is the submitter.
receipt: Promise<any>
: The transaction receipt.
const layer2 = "0x39A13a796A3Cd9f480C28259230D2EF0a7026033";
const privkey = "???";
const receipt = await commitDummy(layer2, privkey);
console.log(receipt);
For more details, refer to the test code pages below.