-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsendCoins.js
111 lines (103 loc) · 3.14 KB
/
sendCoins.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
const { Users } = require("./models");
const rp = require("request-promise");
const Long = require("long");
const { toHexString, parseBal, hexBytes } = require("./utils.js");
const { Network, TransactionType, Transaction, Key } = require("semux-js");
const { faucetAddress, faucetPR } = require("./config/config-bot.json");
const API = "https://api.semux.info/v2.1.0/";
const FEE = 5000000;
async function getAddress(address) {
return JSON.parse(await rp(API + "account?address=" + address));
}
async function sendCoins(authorId, toAddress, value, msg, comment) {
let hexString = "0x746970"; // default "tip"
if (comment) {
let bytesArray = Buffer.from(comment);
hexString = "0x" + toHexString(bytesArray);
}
if (!toAddress || !value) {
return {
error: true,
reason: "Amount of SEM and Discord Username are required.",
};
}
let from = "";
if (authorId) {
from = await Users.findOne({ where: { discord_id: authorId } });
if (!from) {
return {
error: true,
reason: "You don't have account yet, type /getAddress first.",
};
}
} else {
from = {
address: faucetAddress,
private_key: faucetPR,
};
}
var isFrom = await getAddress(from.address);
try {
await getAddress(toAddress);
} catch (e) {
return { error: true, reason: "Wrong recipient, try another one." };
}
if (value.includes(",")) value = value.replace(/,/g, ".");
let amount = parseFloat(value);
if (!amount) return { error: true, reason: "Amount is not correct." };
amount = amount * Math.pow(10, 9);
if (amount < 0.000000001)
return { error: true, reason: "Wrong amount, try another one." };
// check reciever balance before transfer
const fromAddressBal = await getAddress(from.address);
let nonce =
parseInt(isFrom.result.nonce, 10) +
parseInt(isFrom.result.pendingTransactionCount, 10);
const available = parseFloat(fromAddressBal.result.available);
if (available === amount) {
amount = amount - FEE;
}
if (available < amount + FEE) {
return {
error: true,
reason: `Insufficient balance, you have **${parseBal(available)} SEM**`,
};
}
const privateKey = Key.importEncodedPrivateKey(hexBytes(from.private_key));
try {
var tx = new Transaction(
Network.MAINNET,
TransactionType.TRANSFER,
hexBytes(toAddress), // to
Long.fromNumber(amount), // value
Long.fromNumber(FEE), // fee
Long.fromNumber(nonce), // nonce
Long.fromNumber(new Date().getTime()), // timestamp
hexBytes(hexString) // data
).sign(privateKey);
} catch (e) {
console.log(e);
}
let hash = await sendToApi(tx);
if (!hash) {
return { error: true, reason: "Error while tried to create transaction." };
} else {
return { error: false, hash };
}
}
async function sendToApi(tx) {
const serialize = Buffer.from(tx.toBytes().buffer).toString("hex");
try {
var { result } = await rp({
method: "POST",
uri: `${API}transaction/raw?raw=${serialize}&validateNonce=true`,
json: true,
});
} catch (e) {
console.log(e);
}
if (result) {
return result;
}
}
module.exports = sendCoins;