Skip to content

Commit

Permalink
refactor: getBadgesSignerAndContract function
Browse files Browse the repository at this point in the history
  • Loading branch information
1emu committed Aug 31, 2023
1 parent 31c4d99 commit 386c183
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/back/utils/contractInteractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ function checksumAddresses(addresses: string[]): string[] {
return addresses.map((address) => ethers.utils.getAddress(address))
}

function getBadgesSignerAndContract() {
const provider = RpcService.getPolygonProvider()
const signer = new ethers.Wallet(RAFT_OWNER_PK, provider)
const contract = new ethers.Contract(POLYGON_BADGES_CONTRACT_ADDRESS, BadgesAbi, signer)
return { signer, contract }
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function estimateGas(estimateFunction: (...args: any[]) => Promise<any>): Promise<GasConfig> {
const provider = RpcService.getPolygonProvider()
Expand All @@ -26,60 +33,53 @@ export async function estimateGas(estimateFunction: (...args: any[]) => Promise<
}

export async function airdrop(badgeCid: string, recipients: string[], shouldPumpGas = false) {
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 { signer, contract } = getBadgesSignerAndContract()
const ipfsAddress = `ipfs://${badgeCid}/metadata.json`
const formattedRecipients = checksumAddresses(recipients)
logger.log(`Airdropping, pumping gas ${shouldPumpGas}`)
let txn
if (shouldPumpGas) {
const gasConfig = await estimateGas(async () => contract.estimateGas.airdrop(formattedRecipients, ipfsAddress))
txn = await contract.connect(raftOwner).airdrop(formattedRecipients, ipfsAddress, gasConfig)
txn = await contract.connect(signer).airdrop(formattedRecipients, ipfsAddress, gasConfig)
} else {
txn = await contract.connect(raftOwner).airdrop(formattedRecipients, ipfsAddress)
txn = await contract.connect(signer).airdrop(formattedRecipients, ipfsAddress)
}
await txn.wait()
logger.log('Airdropped badge with txn hash:', txn.hash)
return txn.hash
}

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 { signer, contract } = getBadgesSignerAndContract()
const gasConfig = await estimateGas(async () => {
return contract.estimateGas.reinstateBadge(TRIMMED_OTTERSPACE_RAFT_ID, badgeId)
})

const txn = await contract.connect(raftOwner).reinstateBadge(TRIMMED_OTTERSPACE_RAFT_ID, badgeId, gasConfig)
const txn = await contract.connect(signer).reinstateBadge(TRIMMED_OTTERSPACE_RAFT_ID, badgeId, gasConfig)
await txn.wait()
logger.log('Reinstated badge with txn hash:', txn.hash)
return txn.hash
}

export async function revokeBadge(badgeId: string, reason: number) {
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 { signer, contract } = getBadgesSignerAndContract()

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

const txn = await contract.connect(raftOwner).revokeBadge(TRIMMED_OTTERSPACE_RAFT_ID, badgeId, reason, gasConfig)
const txn = await contract.connect(signer).revokeBadge(TRIMMED_OTTERSPACE_RAFT_ID, badgeId, reason, gasConfig)
await txn.wait()
logger.log('Revoked badge with txn hash:', txn.hash)
return txn.hash
}

export async function checkBalance() {
const provider = RpcService.getPolygonProvider()
const raftOwner = new ethers.Wallet(RAFT_OWNER_PK, provider)
const balance = await raftOwner.getBalance()
const { signer } = getBadgesSignerAndContract()
const balance = await signer.getBalance()
const balanceInEther = ethers.utils.formatEther(balance)
const balanceBigNumber = ethers.BigNumber.from(balance)
console.log(`Balance of ${raftOwner.address}: ${balanceInEther} ETH = ${balanceBigNumber}`)
console.log(`Balance of ${signer.address}: ${balanceInEther} ETH = ${balanceBigNumber}`)
}

export function trimOtterspaceId(rawId: string) {
Expand All @@ -95,16 +95,14 @@ export function getIpfsAddress(badgeCid: string) {
}

export async function createSpec(badgeCid: 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 { signer, contract } = getBadgesSignerAndContract()
const ipfsAddress = `ipfs://${badgeCid}/metadata.json`

const gasConfig = await estimateGas(async () => {
return contract.estimateGas.createSpec(ipfsAddress, TRIMMED_OTTERSPACE_RAFT_ID)
})

const txn = await contract.connect(raftOwner).createSpec(ipfsAddress, TRIMMED_OTTERSPACE_RAFT_ID, gasConfig)
const txn = await contract.connect(signer).createSpec(ipfsAddress, TRIMMED_OTTERSPACE_RAFT_ID, gasConfig)
await txn.wait()
logger.log('Create badge spec with txn hash:', txn.hash)
return txn.hash
Expand Down

0 comments on commit 386c183

Please sign in to comment.