Skip to content

Instantly share code, notes, and snippets.

@yurenju
Created July 12, 2019 00:30
Show Gist options
  • Save yurenju/3b9577aaee9cf2590f3bda35074ab1cf to your computer and use it in GitHub Desktop.
Save yurenju/3b9577aaee9cf2590f3bda35074ab1cf to your computer and use it in GitHub Desktop.
import { BigNumber } from "bignumber.js";
const serialized: any = {
blockNumber: "8947598234759823",
txs: [{ price: "45384238946427" }]
};
class Transaction {
price: BigNumber;
constructor(price: BigNumber) {
this.price = price;
}
static fromJSON(json: any) {
return new Transaction(new BigNumber(json.price));
}
toJSON() {
return {
price: this.price.toJSON()
};
}
}
class Block {
blockNumber: BigNumber;
txs: Transaction[];
constructor(blockNumber: BigNumber, txs: Transaction[]) {
this.blockNumber = blockNumber;
this.txs = txs;
}
toJSON() {
return {
blockNumber: this.blockNumber.toJSON(),
txs: this.txs.map(tx => tx.toJSON())
};
}
static fromJSON(json: any) {
return new Block(
new BigNumber(json.blockNumber),
json.txs.map((tx: any) => Transaction.fromJSON(tx))
);
}
}
const block = Block.fromJSON(serialized);
console.log(block);
console.log(JSON.stringify(block));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment