From 6b39f106668d1af6e4fa256788960d3e26709d62 Mon Sep 17 00:00:00 2001 From: xiaoch05 Date: Thu, 28 Dec 2023 15:54:39 +0800 Subject: [PATCH] xtoken using guard --- .../xtoken/messageDispatcher/package.json | 4 +- .../xtoken/messageDispatcher/schema.graphql | 4 ++ .../xtoken/messageDispatcher/src/dispatch.ts | 10 +++- .../xtoken/messageDispatcher/src/guard.ts | 53 ++++++++++++++++--- 4 files changed, 60 insertions(+), 11 deletions(-) diff --git a/subgraph/xtoken/messageDispatcher/package.json b/subgraph/xtoken/messageDispatcher/package.json index bdd42568..9f440b84 100644 --- a/subgraph/xtoken/messageDispatcher/package.json +++ b/subgraph/xtoken/messageDispatcher/package.json @@ -4,8 +4,8 @@ "scripts": { "codegen": "graph codegen", "build": "graph build", - "build-crab": "sh generate.sh dispatch crab 1962560 0x0000000000D2de3e2444926c4577b0A59F1DD8BC 0xf85638B61E0425D6BB91981190B73246e3AF3CA9 && graph codegen && graph build", - "build-sepolia": "sh generate.sh dispatch sepolia 4952560 0x0000000000D2de3e2444926c4577b0A59F1DD8BC 0xc876D0873e4060472334E297b2db200Ca10cc806 && sh generate.sh guard sepolia 0x2e8B5408Ec891AbdeE9CDeDCe407217c5e47196d 4952560 && graph codegen && graph build", + "build-crab": "sh generate.sh dispatch crab 1962560 0x00000000001523057a05d6293C1e5171eE33eE0A 0xf85638B61E0425D6BB91981190B73246e3AF3CA9 && graph codegen && graph build", + "build-sepolia": "sh generate.sh dispatch sepolia 4952560 0x00000000001523057a05d6293C1e5171eE33eE0A 0xc876D0873e4060472334E297b2db200Ca10cc806 && sh generate.sh guard sepolia 0xcc357d5A8E5dBD52bC508E0FE491137d912F6bc8 4952560 && graph codegen && graph build", "create-remote-dev": "graph create --access-token ${KEY} --node https://thegraph-g2.darwinia.network/helix/deploy/ xtokendispatch/${NETWORK}", "deploy-remote-dev": "graph deploy --access-token $KEY --node https://thegraph-g2.darwinia.network/helix/deploy/ --ipfs https://ipfs.network.thegraph.com xtokendispatch/$NETWORK", "create-remote-pro": "graph create --access-token ${KEY} --node https://thegraph.darwinia.network/helix/deploy/ xtokendispatch/${NETWORK}", diff --git a/subgraph/xtoken/messageDispatcher/schema.graphql b/subgraph/xtoken/messageDispatcher/schema.graphql index 142b1cea..a01da2e7 100644 --- a/subgraph/xtoken/messageDispatcher/schema.graphql +++ b/subgraph/xtoken/messageDispatcher/schema.graphql @@ -6,3 +6,7 @@ type MessageDispatchedResult @entity { result: Int } +type TransferId2MessageId @entity { + id: ID! + messageId: String! +} diff --git a/subgraph/xtoken/messageDispatcher/src/dispatch.ts b/subgraph/xtoken/messageDispatcher/src/dispatch.ts index 2c8d710a..d942cbfe 100644 --- a/subgraph/xtoken/messageDispatcher/src/dispatch.ts +++ b/subgraph/xtoken/messageDispatcher/src/dispatch.ts @@ -26,6 +26,10 @@ function isMsglineDispatchEvent(event: ethereum.Log): boolean { isMsglineContract(event); } +function isGuardDepositEvent(event: ethereum.Log): boolean { + return event.topics[0].toHexString() == '0xe15a305c9965c563f86d698c22d072ae55c831930e4bcfc3cacf9050bbdc69d2'; +} + export function handleMessageDispatched(event: MessageDispatched): void { let message_id = event.params.msgHash.toHexString(); let entity = MessageDispatchedResult.load(message_id); @@ -42,6 +46,7 @@ export function handleMessageDispatched(event: MessageDispatched): void { export function handleCallResult(event: CallResult): void { var messageId = ''; + var usingGuard = false; // find the messageId if (event.receipt == null) { return; @@ -50,7 +55,8 @@ export function handleCallResult(event: CallResult): void { for (var idx = 0; idx < logs.length; idx++) { if (isMsglineDispatchEvent(logs[idx])) { messageId = logs[idx].topics[1].toHexString(); - break; + } else if (isGuardDepositEvent(logs[idx])) { + usingGuard = true; } } } @@ -67,7 +73,7 @@ export function handleCallResult(event: CallResult): void { entity.transactionHash = event.transaction.hash; if (!event.params.result) { entity.result = STATUS_FAILED; - } else if(entity.result < STATUS_DELIVERED_SUCCESSED) { + } else if(entity.result < STATUS_DELIVERED_SUCCESSED && !usingGuard) { entity.result = STATUS_DELIVERED_SUCCESSED; } entity.save(); diff --git a/subgraph/xtoken/messageDispatcher/src/guard.ts b/subgraph/xtoken/messageDispatcher/src/guard.ts index 78adb40d..df98da51 100644 --- a/subgraph/xtoken/messageDispatcher/src/guard.ts +++ b/subgraph/xtoken/messageDispatcher/src/guard.ts @@ -1,30 +1,69 @@ -import { BigInt } from "@graphprotocol/graph-ts" +import { BigInt, ethereum } from "@graphprotocol/graph-ts" import { TokenDeposit, TokenClaimed, } from "../generated/Guard/Guard" -import { MessageDispatchedResult } from "../generated/schema" +import { MessageDispatchedResult, TransferId2MessageId } from "../generated/schema" const STATUS_PENDING_TOCLAIM = 3; // claimed const STATUS_CLAIMED = 4; +function isMsglineContract(event: ethereum.Log): boolean { + return event.address.toHexString() == '0x00000000001523057a05d6293c1e5171ee33ee0a' || + event.address.toHexString() == '0x00000000046bc530804d66b6b64f7af69b4e4e81'; +} + +function isMsglineDispatchEvent(event: ethereum.Log): boolean { + return event.topics[0].toHexString() == '0x62b1dc20fd6f1518626da5b6f9897e8cd4ebadbad071bb66dc96a37c970087a8' && + isMsglineContract(event); +} + export function handleTokenDeposit(event: TokenDeposit): void { - let message_id = event.params.id.toHexString(); - let entity = MessageDispatchedResult.load(message_id); + var messageId = ''; + // find the messageId + if (event.receipt == null) { + return; + } else { + const logs = event.receipt!.logs; + for (var idx = 0; idx < logs.length; idx++) { + if (isMsglineDispatchEvent(logs[idx])) { + messageId = logs[idx].topics[1].toHexString(); + break; + } + } + } + + if (messageId === '') { + return; + } + + let entity = MessageDispatchedResult.load(messageId); if (entity == null) { - entity = new MessageDispatchedResult(message_id); + entity = new MessageDispatchedResult(messageId); } entity.transactionHash = event.transaction.hash; entity.timestamp = event.block.timestamp; entity.token = event.params.token; entity.result = STATUS_PENDING_TOCLAIM; entity.save(); + + const transferId = event.params.id.toHexString(); + let idEntity = TransferId2MessageId.load(transferId); + if (idEntity == null) { + idEntity = new TransferId2MessageId(transferId); + } + idEntity.messageId = messageId; + idEntity.save(); } export function handleTokenClaimed(event: TokenClaimed): void { - let message_id = event.params.id.toHexString(); - let entity = MessageDispatchedResult.load(message_id); + let transferId = event.params.id.toHexString(); + let idEntity = TransferId2MessageId.load(transferId); + if (idEntity == null) { + return; + } + let entity = MessageDispatchedResult.load(idEntity.messageId); if (entity == null) { return; }