-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
198 lines (167 loc) · 4.63 KB
/
app.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* DECLARATION
*/
// const endpoint = "http://127.0.0.1:8888";
const endpoint = "http://jungle.atticlab.net:8888";
// const chainId = 'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f' //local
const chainId = 'e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473' //jungle
// const contract = "bob";
const contract = "eostestingaa";
let _eos, _account;
let scatter;
/**
* INITIALIZATION
*/
const rpc = new eosjs_jsonrpc.JsonRpc(endpoint);
//network details to which scatter will connect
const network = ScatterJS.Network.fromJson({
blockchain: 'eos',
chainId: chainId,
host: endpoint,
port: 8888,
protocol: 'http'
});
ScatterJS.plugins(new ScatterEOS()); //initiating scatter
try {
//start to connect to scatter client on user's pc
ScatterJS.scatter.connect(contract).then(connected => {
// User does not have Scatter Desktop, Mobile or Classic installed.
if (!connected) return alert("Issue Connecting");
//get scatter in a global var, to use it for later actions
scatter = ScatterJS.scatter;
const requiredFields = {
accounts: [network]
};
//get the user account's identity, show popup if its not connected yet
scatter.getIdentity(requiredFields).then(async () => {
_account = scatter.identity.accounts.find(
x => x.blockchain === "eos" //get only eos bc accounts, because scatter is also used for other bcs as well other than eos
)
console.log(_account);
$('#lblAccount').text('Account: '+_account.name)
// Get a proxy reference to eosjs which you can use to sign transactions with a user's Scatter.
_eos = scatter.eos(network, eosjs_api.Api, {rpc});
initialize()
});
window.ScatterJS = null; //after getiing details nullify scatter objecct, for security
});
}
catch (error) {
console.log(error);
}
/**
* METHODS
*/
async function initialize () {
let blnc = await getTokenBalance(_account.name, 'TKN');
$('#lblBalance').text('Token Balance: ' + blnc)
}
async function getTokenBalance (account, token_symbol) {
let blnc = await rpc.get_currency_balance(contract, account, token_symbol);
return blnc;
}
function createToken () {
let supply = document.getElementById('txtToken').value;
_eos.transact (
{
actions: [
{
account: contract,
name: "create",
authorization: [
{
actor: _account.name,
permission: _account.authority
}
],
data: {
issuer: _account.name,
maximum_supply: supply,
}
}
]
},
{
blocksBehind: 3,
expireSeconds: 90
}
)
.then(data => {
alert('Token Created! Transaction Id. ' + data.transaction_id);
console.log(data);
})
.catch(err => {
console.log('Err '+ err);
});
}
function issueToken () {
let token = document.getElementById('txtIssueTokens').value;
_eos.transact (
{
actions: [
{
account: contract,
name: "issue",
authorization: [
{
actor: _account.name,
permission: _account.authority
}
],
data: {
to: _account.name,
quantity: token,
memo: "issue memo",
}
}
]
},
{
blocksBehind: 3,
expireSeconds: 90
}
)
.then(data => {
alert('Token Issued! Transaction Id. ' + data.transaction_id);
console.log(data);
})
.catch(err => {
console.log('Err '+ err);
});
}
function transferToken () {
let token = document.getElementById('txtTransferTokens').value;
_eos.transact (
{
actions: [
{
account: contract,
name: "transfer",
authorization: [
{
actor: _account.name,
permission: _account.authority
}
],
data: {
from: _account.name,
to: "eostestingab",
quantity: token,
memo: "transfer memo",
}
}
]
},
{
blocksBehind: 3,
expireSeconds: 90
}
)
.then(data => {
alert('Token Tranfered! Transaction Id. ' + data.transaction_id);
console.log(data);
})
.catch(err => {
console.log('Err '+ err);
});
}