Skip to content

Commit

Permalink
Merge pull request #10 from helix-bridge/xiaoch05-repair-crab-smart
Browse files Browse the repository at this point in the history
repair crab<>crab smart history
  • Loading branch information
xiaoch05 authored May 24, 2022
2 parents b62b145 + 0cc6423 commit 94e7de7
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 212 deletions.
60 changes: 0 additions & 60 deletions subql/crab/new.yml

This file was deleted.

17 changes: 3 additions & 14 deletions subql/crab/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,12 @@ dataSources:
- handler: handleEvent
kind: substrate/EventHandler
filter:
module: balances
method: Transfer # ring: mainnet -> dvm

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: kton
method: Transfer # kton: mainnet -> dvm
module: ethereum
method: DVMTransfer

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: balances
method: Endowed # ring: dvm -> mainnet
method: Transfer

# - handler: handleEvent
# kind: substrate/EventHandler
# filter:
# module: balances
# method: Transfer
2 changes: 2 additions & 0 deletions subql/crab/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Transfer @entity {

sender: Account
recipient: Account
fromChain: String
toChain: String
amount: BigInt
section: String
method: String
Expand Down
4 changes: 4 additions & 0 deletions subql/crab/src/handlers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export class AccountHandler {
return u8aToHex(decodeAddress(address));
}

static isDvmAddress(address: string) {
return address.startsWith("0x64766d3a00000000000000")
}

static async ensureAccount(id: string) {
const account = await Account.get(id);

Expand Down
75 changes: 56 additions & 19 deletions subql/crab/src/handlers/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ export class EventHandler {
await this.handleBridgeDispatchEvent();
}

if (this.method === 'Transfer' && (this.section === 'balances' || this.section === 'kton')) {
await this.handleMainToSmartTransfer();
}

if (this.method === 'Endowed' && this.section === 'balances') {
await this.handleSmartToMainTransfer();
if (this.method === 'DVMTransfer' && this.section === 'ethereum') {
await this.handleDvmToSubstrate();
} else if (this.method === 'Transfer' && this.section === 'balances') {
await this.handleSubstrateToDvm();
}
}

Expand All @@ -77,10 +75,59 @@ export class EventHandler {
await event.save();
}

private async handleTransfer(from: string, to: string, amount: number) {
// Dvm -> 15 -> Substrate
private findDvmToSubstrate(router: string, count: number) {
const event = this.event?.extrinsic?.events.find((item) => {
if (item.event.method === 'DVMTransfer') {
const [_1, to, amount] = JSON.parse(item.event.data.toString());
if (count === amount && to === router) {
return true;
}
}
return false;
});
return event;
}

private async handleDvmToSubstrate() {
const [from, to, amount] = JSON.parse(this.data);
let sender = AccountHandler.formatAddress(from);
const recipient = AccountHandler.formatAddress(to);
const senderIsDvm = AccountHandler.isDvmAddress(sender);
const recipientIsDvm = AccountHandler.isDvmAddress(recipient);

if (senderIsDvm && !recipientIsDvm) {
const event = this.findDvmToSubstrate(from, amount);

if (!event) {
return;
}

const [iFrom] = JSON.parse(event.event.data.toString());
sender = AccountHandler.formatAddress(iFrom);
await this.handleTransfer('crab-dvm', 'crab', sender, recipient, amount);
}
}

private async handleSubstrateToDvm() {
const [from, to, amount] = JSON.parse(this.data);
const sender = AccountHandler.formatAddress(from);
const recipient = AccountHandler.formatAddress(to);
const senderIsDvm = AccountHandler.isDvmAddress(sender);
const recipientIsDvm = AccountHandler.isDvmAddress(recipient);

if (!senderIsDvm && recipientIsDvm) {
await this.handleTransfer('crab', 'crab-dvm', sender, recipient, amount);
}
}

private async handleTransfer(
fromChain: string,
toChain: string,
sender: string,
recipient: string,
amount: number
) {
await AccountHandler.ensureAccount(recipient);
await AccountHandler.updateTransferStatistic(recipient);
await AccountHandler.ensureAccount(sender);
Expand All @@ -95,6 +142,8 @@ export class EventHandler {
transfer.method = this.method;
transfer.amount = BigInt(amount ?? 0);
transfer.timestamp = this.timestamp;
transfer.fromChain = fromChain;
transfer.toChain = toChain;

transfer.block = this.simpleBlock();

Expand All @@ -105,18 +154,6 @@ export class EventHandler {
}
}

private async handleMainToSmartTransfer() {
const [from, to, amount] = JSON.parse(this.data);

await this.handleTransfer(from, to, amount);
}

private async handleSmartToMainTransfer() {
const [to, amount] = JSON.parse(this.data);
const from = '0x0000000000000000000000000000000000000015';

await this.handleTransfer(from, to, amount);
}
private simpleBlock(): Block {
return {
blockHash: this.blockHash,
Expand Down
2 changes: 1 addition & 1 deletion subql/pangolin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"author": "sxlwar",
"license": "MIT",
"devDependencies": {
"@polkadot/api": "^7",
"@polkadot/api": "^8",
"@subql/types": "latest",
"typescript": "^4.2.4",
"@subql/cli": "latest"
Expand Down
24 changes: 3 additions & 21 deletions subql/pangolin/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,16 @@ dataSources:
module: bridgePangoroDispatch
method: MessageDispatched

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: balances
method: Transfer # ring: mainnet -> dvm

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: kton
method: Transfer # kton: mainnet -> dvm

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: ethereum
method: DVMTransfer # ring: dvm -> mainnet

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: ethereum
method: KtonDVMTransfer # kton dvm -> mainnet
method: DVMTransfer

- handler: handleEvent
kind: substrate/EventHandler
filter:
module: ethereum
method: Executed
module: balances
method: Transfer


3 changes: 2 additions & 1 deletion subql/pangolin/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ type Transfer @entity {

sender: Account
recipient: Account
fromChain: String
toChain: String
amount: BigInt
section: String
method: String
timestamp: Date
mediator: Account

block: Block
}
15 changes: 3 additions & 12 deletions subql/pangolin/src/handlers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { decodeAddress } from '@polkadot/util-crypto';
import { u8aToHex, isHex } from '@polkadot/util';

export class AccountHandler {
static convertToDVMAddress(address: string) {
static formatAddress(address: string) {
if (isHex(address)) {
return address;
}
Expand All @@ -15,17 +15,8 @@ export class AccountHandler {
return u8aToHex(decodeAddress(address));
}

static convertToEthereumFormat(address: string): string | null {
if (!address) {
return '';
}

const startAt = 2;
const result = u8aToHex(decodeAddress(address)).slice(startAt);
const PREFIX = '64766d3a00000000000000';

// eslint-disable-next-line no-magic-numbers
return result.startsWith(PREFIX) ? '0x' + result.slice(-42, -2) : null;
static isDvmAddress(address: string) {
return address.startsWith("0x64766d3a00000000000000")
}

static async ensureAccount(id: string) {
Expand Down
Loading

0 comments on commit 94e7de7

Please sign in to comment.