-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-mint.ts
More file actions
62 lines (53 loc) · 2.02 KB
/
verify-mint.ts
File metadata and controls
62 lines (53 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Verify if a mint transaction actually succeeded
* Checks NFT balance or Transfer events to confirm mint
*/
import 'dotenv/config'
import { createPublicClient, http } from 'viem'
import { loadConfig, beraChain, ERC721_ABI } from './config.js'
import { privateKeyToAccount } from 'viem/accounts'
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
const TRANSFER_TOPIC = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
export async function verifyMintSuccess(
txHash: string,
walletAddress: string,
expectedTokenId?: bigint
): Promise<{ success: boolean; mintedTokenId?: bigint; error?: string }> {
const cfg = loadConfig()
const client = createPublicClient({
chain: beraChain,
transport: http(cfg.rpcs[0], { timeout: 10000 }),
})
try {
// Wait for transaction receipt
const receipt = await client.waitForTransactionReceipt({ hash: txHash as `0x${string}` })
if (receipt.status !== 'success') {
return { success: false, error: 'Transaction reverted' }
}
const walletLower = walletAddress.toLowerCase()
const miTeddyLower = cfg.sacrificeAddr.toLowerCase()
for (const log of receipt.logs) {
if (log.address.toLowerCase() !== miTeddyLower) {
continue
}
if (!log.topics || log.topics[0]?.toLowerCase() !== TRANSFER_TOPIC) {
continue
}
const fromTopic = log.topics[1]
const toTopic = log.topics[2]
const tokenTopic = log.topics[3]
if (!fromTopic || !toTopic || !tokenTopic) {
continue
}
const fromAddress = `0x${fromTopic.slice(-40)}`.toLowerCase()
const toAddress = `0x${toTopic.slice(-40)}`.toLowerCase()
if (fromAddress === ZERO_ADDRESS && toAddress === walletLower) {
const tokenId = BigInt(tokenTopic)
return { success: true, mintedTokenId: tokenId }
}
}
return { success: false, error: 'No MiTeddy mint Transfer found in receipt' }
} catch (error: any) {
return { success: false, error: error.message || String(error) }
}
}