|
| 1 | +import { BytesLike, HDNodeWallet, Interface } from 'ethers' |
| 2 | +import { HardhatEthersSigner } from '@nomicfoundation/hardhat-ethers/signers' |
| 3 | + |
| 4 | +import { ISubgraphService } from '@graphprotocol/subgraph-service' |
| 5 | + |
| 6 | +import { PaymentTypes } from '../../horizon/utils/types' |
| 7 | + |
| 8 | +/* ////////////////////////////////////////////////////////////// |
| 9 | + EXPORTS |
| 10 | +////////////////////////////////////////////////////////////// */ |
| 11 | + |
| 12 | +export const SubgraphServiceActions = { |
| 13 | + collect, |
| 14 | + migrateLegacyAllocation, |
| 15 | + register, |
| 16 | + startService, |
| 17 | +} |
| 18 | + |
| 19 | +/* ////////////////////////////////////////////////////////////// |
| 20 | + REGISTRATION |
| 21 | +////////////////////////////////////////////////////////////// */ |
| 22 | + |
| 23 | +interface RegisterParams { |
| 24 | + subgraphService: ISubgraphService |
| 25 | + indexer: HardhatEthersSigner |
| 26 | + data: BytesLike |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Registers an indexer with the subgraph service |
| 31 | + * @param subgraphService The subgraph service contract |
| 32 | + * @param indexer The indexer that is registering |
| 33 | + * @param data The encoded registration data |
| 34 | + */ |
| 35 | +export async function register({ |
| 36 | + subgraphService, |
| 37 | + indexer, |
| 38 | + data, |
| 39 | +}: RegisterParams) { |
| 40 | + const tx = await subgraphService.connect(indexer).register(indexer.address, data) |
| 41 | + await tx.wait() |
| 42 | +} |
| 43 | + |
| 44 | +/* ////////////////////////////////////////////////////////////// |
| 45 | + ALLOCATION MANAGEMENT |
| 46 | +////////////////////////////////////////////////////////////// */ |
| 47 | + |
| 48 | +interface MigrateLegacyAllocationParams { |
| 49 | + subgraphService: ISubgraphService |
| 50 | + governor: HardhatEthersSigner |
| 51 | + indexer: string |
| 52 | + allocationId: string |
| 53 | + subgraphDeploymentId: string |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Migrates a legacy allocation from the old staking contract to the new subgraph service |
| 58 | + * @param subgraphService The subgraph service contract |
| 59 | + * @param governor The governor of the subgraph service |
| 60 | + * @param indexer The indexer owner of the legacy allocation |
| 61 | + * @param allocationId The allocation id of the legacy allocation |
| 62 | + * @param subgraphDeploymentId The subgraph deployment id for the legacy allocation |
| 63 | + */ |
| 64 | +export async function migrateLegacyAllocation({ |
| 65 | + subgraphService, |
| 66 | + governor, |
| 67 | + indexer, |
| 68 | + allocationId, |
| 69 | + subgraphDeploymentId, |
| 70 | +}: MigrateLegacyAllocationParams) { |
| 71 | + const tx = await subgraphService.connect(governor).migrateLegacyAllocation(indexer, allocationId, subgraphDeploymentId) |
| 72 | + await tx.wait() |
| 73 | +} |
| 74 | + |
| 75 | +interface StartServiceParams { |
| 76 | + subgraphService: ISubgraphService |
| 77 | + indexer: HardhatEthersSigner |
| 78 | + data: BytesLike |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Service provider starts providing service for a subgraph deployment |
| 83 | + * @param subgraphService The subgraph service contract |
| 84 | + * @param indexer The indexer that is starting the service |
| 85 | + * @param data The encoded data for the allocation |
| 86 | + */ |
| 87 | +export async function startService({ |
| 88 | + subgraphService, |
| 89 | + indexer, |
| 90 | + data, |
| 91 | +}: StartServiceParams) { |
| 92 | + const tx = await subgraphService.connect(indexer).startService(indexer.address, data) |
| 93 | + await tx.wait() |
| 94 | +} |
| 95 | + |
| 96 | +/* ////////////////////////////////////////////////////////////// |
| 97 | + COLLECT |
| 98 | +////////////////////////////////////////////////////////////// */ |
| 99 | + |
| 100 | +interface CollectParams { |
| 101 | + subgraphService: ISubgraphService |
| 102 | + signer: HardhatEthersSigner | HDNodeWallet |
| 103 | + indexer: string |
| 104 | + paymentType: PaymentTypes |
| 105 | + data: BytesLike |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Collects the allocated funds for a subgraph deployment |
| 110 | + * @param subgraphService The subgraph service contract |
| 111 | + * @param indexer The indexer that is collecting the funds |
| 112 | + * @param data The encoded data for the allocation |
| 113 | + * @returns The payment collected |
| 114 | + */ |
| 115 | +export async function collect({ subgraphService, signer, indexer, paymentType, data }: CollectParams): Promise<bigint> { |
| 116 | + const tx = await subgraphService.connect(signer).collect( |
| 117 | + indexer, |
| 118 | + paymentType, |
| 119 | + data |
| 120 | + ) |
| 121 | + const receipt = await tx.wait() |
| 122 | + if (!receipt) throw new Error('Transaction failed') |
| 123 | + |
| 124 | + const iface = new Interface(['event ServicePaymentCollected(address indexed serviceProvider, uint8 indexed feeType, uint256 tokens)']) |
| 125 | + const event = receipt.logs.find(log => log.topics[0] === iface.getEvent('ServicePaymentCollected')?.topicHash) |
| 126 | + if (!event) throw new Error('ServicePaymentCollected event not found') |
| 127 | + |
| 128 | + return BigInt(event.data) |
| 129 | +} |
0 commit comments