Skip to content

Commit

Permalink
feat(billboard): add slack notifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Mar 5, 2024
1 parent 592955e commit 76f6a56
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 27 deletions.
56 changes: 43 additions & 13 deletions handlers/clear-auction-and-distribute-tax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
walletClient,
} from '../lib/billboard'
import { SimulateContractErrorType } from 'viem'
import { Slack } from '../lib/utils/slack.js'

export const handler = async (
event: APIGatewayEvent & {
Expand All @@ -23,6 +24,7 @@ export const handler = async (
console.log(`Event: ${JSON.stringify(event, null, 2)}`)
console.log(`Context: ${JSON.stringify(context, null, 2)}`)

const slack = new Slack()
const fromTokenId = BigInt(event.fromTokenId) || BigInt(0)
const toTokenId = BigInt(event.toTokenId) || BigInt(0)
const fromBlock = BigInt(event.fromBlock) || BigInt(0)
Expand All @@ -39,9 +41,7 @@ export const handler = async (
if (isInvalidTokenIds || isInvalidBlockNumbers || isInvalidAmount) {
return {
statusCode: 400,
body: JSON.stringify({
error: 'invalid params',
}),
body: JSON.stringify({ error: 'invalid params' }),
}
}

Expand All @@ -51,30 +51,59 @@ export const handler = async (
if (!auctions || auctions.length <= 0) {
return {
statusCode: 200,
body: JSON.stringify({
message: 'no auction to clear.',
}),
body: JSON.stringify({ message: 'no auction to clear.' }),
}
}

// Step 1: clear auctions
try {
// Step 1: clear auctions
const clearAuctionsResult = await publicClient.simulateContract({
...billboardContract,
functionName: 'clearAuctions',
args: [auctions.map(({ tokenId }) => tokenId)],
})
await walletClient.writeContract(clearAuctionsResult.request)
} catch (err) {
const error = err as SimulateContractErrorType
console.error(error.name, err)

slack.sendStripeAlert({
data: { event, errorName: error.name },
message: 'Failed to clear auctions.',
})

return {
statusCode: 500,
body: JSON.stringify({ message: error.name }),
}
}

let tax: bigint
try {
// Step 2: withdraw tax
const withdrawTaxResult = await publicClient.simulateContract({
...billboardContract,
functionName: 'withdrawTax',
})
await walletClient.writeContract(withdrawTaxResult.request)
const tax = withdrawTaxResult.result
tax = withdrawTaxResult.result
} catch (err) {
const error = err as SimulateContractErrorType
console.error(error.name, err)

slack.sendStripeAlert({
data: { event, errorName: error.name },
message: 'Failed to withdraw tax.',
})

return {
statusCode: 500,
body: JSON.stringify({ message: error.name }),
}
}

// Step 3: create new drop with merkle root and tax
// Step 3: create new drop with merkle root and tax
try {
const dropResult = await publicClient.simulateContract({
...distributionContract,
functionName: 'drop',
Expand All @@ -98,13 +127,14 @@ export const handler = async (
const error = err as SimulateContractErrorType
console.error(error.name, err)

// TODO: error handling
slack.sendStripeAlert({
data: { event, errorName: error.name },
message: 'Failed to create new drop.',
})

return {
statusCode: 500,
body: JSON.stringify({
message: error.name,
}),
body: JSON.stringify({ message: error.name }),
}
}
}
36 changes: 23 additions & 13 deletions lib/billboard/client.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import { createPublicClient, createWalletClient, http } from "viem";
import { polygon, polygonMumbai } from "viem/chains";
import { privateKeyToAccount } from "viem/accounts";
import { createPublicClient, createWalletClient, http } from 'viem'
import { optimism, optimismSepolia, polygon, polygonMumbai } from 'viem/chains'
import { privateKeyToAccount } from 'viem/accounts'

export const chains = {
"polygon-mainnet": polygon,
"polygon-mumbai": polygonMumbai,
};
'polygon-mainnet': polygon,
'polygon-mumbai': polygonMumbai,
'op-sepolia': optimismSepolia,
'op-mainnet': optimism,
}

export const network = process.env.NETWORK as keyof typeof chains;
export const network = process.env.NETWORK as keyof typeof chains

const alchemyAPIKey = process.env.ALCHEMY_API_KEY
export const rpcs: { [chain in keyof typeof chains]: string } = {
'polygon-mainnet': `https://polygon-mainnet.g.alchemy.com/v2/${alchemyAPIKey}`,
'polygon-mumbai': `https://polygon-mumbai.g.alchemy.com/v2/${alchemyAPIKey}`,
'op-sepolia': `https://opt-sepolia.g.alchemy.com/v2/${alchemyAPIKey}`,
'op-mainnet': `https://opt-mainnet.g.alchemy.com/v2/${alchemyAPIKey}`,
}

export const account = privateKeyToAccount(
process.env.ACCOUNT_PRIVATE_KEY as `0x${string}`,
);
process.env.ACCOUNT_PRIVATE_KEY as `0x${string}`
)

export const publicClient = createPublicClient({
chain: chains[network],
transport: http(process.env.ALCHEMY_API_URL),
transport: http(rpcs[network]),
cacheTime: 0,
});
})

export const walletClient = createWalletClient({
chain: chains[network],
transport: http(process.env.ALCHEMY_API_URL),
transport: http(rpcs[network]),
cacheTime: 0,
});
})
2 changes: 1 addition & 1 deletion lib/billboard/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const billboardRegsitryContract = {
} as const

export const distributionContract = {
address: process.env.DISTRIBUTION_CONTRACT_ADDRESS as `0x${string}`,
address: process.env.BILLBOARD_DISTRIBUTION_CONTRACT_ADDRESS as `0x${string}`,
abi: distributionAbi,
account,
} as const
Expand Down

0 comments on commit 76f6a56

Please sign in to comment.