1
- import { asciiToHex } from 'web3-utils' ;
2
1
import { IPluginLedgerConnector } from '@hyperledger-labs/bif-core-api' ;
3
2
import { Logger , LoggerProvider } from '@hyperledger-labs/bif-common' ;
4
3
5
4
import Web3 from 'web3' ;
6
5
import { Contract , ContractSendMethod , ContractOptions , DeployOptions , SendOptions } from 'web3-eth-contract/types/index' ;
7
- import { PromiEvent , TransactionReceipt } from 'web3-core/types/index' ;
6
+ import { PromiEvent } from 'web3-core/types/index' ;
8
7
9
8
export interface IPluginLedgerConnectorQuorumOptions {
10
9
rpcApiHttpHost : string ;
11
10
}
12
11
12
+ /**
13
+ * FIXME: This is under construction.
14
+ */
13
15
export interface ITransactionOptions {
14
16
privateKey ?: string ;
15
17
}
16
18
17
- export class PluginLedgerConnectorQuorum implements IPluginLedgerConnector < any , any > {
19
+ export interface IQuorumDeployContractOptions {
20
+ ethAccountUnlockPassword : string ; // The decryption key for geth to unlock the account
21
+ fromAddress : string ; // The address to use
22
+ contractSourceCode ?: string ; // if provided then compile the contract through the API
23
+ contractJsonArtifact ?: { bytecode : string } ; // use this if provided (the pre-compiled JSON artifact)
24
+ gas ?: number ;
25
+ gasPrice ?: number ;
26
+ }
27
+
28
+ export class PluginLedgerConnectorQuorum implements IPluginLedgerConnector < any , Contract > {
18
29
19
30
private readonly web3 : Web3 ;
20
31
private readonly log : Logger ;
@@ -37,68 +48,39 @@ export class PluginLedgerConnectorQuorum implements IPluginLedgerConnector<any,
37
48
return new this . web3 . eth . Contract ( contractJsonArtifact . abi , address , contractOptions ) ;
38
49
}
39
50
40
- public async deployContract ( options : any ) : Promise < any > {
51
+ public async deployContract ( options : IQuorumDeployContractOptions ) : Promise < Contract > {
52
+
53
+ if ( ! options . contractJsonArtifact ) {
54
+ throw new Error ( `PluginLedgerConnectorQuorum#deployContract() options.contractJsonArtifact falsy.` ) ;
55
+ }
56
+
41
57
try {
42
- const ethPassword = '' ;
43
- const unlocked : boolean = await this . web3 . eth . personal . unlockAccount ( options . from , ethPassword , 3600 ) ;
58
+ const unlocked : boolean = await this . web3 . eth . personal . unlockAccount ( options . fromAddress , options . ethAccountUnlockPassword , 3600 ) ;
44
59
this . log . debug ( `Web3 Account unlock outcome: ${ unlocked } ` ) ;
45
60
} catch ( ex ) {
46
- throw new Error ( `PluginLedgerConnectorQuorum#deployContract() failed to unlock account ${ options . from } : ${ ex . stack } ` ) ;
61
+ throw new Error ( `PluginLedgerConnectorQuorum#deployContract() failed to unlock account ${ options . fromAddress } : ${ ex . stack } ` ) ;
47
62
}
48
63
49
- const fromAddress = options . from ;
50
- const contract : Contract = this . instantiateContract ( options . contractJsonArtifact , fromAddress ) ;
64
+ const contract : Contract = this . instantiateContract ( options . contractJsonArtifact , options . fromAddress ) ;
51
65
this . log . debug ( `Instantiated contract OK` ) ;
52
66
53
- let nonce : number ;
54
- try {
55
- this . log . debug ( `Getting transaction count (nonce for account)` ) ;
56
- nonce = await this . web3 . eth . getTransactionCount ( fromAddress ) ;
57
- this . log . debug ( `Transaction count (nonce) acquird OK: ${ nonce } ` ) ;
58
- } catch ( ex ) {
59
- throw new Error ( `Failed to obtain nonce: ${ ex . stack } ` ) ;
60
- }
61
-
62
67
const deployOptions : DeployOptions = {
63
68
data : '0x' + options . contractJsonArtifact . bytecode ,
64
69
} ;
65
70
this . log . debug ( `Calling contract deployment...` ) ;
66
71
const deployTask : ContractSendMethod = contract . deploy ( deployOptions ) ;
67
72
this . log . debug ( `Called deploy task OK with options: ` , { deployOptions } ) ;
68
73
69
- // try {
70
- // this.log.debug(`Asking ledger for gas estimate...`);
71
- // const gasEstimate = await deployTask.estimateGas({ gas: 5000000, });
72
- // this.log.debug(`Got GasEstimate=${gasEstimate}`);
73
- // // const gas = gasEstimate * 3; // offer triple the gas estimate to be sure
74
- // } catch (ex) {
75
- // throw new Error(`PluginLedgerConnectorQuorum#deployContract() failed to get gas estimate: ${ex.stack}`);
76
- // }
77
-
78
74
const sendOptions : SendOptions = {
79
- from : fromAddress ,
80
- gas : 1500000 ,
81
- gasPrice : '0' ,
75
+ from : options . fromAddress ,
76
+ gas : options . gas || 1000000 ,
77
+ gasPrice : ` ${ options . gasPrice || 0 } ` ,
82
78
} ;
83
79
84
- this . log . debug ( `Calling send on deploy task...` ) ;
80
+ this . log . debug ( `Calling send on deploy task...` , { sendOptions } ) ;
85
81
const promiEventContract : PromiEvent < Contract > = deployTask . send ( sendOptions ) ;
86
82
this . log . debug ( `Called send OK with options: ` , { sendOptions } ) ;
87
83
88
- // promiEventContract
89
- // .once('confirmation', (confNumber: number, receipt: TransactionReceipt) => {
90
- // this.log.debug(`deployContract() - confirmation: `, { confNumber, receipt });
91
- // })
92
- // .once('error', (error: Error) => {
93
- // this.log.error(`deployContract() - error: `, error);
94
- // })
95
- // .once('receipt', (receipt: TransactionReceipt) => {
96
- // this.log.debug(`deployContract() - receipt: `, { receipt });
97
- // })
98
- // .once('transactionHash', (receipt: string) => {
99
- // this.log.debug(`deployContract() - transactionHash: `, { receipt });
100
- // });
101
-
102
84
try {
103
85
this . log . debug ( `Starting await for contract deployment promise...` ) ;
104
86
const deployedContract : Contract = await promiEventContract ;
0 commit comments