packageDocumentation module hidden import { HttpProvider , WSProvider , RPCRequestPayload , ResponseMiddleware , } from '@harmony-js/network' ; import { ChainID , ChainType , Unit , isBech32Address } from '@harmony-js/utils' ; import { HDNode } from '@harmony-js/account' ; import { fromBech32 , HRP } from '@harmony-js/crypto' ; const packageInfo = { version : '1.0.0' } ; export interface ArgsResolver { newArgs : any ; id : number ; params : any ; newMethod : string ; callback : ( error : any , res ? : any ) => void ; } export interface HDOptions { menmonic ? : string ; index : number ; addressCount : number ; } export interface ChainOptions { shardID : number ; chainType : ChainType ; chainId : ChainID ; } export interface TransactionOptions { gasLimit : string ; gasPrice : string ; } export class TruffleProvider extends HDNode { constructor ( provider = 'http://localhost:9500' , hdOptions = { menmonic : undefined , index : 0 , addressCount : 1 , } , chainOptions = { shardID : 0 , chainType : ChainType . Harmony , chainId : ChainID . HmyLocal , } , transactionOptions = { gasLimit : '10000000' , gasPrice : '20000000000' , } ) { super ( provider , hdOptions . menmonic , hdOptions . index , hdOptions . addressCount , chainOptions . shardID , chainOptions . chainType , chainOptions . chainId , transactionOptions . gasLimit , transactionOptions . gasPrice ) ; } async send ( ... args ) { const { newArgs , id , params , newMethod , callback } = this . resolveArgs ( ... args ) ; switch ( newMethod ) { case 'hmy_accounts' : { const accounts = this . getAccounts ( ) ; callback ( null , { result : accounts , id , jsonrpc : '2.0' , } ) ; return { result : accounts , id , jsonrpc : '2.0' , } ; } case 'hmy_sendTransaction' : { const txObj = params [ 0 ] ; const rawTxn = await this . signTransaction ( txObj ) ; const result = await this . provider . send ( { id , method : 'hmy_sendRawTransaction' , params : [ rawTxn ] , jsonrpc : '2.0' , } , ( err , res ) => this . resolveCallback ( err , res , callback ) ) ; return this . resolveResult ( result ) ; } case 'hmy_getTransactionReceipt' : { const result = await this . provider . send ( { id , method : 'hmy_getTransactionReceipt' , params : [ params [ 0 ] ] , jsonrpc : '2.0' , } , ( err , res ) => { try { if ( err ) { callback ( err ) ; } const response = this . resolveResult ( res ) ; if ( response . result !== null ) { response . result . status = '0x1' ; } callback ( null , response ) ; } catch ( error ) { throw error ; } } ) ; return this . resolveResult ( result ) ; } case 'net_version' : { callback ( null , { result : String ( this . messenger . chainId ) , id , jsonrpc : '2.0' , } ) ; return { result : String ( this . messenger . chainId ) , id , jsonrpc : '2.0' , } ; } case 'web3_clientVersion' : { callback ( null , { result : `Harmony/${ packageInfo . version }/@harmony-js` , id , jsonrpc : '2.0' , } ) ; return { result : `Harmony/${ packageInfo . version }/@harmony-js` , id , jsonrpc : '2.0' , } ; } case 'hmy_getBlockByNumber' : { const result = await this . provider . send ( newArgs , ( err , res ) => { try { if ( err ) { callback ( err ) ; } const response = this . resolveResult ( res ) ; if ( response . error ) { callback ( response . error ) ; return ; } if ( new Unit ( response . result . gasLimit ) . asWei ( ) . toWei ( ) . gt ( new Unit ( this . gasLimit ) . asWei ( ) . toWei ( ) ) ) { response . result . gasLimit = `0x${ new Unit ( this . gasLimit ) . asWei ( ) . toWei ( ) . toString ( 'hex' ) }` ; } if ( isBech32Address ( response . result . miner ) ) { response . result . miner = fromBech32 ( response . result . miner , HRP ) ; } callback ( null , response ) ; } catch ( error ) { throw error ; } } ) ; return this . resolveResult ( result ) ; } default : { const result = await this . provider . send ( newArgs , ( err , res ) => this . resolveCallback ( err , res , callback ) ) ; return this . resolveResult ( result ) ; } } } sendAsync ( ... args ) { return this . send ( ... args ) ; } resolveArgs ( ... args ) { const method = args [ 0 ] . method ; const params = args [ 0 ] . params ; let newMethod = method ; if ( method . startsWith ( 'eth' ) ) { newMethod = method . replace ( 'eth' , 'hmy' ) ; } args [ 0 ] . method = newMethod ; const { id } = args [ 0 ] ; return { newArgs : args [ 0 ] , id , params , newMethod , callback : args [ 1 ] , } ; } resolveResult = ( response ) => { const final = response . getRaw || response ; delete final . req ; delete final . responseType ; return final ; } ; resolveCallback = ( err , res , callback ) => { try { if ( err ) { callback ( err ) ; } const response = this . resolveResult ( res ) ; callback ( null , response ) ; } catch ( error ) { throw error ; } } ; }