Skip to content

Commit 4ce709c

Browse files
committed
fix: proper decoding for ServiceProviderRegistered
Signed-off-by: Tomás Migone <[email protected]>
1 parent 7c58a06 commit 4ce709c

File tree

6 files changed

+110
-6
lines changed

6 files changed

+110
-6
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as fs from 'fs'
2+
import * as mustache from 'mustache'
3+
// Replace with proper imports once the packages are published
4+
import * as networkAddresses from '/opt/contracts.json'
5+
import { Addresses } from './addresses.template'
6+
7+
// mustache doesn't like numbered object keys
8+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9+
let renameAddresses: any = networkAddresses
10+
renameAddresses['localnetwork'] = networkAddresses['1337']
11+
12+
export let addresses: Addresses = {
13+
controller: '{{localnetwork.Controller.address}}',
14+
graphToken: '{{localnetwork.L2GraphToken.address}}',
15+
epochManager: '{{localnetwork.EpochManager.address}}',
16+
disputeManager: '{{localnetwork.DisputeManager.address}}',
17+
staking: '{{localnetwork.HorizonStaking.address}}',
18+
stakingExtension: '{{localnetwork.HorizonStaking.address}}',
19+
curation: '{{localnetwork.L2Curation.address}}',
20+
rewardsManager: '{{localnetwork.RewardsManager.address}}',
21+
serviceRegistry: '0x0000000000000000000000000000000000000000',
22+
gns: '{{localnetwork.L2GNS.address}}',
23+
ens: '{{localnetwork.IENS.address}}',
24+
ensPublicResolver: '{{localnetwork.IPublicResolver.address}}',
25+
blockNumber: '',
26+
bridgeBlockNumber: '',
27+
network: '',
28+
tokenLockManager: '',
29+
subgraphNFT: '{{localnetwork.SubgraphNFT.address}}',
30+
l1GraphTokenGateway: '',
31+
l2GraphTokenGateway: '{{localnetwork.L2GraphTokenGateway.address}}',
32+
ethereumDIDRegistry: '{{localnetwork.EthereumDIDRegistry.address}}',
33+
subgraphService: '{{localnetwork.SubgraphService.address}}',
34+
graphPayments: '{{localnetwork.GraphPayments.address}}',
35+
isL1: false,
36+
}
37+
38+
const main = (): void => {
39+
try {
40+
let output = JSON.parse(mustache.render(JSON.stringify(addresses), renameAddresses))
41+
output.blockNumber = '1'
42+
output.bridgeBlockNumber = '1'
43+
output.network = 'hardhat'
44+
output.useTokenLockManager = false
45+
if(output.ens == '') {
46+
output.ens = '0x0000000000000000000000000000000000000000' // to avoid crashes due to bad config
47+
}
48+
if(output.ethereumDIDRegistry == '') {
49+
output.ethereumDIDRegistry = '0x0000000000000000000000000000000000000000' // to avoid crashes due to bad config
50+
}
51+
// remove once we have proper packages
52+
if(output.subgraphService == '') {
53+
output.subgraphService = '0x0000000000000000000000000000000000000000' // to avoid crashes due to bad config
54+
}
55+
if(output.graphPayments == '') {
56+
output.graphPayments = '0x0000000000000000000000000000000000000000' // to avoid crashes due to bad config
57+
}
58+
fs.writeFileSync(__dirname + '/generatedAddresses.json', JSON.stringify(output, null, 2))
59+
} catch (e) {
60+
console.log(`Error saving artifacts: ${e.message}`)
61+
}
62+
}
63+
64+
main()

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"devDependencies": {
4444
"@graphprotocol/contracts": "6.2.0",
4545
"@graphprotocol/graph-cli": "0.68.5",
46-
"@graphprotocol/graph-ts": "0.32.0",
46+
"@graphprotocol/graph-ts": "0.38.0",
4747
"@types/node": "^14.0.13",
4848
"@typescript-eslint/eslint-plugin": "^3.3.0",
4949
"@typescript-eslint/parser": "^3.3.0",
@@ -57,6 +57,5 @@
5757
"bugs": {
5858
"url": "https://github.com/graphprotocol/graph-network-subgraph/issues"
5959
},
60-
"homepage": "https://github.com/graphprotocol/graph-network-subgraph#readme",
61-
"dependencies": {}
60+
"homepage": "https://github.com/graphprotocol/graph-network-subgraph#readme"
6261
}

schema.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,8 @@ type Indexer @entity {
693693
url: String
694694
"Geohash of the indexer. Shows where their indexer is located in the world"
695695
geoHash: String
696+
"Address that receives the rewards from the indexer"
697+
rewardsDestination: Bytes
696698
"Default display name is the current default name. Used for filtered queries"
697699
defaultDisplayName: String
698700

src/mappings/helpers/decoder.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ByteArray, Bytes } from '@graphprotocol/graph-ts';
2+
3+
4+
// Wrap arguments with this function if (and only if) one of the arguments is an array or 'bytes' or 'string' (i.e. variable length arg)
5+
// See:
6+
// - https://medium.com/@r2d2_68242/indexing-transaction-input-data-in-a-subgraph-6ff5c55abf20
7+
// - https://ethereum.stackexchange.com/questions/114582/the-graph-nodes-cant-decode-abi-encoded-data-containing-arrays
8+
// - https://github.com/enzymefinance/subgraphs/blob/main/packages/utils/utils/decode.ts
9+
export function tuplePrefixBytes(input: Bytes): Bytes {
10+
let inputTypedArray = input.subarray(0);
11+
12+
let tuplePrefix = ByteArray.fromHexString('0x0000000000000000000000000000000000000000000000000000000000000020');
13+
14+
let inputAsTuple = new Uint8Array(tuplePrefix.length + inputTypedArray.length);
15+
16+
inputAsTuple.set(tuplePrefix, 0);
17+
inputAsTuple.set(inputTypedArray, tuplePrefix.length);
18+
19+
return Bytes.fromUint8Array(inputAsTuple);
20+
}

src/mappings/helpers/helpers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ export function createOrLoadIndexer(indexerAddress: Bytes, timestamp: BigInt): I
178178
indexer.annualizedReturn = BigDecimal.fromString('0')
179179
indexer.stakingEfficiency = BigDecimal.fromString('0')
180180

181+
indexer.url = ''
182+
indexer.geoHash = ''
183+
indexer.rewardsDestination = Address.fromString('0x0000000000000000000000000000000000000000')
184+
181185
let graphAccount = createOrLoadGraphAccount(indexerAddress, timestamp)
182186
graphAccount.indexer = id
183187
graphAccount.save()

src/mappings/subgraphService.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,30 @@ import { AllocationClosed, AllocationCreated, AllocationResized, DelegationRatio
33
import { batchUpdateSubgraphSignalledTokens, calculatePricePerShare, createOrLoadDataService, createOrLoadEpoch, createOrLoadGraphNetwork, createOrLoadIndexerQueryFeePaymentAggregation, createOrLoadPaymentSource, createOrLoadProvision, createOrLoadSubgraphDeployment, joinID, updateDelegationExchangeRate } from "./helpers/helpers"
44
import { Allocation, GraphAccount, Indexer, PoiSubmission, SubgraphDeployment } from "../types/schema"
55
import { addresses } from "../../config/addresses"
6+
import { tuplePrefixBytes } from "./helpers/decoder"
7+
import { createOrLoadIndexer } from "./helpers/helpers"
68

79
export function handleServiceProviderRegistered(event: ServiceProviderRegistered): void {
8-
let decodedCalldata = ethereum.decode('(string,string,address)', event.params.data)
10+
let decodedCalldata = ethereum.decode('(string,string,address)', tuplePrefixBytes(event.params.data))
911
if (decodedCalldata != null && decodedCalldata.kind == ethereum.ValueKind.TUPLE) {
1012
let tupleData = decodedCalldata.toTuple()
13+
let url = tupleData[0].toString()
14+
let geoHash = tupleData[1].toString()
15+
let rewardsDestination = tupleData[2].toAddress()
16+
17+
// Update provision
1118
let provision = createOrLoadProvision(event.params.serviceProvider, event.address, event.block.timestamp)
12-
provision.url = tupleData[0].toString()
13-
provision.geoHash = tupleData[1].toString()
19+
provision.url = url
20+
provision.geoHash = geoHash
21+
provision.rewardsDestination = rewardsDestination
1422
provision.save()
23+
24+
// Update indexer
25+
let indexer = createOrLoadIndexer(event.params.serviceProvider, event.block.timestamp)
26+
indexer.url = url
27+
indexer.geoHash = geoHash
28+
indexer.rewardsDestination = rewardsDestination
29+
indexer.save()
1530
} else {
1631
log.warning("ServiceProviderRegistered failed to decode: {}", [event.params.data.toHexString()])
1732
}

0 commit comments

Comments
 (0)