11import { injectable } from "inversify" ;
2+ import { Web3Instance } from "../../blockchain/Web3Instance" ;
3+ import { IWeb3 } from "../../blockchain/IWeb3" ;
4+ import { TransactionBase } from "../bean/TransactionBase" ;
25let solc = require ( 'solc' )
36let fs = require ( 'fs' )
47let nodePath = require ( 'path' )
58
69@injectable ( )
710export class ContractService {
811
9- getAbi ( contractName : string , source : string , path : string ) {
12+ getAbi ( contractName : string , source : string , path : string ) : any {
1013 const contract = this . compileContract ( contractName , source , path )
1114 return contract . abi
1215 }
1316
14- compileContract ( contractName : string , source : string , path : string ) {
17+ compileContract ( contractName : string , source : string , path : string ) : any {
1518 const compileJson = this . generateCompileObject ( contractName , source , path )
1619 const compiledContract = JSON . parse ( solc . compileStandardWrapper ( JSON . stringify ( compileJson ) ) )
1720 const contractWithExt = `${ contractName } .sol`
@@ -22,6 +25,33 @@ export class ContractService {
2225 return contract
2326 }
2427
28+ async deployContractSource < T extends TransactionBase > ( web3config : any , contractName : string , source : string , path : string , txBase : T ) : Promise < any > {
29+ const iWeb3 : IWeb3 = new Web3Instance ( web3config )
30+ const web3 = iWeb3 . getInstance ( )
31+ const compiledContract = this . compileContract ( contractName , source , path )
32+ const bytecode = compiledContract . evm . bytecode . object
33+ return this . sendTx ( web3 , null , bytecode , txBase . from , txBase . gas , txBase . gasPrice , txBase . value )
34+ }
35+
36+ async runFunction < T extends TransactionBase > ( web3config : any , abi : any , params : string [ ] , to : string , txBase : T ) : Promise < any > {
37+ const iWeb3 : IWeb3 = new Web3Instance ( web3config )
38+ const web3 = iWeb3 . getInstance ( )
39+ const functionCallEncoded : string = web3 . eth . abi . encodeFunctionCall ( abi , params )
40+ return this . sendTx ( web3 , to , functionCallEncoded , txBase . from , txBase . gas , txBase . gasPrice , txBase . value )
41+ }
42+
43+ private async sendTx ( web3 : any , to : string , input : string , from ?: string , gas ?: number , gasPrice ?: number , value ?: number ) : Promise < any > {
44+ const accounts = await web3 . eth . getAccounts ( )
45+ return web3 . eth . sendTransaction ( {
46+ to,
47+ from : from || accounts [ 0 ] ,
48+ gas,
49+ gasPrice,
50+ value,
51+ input
52+ } ) ;
53+ }
54+
2555 private generateCompileObject ( contractName : string , content : string , path : string ) {
2656 const sources = { }
2757 sources [ `${ contractName } .sol` ] = {
0 commit comments