Skip to content

Commit b6ab258

Browse files
committed
feat(quorum): code cleanup
Signed-off-by: Peter Somogyvari <[email protected]>
1 parent 9d8ece1 commit b6ab258

File tree

2 files changed

+41
-51
lines changed

2 files changed

+41
-51
lines changed

packages/bif-plugin-ledger-connector-quorum/src/main/typescript/plugin-ledger-connector-quorum.ts

+27-45
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
import { asciiToHex } from 'web3-utils';
21
import { IPluginLedgerConnector } from '@hyperledger-labs/bif-core-api';
32
import { Logger, LoggerProvider } from '@hyperledger-labs/bif-common';
43

54
import Web3 from 'web3';
65
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';
87

98
export interface IPluginLedgerConnectorQuorumOptions {
109
rpcApiHttpHost: string;
1110
}
1211

12+
/**
13+
* FIXME: This is under construction.
14+
*/
1315
export interface ITransactionOptions {
1416
privateKey?: string;
1517
}
1618

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> {
1829

1930
private readonly web3: Web3;
2031
private readonly log: Logger;
@@ -37,68 +48,39 @@ export class PluginLedgerConnectorQuorum implements IPluginLedgerConnector<any,
3748
return new this.web3.eth.Contract(contractJsonArtifact.abi, address, contractOptions);
3849
}
3950

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+
4157
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);
4459
this.log.debug(`Web3 Account unlock outcome: ${unlocked}`);
4560
} 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}`);
4762
}
4863

49-
const fromAddress = options.from;
50-
const contract: Contract = this.instantiateContract(options.contractJsonArtifact, fromAddress);
64+
const contract: Contract = this.instantiateContract(options.contractJsonArtifact, options.fromAddress);
5165
this.log.debug(`Instantiated contract OK`);
5266

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-
6267
const deployOptions: DeployOptions = {
6368
data: '0x' + options.contractJsonArtifact.bytecode,
6469
};
6570
this.log.debug(`Calling contract deployment...`);
6671
const deployTask: ContractSendMethod = contract.deploy(deployOptions);
6772
this.log.debug(`Called deploy task OK with options: `, { deployOptions });
6873

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-
7874
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}`,
8278
};
8379

84-
this.log.debug(`Calling send on deploy task...`);
80+
this.log.debug(`Calling send on deploy task...`, { sendOptions });
8581
const promiEventContract: PromiEvent<Contract> = deployTask.send(sendOptions);
8682
this.log.debug(`Called send OK with options: `, { sendOptions });
8783

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-
10284
try {
10385
this.log.debug(`Starting await for contract deployment promise...`);
10486
const deployedContract: Contract = await promiEventContract;

packages/bif-plugin-ledger-connector-quorum/src/test/typescript/integration/plugin-ledger-connector-quorum/deploy-contract/deploy-contract-from-json.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// tslint:disable-next-line: no-var-requires
22
const tap = require('tap');
3+
import { Contract, SendOptions } from 'web3-eth-contract/types/index';
34
import { PluginLedgerConnectorQuorum, PluginFactoryLedgerConnector } from '../../../../../main/typescript/public-api';
45
import { QuorumTestLedger, IQuorumGenesisOptions, IAccount } from '@hyperledger-labs/bif-test-tooling';
56
import HelloWorldContractJson from '../../../../solidity/hello-world-contract/HelloWorld.json';
67
import { Logger, LoggerProvider } from '@hyperledger-labs/bif-common';
8+
import { IQuorumDeployContractOptions } from '../../../../../main/typescript/plugin-ledger-connector-quorum';
79

810
const log: Logger = LoggerProvider.getOrCreate({ label: 'test-deploy-contract-from-json', level: 'trace' })
911

@@ -36,15 +38,21 @@ tap.test('deploys contract via .json file', async (assert: any) => {
3638
const factory = new PluginFactoryLedgerConnector();
3739
const connector: PluginLedgerConnectorQuorum = await factory.create({ rpcApiHttpHost });
3840

39-
const options = {
40-
from: firstHighNetWorthAccount, // 0xed9d02e382b34818e88b88a309c7fe71e65f419d from the gensis json alloc property
41+
const options: IQuorumDeployContractOptions = {
42+
ethAccountUnlockPassword: '',
43+
fromAddress: firstHighNetWorthAccount,
4144
contractJsonArtifact: HelloWorldContractJson,
42-
// gas: 100000000000,
43-
// gasPrice: 0,
4445
};
4546

46-
const out = await connector.deployContract(options);
47-
assert.ok(out);
47+
const contract: Contract = await connector.deployContract(options);
48+
assert.ok(contract);
49+
50+
const contractMethod = contract.methods.sayHello();
51+
assert.ok(contractMethod);
52+
53+
const callResponse = await contractMethod.call({ from: firstHighNetWorthAccount });
54+
log.debug(`Got message from smart contract method:`, { callResponse });
55+
assert.ok(callResponse);
4856

4957
assert.end();
5058
log.debug('Assertion ended OK.');

0 commit comments

Comments
 (0)