Integrate with Ethers.js
Integrate directly from your website / (d)app
What is Ethers.js ?
From Ethers Website:
The ethers.js library aims to be a complete and compact library for interacting with the Ethereum Blockchain and its ecosystem.
It is often used to create decentralized applications (dapps), wallets (such as MetaMask and Tally) and other tools and simple scripts that require reading and writing to the blockchain.
In the following we show you some examples like:
Set Up
This is the basic setup to call contract functions with ethers and node.js
import { ethers } from "ethers";
const xchfAddress = "0xb4272071ecadd69d933adcd19ca99fe80664fc08";
const xchfABI = ["function Approve(address spender, uint256 amount)"];
const shareAddress = "0x6f38e0f1a73c96cb3f42598613ea3474f09cb200";
const shareABI = [
"function transferAndCall(address recipient, uint amount, bytes calldata data)",
"function recovery() view returns (address)",
"function declareLost(address collateralType, address lostAddress)"
];
let paymentHubAddress;
const paymentHubABI = [
"function approveERC20(address erc20In)",
"function getPriceInERC20(uint256 amountInBase, bytes memory path) view returns (uint256)",
"function getPriceInEther(uint256 amountInBase, IBrokerbot brokerBot) view returns (uint256)",
"function payFromEtherAndNotify(IBrokerbot recipient, uint256 amountInBase, bytes calldata ref)",
"function payFromERC20AndNotify(address recipient, uint256 amountBase, address erc20, uint256 amountInMaximum, bytes memory path, bytes calldata ref)",
"function payAndNotify(address recipient, uint256 amountInBase, bytes calldata ref)"
];
let brokerbotAddress;
const brokerbotABI = [
"function paymenthub() view returns (address)",
"function getBuyPrice(uint256 shares) view returns (uint256)",
"function settings() view returns (uint256)"
];
let recoveryHubAddress;
const recoveryHubABI = [
"function declareLost(address token, address collateralType, address lostAddress)"
];
const brokerbotRegistryAddress = "0xcB3e482df38d62E73A7aE0E15a2605caDcc5aE98";
const brokerbotRegistryABI = ["function getBrokerbot(address base, address token) view returns (address)"];
const amountOfShares = 100;
const buyEnabled = 0x1;
async function main() {
// get a RPC provider
const ethersProvider = new ethers.providers.InfuraProvider();
// get a signer
const wallet = ethers.Wallet.createRandom().connect(ethersProvider);
// get contract instances
const xchfContract = new ethers.Contract( xchfAddress , xchfABI, wallet);
const shareConract = new ethers.Contract(shareAddress, shareABI, wallet);
const brokerbotRegistryContract = new ethers.Contract(brokerbotRegistryAddress, brokerbotRegistryABI, wallet);
brokerbotAddress = await brokerbotRegistryContract.getBrokerbot(xchfAddress, shareAddress);
const brokerbotContract = new ethers.Contract(brokerbotAddress, brokerbotABI, wallet);
paymentHubAddress = await brokerbotContract.paymenthub();
const paymentHubContract = new ethers.Contract(paymentHubAddress, paymentHubABI, wallet);
///.... include here the example calls Getting Price for Buying Shares
Check if Buying is Enabled in Brokerbot Setting
Buying Shares
This will execute a transaction on the blockchain, like this you will need an wallet(signer) with ETH and XCHF.
Selling Shares
This will execute a transaction on the blockchain, like this you will need an wallet(signer) with ETH and share token.
Create an Offer
This will execute a transaction on the blockchain, like this you will need an wallet(signer) with ETH and share token.
Initiate Recovery
This will execute a transaction on the blockchain, like this you will need an wallet(signer) with ETH.
The Full Script
Last updated
Was this helpful?