Created
March 27, 2022 02:52
-
-
Save JacobThaDev/01bf7404ced1f45365b36eedf2230e4a to your computer and use it in GitHub Desktop.
A quick code snippet of how the discord bot draws a random address to receive the lottery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Transparency Notice: | |
* This snippet was stripped and modified from the actual bot in a way | |
* so it's easier to explain. The bot functions the same. -OGKingFox | |
*/ | |
// this will sync all of the people that have ever touched glow to a database. | |
// once stored, we can then select a random one. BscScan limits to 10,000 entries so | |
// if you're an old token, you're gonna need to run this first a few times... | |
await TxnUpdater.syncAddresses(); | |
// grab all holders from the database. | |
let wallets = await db.holders.findAll(); | |
let winner = await drawWinner(wallets); | |
export const drawWinner(holders){ | |
// -1 cause it starts at 0. if it draws "wallet.length" it will throw an overflow error | |
let number = Functions.randomInt(0, wallets.length - 1); | |
let wallet = wallets[number]; | |
// get contract address and the pool you wish to use for price | |
let contract = process.env.contract; | |
let pool_addr = process.env.primary_pool; | |
// need bnb price to calculate token price because it depends on it. | |
let bnb = await Binance.getBnbPrice(); | |
let price = await Token.getPrice2(contract, pool_addr, bnb); | |
// get the address's balance for the contract | |
let balance = await Functions.getBalance(wallet.account, contract); | |
// calculate total value of their bag | |
let total = parseFloat((price * balance).toFixed(2)); | |
// if the bag value is less than $20, call this function again to redraw. | |
if (total < 20) { | |
await this.drawWinner(wallets); | |
return; | |
} | |
// send message to discord or whatever here | |
// .... | |
// return the data above | |
return { | |
wallet: wallet.account, | |
balance: balance, | |
price: price, | |
total: parseFloat((price * balance).toFixed(2)) | |
}; | |
} | |
// - sepearate class | |
class TxnUpdater { | |
async syncAddresses() { | |
Logger.log("Syncing holders..."); | |
// load the cache file that stores the last block | |
let cpath = "./cache/data.json"; | |
let cache = Functions.readCache(cpath, true, true); | |
let bsc = new Binance(); | |
bsc.setContract(process.env.contract); // set the contract we need to look up | |
bsc.setStartBlock(cache.last_block); // set the block we start at | |
// grab a list of all transactions starting at the specified block number | |
let txn_list = await bsc.getTokenTx(); | |
let entries = []; | |
// loop through the transactions to store then in the array above, | |
// ensuring that they're unique by checking if they're in the array already | |
for await (let txn of txn_list) { | |
let from = txn.from ? txn.from.toLowerCase() : null; | |
let to = txn.to ? txn.to.toLowerCase() : null; | |
let map = entries.map(entry => entry.account); | |
if (from && !map.includes(from)) // push to the array if the "from" field is not already there | |
entries.push({ account: from }); | |
if (to && !map.includes(to)) // push to the array if the "to" field is not already there | |
entries.push({ account: to }); | |
} | |
// if we have some entries, we need to add them to the database. | |
if (entries.length != 0) { | |
// stores all addresses to the database in a single call. | |
await db.holders.bulkCreate(entries, { ignoreDuplicates: true }); | |
Logger.log("Database updated with new entries, if any."); | |
} | |
// now grab the last transaction in the list and store it, so when we run it again, | |
// it starts off there instead of starting over. This ensures the list is always updated. | |
cache.last_block = txn_list[txn_list.length - 1].blockNumber; | |
// then just write the data to the cache file. | |
Functions.writeCache(cpath, JSON.stringify(cache, null, 4)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment