Skip to content

Commit

Permalink
chore: minor refactor to estimateGas
Browse files Browse the repository at this point in the history
  • Loading branch information
1emu committed Aug 28, 2023
1 parent a18892a commit 4edc832
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/entities/Badges/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Contract } from '@ethersproject/contracts'
import { abi as BadgesAbi } from '@otterspace-xyz/contracts/out/Badges.sol/Badges.json'
import logger from 'decentraland-gatsby/dist/entities/Development/logger'
import { ApiResponse } from 'decentraland-gatsby/dist/utils/api/types'
Expand All @@ -22,10 +21,8 @@ function checksumAddresses(addresses: string[]): string[] {
return addresses.map((address) => ethers.utils.getAddress(address))
}

export async function estimateGas(
contract: Contract,
estimateFunction: (...args: any[]) => Promise<any>
): Promise<GasConfig> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function estimateGas(estimateFunction: (...args: any[]) => Promise<any>): Promise<GasConfig> {
const provider = RpcService.getPolygonProvider()
const gasLimit = await estimateFunction()
const gasPrice = await provider.getGasPrice()
Expand All @@ -45,9 +42,7 @@ export async function airdrop(badgeCid: string, recipients: string[], pumpGas =
logger.log(`Airdropping, pumping gas ${pumpGas}`)
let txn
if (pumpGas) {
const gasConfig = await estimateGas(contract, async () =>
contract.estimateGas.airdrop(formattedRecipients, ipfsAddress)
)
const gasConfig = await estimateGas(async () => contract.estimateGas.airdrop(formattedRecipients, ipfsAddress))
txn = await contract.connect(raftOwner).airdrop(formattedRecipients, ipfsAddress, gasConfig)
} else {
txn = await contract.connect(raftOwner).airdrop(formattedRecipients, ipfsAddress)
Expand All @@ -60,7 +55,7 @@ export async function reinstateBadge(badgeId: string) {
const provider = RpcService.getPolygonProvider()
const raftOwner = new ethers.Wallet(RAFT_OWNER_PK, provider)
const contract = new ethers.Contract(POLYGON_BADGES_CONTRACT_ADDRESS, BadgesAbi, raftOwner)
const gasConfig = await estimateGas(contract, async () => {
const gasConfig = await estimateGas(async () => {
return contract.estimateGas.reinstateBadge(TRIMMED_OTTERSPACE_RAFT_ID, badgeId)
})

Expand All @@ -74,7 +69,7 @@ export async function revokeBadge(badgeId: string, reason: number) {
const raftOwner = new ethers.Wallet(RAFT_OWNER_PK, provider)
const contract = new ethers.Contract(POLYGON_BADGES_CONTRACT_ADDRESS, BadgesAbi, raftOwner)

const gasConfig = await estimateGas(contract, async () => {
const gasConfig = await estimateGas(async () => {
return contract.estimateGas.revokeBadge(TRIMMED_OTTERSPACE_RAFT_ID, badgeId, reason)
})

Expand All @@ -85,7 +80,7 @@ export async function revokeBadge(badgeId: string, reason: number) {

export async function checkBalance() {
const provider = RpcService.getPolygonProvider()
const raftOwner = await new ethers.Wallet(RAFT_OWNER_PK, provider)
const raftOwner = new ethers.Wallet(RAFT_OWNER_PK, provider)
const balance = await raftOwner.getBalance()
const balanceInEther = ethers.utils.formatEther(balance)
const balanceBigNumber = ethers.BigNumber.from(balance)
Expand Down

0 comments on commit 4edc832

Please sign in to comment.