getQuoteBuy
Calculate how many tokens you would receive for a given ETH amount.
Function Signature
function getQuoteBuy(
address tokenAddress,
uint256 ethAmount
) external view returns (uint256)
Parameters
Address of the token to quote
Amount of ETH to spend (in wei)
Returns
Number of tokens that would be received
Example
import { ethers } from 'ethers'
const factory = new ethers.Contract(
TOKEN_FACTORY_ADDRESS,
FACTORY_ABI,
provider
)
async function getQuote(tokenAddress: string, ethAmount: string) {
const amountInWei = ethers.utils.parseEther(ethAmount)
const tokensOut = await factory.getQuoteBuy(
tokenAddress,
amountInWei
)
console.log(`Tokens out: ${ethers.utils.formatEther(tokensOut)}`)
const pricePerToken = amountInWei.mul(1e18).div(tokensOut)
console.log(`Price per token: ${ethers.utils.formatEther(pricePerToken)} ETH`)
return tokensOut
}
getQuoteSell
Calculate how much ETH you would receive for selling tokens.
Function Signature
function getQuoteSell(
address tokenAddress,
uint256 tokenAmount
) external view returns (uint256)
Parameters
Address of the token to sell
Returns
Amount of ETH that would be received (before fees)
Example
async function getSellQuote(
tokenAddress: string,
tokenAmount: string
) {
const amount = ethers.utils.parseEther(tokenAmount)
const ethOut = await factory.getQuoteSell(
tokenAddress,
amount
)
console.log(`ETH out (before fees): ${ethers.utils.formatEther(ethOut)}`)
const feePercent = await factory.getCurrentFeePercent(
tokenAddress,
await signer.getAddress()
)
const feeAmount = ethOut.mul(feePercent).div(10000)
const ethAfterFees = ethOut.sub(feeAmount)
console.log(`ETH after fees: ${ethers.utils.formatEther(ethAfterFees)}`)
return ethAfterFees
}