|
| 1 | +import { Address, createPublicClient, http, encodeFunctionData } from 'viem' |
| 2 | +import { celo } from 'viem/chains' |
| 3 | +import { ShortcutsHook } from '../../types/shortcuts' |
| 4 | +import { getHedgeyPlanNfts } from './nfts' |
| 5 | +import { tokenVestingPlansAbi } from './abis/token-vesting-plans' |
| 6 | + |
| 7 | +const client = createPublicClient({ |
| 8 | + chain: celo, |
| 9 | + transport: http(), |
| 10 | +}) |
| 11 | + |
| 12 | +const hook: ShortcutsHook = { |
| 13 | + async getShortcutDefinitions(network?: string, address?: string) { |
| 14 | + if (network !== 'celo' || !address) { |
| 15 | + return [] |
| 16 | + } |
| 17 | + |
| 18 | + const planNfts = await getHedgeyPlanNfts({ address }) |
| 19 | + |
| 20 | + return planNfts.map((planNft) => ({ |
| 21 | + id: `${planNft.contractAddress}:${planNft.tokenId}`, |
| 22 | + name: 'Claim', |
| 23 | + description: 'Claim vested rewards', |
| 24 | + networks: ['celo'], |
| 25 | + category: 'claim', |
| 26 | + async onTrigger(network, address, positionAddress) { |
| 27 | + // positionAddress === planNft.contractAddress |
| 28 | + const { request } = await client.simulateContract({ |
| 29 | + address: positionAddress as Address, |
| 30 | + abi: tokenVestingPlansAbi, |
| 31 | + functionName: 'redeemPlans', |
| 32 | + args: [[BigInt(planNft.tokenId)]], |
| 33 | + account: address as Address, |
| 34 | + }) |
| 35 | + |
| 36 | + const data = encodeFunctionData({ |
| 37 | + abi: request.abi, |
| 38 | + args: request.args, |
| 39 | + functionName: request.functionName, |
| 40 | + }) |
| 41 | + |
| 42 | + return [ |
| 43 | + { |
| 44 | + network, |
| 45 | + from: address, |
| 46 | + to: positionAddress as Address, |
| 47 | + data, |
| 48 | + }, |
| 49 | + ] |
| 50 | + }, |
| 51 | + })) |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +export default hook |
0 commit comments