Utilities

Constants

BigGas

Following up your function arguments with BigGas to set a high gas limit and price so that your transactions are more likely to execute. You can change these values within the ReaperSDK.js file.

reaper.BigGas === { gasPrice: 100000000000, gasLimit: 5000000 }

For example:

await wrappedFantom.transfer(momAddress, 1000, reaper.BigGas);

Addresses.json

Addresses.json contains the addresses of many popular contracts on the Fantom network, and it's easy to add more. Simply import it from the directory's root with a require statement and use dot notation to reference the addresses within. The object is separated by token addresses, testnet contract addresses, and mainnet contract addresses.

Import them each with the following:

const { tokens, testnet, mainnet } = require("../Addresses.json");

const { tokens, testnet, mainnet } = require("../Addresses.json");

async function main() {
  
  console.log(tokens.usdc);
  console.log(tokens.wftm);
  console.log(testnet.router);
  console.log(mainnet.spooky.masterChef);

}

> 0x04068DA6C83AFCFA0e13ba15A6696662335D5B75
> 0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83
> 0xcCAFCf876caB8f9542d6972f87B5D62e1182767d
> 0x2b2929E785374c651a81A63878Ab22742656DcDd

Contract Instantiation

You can create a contract interface and attach it to an address in a single function call with our createContract function. Just make sure you've already compiled your contracts with Hardhat. Refer to the contract title as a string for the first argument and use the target address as the second like so:

reaper.createContract(contractTitle: string, contractAddress: string);

  let usdc = await reaper.createContract("ERC20", tokens.usdc);
  let usdcSupply = await usdc.totalSupply();
  console.log(usdcSupply);

  let spookyChef = await reaper.createContract("MasterChef", mainnet.spooky.masterChef);
  let farmCount = await spookyChef.poolLength();
  console.log(farmCount);

> BigNumber { _hex: '0x343b58ee24c1', _isBigNumber: true }
> BigNumber { _hex: '0x0911258c64', _isBigNumber: true }
> BigNumber { _hex: '0xc100e8020d8478e62c7e79', _isBigNumber: true }

Decimal Formatting

Formatting human-readable inputs with reaper.parseToken()

You can turn floating point inputs of any type into a blockchain-readable BigNumber value in parts-per notation with 18 decimal places. An optional tokenAddress argument will automatically format your input using the correct amount of decimal places, such as 6 for USDC or 8 for BTC.

reaper.parseToken(amount:string or number, { tokenAddress: string });

  let usdcInput = await reaper.parseToken("100", tokens.usdc);
  console.log(usdcInput);
  console.log(usdcInput.toString());
  
  let wbtcInput = await reaper.parseToken("100", tokens.wbtc);
  console.log(wbtcInput);
  console.log(wbtcInput.toString());
  
  let wftmInput = await reaper.parseToken("100", tokens.wftm);
  console.log(wftmInput);
  console.log(wftmInput.toString());

> BigNumber { _hex: '0x05f5e100', _isBigNumber: true }
> 100000000

> BigNumber { _hex: '0x02540be400', _isBigNumber: true }
> 10000000000

> BigNumber { _hex: '0x056bc75e2d63100000', _isBigNumber: true }
> 100000000000000000000

Formatting big number outputs with reaper.formatToken()

Using the format() function, you can turn any bigNumber-ish value into a human readable fixed-point integer. Using the optional tokenAddress input will allow the function to automatically determine the token's decimals and adjust accordingly. See an example of this below - the function automatically adjusts to USDC's 6 decimals and BTC's 8 decimals.

reaper.formatToken(amount:BigNumber, { tokenAddress: string });

  let usdc = await reaper.createContract("ERC20", tokens.usdc);
  let usdcSupply = await usdc.totalSupply();
  console.log(await reaper.formatToken(usdcSupply, tokens.usdc));
  
  let wbtc = await reaper.createContract("ERC20", tokens.wbtc);
  let wbtcSupply = await wbtc.totalSupply();
  console.log(await reaper.formatToken(wbtcSupply, tokens.wbtc));
  
  let wftm = await reaper.createContract("ERC20", tokens.wftm);
  let wftmSupply = await wftm.totalSupply();
  console.log(await reaper.formatToken(wftmSupply, tokens.wftm));
  
> 58931849.963579

> 398.79588332

> 233007968.40776092

Pausing Execution

Due to its unpredictable nature, blockchain development can be a tricky beast. On Fantom, you may find your transactions failing due to being sent too soon after the preceding transaction. You may also want to manage transaction timing off-chain when running scripts or simulations. You can handle all of this with Reaper's sleep function.

reaper.sleep(miliseconds:uint)

  let wftm = await reaper.createContract("ERC20", tokens.wftm);
  sleep(5000) //5 second pause to wait for the contract to be deployed
  await wftm.deposit({ value: ethers.utils.parseEther("1") });

Getting a timestamp

Getting the block.timestamp value in Javascript can be kind of tricky, so Reaper took care of that for you. It's so simple I'm not even going to include an example >:^)

reaper.getTimestamp() => timestamp:uint

Last updated