Skip to content

Commit

Permalink
update event operations
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTxbi committed Dec 6, 2024
1 parent 08ad4e3 commit fb4c227
Showing 1 changed file with 123 additions and 1 deletion.
124 changes: 123 additions & 1 deletion locksmith/src/operations/eventOperations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SubgraphService } from '@unlock-protocol/unlock-js'
import { SubgraphService, Web3Service } from '@unlock-protocol/unlock-js'
import { networks } from '@unlock-protocol/networks'

import dayjs from '../config/dayjs'
import { kebabCase, defaultsDeep } from 'lodash'
Expand All @@ -13,6 +14,7 @@ import { saveCheckoutConfig } from './checkoutConfigOperations'
import { EventBodyType } from '../controllers/v2/eventsController'
import { Op } from 'sequelize'
import { removeProtectedAttributesFromObject } from '../utils/protectedAttributes'
import { ethers } from 'ethers'

interface AttributeProps {
value: string
Expand Down Expand Up @@ -313,3 +315,123 @@ export const getCheckedInAttendees = async (slug: string) => {
)
return keys.map((key) => key.owner)
}

/**
* Updates an event with pending lock deployment information
*/
export const updateEventPendingLock = async (
slug: string,
pendingLock: {
transaction: string
network: number
}
) => {
const event = await EventData.findOne({
where: { slug },
})

if (!event) {
throw new Error('Event not found')
}

await event.setPendingLock(pendingLock)
return event
}

/**
* Checks if a lock has been deployed from a transaction
* Returns the lock address if found
*/
export const getLockAddressFromTransaction = async (
transactionHash: string,
network: number
): Promise<string | null> => {
const web3Service = new Web3Service(networks)
const provider = web3Service.providerForNetwork(network)

try {
const receipt = await provider.getTransactionReceipt(transactionHash)

if (!receipt || receipt.status !== 1) {
return null
}

const unlock = new ethers.Contract(
networks[network].unlockAddress,
[
'event NewLock(address indexed lockOwner, address indexed newLockAddress)',
],
provider
)

const newLockEvent = receipt.logs
.map((log) => {
try {
return unlock.interface.parseLog(log)
} catch {
return null
}
})
.find((log) => log?.name === 'NewLock')

return newLockEvent ? newLockEvent.args.newLockAddress : null
} catch (error) {
console.error('Error getting lock address from transaction:', error)
return null
}
}

/**
* Updates an event with deployed lock information
*/
export const updateEventWithDeployedLock = async (
slug: string,
lockAddress: string,
checkoutConfig: any
) => {
const event = await EventData.findOne({
where: { slug },
})

if (!event) {
throw new Error('Event not found')
}

await event.update({
lockAddress,
checkoutConfigId: checkoutConfig.id,
pendingTransactionHash: null,
isPending: false,
})

return event
}

/**
* Gets all events with pending lock deployments
*/
export const getPendingLockDeployments = async () => {
return EventData.findAll({
where: {
pendingLockTransaction: {
[Op.not]: null,
},
},
})
}

export const createEventWithPendingLock = async (
eventData: any,
transactionHash: string,
network: number,
creator: string
) => {
const event = await EventData.create({
...eventData,
pendingTransactionHash: transactionHash,
network,
isPending: true,
createdBy: creator,
})
return event
}

0 comments on commit fb4c227

Please sign in to comment.