Skip to content

Instantly share code, notes, and snippets.

@AdvaithD
Created December 6, 2019 05:43
Show Gist options
  • Save AdvaithD/ba36ddb13d04db213efdc77d81ef6f95 to your computer and use it in GitHub Desktop.
Save AdvaithD/ba36ddb13d04db213efdc77d81ef6f95 to your computer and use it in GitHub Desktop.
gist.js
const config = require('../config.json')
const uuidv4 = require('uuid/v4')
const low = require('lowdb')
const log = require('npmlog')
const FileSync = require('lowdb/adapters/FileSync')
// Read from bank of keys
const readAdapter = new FileSync('../keysDB.json')
const readDB = low(readAdapter)
// Sample DB to mock medici distribution state
const writeAdapter = new FileSync('./mockMedici.json')
const sampleDB = low(writeAdapter)
const mongoose = require('mongoose')
mongoose.Promise = global.Promise
// mongoose.set('debug', true)
const Schema = mongoose.Schema
let currentExchange = 1
let currentExchangeUsers = 0
let tempPrivKey
let tempPubKey
let j
// Mongoose Schemas and Models
const exchangeSchema = new Schema({
id: Number,
name: String,
address: String,
privKey: String
})
const userSchema = new Schema({
id: Number,
exchange: String,
address: String,
privKey: String
})
const Exchange = mongoose.model('Exchange', exchangeSchema)
const User = mongoose.model('User', userSchema)
const findExchange = async identifier => {
return new Promise(async (resolve, reject) => {
try {
const res = await Exchange.findOne({ id: identifier })
if (res) {
resolve(res)
}
} catch (error) {
reject(error)
throw new Error(error)
}
})
}
const saveExchange = async ({ id, name, address, privKey }) => {
return new Promise((resolve, reject) => {
const tempExch = new Exchange({
id,
name,
address,
privKey
})
tempExch.save().then(() => {
resolve(true)
})
})
}
const saveUser = async ({ id, exchange, address, privKey }) => {
return new Promise(async (resolve, reject) => {
const tempUser = new User({
id,
exchange,
address,
privKey
})
tempUser.save().then(() => {
resolve(true)
})
})
}
const setupMongoose = async () => {
return new Promise((resolve, reject) => {
mongoose
.connect('mongodb://localhost:27017/keys', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
// console.log(mongoose.connection.collections)
mongoose.connection.collections.exchanges.drop()
mongoose.connection.collections.users.drop()
console.log('dropped collections')
resolve(true)
})
.catch(e => {
console.log(`error with dropping collections ${e}`)
reject(e)
})
})
}
const setupExchanges = async () => {
return new Promise(async (resolve, reject) => {
const totalUserKeys = (config.totalExchanges * config.usersPerExchange)
for (let i = 1; i <= config.totalExchanges; i++) {
// console.log(readDB.get('keys').find({ id: i }).value())
var tempUUID = uuidv4()
// Exchange acc created <>
// exchangeBook = new book(tempUUID)
const addy = await readDB
.get('keys')
.find({ id: i })
.value().address
const priv = await readDB
.get('keys')
.find({ id: i })
.value().privKey
// console.log(i, addy, priv)
await saveExchange({
id: i,
name: tempUUID,
address: addy,
privKey: priv
})
}
resolve(true)
})
}
const setupUsers = async () => {
for (j = config.totalExchanges; j < config.totalExchanges * config.usersPerExchange + config.totalExchanges; j++) {
// const maxRounds = config.totalExchanges;
// console.log(j+1)
while (!(currentExchange > config.totalExchanges)) {
console.log('second loop', currentExchange)
// console.log(j+1)
// console.log(`Currently on exchange # ${currentExchange}`);
// console.log(`Total users on exchange so far: ${currentExchangeUsers}`)
// console.log(`Adding user #${j+1} to exchange ${currentExchange}`);
// console.log(`CURRENT EXCHANGE: #${currentExchange}`)
// console.log(
// `USERS IN THIS EXCHANGE #${currentExchange}: ${currentExchangeUsers}`
// )
const tempMagic = config.totalExchages + currentExchange
// tempExName = sampleDB.get('exchangeKeys').find({ id: currentExchange }).value().name
tempPubKey = readDB.get('keys').find({ id: config.usersPerExchange * currentExchange + currentExchangeUsers }).value().address
tempPrivKey = readDB.get('keys').find({ id: config.usersPerExchange * currentExchange + currentExchangeUsers }).value().privKey
const exchangeFind = await findExchange(currentExchange)
const nameOfExchange = JSON.parse(exchangeFind).name
console.log('exchange name', exchangeFind)
const userId = currentExchangeUsers + 1
await saveUser({
userId,
nameOfExchange,
tempPubKey,
tempPrivKey
})
// sampleDB.get('exchanges').find({ name: tempExName }).get('users')
// .push({
// id: currentExchangeUsers + 1,
// address: tempPubKey,
// privKey: tempPrivKey,
// dollarsPerUser: config.dollarsPerUser,
// tokensPerUser: config.tokensPerUser
// })
// .write()
// console.log(`CURRENT LOOP USER: ${currentExchangeUsers}`)
currentExchangeUsers = currentExchangeUsers + 1
console.log(`ADDED, USERS: ${currentExchangeUsers}`)
// if(currentExchangeUsers + )
if (config.usersPerExchange === currentExchangeUsers) {
currentExchange = currentExchange + 1
currentExchangeUsers = 0
}
}
/*
while(config.totalExchanges> currentExchange){
//console.log(`exchange #${currentExchange}`)
currentExchangeCounter++
console.log(currentExchangeCounter)
console.log(currentExchange)
if(currentExchangeCounter>config.usersPerExchange){
currentExchange++
}
//currentExchange++
}
*/
}
resolve(true)
}
module.exports = async function (deployer, network, accounts) {
await setupMongoose()
await setupExchanges()
await setupUsers()
/*
console.log(config)
await deployer.deploy(InxToken, accounts[0])
await deployer.deploy(TransferRestrictions, InxToken.address)
const inxInstance = await InxToken.deployed()
await inxInstance.updateTransferRestrictions(TransferRestrictions.address)
await inxInstance.addWhitelister(accounts[0], {from:accounts[0]})
//console.log(await inxInstance.isWhitelister(accounts[0]))
await inxInstance.setWhitelist('0x08b3A1817b57b759B61e7358Fa1264D280FeD731', true, 'Approved via KYC', {from:accounts[0]})
await inxInstance.setWhitelist(accounts[0], true, 'Approved because admin', {from:accounts[0]})
//console.log(await inxInstance.getWhitelistStatus('0x08b3A1817b57b759B61e7358Fa1264D280FeD731'))
//console.log(await inxInstance.getWhitelistData('0x08b3A1817b57b759B61e7358Fa1264D280FeD731'))
//console.log(await inxInstance.getWhitelistStatus(accounts[0]))
//console.log(await inxInstance.getWhitelistStatus('0x08b3A1817b57b759B61e7358Fa1264D280FeD731'))
//console.log((await inxInstance.detectTransferRestriction(accounts[0], '0x08b3A1817b57b759B61e7358Fa1264D280FeD731', 400, {from:accounts[0]})).toString())
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment