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
Copy 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
Copy const priceBase = await brokerbotContract .getBuyPrice (amountOfShares);
console .log ( `Price for ${ amountOfShares } Shares in XCHF: ${ ethers . utils .formatUnits (priceBase , 18 ) } ` );
Check if Buying is Enabled in Brokerbot Setting
Copy const settings = await brokerbotContract .settings ();
console .log ( `Buying enabled: ${ (settings & buyEnabled) == buyEnabled } ` );
Buying Shares
This will execute a transaction on the blockchain, like this you will need an wallet(signer) with ETH and XCHF.
Copy await xchfContract .connect (wallet) .approve (paymentHubAddress , priceBase); //allowance for xchf
const paymentHubConnected = await paymentHubContract .connect (wallet);
await paymentHubConnected[ "payAndNotify(address,uint256,bytes)" ](brokerbotAddress , priceBase , "0x01" );
Selling Shares
This will execute a transaction on the blockchain, like this you will need an wallet(signer) with ETH and share token.
Copy await shareConract .connect (wallet) .transferAndCall (brokerbotAddress , amountOfShares , "0x" )
Create an Offer
This will execute a transaction on the blockchain, like this you will need an wallet(signer) with ETH and share token.
Copy const overrides = {
value : ethers . utils .parseEther ( "3" ) // needed for payment of license fee
}
pricePerShare = ethers . utils .parseUnits ( "2" , 18 );
salt = ethers . utils .formatBytes32String ( '1' );
await draggable .connect (sig1) .makeAcquisitionOffer (salt , pricePerShare , xchfAddress , overrides)
Initiate Recovery
This will execute a transaction on the blockchain, like this you will need an wallet(signer) with ETH.
Copy recoveryHubAddress = await shareConract .recovery ();
const recoveryHubContract = new ethers .Contract (recoveryHubAddress , recoveryHubABI , wallet);
recoveryHubContract .connect (wallet) .declareLost (shareAddress , shareAddress , lostAddress);
The Full Script
Copy 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 () {
const ethersProvider = new ethers . providers .InfuraProvider ();
const wallet = ethers . Wallet .createRandom () .connect (ethersProvider);
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
// get price for shares
const priceBase = await brokerbotContract .getBuyPrice (amountOfShares);
console .log ( `Price for ${ amountOfShares } Shares in XCHF: ${ ethers . utils .formatUnits (priceBase , 18 ) } ` );
// check if buying is enabled
const settings = await brokerbotContract .settings ();
console .log ( `Buying enabled: ${ (settings & buyEnabled) == buyEnabled } ` );
// buying shares
await xchfContract .connect (wallet) .approve (paymentHubAddress , priceBase); //allowance for xchf
const paymentHubConnected = await paymentHubContract .connect (wallet);
await paymentHubConnected[ "payAndNotify(address,uint256,bytes)" ](brokerbotAddress , priceBase , "0x01" );
// sell shares
await shareConract .connect (wallet) .transferAndCall (brokerbotAddress , amountOfShares , "0x" )
// make an offer
const overrides = {
value : ethers . utils .parseEther ( "3" ) // needed for payment of license fee
}
pricePerShare = ethers . utils .parseUnits ( "2" , 18 );
salt = ethers . utils .formatBytes32String ( '1' );
await draggable .connect (sig1) .makeAcquisitionOffer (salt , pricePerShare , xchfAddress , overrides)
const lostAddress = wallet.getAddress(); // this is just a example - for real case the lost address shouldn't be the same as the claimer address
// init recovery mechanism old
shareConract .connect (wallet) .declareLost (shareAddress , lostAddress);
// init recovery mechanism new
recoveryHubAddress = await shareConract .recovery ();
const recoveryHubContract = new ethers .Contract (recoveryHubAddress , recoveryHubABI , wallet);
recoveryHubContract .connect (wallet) .declareLost (shareAddress , shareAddress , lostAddress);
}
main () .catch ((error) => {
console .error (error);
process .exitCode = 1 ;
});