-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (98 loc) · 4.17 KB
/
index.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
const {Transactions, Managers, Utils} = require("@smartholdem/crypto");
const {Connection} = require("@smartholdem/client");
const jsonReader = require("jsonfile");
const axios = require('axios');
const {Buffer} = require("node:buffer");
const {
randomInt,
} = require('node:crypto');
const config = jsonReader.readFileSync('./config.json');
let successTransactions = jsonReader.readFileSync('./successTxs.json'); // load old transactions
// init blockchain connection
const client = new Connection(config.node);
Managers.configManager.setFromPreset("mainnet");
Managers.configManager.setHeight(1000000);
// transfer amount to winner
async function txTransfer(payload) {
const txs = [];
const senderWallet = await client.api("wallets").get(config['gameBankAddress']);
const senderNonce = Utils.BigNumber.make(senderWallet.body.data.nonce).plus(1);
const transaction = Transactions.BuilderFactory.transfer()
.fee((1e8 * config['fee']).toString()) // fee
.version(2)
.nonce(senderNonce.toFixed()) // nonce
.recipientId(payload.recipientId) // winner address
.amount((payload.amount * 1e8).toFixed(0)) // amount win
.vendorField(payload.memo) // message for winner
.sign(config['gameBankSecret']); //
txs.push(transaction.build().toJson());
//console.log('Prepared tx', txs[0]);
let broadcastResponse = {};
try {
broadcastResponse = (await client.api("transactions").create({transactions: txs})).body.data;
} catch (e) {
console.log("err: tx send");
}
return broadcastResponse;
}
async function getTransactions() {
let result = null;
try {
result = (await axios.get(config.node + '/transactions?recipientId=' + config['gameBankAddress'] + '&page=1&limit=10&type=0')).data.data;
} catch (e) {
console.log('err get transactions')
}
return result;
}
async function calcRNG(blockId) {
const rnd = await Uint8Array.from(Buffer.from(blockId, 'hex'));
console.log(rnd);
return (rnd);
}
// Game Start
async function start() {
console.log('app running with bank address', config['gameBankAddress']);
let txTimer = null;
clearInterval(txTimer);
txTimer = setInterval(async () => {
const walletTransactions = await getTransactions();
for (let i = 0; i < walletTransactions.length; i++) {
// if gaming transaction & new transaction
// memo: - = <127
// memo: + = >128
const amount = walletTransactions[i].amount / 1e8;
if ((walletTransactions[i].vendorField === '-' || walletTransactions[i].vendorField === '+') //is game tx
&& !successTransactions[walletTransactions[i].id] //isNew tx
&& amount >= config['minBet'] && amount <= config['maxBet'] // is bet range
)
{
const rngResult = (await calcRNG(walletTransactions[i].blockId))[0];
let isWin = walletTransactions[i].vendorField === '-' ? rngResult < 127 : rngResult > 128;
const amountWin = isWin ? (amount * 2) - 1 : 0; // win x2, -1 STH Fee
const bet = walletTransactions[i].vendorField === '-' ? '<127' : '>128';
const tx = {
blockId: walletTransactions[i].blockId,
playerAddress: walletTransactions[i].sender,
amount: amount,
bet: bet,
randomNumber: rngResult,
isWin: isWin,
amountWin: amountWin.toFixed(8),
};
if (isWin) {
// transfer prize transaction to winner address
await txTransfer({
recipientId: tx.playerAddress,
amount: tx.amountWin,
memo: 'DICE Win:' + bet + ':' + rngResult
})
}
// save transaction as old
successTransactions[walletTransactions[i].id] = tx;
jsonReader.writeFileSync('./successTxs.json', successTransactions, ); // save old results in file
console.log('new tx', tx);
}
}
}, 10000)
}
start();