From a8601ff21c5da99d5afada328e4fa0ef7c97ec98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20L=C3=B3pez?= Date: Thu, 19 Jun 2025 14:55:24 +0200 Subject: [PATCH 1/3] Improve handleCompleted to create InternalTransaction and reset escrow balance --- .../subgraph/src/mapping/EscrowTemplate.ts | 24 ++++++++--- .../subgraph/tests/escrow/escrow.test.ts | 40 +++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/packages/sdk/typescript/subgraph/src/mapping/EscrowTemplate.ts b/packages/sdk/typescript/subgraph/src/mapping/EscrowTemplate.ts index a95556a16b..9643952ce0 100644 --- a/packages/sdk/typescript/subgraph/src/mapping/EscrowTemplate.ts +++ b/packages/sdk/typescript/subgraph/src/mapping/EscrowTemplate.ts @@ -639,11 +639,7 @@ export function handleCompleted(event: Completed): void { // Update escrow entity const escrowEntity = Escrow.load(dataSource.address()); if (escrowEntity) { - escrowEntity.status = 'Complete'; - escrowEntity.save(); - eventEntity.launcher = escrowEntity.launcher; - - createTransaction( + const transaction = createTransaction( event, 'complete', event.transaction.from, @@ -651,6 +647,24 @@ export function handleCompleted(event: Completed): void { null, Address.fromBytes(escrowEntity.address) ); + if (escrowEntity.balance && escrowEntity.balance.gt(ZERO_BI)) { + const internalTransaction = new InternalTransaction( + event.transaction.hash.concatI32(event.logIndex.toI32()) + ); + internalTransaction.from = escrowEntity.address; + internalTransaction.to = escrowEntity.launcher; + internalTransaction.value = escrowEntity.balance; + internalTransaction.transaction = transaction.id; + internalTransaction.method = 'transfer'; + internalTransaction.escrow = escrowEntity.address; + internalTransaction.token = escrowEntity.token; + internalTransaction.save(); + + escrowEntity.balance = ZERO_BI; + } + escrowEntity.status = 'Complete'; + escrowEntity.save(); + eventEntity.launcher = escrowEntity.launcher; } eventEntity.save(); } diff --git a/packages/sdk/typescript/subgraph/tests/escrow/escrow.test.ts b/packages/sdk/typescript/subgraph/tests/escrow/escrow.test.ts index 52ffc384eb..303b448d44 100644 --- a/packages/sdk/typescript/subgraph/tests/escrow/escrow.test.ts +++ b/packages/sdk/typescript/subgraph/tests/escrow/escrow.test.ts @@ -1395,6 +1395,46 @@ describe('Escrow', () => { 'to', escrowAddressString ); + + // InternalTransaction + const internalTxId = newCompleted.transaction.hash + .concatI32(newCompleted.logIndex.toI32()) + .toHex(); + + assert.fieldEquals( + 'InternalTransaction', + internalTxId, + 'from', + escrowAddressString + ); + assert.fieldEquals( + 'InternalTransaction', + internalTxId, + 'to', + launcherAddressString + ); + assert.fieldEquals('InternalTransaction', internalTxId, 'value', '1234'); + assert.fieldEquals( + 'InternalTransaction', + internalTxId, + 'method', + 'transfer' + ); + assert.fieldEquals( + 'InternalTransaction', + internalTxId, + 'escrow', + escrowAddressString + ); + assert.fieldEquals( + 'InternalTransaction', + internalTxId, + 'transaction', + newCompleted.transaction.hash.toHex() + ); + + // Escrow balance should be 0 after completion + assert.fieldEquals('Escrow', escrowAddress.toHex(), 'balance', '0'); }); test('Should properly handle Withdraw event', () => { From a00ed5a431e999bfedb06bf3f41cd989077ee329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20L=C3=B3pez?= Date: Thu, 19 Jun 2025 16:12:10 +0200 Subject: [PATCH 2/3] Enhance handleCompleted to create InternalTransaction for non-HMT_ADDRESS escrows and update createCompletedEvent fixture to include timestamp --- .../subgraph/src/mapping/EscrowTemplate.ts | 10 +- .../subgraph/tests/escrow/escrow.test.ts | 117 ++++++++++++++++-- .../subgraph/tests/escrow/fixtures.ts | 10 +- 3 files changed, 125 insertions(+), 12 deletions(-) diff --git a/packages/sdk/typescript/subgraph/src/mapping/EscrowTemplate.ts b/packages/sdk/typescript/subgraph/src/mapping/EscrowTemplate.ts index 9643952ce0..cbfaeeaf3c 100644 --- a/packages/sdk/typescript/subgraph/src/mapping/EscrowTemplate.ts +++ b/packages/sdk/typescript/subgraph/src/mapping/EscrowTemplate.ts @@ -647,10 +647,12 @@ export function handleCompleted(event: Completed): void { null, Address.fromBytes(escrowEntity.address) ); - if (escrowEntity.balance && escrowEntity.balance.gt(ZERO_BI)) { - const internalTransaction = new InternalTransaction( - event.transaction.hash.concatI32(event.logIndex.toI32()) - ); + if ( + escrowEntity.balance && + escrowEntity.balance.gt(ZERO_BI) && + escrowEntity.token != HMT_ADDRESS + ) { + const internalTransaction = new InternalTransaction(toEventId(event)); internalTransaction.from = escrowEntity.address; internalTransaction.to = escrowEntity.launcher; internalTransaction.value = escrowEntity.balance; diff --git a/packages/sdk/typescript/subgraph/tests/escrow/escrow.test.ts b/packages/sdk/typescript/subgraph/tests/escrow/escrow.test.ts index 303b448d44..22e01f2143 100644 --- a/packages/sdk/typescript/subgraph/tests/escrow/escrow.test.ts +++ b/packages/sdk/typescript/subgraph/tests/escrow/escrow.test.ts @@ -93,7 +93,7 @@ describe('Escrow', () => { const escrow = new Escrow(escrowAddress); escrow.address = escrowAddress; - escrow.token = Address.zero(); + escrow.token = tokenAddress; escrow.factoryAddress = Address.zero(); escrow.launcher = launcherAddress; escrow.canceler = launcherAddress; @@ -1318,7 +1318,10 @@ describe('Escrow', () => { }); test('Should properly handle Completed event', () => { - const newCompleted = createCompletedEvent(operatorAddress); + const newCompleted = createCompletedEvent( + operatorAddress, + BigInt.fromI32(12) + ); handleCompleted(newCompleted); @@ -1397,9 +1400,103 @@ describe('Escrow', () => { ); // InternalTransaction - const internalTxId = newCompleted.transaction.hash - .concatI32(newCompleted.logIndex.toI32()) - .toHex(); + const internalTxId = toEventId(newCompleted).toHex(); + assert.notInStore('InternalTransaction', internalTxId); + + // Escrow balance should be 0 after completion + assert.fieldEquals('Escrow', escrowAddress.toHex(), 'balance', '0'); + }); + + test('Should properly handle Completed event and create InternalTransaction if escrow has balance', () => { + const escrow = Escrow.load(escrowAddress); + if (escrow) { + escrow.balance = BigInt.fromI32(1234); + escrow.save(); + } + + const newCompleted = createCompletedEvent( + operatorAddress, + BigInt.fromI32(13) + ); + + handleCompleted(newCompleted); + + const id = toEventId(newCompleted).toHex(); + + // EscrowStatusEvent + assert.fieldEquals( + 'EscrowStatusEvent', + id, + 'block', + newCompleted.block.number.toString() + ); + assert.fieldEquals( + 'EscrowStatusEvent', + id, + 'timestamp', + newCompleted.block.timestamp.toString() + ); + assert.fieldEquals( + 'EscrowStatusEvent', + id, + 'txHash', + newCompleted.transaction.hash.toHex() + ); + assert.fieldEquals( + 'EscrowStatusEvent', + id, + 'escrowAddress', + escrowAddressString + ); + assert.fieldEquals( + 'EscrowStatusEvent', + id, + 'sender', + operatorAddressString + ); + assert.fieldEquals('EscrowStatusEvent', id, 'status', 'Complete'); + assert.fieldEquals( + 'EscrowStatusEvent', + id, + 'launcher', + launcherAddressString + ); + + // Escrow + assert.fieldEquals('Escrow', escrowAddress.toHex(), 'status', 'Complete'); + assert.fieldEquals( + 'Transaction', + newCompleted.transaction.hash.toHex(), + 'txHash', + newCompleted.transaction.hash.toHex() + ); + assert.fieldEquals( + 'Transaction', + newCompleted.transaction.hash.toHex(), + 'method', + 'complete' + ); + assert.fieldEquals( + 'Transaction', + newCompleted.transaction.hash.toHex(), + 'block', + newCompleted.block.number.toString() + ); + assert.fieldEquals( + 'Transaction', + newCompleted.transaction.hash.toHex(), + 'from', + newCompleted.transaction.from.toHex() + ); + assert.fieldEquals( + 'Transaction', + newCompleted.transaction.hash.toHex(), + 'to', + escrowAddressString + ); + + // InternalTransaction + const internalTxId = toEventId(newCompleted).toHex(); assert.fieldEquals( 'InternalTransaction', @@ -1720,8 +1817,14 @@ describe('Escrow', () => { }); test('Should properly calculate completed event in statstics', () => { - const newCompleted1 = createCompletedEvent(operatorAddress); - const newCompleted2 = createCompletedEvent(operatorAddress); + const newCompleted1 = createCompletedEvent( + operatorAddress, + BigInt.fromI32(12) + ); + const newCompleted2 = createCompletedEvent( + operatorAddress, + BigInt.fromI32(13) + ); handleCompleted(newCompleted1); handleCompleted(newCompleted2); diff --git a/packages/sdk/typescript/subgraph/tests/escrow/fixtures.ts b/packages/sdk/typescript/subgraph/tests/escrow/fixtures.ts index 7546898636..dbc765a4a7 100644 --- a/packages/sdk/typescript/subgraph/tests/escrow/fixtures.ts +++ b/packages/sdk/typescript/subgraph/tests/escrow/fixtures.ts @@ -237,8 +237,16 @@ export function createCancelledEvent(sender: Address): Cancelled { return newCancelledEvent; } -export function createCompletedEvent(sender: Address): Completed { +export function createCompletedEvent( + sender: Address, + timestamp: BigInt +): Completed { const newCompletedEvent = changetype(newMockEvent()); + newCompletedEvent.transaction.hash = generateUniqueHash( + sender.toString(), + timestamp, + newCompletedEvent.transaction.nonce + ); newCompletedEvent.transaction.from = sender; From cbc9eda254e62f176d18a01615bf94972332eea8 Mon Sep 17 00:00:00 2001 From: portuu3 Date: Mon, 23 Jun 2025 16:59:59 +0200 Subject: [PATCH 3/3] deploy subgraphs --- .../human_protocol_sdk/constants.py | 12 ++++++------ .../typescript/human-protocol-sdk/src/constants.ts | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/constants.py b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/constants.py index 00b433d0ac..a9433ff89a 100644 --- a/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/constants.py +++ b/packages/sdk/python/human-protocol-sdk/human_protocol_sdk/constants.py @@ -36,7 +36,7 @@ class OperatorCategory(Enum): "https://api.studio.thegraph.com/query/74256/ethereum/version/latest" ), "subgraph_url_api_key": ( - "https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmZEF1exsjDwjDXy1kmN3MbdZKxfkkoTj2MbEPUyhLfEG3" + "https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmUe2Zpp9uZmYRLsgiEJQA87CCna4ZaRGUiQ7cU1f58dVV" ), "hmt_address": "0xd1ba9BAC957322D6e8c07a160a3A8dA11A0d2867", "factory_address": "0xD9c75a1Aa4237BB72a41E5E26bd8384f10c1f55a", @@ -52,7 +52,7 @@ class OperatorCategory(Enum): "https://api.studio.thegraph.com/query/74256/sepolia/version/latest" ), "subgraph_url_api_key": ( - "https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmeHhtntEYGdgTqHQTJu5mUXHS8JPt3RiwidLYeeEu6VpP" + "https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmbPBqzyRt3zAtF6BtY52P8wn7Gc3qdjWuKLHdargK8WZp" ), "hmt_address": "0x792abbcC99c01dbDec49c9fa9A828a186Da45C33", "factory_address": "0x5987A5558d961ee674efe4A8c8eB7B1b5495D3bf", @@ -68,7 +68,7 @@ class OperatorCategory(Enum): "https://api.studio.thegraph.com/query/74256/bsc/version/latest" ), "subgraph_url_api_key": ( - "hthttps://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmWsXVhdFuZZcXDjXrv1QLSBYcjNShazFQmNUdpAErjQqD" + "hthttps://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmYP2yo1NGNWGf585uwyRifERv6ZJbJqse3q9Xv1izrR5D" ), "hmt_address": "0x711Fd6ab6d65A98904522d4e3586F492B989c527", "factory_address": "0x92FD968AcBd521c232f5fB8c33b342923cC72714", @@ -84,7 +84,7 @@ class OperatorCategory(Enum): "https://api.studio.thegraph.com/query/74256/bsc-testnet/version/latest" ), "subgraph_url_api_key": ( - "https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmPv7asd21BA5LZJxjDPfoL5ZJkEGMvahrdMqgNQnP5sxn" + "https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmekNGfmcY6trj3m471kQLWg3YSKM7UaRJ3Ef3MBxWfu6o" ), "hmt_address": "0xE3D74BBFa45B4bCa69FF28891fBE392f4B4d4e4d", "factory_address": "0x2bfA592DBDaF434DDcbb893B1916120d181DAD18", @@ -102,7 +102,7 @@ class OperatorCategory(Enum): "https://api.studio.thegraph.com/query/74256/polygon/version/latest" ), "subgraph_url_api_key": ( - "https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmZSsJn5TERyEfRrrbY926hLHD321ijoMynrxWxc3boRa6" + "https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmTv69h7rW9SMSfLJN4WHzMA4qYttX2GzzfM5Drey4mCRp" ), "hmt_address": "0xc748B2A084F8eFc47E086ccdDD9b7e67aEb571BF", "factory_address": "0xBDBfD2cC708199C5640C6ECdf3B0F4A4C67AdfcB", @@ -120,7 +120,7 @@ class OperatorCategory(Enum): "https://api.studio.thegraph.com/query/74256/amoy/version/latest" ), "subgraph_url_api_key": ( - "https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmYWc4ciJbAvTvcjoBzRSTWZrf1xD8WPRqEk3xtJZUKZqY" + "https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmUe97VzETuP1zQ6oD2trtu9RvSxpRSUrv4uhF1y5dzQDN" ), "hmt_address": "0x792abbcC99c01dbDec49c9fa9A828a186Da45C33", "factory_address": "0xAFf5a986A530ff839d49325A5dF69F96627E8D29", diff --git a/packages/sdk/typescript/human-protocol-sdk/src/constants.ts b/packages/sdk/typescript/human-protocol-sdk/src/constants.ts index 15bbc397ed..0351a400ae 100644 --- a/packages/sdk/typescript/human-protocol-sdk/src/constants.ts +++ b/packages/sdk/typescript/human-protocol-sdk/src/constants.ts @@ -36,7 +36,7 @@ export const NETWORKS: { subgraphUrl: 'https://api.studio.thegraph.com/query/74256/ethereum/version/latest', subgraphUrlApiKey: - 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmZEF1exsjDwjDXy1kmN3MbdZKxfkkoTj2MbEPUyhLfEG3', + 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmUe2Zpp9uZmYRLsgiEJQA87CCna4ZaRGUiQ7cU1f58dVV', oldSubgraphUrl: '', oldFactoryAddress: '', }, @@ -51,7 +51,7 @@ export const NETWORKS: { subgraphUrl: 'https://api.studio.thegraph.com/query/74256/sepolia/version/latest', subgraphUrlApiKey: - 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmeHhtntEYGdgTqHQTJu5mUXHS8JPt3RiwidLYeeEu6VpP', + 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmbPBqzyRt3zAtF6BtY52P8wn7Gc3qdjWuKLHdargK8WZp', oldSubgraphUrl: '', oldFactoryAddress: '', }, @@ -66,7 +66,7 @@ export const NETWORKS: { subgraphUrl: 'https://api.studio.thegraph.com/query/74256/bsc/version/latest', subgraphUrlApiKey: - 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmWsXVhdFuZZcXDjXrv1QLSBYcjNShazFQmNUdpAErjQqD', + 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmYP2yo1NGNWGf585uwyRifERv6ZJbJqse3q9Xv1izrR5D', oldSubgraphUrl: 'https://api.thegraph.com/subgraphs/name/humanprotocol/bsc', oldFactoryAddress: '0xc88bC422cAAb2ac8812de03176402dbcA09533f4', }, @@ -81,7 +81,7 @@ export const NETWORKS: { subgraphUrl: 'https://api.studio.thegraph.com/query/74256/bsc-testnet/version/latest', subgraphUrlApiKey: - 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmPv7asd21BA5LZJxjDPfoL5ZJkEGMvahrdMqgNQnP5sxn', + 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmekNGfmcY6trj3m471kQLWg3YSKM7UaRJ3Ef3MBxWfu6o', oldSubgraphUrl: 'https://api.thegraph.com/subgraphs/name/humanprotocol/bsctest', oldFactoryAddress: '0xaae6a2646c1f88763e62e0cd08ad050ea66ac46f', @@ -97,7 +97,7 @@ export const NETWORKS: { subgraphUrl: 'https://api.studio.thegraph.com/query/74256/polygon/version/latest', subgraphUrlApiKey: - 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmZSsJn5TERyEfRrrbY926hLHD321ijoMynrxWxc3boRa6', + 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmTv69h7rW9SMSfLJN4WHzMA4qYttX2GzzfM5Drey4mCRp', oldSubgraphUrl: 'https://api.thegraph.com/subgraphs/name/humanprotocol/polygon', oldFactoryAddress: '0x45eBc3eAE6DA485097054ae10BA1A0f8e8c7f794', @@ -113,7 +113,7 @@ export const NETWORKS: { subgraphUrl: 'https://api.studio.thegraph.com/query/74256/amoy/version/latest', subgraphUrlApiKey: - 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmYWc4ciJbAvTvcjoBzRSTWZrf1xD8WPRqEk3xtJZUKZqY', + 'https://gateway-arbitrum.network.thegraph.com/api/[SUBGRAPH_API_KEY]/deployments/id/QmUe97VzETuP1zQ6oD2trtu9RvSxpRSUrv4uhF1y5dzQDN', oldSubgraphUrl: '', oldFactoryAddress: '', },