Skip to content

Commit

Permalink
xtoken using guard
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoch05 committed Dec 28, 2023
1 parent ffdd213 commit 6b39f10
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
4 changes: 2 additions & 2 deletions subgraph/xtoken/messageDispatcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
4 changes: 4 additions & 0 deletions subgraph/xtoken/messageDispatcher/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ type MessageDispatchedResult @entity {
result: Int
}

type TransferId2MessageId @entity {
id: ID!
messageId: String!
}
10 changes: 8 additions & 2 deletions subgraph/xtoken/messageDispatcher/src/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
}
}
Expand All @@ -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();
Expand Down
53 changes: 46 additions & 7 deletions subgraph/xtoken/messageDispatcher/src/guard.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down

0 comments on commit 6b39f10

Please sign in to comment.