Created
July 12, 2019 00:30
-
-
Save yurenju/3b9577aaee9cf2590f3bda35074ab1cf to your computer and use it in GitHub Desktop.
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
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