Skip to content

Commit b2d2a7b

Browse files
Merge pull request #206 from saoussen/improve_besu_cli
add command parameters to handle account address n addition to account index, add to customize network parameters
2 parents c623725 + 86914cf commit b2d2a7b

File tree

6 files changed

+226
-31
lines changed

6 files changed

+226
-31
lines changed

samples/besu/besu-cli/src/commands/asset/claim.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const command: GluegunCommand = {
1717
print,
1818
toolbox,
1919
`besu-cli asset claim --network=network1 --lock_contract_id=lockContractID --recipient_account=2 --preimage=preimage`,
20-
'besu-cli asset claim --network=<network1|network2> --lock_contract_id=<lockContractID> --recipient_account=<2|1> --preimage=<preimage>',
20+
'besu-cli asset claim --network=<network1|network2> --lock_contract_id=<lockContractID> --recipient_account=<2|1> --preimage=<preimage> --recipient_account_address=<recipient-account-address> --network_port=<port> --network_host=<host>',
2121
[
2222
{
2323
name: '--network',
@@ -38,7 +38,22 @@ const command: GluegunCommand = {
3838
name: '--preimage',
3939
description:
4040
'The preimage of hash with which the asset was locked with.'
41-
}
41+
},
42+
{
43+
name: '--recipient_account_address',
44+
description:
45+
'The address of the recipient account. We can set this parameter if we want to use account address instead of account index '
46+
},
47+
{
48+
name: '--network_host',
49+
description:
50+
'The network host. Default value is taken from config.json'
51+
},
52+
{
53+
name: '--network_port',
54+
description:
55+
'The network port. Default value is taken from config.json'
56+
}
4257
],
4358
command,
4459
['asset', 'claim']
@@ -53,7 +68,21 @@ const command: GluegunCommand = {
5368
return
5469
}
5570
const networkConfig = getNetworkConfig(options.network)
56-
const provider = new Web3.providers.HttpProvider('http://'+networkConfig.networkHost+':'+networkConfig.networkPort)
71+
72+
var networkPort = networkConfig.networkPort
73+
if (options.network_port){
74+
networkPort = options.network_port
75+
console.log('Use network port : ', networkPort)
76+
}
77+
var networkHost = networkConfig.networkHost
78+
if(options.network_host){
79+
networkHost = options.network_host
80+
console.log('Use network host : ', networkHost)
81+
}
82+
83+
const provider = new Web3.providers.HttpProvider('http://'+networkHost+':'+networkPort)
84+
85+
5786
const web3N = new Web3(provider)
5887
const interopContract = await getContractInstance(provider, networkConfig.interopContract).catch(function () {
5988
console.log("Failed getting interopContract!");
@@ -68,6 +97,9 @@ const command: GluegunCommand = {
6897
if(options.recipient_account){
6998
recipient = accounts[options.recipient_account]
7099
}
100+
else if(options.recipient_account_address){
101+
recipient = '0x'+ options.recipient_account_address
102+
}
71103
else{
72104
print.info('Recipient account index not provided. Taking from networkConfig..')
73105
recipient = accounts[networkConfig.recipientAccountIndex]
@@ -89,7 +121,7 @@ const command: GluegunCommand = {
89121
console.log('Preimage', options.preimage)
90122

91123
const preimage_bytes32 = web3N.utils.utf8ToHex(options.preimage)
92-
console.log('Preimage bytes32:', preimage_bytes32)
124+
console.log('Preimage bytes32:', preimage_bytes32)
93125

94126
// Balance of the recipient before claiming
95127
var recipientBalance = await tokenContract.balanceOf(recipient)

samples/besu/besu-cli/src/commands/asset/get-balance.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const command: GluegunCommand = {
1717
print,
1818
toolbox,
1919
`besu-cli asset get-balance --network=network1 --account=1`,
20-
'besu-cli asset get-balance --network=<network1|network2> --account=<1|2>',
20+
'besu-cli asset get-balance --network=<network1|network2> --account=<1|2> --account_address=<account-address> --network_port=<port> --network_host=<host>',
2121
[
2222
{
2323
name: '--network',
@@ -29,6 +29,21 @@ const command: GluegunCommand = {
2929
description:
3030
'The index of the account from the list obtained through web3.eth.getAccounts(). For example, we can set Alice as accounts[1] and hence value of this parameter for Alice can be 1.'
3131
},
32+
{
33+
name: '--account_address',
34+
description:
35+
'The address of the account. We can set this parameter if we want to use account address instead of account index '
36+
},
37+
{
38+
name: '--network_host',
39+
description:
40+
'The network host. Default value is taken from config.json'
41+
},
42+
{
43+
name: '--network_port',
44+
description:
45+
'The network port. Default value is taken from config.json'
46+
}
3247
],
3348
command,
3449
['asset', 'get-balance']
@@ -37,25 +52,43 @@ const command: GluegunCommand = {
3752
}
3853
print.info('Get account balance of tokens')
3954
const networkConfig = getNetworkConfig(options.network)
40-
const provider = new Web3.providers.HttpProvider('http://'+networkConfig.networkHost+':'+networkConfig.networkPort)
55+
56+
var networkPort = networkConfig.networkPort
57+
if(options.network_port){
58+
networkPort = options.network_port
59+
console.log('Use network port : ', networkPort)
60+
}
61+
var networkHost = networkConfig.networkHost
62+
if(options.network_host){
63+
networkHost = options.network_host
64+
console.log('Use network host : ', networkHost)
65+
}
66+
67+
const provider = new Web3.providers.HttpProvider('http://'+networkHost+':'+networkPort)
4168
const web3N = new Web3(provider)
4269
const accounts = await web3N.eth.getAccounts()
4370

4471
const tokenContract = await getContractInstance(provider, networkConfig.tokenContract).catch(function () {
4572
console.log("Failed getting tokenContract!");
4673
})
47-
48-
if(!options.account){
74+
75+
var accountAddress
76+
if(options.account){
77+
accountAddress = accounts[options.account]
78+
}
79+
else if(options.account_address){
80+
accountAddress = '0x'+ options.account_address
81+
}
82+
else{
4983
print.error('Account index not provided')
5084
return
5185
}
52-
const accountAddress = accounts[options.account]
5386

5487
console.log('Parameters')
5588
console.log('networkConfig', networkConfig)
5689
console.log('Account', accountAddress)
5790
var balance = await tokenContract.balanceOf(accountAddress)
58-
console.log(`Account balance of accounts[${options.account}] in Network ${options.network}: ${balance.toString()}`)
91+
console.log(`Account balance of ${accountAddress} in Network ${options.network}: ${balance.toString()}`)
5992
}
6093
}
6194

samples/besu/besu-cli/src/commands/asset/is-locked.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const command: GluegunCommand = {
1717
print,
1818
toolbox,
1919
`besu-cli asset is-locked --network=network1 --lock_contract_id=lockContractID`,
20-
'besu-cli asset is-locked --network=<network1|network2> --lock_contract_id=<lockContractID>',
20+
'besu-cli asset is-locked --network=<network1|network2> --lock_contract_id=<lockContractID> --network_port=<port> --network_host=<host>',
2121
[
2222
{
2323
name: '--network',
@@ -29,6 +29,16 @@ const command: GluegunCommand = {
2929
description:
3030
'The address / ID of the lock contract.'
3131
},
32+
{
33+
name: '--network_host',
34+
description:
35+
'The network host. Default value is taken from config.json'
36+
},
37+
{
38+
name: '--network_port',
39+
description:
40+
'The network port. Default value is taken from config.json'
41+
}
3242
],
3343
command,
3444
['asset', 'is-locked']
@@ -46,9 +56,20 @@ const command: GluegunCommand = {
4656
console.log('Parameters')
4757
console.log('networkConfig', networkConfig)
4858
console.log('Lock Contract ID', lockContractId)
49-
50-
const provider = new Web3.providers.HttpProvider('http://'+networkConfig.networkHost+':'+networkConfig.networkPort)
51-
const web3N = new Web3(provider)
59+
60+
var networkPort = networkConfig.networkPort
61+
if(options.network_port){
62+
networkPort = options.network_port
63+
console.log('Use network port : ', networkPort)
64+
}
65+
var networkHost = networkConfig.networkHost
66+
if(options.network_host){
67+
networkHost = options.network_host
68+
console.log('Use network host : ', networkHost)
69+
}
70+
71+
const provider = new Web3.providers.HttpProvider('http://'+networkHost+':'+networkPort)
72+
const web3N = new Web3(provider)
5273
const interopContract = await getContractInstance(provider, networkConfig.interopContract)
5374
const accounts = await web3N.eth.getAccounts()
5475
var sender = accounts[networkConfig.senderAccountIndex]

samples/besu/besu-cli/src/commands/asset/issue.ts

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const command: GluegunCommand = {
1616
print,
1717
toolbox,
1818
`besu-cli asset issue --network=network1 --account=1 --amount=10`,
19-
'besu-cli asset issue --network=<network1|network2> --account=<account-index> --amount=<issue-ammount>',
19+
'besu-cli asset issue --network=<network1|network2> --account=<account-index> --account_address=<account-address> --contract_owner_address=<contract-owner-address> --amount=<issue-amount> --network_port=<port> --network_host=<host> ' ,
2020
[
2121
{
2222
name: '--network',
@@ -28,10 +28,29 @@ const command: GluegunCommand = {
2828
description:
2929
'The index of the account from the list obtained through web3.eth.getAccounts(). For example, we can set Alice as accounts[1] and hence value of this parameter for Alice can be 1.'
3030
},
31+
{
32+
name: '--account_address',
33+
description:
34+
'The address of the account. We can set this parameter if we want to use account address instead of account index '
35+
}, {
36+
name: '--contract_owner_address',
37+
description:
38+
'The address of the contract owner. The default value is accounts[0] '
39+
},
3140
{
3241
name: '--amount',
3342
description:
3443
'The amount to be added to the account specified on the network'
44+
},
45+
{
46+
name: '--network_host',
47+
description:
48+
'The network host. Default value is taken from config.json'
49+
},
50+
{
51+
name: '--network_port',
52+
description:
53+
'The network port. Default value is taken from config.json'
3554
}
3655
],
3756
command,
@@ -46,34 +65,56 @@ const command: GluegunCommand = {
4665
}
4766
const networkConfig = getNetworkConfig(options.network)
4867
console.log('networkConfig', networkConfig)
49-
50-
const provider = new Web3.providers.HttpProvider('http://'+networkConfig.networkHost+':'+networkConfig.networkPort)
68+
69+
var networkPort = networkConfig.networkPort
70+
if(options.network_port){
71+
networkPort = options.network_port
72+
console.log('Use network port : ', networkPort)
73+
}
74+
var networkHost = networkConfig.networkHost
75+
if(options.network_host){
76+
networkHost = options.network_host
77+
console.log('Use network host : ', networkHost)
78+
}
79+
80+
81+
const provider = new Web3.providers.HttpProvider('http://'+networkHost+':'+networkPort)
5182
const web3N = new Web3(provider)
5283
const tokenContract = await getContractInstance(provider, networkConfig.tokenContract).catch(function () {
5384
console.log("Failed getting tokenContract!");
5485
})
5586
const accounts = await web3N.eth.getAccounts()
5687

57-
var accountIndex
58-
const contractOwner = accounts[0]
88+
89+
var contractOwner = accounts[0]
90+
if(options.contract_owner_address){
91+
contractOwner = '0x'+ options.contract_owner_address
92+
}
93+
94+
var account
5995
if(options.account){
60-
accountIndex = options.account
96+
account = accounts[options.account]
6197
}
62-
else{
63-
accountIndex = networkConfig.senderAccountIndex
98+
else if(options.account_address){
99+
account = '0x'+ options.account_address
100+
}else{
101+
account = accounts[networkConfig.senderAccountIndex]
64102
}
103+
65104
if(!options.amount){
66105
print.error('Amount not provided')
67106
return
68107
}
69-
108+
109+
console.log(`Contract owner address is ${contractOwner} `)
110+
console.log(`Receiver address is ${account} `)
70111
// Transfer from the contract owner to the account specified
71-
await tokenContract.transfer(accounts[accountIndex], options.amount, {from: contractOwner}).catch(function () {
112+
await tokenContract.transfer(account, options.amount, {from: contractOwner}).catch(function () {
72113
console.log("tokenContract transfer threw an error; Probably the token supply is used up!");
73114
})
74115

75-
var balance = await tokenContract.balanceOf(accounts[options.account])
76-
console.log(`Account balance of accounts[${options.account}] in Network ${options.network}: ${balance.toString()}`)
116+
var balance = await tokenContract.balanceOf(account)
117+
console.log(`Account balance of ${account} in Network ${options.network}: ${balance.toString()}`)
77118
}
78119
}
79120

0 commit comments

Comments
 (0)