forked from bitrocks/godwoken-web3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bitrocks#38 from nervosnetwork/refactor
Refactor
- Loading branch information
Showing
26 changed files
with
1,453 additions
and
1,958 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { env } from "process"; | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config({ path: "./.env" }); | ||
|
||
export const envConfig = { | ||
databaseUrl: getRequired("DATABASE_URL"), | ||
ethAccountLockHash: getRequired("ETH_ACCOUNT_LOCK_HASH"), | ||
rollupTypeHash: getRequired("ROLLUP_TYPE_HASH"), | ||
godwokenJsonRpc: getRequired("GODWOKEN_JSON_RPC"), | ||
creatorAccountId: getRequired("CREATOR_ACCOUNT_ID"), | ||
chainId: getRequired("CHAIN_ID"), | ||
defaultFromAddress: getRequired("DEFAULT_FROM_ADDRESS"), | ||
polyjuiceValidatorTypeHash: getOptional("POLYJUICE_VALIDATOR_TYPE_HASH"), | ||
rollupConfigHash: getOptional("ROLLUP_CONFIG_HASH"), | ||
}; | ||
|
||
function getRequired(name: string): string { | ||
const value = env[name]; | ||
if (value == null) { | ||
throw new Error(`no env ${name} provided`); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
function getOptional(name: string): string | undefined { | ||
return env[name]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function defaultLogger(level: string, ...messages: any[]) { | ||
console.log(`[${level}] `, ...messages); | ||
} | ||
|
||
export const logger = { | ||
debug: (...args: any[]) => defaultLogger("debug", ...args), | ||
info: (...args: any[]) => defaultLogger("info", ...args), | ||
warn: (...args: any[]) => defaultLogger("warn", ...args), | ||
error: (...args: any[]) => defaultLogger("error", ...args), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Hash, HexNumber, HexString } from "@ckb-lumos/base"; | ||
|
||
export interface EthTransaction { | ||
hash: Hash; | ||
// when pending, blockNumber & blockHash = null | ||
blockHash: Hash | null; | ||
blockNumber: HexNumber | null; | ||
transactionIndex: HexNumber; | ||
from: HexString; | ||
to: HexString | null; | ||
gas: HexNumber; | ||
gasPrice: HexNumber; | ||
input: HexString; | ||
nonce: HexNumber; | ||
value: HexNumber; | ||
v: HexNumber; | ||
r: HexString; | ||
s: HexString; | ||
} | ||
|
||
export interface EthBlock { | ||
// when pending, number & hash & nonce & logsBloom = pending | ||
number: HexNumber | null; | ||
hash: Hash; | ||
parentHash: Hash; | ||
gasLimit: HexNumber; | ||
gasUsed: HexNumber; | ||
miner: HexString; | ||
size: HexNumber; | ||
logsBloom: HexString; | ||
transactions: (EthTransaction | Hash)[]; | ||
timestamp: HexNumber; | ||
mixHash: Hash; | ||
nonce: HexNumber; | ||
stateRoot: Hash; | ||
sha3Uncles: Hash; | ||
receiptsRoot: Hash; | ||
transactionsRoot: Hash; | ||
uncles: []; | ||
totalDifficulty: HexNumber; | ||
extraData: HexString; | ||
} | ||
|
||
export interface EthTransactionReceipt { | ||
transactionHash: Hash; | ||
transactionIndex: HexNumber; | ||
blockHash: Hash; | ||
blockNumber: HexNumber; | ||
from: HexString; | ||
to: HexString | null; | ||
gasUsed: HexNumber; | ||
cumulativeGasUsed: HexNumber; | ||
logsBloom: HexString; | ||
logs: EthLog[]; | ||
contractAddress: HexString | null; | ||
status: HexNumber; | ||
} | ||
|
||
export interface EthLog { | ||
// when pending logIndex, transactionIndex, transactionHash, blockHash, blockNumber = null | ||
address: HexString; | ||
blockHash: Hash | null; | ||
blockNumber: HexNumber | null; | ||
transactionIndex: HexNumber | null; | ||
transactionHash: Hash | null; | ||
data: HexString; | ||
logIndex: HexNumber | null; | ||
topics: HexString[]; | ||
removed: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { HexNumber, HexString } from "@ckb-lumos/base"; | ||
|
||
export function toHexNumber(num: number | bigint): HexNumber { | ||
return "0x" + num.toString(16); | ||
} | ||
|
||
export class Uint32 { | ||
private value: number; | ||
|
||
public static MIN = 0; | ||
public static MAX = 2 ** 32 - 1; | ||
|
||
constructor(value: number) { | ||
if (typeof value !== "number") { | ||
throw new Error("Uint32 value must be a number!"); | ||
} | ||
if (value < Uint32.MIN || value > Uint32.MAX) { | ||
throw new Error("value to small or too big"); | ||
} | ||
this.value = value; | ||
} | ||
|
||
public getValue(): number { | ||
return this.value; | ||
} | ||
|
||
public toHex(): HexNumber { | ||
return toHexNumber(this.value); | ||
} | ||
|
||
public static fromHex(value: HexNumber): Uint32 { | ||
assertHexNumber("Uint32.fromHex args", value); | ||
return new Uint32(+value); | ||
} | ||
|
||
public toLittleEndian(): HexString { | ||
const buf = Buffer.alloc(4); | ||
buf.writeUInt32LE(this.value); | ||
return `0x${buf.toString("hex")}`; | ||
} | ||
|
||
public static fromLittleEndian(hex: HexString): Uint32 { | ||
assertHexNumber("Uint32.fromLittleEndian args", hex); | ||
if (hex.length !== 10 || !hex.startsWith("0x")) { | ||
throw new Error(`little endian hex format error`); | ||
} | ||
const buf = Buffer.from(hex.slice(2), "hex"); | ||
const num = buf.readUInt32LE(); | ||
return new Uint32(num); | ||
} | ||
} | ||
|
||
export class Uint64 { | ||
private value: bigint; | ||
|
||
public static MIN = 0; | ||
public static MAX = 2n ** 64n - 1n; | ||
|
||
constructor(value: bigint) { | ||
if (typeof value !== "bigint") { | ||
throw new Error("Uint64 value must be a bigint!"); | ||
} | ||
if (value < Uint64.MIN || value > Uint64.MAX) { | ||
throw new Error("value to small or too big"); | ||
} | ||
this.value = value; | ||
} | ||
|
||
public getValue(): bigint { | ||
return this.value; | ||
} | ||
|
||
public toHex(): HexNumber { | ||
return toHexNumber(this.value); | ||
} | ||
|
||
public static fromHex(value: HexNumber): Uint64 { | ||
assertHexNumber("Uint64.fromHex args", value); | ||
return new Uint64(BigInt(value)); | ||
} | ||
|
||
public toLittleEndian(): HexString { | ||
const buf = Buffer.alloc(8); | ||
buf.writeBigUInt64LE(this.value); | ||
return `0x${buf.toString("hex")}`; | ||
} | ||
|
||
public static fromLittleEndian(hex: HexNumber): Uint64 { | ||
assertHexNumber("Uint64.fromLittleEndian args", hex); | ||
if (hex.length !== 18 || !hex.startsWith("0x")) { | ||
throw new Error(`little endian hex format error`); | ||
} | ||
const buf = Buffer.from(hex.slice(2), "hex"); | ||
const num = buf.readBigUInt64LE(); | ||
return new Uint64(num); | ||
} | ||
} | ||
|
||
export class Uint128 { | ||
private value: bigint; | ||
|
||
public static MIN: bigint = 0n; | ||
public static MAX: bigint = 2n ** 128n - 1n; | ||
|
||
constructor(value: bigint) { | ||
if (typeof value !== "bigint") { | ||
throw new Error("Uint128 value must be a bigint!"); | ||
} | ||
if (value < Uint128.MIN || value > Uint128.MAX) { | ||
throw new Error("value to small or too big"); | ||
} | ||
this.value = value; | ||
} | ||
|
||
public getValue(): bigint { | ||
return this.value; | ||
} | ||
|
||
public toHex(): HexNumber { | ||
return toHexNumber(this.value); | ||
} | ||
|
||
public static fromHex(value: HexNumber): Uint128 { | ||
assertHexNumber("Uint128.fromHex args", value); | ||
return new Uint128(BigInt(value)); | ||
} | ||
|
||
public toLittleEndian(): HexString { | ||
const buf = Buffer.alloc(16); | ||
buf.writeBigUInt64LE(this.value & BigInt("0xFFFFFFFFFFFFFFFF"), 0); | ||
buf.writeBigUInt64LE(this.value >> BigInt(64), 8); | ||
return "0x" + buf.toString("hex"); | ||
} | ||
|
||
public static fromLittleEndian(hex: HexNumber): Uint128 { | ||
if (hex.length !== 34 || !hex.startsWith("0x")) { | ||
throw new Error(`little endian hex format error`); | ||
} | ||
const buf = Buffer.from(hex.slice(2, 34), "hex"); | ||
const num = (buf.readBigUInt64LE(8) << BigInt(64)) + buf.readBigUInt64LE(0); | ||
return new Uint128(num); | ||
} | ||
} | ||
|
||
function assertHexNumber(debugPath: string, str: string) { | ||
if (!/^0x(0|[0-9a-fA-F]+)$/.test(str)) { | ||
throw new Error(`${debugPath} must be a hex number!`); | ||
} | ||
} |
Oops, something went wrong.