FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Zilliqa-JavaScript-Library/examples/deployContract.js at v2.0.0 · javascriptit/Zilliqa-JavaScript-Library · GitHub
javascriptit
Zilliqa-JavaScript-Library
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Zilliqa-JavaScript-Library
/
examples
/
deployContract.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
154 lines (127 loc) · 4.99 KB
Breadcrumbs
Zilliqa-JavaScript-Library
/
examples
/
deployContract.js
Copy path
File metadata and controls
154 lines (127 loc) · 4.99 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (C) 2018 Zilliqa
//
// This file is part of Zilliqa-Javascript-Library.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const
{
BN
,
Long
,
bytes
,
units
}
=
require
(
'@zilliqa-js/util'
)
;
const
{
Zilliqa
}
=
require
(
'@zilliqa-js/zilliqa'
)
;
const
{
toBech32Address
,
getAddressFromPrivateKey
,
}
=
require
(
'@zilliqa-js/crypto'
)
;
const
zilliqa
=
new
Zilliqa
(
'https://dev-api.zilliqa.com'
)
;
// These are set by the core protocol, and may vary per-chain.
// You can manually pack the bytes according to chain id and msg version.
// For more information: https://apidocs.zilliqa.com/?shell#getnetworkid
const
chainId
=
333
;
// chainId of the developer testnet
const
msgVersion
=
1
;
// current msgVersion
const
VERSION
=
bytes
.
pack
(
chainId
,
msgVersion
)
;
// Populate the wallet with an account
const
privateKey
=
'deb5c896228f8515146aa16f94a558ba14e52d8496b4b267b2d59cd9036f39a6'
;
zilliqa
.
wallet
.
addByPrivateKey
(
privateKey
)
;
const
address
=
getAddressFromPrivateKey
(
privateKey
)
;
console
.
log
(
`My account address is:
${
address
}
`
)
;
console
.
log
(
`My account bech32 address is:
${
toBech32Address
(
address
)
}
`
)
;
async
function
testBlockchain
(
)
{
try
{
// Get Balance
const
balance
=
await
zilliqa
.
blockchain
.
getBalance
(
address
)
;
// Get Minimum Gas Price from blockchain
const
minGasPrice
=
await
zilliqa
.
blockchain
.
getMinimumGasPrice
(
)
;
// Account balance (See note 1)
console
.
log
(
`Your account balance is:`
)
;
console
.
log
(
balance
.
result
)
;
console
.
log
(
`Current Minimum Gas Price:
${
minGasPrice
.
result
}
`
)
;
const
myGasPrice
=
units
.
toQa
(
'1000'
,
units
.
Units
.
Li
)
;
// Gas Price that will be used by all transactions
console
.
log
(
`My Gas Price
${
myGasPrice
.
toString
(
)
}
`
)
;
const
isGasSufficient
=
myGasPrice
.
gte
(
new
BN
(
minGasPrice
.
result
)
)
;
// Checks if your gas price is less than the minimum gas price
console
.
log
(
`Is the gas price sufficient?
${
isGasSufficient
}
`
)
;
// Deploy a contract
console
.
log
(
`Deploying a new contract....`
)
;
const
code
=
`scilla_version 0
(* HelloWorld contract *)
import ListUtils
(***************************************************)
(* Associated library *)
(***************************************************)
library HelloWorld
let not_owner_code = Int32 1
let set_hello_code = Int32 2
(***************************************************)
(* The contract definition *)
(***************************************************)
contract HelloWorld
(owner: ByStr20)
field welcome_msg : String = ""
transition setHello (msg : String)
is_owner = builtin eq owner _sender;
match is_owner with
| False =>
e = {_eventname : "setHello()"; code : not_owner_code};
event e
| True =>
welcome_msg := msg;
e = {_eventname : "setHello()"; code : set_hello_code};
event e
end
end
transition getHello ()
r <- welcome_msg;
e = {_eventname: "getHello()"; msg: r};
event e
end`
;
const
init
=
[
// this parameter is mandatory for all init arrays
{
vname
:
'_scilla_version'
,
type
:
'Uint32'
,
value
:
'0'
,
}
,
{
vname
:
'owner'
,
type
:
'ByStr20'
,
value
:
`
${
address
}
`
,
}
,
]
;
const
contract
=
zilliqa
.
contracts
.
new
(
code
,
init
)
;
// Deploy the contract.
// Also notice here we have a default function parameter named toDs as mentioned above.
// A contract can be deployed at either the shard or at the DS. Always set this value to false.
const
[
deployTx
,
deployedContract
]
=
await
contract
.
deployWithoutConfirm
(
{
version
:
VERSION
,
gasPrice
:
myGasPrice
,
gasLimit
:
Long
.
fromNumber
(
10000
)
,
}
,
false
,
)
;
// check the pending status
const
pendingStatus
=
await
zilliqa
.
blockchain
.
getPendingTxn
(
deployTx
.
id
)
;
console
.
log
(
`Pending status is: `
)
;
console
.
log
(
pendingStatus
)
;
// process confirm
console
.
log
(
`The transaction id is:`
,
deployTx
.
id
)
;
console
.
log
(
`Waiting transaction be confirmed`
)
;
const
confirmedTxn
=
await
deployTx
.
confirm
(
deployTx
.
id
)
;
console
.
log
(
`The transaction status is:`
)
;
console
.
log
(
confirmedTxn
.
receipt
)
;
if
(
confirmedTxn
.
receipt
.
success
===
true
)
{
console
.
log
(
`Contract address is:
${
deployedContract
.
address
}
`
)
;
}
}
catch
(
err
)
{
console
.
log
(
err
)
;
}
}
testBlockchain
(
)
;
Back
|
FazBrowse Home
|
New Git URL