forked from phantasma-io-archive/Phantasma-node-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.ts
292 lines (252 loc) · 8.65 KB
/
examples.ts
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { PhantasmaAPI, Base16, VMObject, PBinaryReader, ScriptBuilder, PhantasmaKeys, Transaction, Address, getPrivateKeyFromWif, getWifFromPrivateKey, Balance, getTokenEventData} from "phantasma-ts";
/**
* Setup the connection to the API using the testnet
*/
const CHAIN_NAME = "main"; // This is the name of the chain, please don't change it.
const NETWORK_API_URL = "https://testnet.phantasma.io/rpc"; // for mainnet this should be https://pharpc1.phantasma.io/rpc
const NETWORK_PEER_URL = undefined; // this the peers URL to get the list of peers, if not provided it will use the default one "https://peers.phantasma.io/"
const NEXUS_NAME = "testnet"; // For mainnet use this "mainnet"
const RPC = new PhantasmaAPI(
NETWORK_API_URL,
NETWORK_PEER_URL,
NEXUS_NAME
);
const GAS_LIMIT = 210000;
const GAS_PRICE = 100000;
/**
* Function to delay the execution of the code
* @param time
* @returns
*/
function delay(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
/**
* Decode the information of a VMOBject
* @param data Bytes of VMOBject (VMObject is the encoded data inside of a transaction)
* @returns
*/
function DecodeInformation(data) {
const bytes = Base16.decodeUint8Array(data.toUpperCase());
const vm = new VMObject();
const reader = new PBinaryReader(bytes);
vm.UnserializeData(reader);
return vm;
}
/**
* On Transaction Received
* @param address User that received
* @param symbol Symbol received
* @param amount Amount of the symbol received
*/
function onTransactionReceived(address, symbol, amount) {}
// Function that periodically checks the height of the chain and fetches the latest block if the height has increased
async function CheckForNewBlocks() {
// Get the current height of the chain
let newHeight = await RPC.getBlockHeight(CHAIN_NAME);
let currentHeight = newHeight;
// Check if the height has increased
if (newHeight > currentHeight) {
// Fetch the latest block
let latestBlock = await RPC.getBlockByHeight(CHAIN_NAME, newHeight);
// Check all transactions in this block
for (let i = 0; i < latestBlock.txs.length; i++) {
let tx = latestBlock.txs[i];
// Check all events in this transaction
for (let j = 0; j < tx.events.length; j++) {
let evt = tx.events[j];
if (evt.kind == "TokenReceive") {
var data = getTokenEventData(evt.data);
onTransactionReceived(evt.address, data.symbol, data.value);
}
}
}
// Update the current height of the chain
currentHeight = newHeight;
}
// Repeat this process after a delay
setTimeout(CheckForNewBlocks, 1000);
}
/*async function TransferTokens() {
let wif = "";
let keys = PhantasmaKeys.fromWIF(wif);
let fromAddress = keys.Address;
let toAddress = "P2K56BVqGndVhEmyaX9CVcqGHGkAfKUgeTnK1LfDjddqFPn";
let amount = String(1 * 10 ** 8); // 1 Soul
let payload = Base16.encode("Phantasma-NodeJS");
let gasPrice = 100000;
let gasLimit = 210000;
let sb = new ScriptBuilder();
let script = sb
.AllowGas(fromAddress.Text, Address.Null, gasPrice, gasLimit)
.CallInterop("Runtime.TransferTokens", [
fromAddress.Text,
toAddress,
"SOUL",
amount,
])
.SpendGas(fromAddress.Text)
.EndScript();
let myScript = sb.str;
let date = new Date(Date.now());
let minutes = 5;
date.setUTCMinutes(date.getUTCMinutes() + minutes);
date.setUTCHours(date.getUTCHours() + 1);
var newDateObj = new Date(date);
let transaction = new Transaction(
NEXUS_NAME, //Nexus Name
CHAIN_NAME //Chain
myScript, //In string format
newDateObj, //Date Object
payload //Extra Info to attach to Transaction in Serialized Hex
);
let txHash = await RPC.sendRawTransaction(transactionSigned);
console.log({ txHash });
await delay(3000);
let result = await GetResult(txHash);
return txHash;
}*/
/**
* Get the user balance
* @param address Address of the account you want to get the balance
* @returns
*/
async function GetUserBalance(address) : Promise<Balance[]> {
let account = await RPC.getAccount(address);
return account.balances;
}
/**
* This will return the token transfer event data
* @param data Should be from a TokenSent or TokenReceived event
* @returns {tokenSymbol, amount, chainName}
*/
async function DecodeTransactionTransferEventData(data: string) {
return getTokenEventData(data);
}
/**
* Send a transaction
* @param transaction Transaction to send already signed
*/
async function SendATransaction(transaction: Transaction){
let transactionSignedBytes = transaction.toString(true); // Get the transaction in bytes convert it to string
let txHash = await RPC.sendRawTransaction(transactionSignedBytes); // Send the transaction to the network
await delay(5000); // Wait 5 seconds or more
let result = await GetATransaction(txHash); // Get the result of the transaction
return result;
}
/**
* Sign a transaction
* @param transaction Transaction to sign
*/
function SignATransaction(transaction: Transaction){
let wif = "";
let keys = PhantasmaKeys.fromWIF(wif);
transaction.signWithKeys(keys);
return transaction;
}
/**
* Make a transaction
*/
function MakeATransaction() {
// A transaction contains the following information
// 1. Nexus Name
// 2. Chain Name
// 3. Script -> Is a set of instructions to execute on the chain.
// 4. Expiration Date
// 5. Payload
let from = "P...."; // From the address that is sending the txs
let to = "P...."; // The address that is receiving the tx
let symbol = "SOUL"; // The token Symbol to transfer
let amount = String(10 * 10 ** 8); // The amount to transfer, in this case is 10 SOUL because SOUL has 8 decimals. For KCAL is 10 ** 10
let scriptBuilder = new ScriptBuilder();
let myScript = scriptBuilder
.AllowGas(from, Address.Null, GAS_PRICE, GAS_LIMIT) // This is the gas that you are willing to pay for the transaction
.CallInterop("Runtime.TransferTokens", [from, to, symbol, amount]) // This is the function you want to call
.SpendGas(from) // This is the address that is paying the gas
.EndScript(); // This is the script you want to execute
const payload = Base16.encode("Phantasma-NodeJS"); // This is like an identifier for the transaction
// Setup the expiration date 5 minutes from now
let experiationDate = new Date(Date.now());
let minutes = 5;
experiationDate.setUTCDate(experiationDate.getUTCDate());
experiationDate.setUTCMinutes(experiationDate.getUTCMinutes() + minutes);
let transaction = new Transaction(
NEXUS_NAME, //Nexus Name
CHAIN_NAME, //Chain
myScript, //In string format
experiationDate, //Date Object // Experiation Date
payload //Extra Info to attach to Transaction in Serialized Hex
);
}
/**
* Get the result of a transaction
* @param txHash TxHash of the transaction you want to get
*/
async function GetATransaction(txHash) {
let result = await RPC.getTransaction(txHash);
console.log({ result });
return result;
}
/**
*
* @param height height of the block you want to get
* @returns
*/
async function GetBlockHeight(height) {
let result = await RPC.getBlockByHeight(CHAIN_NAME, height);
console.log({ result });
return result;
}
/**
* Returns the WIF of a private key
* @param privKey Private Key of the wallet
* @returns
*/
function GetWifFromPrivateKey(privKey)
{
return getWifFromPrivateKey(privKey);
}
/**
* Get the private key from a WIF
*/
function GetPrivateKeyFromWIF(wif){
let privKey = getPrivateKeyFromWif(wif);
return privKey;
}
/**
* Generate's a new wallet
*/
function GenerateNewWallet(){
let keys = PhantasmaKeys.generate();
console.log("Private Key: ", keys.PrivateKey);
console.log("Public Key: ", keys.PublicKey);
console.log("Address: ", keys.Address.Text);
console.log("WIF: ", keys.toWIF());
}
/**
* Generate's a wallet from a private key
* @param privKey Private Key of the wallet you want to generate
*/
function GenerateAWalletFromPrivateKey(privKey){
let wif = getWifFromPrivateKey(privKey);
let keys = PhantasmaKeys.fromWIF(wif);
console.log("Private Key: ", keys.PrivateKey);
console.log("Public Key: ", keys.PublicKey);
console.log("Address: ", keys.Address.Text);
console.log("WIF: ", keys.toWIF());
}
/**
* Generate's a wallet from a private key
* @param wif WIF of the wallet you want to generate
*/
function GenerateAWalletFromWIF(wif){
let keys = PhantasmaKeys.fromWIF(wif);
console.log("Private Key: ", keys.PrivateKey);
console.log("Public Key: ", keys.PublicKey);
console.log("Address: ", keys.Address.Text);
console.log("WIF: ", keys.toWIF());
}
console.log("Hello");
let decodeStr = "044B43414C07005039278C0400046D61696E";
console.log(await DecodeTransactionTransferEventData(decodeStr));
//TransferTokens();