deth
Version:
Ethereum node focused on Developer Experience
26 lines (25 loc) • 1.21 kB
JavaScript
import { callbackify } from 'util';
import { BN } from 'ethereumjs-util';
import { bnToQuantity, numberToQuantity, bufferToHash } from '../../primitives';
import { callbackifySync } from './adapter-utils';
/**
* Wraps DethBlockchain into ethereum-js/blockchain compatible interface
*/
export class BlockchainAdapter {
constructor(dethBlockchain) {
this.dethBlockchain = dethBlockchain;
this.putGenesis = callbackifySync(this.dethBlockchain.putGenesis.bind(this.dethBlockchain));
this.getLatestBlock = callbackifySync(this.dethBlockchain.getLatestBlock.bind(this.dethBlockchain));
this.getBlock = callbackify(async (numberOrTag) => {
if (numberOrTag instanceof Buffer) {
return this.dethBlockchain.getBlockByHash(bufferToHash(numberOrTag));
}
if (numberOrTag instanceof BN) {
return this.dethBlockchain.getBlockByNumber(bnToQuantity(numberOrTag));
}
return this.dethBlockchain.getBlockByNumber(numberToQuantity(numberOrTag));
});
// @todo isGenesis arg is missing
this.putBlock = callbackifySync(this.dethBlockchain.putBlock.bind(this.dethBlockchain));
}
}