forked from phantasma-io-archive/Phantasma-node-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.js
129 lines (109 loc) · 3.26 KB
/
examples.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
const {
PhantasmaAPI,
Base16,
VMObject,
PBinaryReader,
ScriptBuilder,
PhantasmaKeys,
Transaction,
Address,
getTokenEventData,
getPrivateKeyFromWif,
getWifFromPrivateKey,
} = require("phantasma-ts");
const CHAIN_NAME = "main";
const NETWORK_API_URL = "https://testnet.phantasma.io/rpc";
const NETWORK_PEER_URL = undefined;
const NEXUS_NAME = "testnet";
const RPC = new PhantasmaAPI(NETWORK_API_URL, NETWORK_PEER_URL, NEXUS_NAME);
const GAS_LIMIT = 2100000;
const GAS_PRICE = 100000;
function delay(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
function DecodeInformation(data) {
const bytes = Base16.decodeUint8Array(data.toUpperCase());
const vm = new VMObject();
const reader = new PBinaryReader(bytes);
vm.UnserializeData(reader);
return vm;
}
async function GetUserBalance(address) {
let account = await RPC.getAccount(address);
return account.balances;
}
async function DecodeTransactionTransferEventData(data) {
return getTokenEventData(data);
}
async function SendATransaction(transaction) {
let transactionSignedBytes = transaction.toString(true);
let txHash = await RPC.sendRawTransaction(transactionSignedBytes);
await delay(5000);
let result = await GetATransaction(txHash);
return result;
}
function SignATransaction(transaction) {
let wif = "";
let keys = PhantasmaKeys.fromWIF(wif);
transaction.signWithKeys(keys);
return transaction;
}
function MakeATransaction() {
let from = "P....";
let to = "P....";
let symbol = "SOUL";
let amount = String(10 * Math.pow(10, 8));
let scriptBuilder = new ScriptBuilder();
let myScript = scriptBuilder
.AllowGas(from, Address.Null, GAS_PRICE, GAS_LIMIT)
.CallInterop("Runtime.TransferTokens", [from, to, symbol, amount])
.SpendGas(from)
.EndScript();
const payload = Base16.encode("Phantasma-NodeJS");
let expirationDate = new Date(Date.now());
let minutes = 5;
expirationDate.setUTCMinutes(expirationDate.getUTCMinutes() + minutes);
let transaction = new Transaction(
NEXUS_NAME,
CHAIN_NAME,
myScript,
expirationDate,
payload
);
}
async function GetATransaction(txHash) {
let result = await RPC.getTransaction(txHash);
console.log({ result });
return result;
}
function GetWifFromPrivateKey(privKey) {
return getWifFromPrivateKey(privKey);
}
function GetPrivateKeyFromWIF(wif) {
let privKey = getPrivateKeyFromWif(wif);
return privKey;
}
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());
}
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());
}
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");
//TransferTokens();