Complete examples for integrating with InkyPump contracts
Install Dependencies
npm install ethers
Setup Provider
import { ethers } from 'ethers'
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
Connect to Contract
const TOKEN_FACTORY_ADDRESS = "0x1D74317d760f2c72A94386f50E8D10f2C902b899"
const factory = new ethers.Contract(
TOKEN_FACTORY_ADDRESS,
FACTORY_ABI,
signer
)
import { ethers } from 'ethers'
class InkyPumpClient {
private factory: ethers.Contract
private signer: ethers.Signer
constructor(signer: ethers.Signer) {
this.signer = signer
this.factory = new ethers.Contract(
TOKEN_FACTORY_ADDRESS,
FACTORY_ABI,
signer
)
}
async createToken(metadata: TokenMetadata, referral = "") {
const tx = await this.factory.createToken(
metadata,
referral,
{ value: ethers.utils.parseEther("0.001") }
)
const receipt = await tx.wait()
const tokenAddress = receipt.events[0].args.token
return tokenAddress
}
async buyTokens(
tokenAddress: string,
ethAmount: string,
referral = ""
) {
const quote = await this.factory.getQuoteBuy(
tokenAddress,
ethers.utils.parseEther(ethAmount)
)
if (quote.eq(0)) {
throw new Error("Token graduated or invalid")
}
const tx = await this.factory.buy(
tokenAddress,
referral,
{ value: ethers.utils.parseEther(ethAmount) }
)
return await tx.wait()
}
async sellTokens(
tokenAddress: string,
tokenAmount: string,
referral = ""
) {
const token = new ethers.Contract(
tokenAddress,
ERC20_ABI,
this.signer
)
const amount = ethers.utils.parseEther(tokenAmount)
const allowance = await token.allowance(
await this.signer.getAddress(),
TOKEN_FACTORY_ADDRESS
)
if (allowance.lt(amount)) {
const approveTx = await token.approve(
TOKEN_FACTORY_ADDRESS,
ethers.constants.MaxUint256
)
await approveTx.wait()
}
const tx = await this.factory.sell(
tokenAddress,
amount,
referral
)
return await tx.wait()
}
}
async function safeTransaction(fn: () => Promise<any>) {
try {
return await fn()
} catch (error: any) {
if (error.code === 'ACTION_REJECTED') {
console.log("User rejected transaction")
return null
}
if (error.reason) {
switch(error.reason) {
case "InsufficientETH":
alert("Not enough ETH for this transaction")
break
case "TokenNotFunding":
alert("This token has already graduated")
break
case "InvalidAmount":
alert("Invalid amount specified")
break
default:
alert(`Transaction failed: ${error.reason}`)
}
return null
}
console.error("Unknown error:", error)
throw error
}
}
const result = await safeTransaction(async () => {
return await factory.buy(tokenAddress, "", { value: amount })
})
Batch Reading
const multicall = new ethers.Contract(MULTICALL_ADDRESS, MULTICALL_ABI, provider)
const calls = tokens.map(token => ({
target: TOKEN_FACTORY_ADDRESS,
callData: factory.interface.encodeFunctionData('getQuoteBuy', [token, amount])
}))
const results = await multicall.aggregate(calls)
Gas Estimation
async function estimateGas(tokenAddress: string, ethAmount: string) {
const gasEstimate = await factory.estimateGas.buy(
tokenAddress,
"",
{ value: ethers.utils.parseEther(ethAmount) }
)
const gasPrice = await provider.getGasPrice()
const gasCost = gasEstimate.mul(gasPrice)
console.log(`Estimated gas: ${ethers.utils.formatEther(gasCost)} ETH`)
return gasCost
}
function listenToEvents(tokenAddress: string) {
const filter = factory.filters.TokenPriceUpdate(tokenAddress)
factory.on(filter, (
token,
pair,
txType,
priceData,
txData,
referral
) => {
console.log("Price update:", {
token,
type: txType,
price: ethers.utils.formatEther(priceData.price),
trader: txData.trader,
tokenAmount: ethers.utils.formatEther(txData.tokenAmount),
ethAmount: ethers.utils.formatEther(txData.ethAmount)
})
})
return () => factory.removeAllListeners(filter)
}