Skip to content
This repository has been archived by the owner on Jul 8, 2021. It is now read-only.

Commit

Permalink
Complete receipt API (#73)
Browse files Browse the repository at this point in the history
* perf(proposal): update the genesis confirm part of proposal cmd

* perf(util): clean util

* perf(types): remove outdated types

* refactor(listener): move listeners to one folder

* chore(types): update receipt types

* perf(proposal): fix the genesis lastconfirmed block

* perf(listener): add tx type to the callback of ethereum listener

* perf(eth): clean db files of ethereum listener

* perf(cfg): merge configs

* refactor(cmd): use run command for default command, remove proposal cmd

* refactor(listener): invoke blockNumber into ethereum listener callback

* feat(listener): complete listener queue

* chore(api): unly delay exs

* refactor(api): use array as tuple in redeem api

* feat(redeem): complete redeem process

* chore(static): update types.json of darwinia

* perf(cmd): optimize the log of ethereum listener

* chore(config): update the config of ethereum listener

* perf(badge): add npm badge
  • Loading branch information
clearloop authored Sep 9, 2020
1 parent 6e59c91 commit 72c249a
Show file tree
Hide file tree
Showing 35 changed files with 519 additions and 788 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# @darwinia/dj

[![dj][workflow-badge]][github]
[![dj](https://img.shields.io/npm/v/@darwinia/dj)](https://www.npmjs.com/package/@darwinia/dj)
[![LICENSE](https://img.shields.io/crates/l/darwinia-shadow.svg)](https://choosealicense.com/licenses/gpl-3.0/)

## **Introduction**
Expand Down
19 changes: 0 additions & 19 deletions config/darwiniadev.json

This file was deleted.

20 changes: 0 additions & 20 deletions config/default.json

This file was deleted.

95 changes: 47 additions & 48 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
{
"private": false,
"name": "@darwinia/dj",
"version": "",
"description": "Darwinia bridge relayer tool",
"homepage": "https://github.com/darwinia-network/dj",
"repository": {
"type": "git",
"url": "https://github.com/darwinia-network/dj"
},
"author": "darwinia-network <[email protected]>",
"license": "GPL-3.0",
"main": "lib/index.js",
"bin": {
"dj": "lib/index.js"
},
"files": ["lib/**/*"],
"dependencies": {
"@polkadot/api": "1.30.0-beta.0",
"@polkadot/keyring": "3.3.1",
"@polkadot/util-crypto": "3.3.1",
"axios": "^0.19.2",
"ethereumjs-util": "^7.0.4",
"got": "^11.5.2",
"javascript-stringify": "^2.0.1",
"progress": "^2.0.3",
"prompts": "^2.3.2",
"tar": "^6.0.5",
"yargs": "^15.4.1",
"web3": "^1.2.11",
"config": "^3.3.1"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@polkadot/types": "1.30.0-beta.0",
"@types/node": "^13.11.1",
"@types/progress": "^2.0.3",
"@types/prompts": "^2.0.8",
"@types/tar": "^4.0.3",
"@types/yargs": "^15.0.4",
"husky": "^4.2.5",
"tslint": "^6.1.1",
"typescript": "^3.8.3"
},
"scripts": {
"build": "tsc --strict",
"lint": "tsc --noEmit --strict && tslint --project ./tsconfig.json"
}
"private": false,
"name": "@darwinia/dj",
"version": "",
"description": "Darwinia bridge relayer tool",
"homepage": "https://github.com/darwinia-network/dj",
"repository": {
"type": "git",
"url": "https://github.com/darwinia-network/dj"
},
"author": "darwinia-network <[email protected]>",
"license": "GPL-3.0",
"main": "lib/index.js",
"bin": {
"dj": "lib/index.js"
},
"files": ["lib/**/*"],
"dependencies": {
"@polkadot/api": "1.30.0-beta.0",
"@polkadot/keyring": "3.3.1",
"@polkadot/util-crypto": "3.3.1",
"axios": "^0.19.2",
"got": "^11.5.2",
"javascript-stringify": "^2.0.1",
"progress": "^2.0.3",
"prompts": "^2.3.2",
"tar": "^6.0.5",
"yargs": "^15.4.1",
"web3": "^1.2.11",
"config": "^3.3.1"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@polkadot/types": "1.30.0-beta.0",
"@types/node": "^13.11.1",
"@types/progress": "^2.0.3",
"@types/prompts": "^2.0.8",
"@types/tar": "^4.0.3",
"@types/yargs": "^15.0.4",
"husky": "^4.2.5",
"tslint": "^6.1.1",
"typescript": "^3.8.3"
},
"scripts": {
"build": "tsc --strict",
"lint": "tsc --noEmit --strict && tslint --project ./tsconfig.json"
}
}
25 changes: 25 additions & 0 deletions src/__test__/eth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ethereum } from "../listener";

ethereum({
"RPC_SERVER": "https://ropsten.infura.io/v3/0bfb9acbb13c426097aabb1d81a9d016",
"START_BLOCK_NUMBER": 8609800,
"CONTRACT": {
"RING": {
"address": "0xb52FBE2B925ab79a821b261C82c5Ba0814AAA5e0",
"burnAndRedeemTopics": "0xc9dcda609937876978d7e0aa29857cb187aea06ad9e843fd23fd32108da73f10"
},
"KTON": {
"address": "0x1994100c58753793D52c6f457f189aa3ce9cEe94",
"burnAndRedeemTopics": "0xc9dcda609937876978d7e0aa29857cb187aea06ad9e843fd23fd32108da73f10"
},
"BANK": {
"address": "0x6EF538314829EfA8386Fc43386cB13B4e0A67D1e",
"burnAndRedeemTopics": "0xe77bf2fa8a25e63c1e5e29e1b2fcb6586d673931e020c4e3ffede453b830fb12"
},
"ISSUING": {
"address": "0x49262B932E439271d05634c32978294C7Ea15d0C"
}
}
}, (tx: any) => {
console.log('test::callback:', tx)
});
52 changes: 23 additions & 29 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
/* tslint:disable:variable-name */
import { log, IDarwiniaEthBlock } from "../util";
import { log, delay } from "../util";
import { ApiPromise, SubmittableResult, WsProvider } from "@polkadot/api";
import { SubmittableExtrinsic } from "@polkadot/api/types";
import Keyring from "@polkadot/keyring";
import { KeyringPair } from "@polkadot/keyring/types";
import { DispatchError, EventRecord } from "@polkadot/types/interfaces/types";
import { cryptoWaitReady } from "@polkadot/util-crypto";
import { SignedBlock } from "@polkadot/types/interfaces";
import { IEthereumHeaderThingWithProof } from "./types/block";
import {
IEthereumHeaderThingWithProof,
IReceiptWithProof,
} from "./types";

export interface IErrorDoc {
name: string;
section: string;
documentation: string[];
}

export interface IReceipt {
index: string;
proof: string;
header_hash: string;
}

/**
* Extrinsic Result
*
Expand Down Expand Up @@ -148,13 +145,13 @@ export class API {
/**
* Get last confirm block
*/
public async lastConfirm(): Promise<number | null> {
public async lastConfirm(): Promise<number> {
const res = await this._.query.ethereumRelay.lastConfirmedHeaderInfo();
if (res.toJSON() === null) {
return null;
return 0;
}

return (res.toJSON() as any)[0];
return (res.toJSON() as any)[0] as number;
}

/**
Expand Down Expand Up @@ -194,6 +191,8 @@ export class API {
} else {
return new ExResult(false, "", "");
}
log.event(`Approve block ${block}`);
await delay(3000);
return await this.blockFinalized(ex, true);
}

Expand All @@ -209,6 +208,8 @@ export class API {
} else {
return new ExResult(false, "", "");
}
log.event(`Reject block ${block}`);
await delay(3000);
return await this.blockFinalized(ex);
}

Expand All @@ -218,7 +219,9 @@ export class API {
* @param {IEthHeaderThing} headerThings - Eth Header Things
*/
public async submitProposal(headerThings: IEthereumHeaderThingWithProof[]): Promise<ExResult> {
log.event(`Submit proposal contains block ${headerThings[headerThings.length - 1].header.number}`);
const ex = this._.tx.ethereumRelay.submitProposal(headerThings);
await delay(3000);
return await this.blockFinalized(ex);
}

Expand All @@ -227,23 +230,14 @@ export class API {
*
* @param {DarwiniaEthBlock} block - darwinia style eth block
*/
public async redeem(receipt: IReceipt): Promise<ExResult> {
const ex: SubmittableExtrinsic<"promise"> = this._.tx.ethRelay.redeem({
Ring: receipt,
});
return await this.blockFinalized(ex);
}

/**
* reset darwinia header
*
* @param {DarwiniaEthBlock} block - darwinia style eth block
*/
public async reset(block: IDarwiniaEthBlock): Promise<ExResult> {
const ex: SubmittableExtrinsic<"promise"> = this._.tx.ethRelay.resetGenesisHeader(
block, block.difficulty,
);

public async redeem(act: string, proof: IReceiptWithProof): Promise<ExResult> {
log.event(`Redeem tx in block ${proof.header.number}`);
const ex: SubmittableExtrinsic<"promise"> = this._.tx.ethereumBacking.redeem(act, [
proof.header,
proof.receipt_proof,
proof.mmr_proof,
]);
await delay(3000);
return await this.blockFinalized(ex);
}

Expand All @@ -255,6 +249,7 @@ export class API {
*/
public async transfer(addr: string, amount: number): Promise<ExResult> {
const ex = this._.tx.balances.transfer(addr, amount);
await delay(3000);
return await this.blockFinalized(ex);
}

Expand Down Expand Up @@ -315,7 +310,6 @@ export class API {
});
}
} else if (status.isInvalid) {
console.log(res)
log.warn("Invalid Extrinsic");
reject(res);
} else if (status.isRetracted) {
Expand Down
7 changes: 5 additions & 2 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { API, ExResult } from "./api";
import { autoAPI } from "./auto";
import { ShadowAPI } from "./shadow";
import { IEthereumHeaderThing, IEthereumHeaderThingWithProof } from "./types/block";
import { IEthereumHeaderThing, IEthereumHeaderThingWithProof } from "./types";

export {
API, autoAPI, ExResult, ShadowAPI, IEthereumHeaderThing, IEthereumHeaderThingWithProof,
API, autoAPI,
ExResult, ShadowAPI,
IEthereumHeaderThing,
IEthereumHeaderThingWithProof,
}
17 changes: 8 additions & 9 deletions src/api/shadow.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* tslint:disable:variable-name */
import axios, { AxiosResponse } from "axios";
import {
log, Block, IDarwiniaEthBlock,
} from "../util";
import { log } from "../util";
import {
IReceiptWithProof,
IEthereumHeaderThing,
IEthereumHeaderThingWithProof,
} from "./types/block";
IDarwiniaEthBlock,
} from "./types";

/**
* Shadow APIs
Expand All @@ -29,11 +28,11 @@ export class ShadowAPI {
*/
public async getBlock(block: number | string): Promise<IDarwiniaEthBlock> {
let r: AxiosResponse;
r = await axios.get(this.api + "/header/" + block);
r = await axios.get(this.api + "/eth/header/" + block);

// Trace the back data
log.trace(JSON.stringify(r.data, null, 2));
return Block.from(r.data);
log.trace(JSON.stringify(r.data.header, null, 2));
return r.data.header;
}

/**
Expand All @@ -56,9 +55,9 @@ export class ShadowAPI {
*
* @param {number} block - block number
*/
async getReceipt(tx: string, last: number): Promise<IReceiptWithProof> {
async getReceipt(tx: string, lastConfirmed: number): Promise<IReceiptWithProof> {
const r: AxiosResponse = await axios.get(
this.api + "/eth/receipt/" + tx + "?last=" + last
this.api + "/eth/receipt/" + tx + "/" + lastConfirmed
);

// Trace the back data
Expand Down
Loading

0 comments on commit 72c249a

Please sign in to comment.