Skip to content

Commit 88b0527

Browse files
committed
feat: add legacy allo proof encoding
Signed-off-by: Tomás Migone <[email protected]>
1 parent ad5e7f4 commit 88b0527

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed

packages/horizon/tasks/test/seed.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { generateAllocationProof, randomAllocationMetadata } from '@graphprotocol/toolshed'
1+
import { encodeLegacyAllocationProof, randomAllocationMetadata } from '@graphprotocol/toolshed'
22
import { requireLocalNetwork, setGRTBalance } from '@graphprotocol/toolshed/hardhat'
33
import { delegators } from './fixtures/delegators'
44
import { indexers } from './fixtures/indexers'
@@ -73,7 +73,7 @@ task('test:seed', 'Sets up some protocol state for testing')
7373
allocation.tokens,
7474
allocation.allocationID,
7575
randomAllocationMetadata(),
76-
await generateAllocationProof(indexer.address, allocation.allocationPrivateKey),
76+
await encodeLegacyAllocationProof(indexer.address, allocation.allocationPrivateKey),
7777
)
7878
}
7979
}

packages/toolshed/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The package is organized into several modules that can be imported separately:
2121

2222
```typescript
2323
// Import core functionality
24-
import { generateAllocationProof } from '@graphprotocol/toolshed';
24+
import { encodeAllocationProof } from '@graphprotocol/toolshed';
2525

2626
// Import deployment
2727
import { loadGraphHorizon } from '@graphprotocol/toolshed/deployments/horizon';

packages/toolshed/src/core/accounts.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ export async function getTestAccounts(provider: HardhatEthersProvider, grtTokenA
8686
async function _getAccount(provider: HardhatEthersProvider, accountIndex: number | string, grtTokenAddress?: string | Addressable) {
8787
const account = await provider.getSigner(accountIndex)
8888

89-
// If the chain is local, send 10M GRT to the account
90-
const chainId = await provider.send('eth_chainId', []) as string
91-
const isLocal = [toBeHex(1337), toBeHex(31337)].includes(toBeHex(BigInt(chainId)))
92-
if (grtTokenAddress && isLocal) {
93-
await setGRTBalance(provider, grtTokenAddress, account.address, TEN_MILLION)
89+
// If the chain is local, set balance to 10M GRT
90+
if (grtTokenAddress) {
91+
const chainId = await provider.send('eth_chainId', []) as string
92+
const isLocal = [toBeHex(1337), toBeHex(31337)].includes(toBeHex(BigInt(chainId)))
93+
if (isLocal) {
94+
await setGRTBalance(provider, grtTokenAddress, account.address, TEN_MILLION)
95+
}
9496
}
9597

9698
return account
+34-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { ethers } from 'ethers'
1+
import { ethers, id } from 'ethers'
22
import { randomHexBytes } from '../lib/bytes'
33

4-
// Generate allocation proof with the indexer's address and the allocation id, signed by the allocation private key
5-
export async function generateAllocationProof(indexerAddress: string, allocationPrivateKey: string) {
4+
// For legacy allocations in the staking contract
5+
export async function encodeLegacyAllocationProof(indexerAddress: string, allocationPrivateKey: string) {
66
const wallet = new ethers.Wallet(allocationPrivateKey)
77
const messageHash = ethers.solidityPackedKeccak256(
88
['address', 'address'],
@@ -12,6 +12,37 @@ export async function generateAllocationProof(indexerAddress: string, allocation
1212
return wallet.signMessage(messageHashBytes)
1313
}
1414

15+
export const EIP712_ALLOCATION_PROOF_TYPEHASH = id('AllocationIdProof(address indexer,address allocationId)')
16+
17+
export const EIP712_ALLOCATION_ID_PROOF_TYPES = {
18+
AllocationIdProof: [
19+
{ name: 'indexer', type: 'address' },
20+
{ name: 'allocationId', type: 'address' },
21+
],
22+
}
23+
24+
// For new allocations in the subgraph service
25+
export async function encodeAllocationProof(
26+
indexerAddress: string,
27+
allocationPrivateKey: string,
28+
subgraphServiceAddress: string,
29+
chainId: number,
30+
) {
31+
const wallet = new ethers.Wallet(allocationPrivateKey)
32+
33+
const domain = {
34+
name: 'SubgraphService',
35+
version: '1.0',
36+
chainId: chainId,
37+
verifyingContract: subgraphServiceAddress,
38+
}
39+
40+
return wallet.signTypedData(domain, EIP712_ALLOCATION_ID_PROOF_TYPES, {
41+
indexer: indexerAddress,
42+
allocationId: wallet.address,
43+
})
44+
}
45+
1546
export function randomAllocationMetadata() {
1647
return randomHexBytes(32)
1748
}
+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { HardhatRuntimeEnvironment } from 'hardhat/types'
1+
import type { HardhatEthersProvider } from '@nomicfoundation/hardhat-ethers/internal/hardhat-ethers-provider'
2+
import type { HardhatRuntimeEnvironment } from 'hardhat/types'
23

34
const localNetworks = ['localhost', 'hardhat', 'localNetwork']
45

@@ -7,3 +8,12 @@ export function requireLocalNetwork(hre: HardhatRuntimeEnvironment) {
78
throw new Error(`Network ${hre.network.name} is not a local network.`)
89
}
910
}
11+
12+
export async function warp(provider: HardhatEthersProvider, seconds: number) {
13+
await provider.send('evm_increaseTime', [seconds])
14+
await provider.send('evm_mine', [])
15+
}
16+
17+
export async function mine(provider: HardhatEthersProvider, blocks: number) {
18+
await provider.send('evm_mine', [blocks])
19+
}

0 commit comments

Comments
 (0)