Skip to content

Commit

Permalink
refactor: return same params to topWallet and WhiteList
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavShevchenko committed Jan 20, 2020
1 parent 0cca09c commit 113e772
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 28 deletions.
53 changes: 39 additions & 14 deletions src/server/blockchain/AdminWallet.js
Expand Up @@ -185,8 +185,12 @@ export class Wallet {
ContractsAddress: ContractsAddress[this.network]
})
await this.removeWhitelisted('0x6ddfF36dE47671BF9a2ad96438e518DD633A0e63').catch(_ => _)
const whitelistTest = await this.whitelistUser('0x6ddfF36dE47671BF9a2ad96438e518DD633A0e63')
const topwalletTest = await this.topWallet('0x6ddfF36dE47671BF9a2ad96438e518DD633A0e63')
const whitelistTest = await this.whitelistUser('0x6ddfF36dE47671BF9a2ad96438e518DD633A0e63', 'x')
const topwalletTest = await this.topWallet(
'0x6ddfF36dE47671BF9a2ad96438e518DD633A0e63',
moment().subtract(1, 'day'),
true
)
log.info('wallet tests:', { whitelist: whitelistTest.status, topwallet: topwalletTest.status })
} catch (e) {
log.error('Error initializing wallet', { e, errMessage: e.message })
Expand Down Expand Up @@ -294,20 +298,21 @@ export class Wallet {
/**
* whitelist an user in the `Identity` contract
* @param {string} address
* @param {string} did
* @returns {Promise<TransactionReceipt>}
*/
async whitelistUser(address: string): Promise<TransactionReceipt | boolean> {
async whitelistUser(address: string, did: string): Promise<TransactionReceipt | boolean> {
const isVerified = await this.isVerified(address)
if (isVerified) {
return { status: true }
}
const tx: TransactionReceipt = await this.sendTransaction(this.proxyContract.methods.whitelist(address)).catch(
const tx: TransactionReceipt = await this.sendTransaction(this.proxyContract.methods.whitelist(address, did)).catch(
e => {
log.error('Error whitelistUser', { e }, e.message, { address })
log.error('Error whitelistUser', { e, errMessage: e.message, address, did })
throw e
}
)
log.info('Whitelisted user', { address, tx })
log.info('Whitelisted user', { address, did, tx })
return tx
}

Expand All @@ -320,7 +325,7 @@ export class Wallet {
const tx: TransactionReceipt = await this.sendTransaction(
this.identityContract.methods.addBlacklisted(address)
).catch(e => {
log.error('Error blackListUser', { e }, e.message, { address })
log.error('Error blackListUser', { e, errMessage: e.message, address })
throw e
})

Expand Down Expand Up @@ -362,16 +367,36 @@ export class Wallet {
/**
* top wallet if needed
* @param {string} address
* @param {moment.Moment} lastTopping
* @param {boolean} force
* @returns {PromiEvent<TransactionReceipt>}
*/
async topWallet(address: string): PromiEvent<TransactionReceipt> {
const tx: TransactionReceipt = await this.sendTransaction(this.proxyContract.methods.topWallet(address)).catch(
e => {
log.error('Error topWallet', { e }, e.message, { address })
throw e
async topWallet(
address: string,
lastTopping?: moment.Moment = moment().subtract(1, 'day'),
force: boolean = false
): PromiEvent<TransactionReceipt> {
let daysAgo = moment().diff(moment(lastTopping), 'days')
if (conf.env !== 'development' && daysAgo < 1) throw new Error('Daily limit reached')
try {
let userBalance = await this.web3.eth.getBalance(address)
let maxTopWei = parseInt(web3Utils.toWei('1000000', 'gwei'))
let toTop = maxTopWei - userBalance
log.debug('TopWallet:', { userBalance, toTop })
if (toTop > 0 && (force || toTop / maxTopWei >= 0.75)) {
let res = await this.sendTransaction(this.proxyContract.methods.topWallet(address)).catch(e => {
log.error('Error topWallet', { e }, e.message, { address })
throw e
})
log.debug('Topwallet result:', { res })
return res
}
)
return tx
log.debug("User doesn't need topping")
return { status: 1 }
} catch (e) {
log.error('Error topWallet', { errMessage: e.message, address, lastTopping, force })
throw e
}
}

async getAddressBalance(address: string): Promise<number> {
Expand Down
16 changes: 8 additions & 8 deletions src/server/blockchain/__tests__/adminWallet.test.js
Expand Up @@ -13,7 +13,7 @@ describe('adminwallet', () => {

test(`adminWallet top wallet shouldn't throws an error when user is not whitelisted/verified`, async () => {
const unverifiedAddress = generateWalletAddress()
const tx = await AdminWallet.topWallet(unverifiedAddress).catch(e => false)
const tx = await AdminWallet.topWallet(unverifiedAddress, null).catch(e => false)
expect(tx).toBeTruthy()
})

Expand All @@ -23,7 +23,7 @@ describe('adminwallet', () => {

test('adminWallet can whitelist user', async () => {
await AdminWallet.removeWhitelisted('0x888185b656fe770677a91412f9f09B23A787242A').catch(_ => _)
const tx = await AdminWallet.whitelistUser('0x888185b656fe770677a91412f9f09B23A787242A')
const tx = await AdminWallet.whitelistUser('0x888185b656fe770677a91412f9f09B23A787242A', 'did:gd')
const isVerified = await AdminWallet.isVerified('0x888185b656fe770677a91412f9f09B23A787242A')
expect(isVerified).toBeTruthy()
})
Expand All @@ -48,8 +48,8 @@ describe('adminwallet', () => {
const promises = []
for (let i = 0; i < 5; i++) {
const unverifiedAddress = generateWalletAddress()
const tx = await AdminWallet.whitelistUser(unverifiedAddress)
promises.push(AdminWallet.topWallet(unverifiedAddress))
const tx = await AdminWallet.whitelistUser(unverifiedAddress, 'did:gd')
promises.push(AdminWallet.topWallet(unverifiedAddress, null, true))
}
const res = await Promise.all(promises).catch(_ => false)
expect(res).toBeTruthy()
Expand All @@ -64,8 +64,8 @@ describe('adminwallet', () => {
let tx

//good tx
await AdminWallet.whitelistUser(unverifiedAddress)
tx = await AdminWallet.topWallet(unverifiedAddress)
await AdminWallet.whitelistUser(unverifiedAddress, 'did:gd')
tx = await AdminWallet.topWallet(unverifiedAddress, null, true)
expect(tx).toBeTruthy()

//bad tx
Expand All @@ -80,8 +80,8 @@ describe('adminwallet', () => {
).rejects.toThrow()

//good tx
await AdminWallet.whitelistUser(unverifiedAddress2)
tx = await AdminWallet.topWallet(unverifiedAddress2)
await AdminWallet.whitelistUser(unverifiedAddress2, 'did:gd')
tx = await AdminWallet.topWallet(unverifiedAddress2, null, true)
expect(tx).toBeTruthy()
})

Expand Down
2 changes: 1 addition & 1 deletion src/server/loadtest/loadtest-middleware.js
Expand Up @@ -25,7 +25,7 @@ const setup = (app: Router) => {
console.log('#############################################')
console.log(gdPublicAddress)
console.log('#############################################')
const hash = await AdminWallet.whitelistUser(gdPublicAddress)
const hash = await AdminWallet.whitelistUser(gdPublicAddress, body.profilePublickey)
console.log('xxxxxxxx hash', { hash })
res.json({ ok: 1 })
})
Expand Down
4 changes: 2 additions & 2 deletions src/server/storage/addUserSteps.js
Expand Up @@ -19,7 +19,7 @@ const addUserToWhiteList = async (userRecord: UserRecord) => {
let user = await UserDBPrivate.getUser(userRecord.identifier)
const whiteList = get(user, 'isCompleted.whiteList', false)
if (conf.disableFaceVerification && !whiteList) {
await AdminWallet.whitelistUser(userRecord.gdAddress)
await AdminWallet.whitelistUser(userRecord.gdAddress, userRecord.profilePublickey)
.then(async r => {
await UserDBPrivate.completeStep(user.identifier, 'whiteList')
})
Expand Down Expand Up @@ -87,7 +87,7 @@ const topUserWallet = async (userRecord: UserRecord) => {
let user = await UserDBPrivate.getUser(userRecord.identifier)
const topWallet = get(user, 'isCompleted.topWallet', false)
if (!topWallet) {
return Promise.race([AdminWallet.topWallet(userRecord.gdAddress), Timeout(15000, 'topWallet')])
return Promise.race([AdminWallet.topWallet(userRecord.gdAddress, null, true), Timeout(15000, 'topWallet')])
.then(r => {
UserDBPrivate.completeStep(userRecord.identifier, 'topWallet')
return { ok: 1 }
Expand Down
2 changes: 1 addition & 1 deletion src/server/verification/__tests__/verificationAPI.js
Expand Up @@ -234,7 +234,7 @@ describe('verificationAPI', () => {
const creds = await getCreds(true)
const token = await getToken(server, creds)
await AdminWallet.ready
await AdminWallet.whitelistUser(creds.address)
await AdminWallet.whitelistUser(creds.address, 'x')
const res = await request(server)
.get('/verify/w3/bonuses')
.set('Authorization', `Bearer ${token}`)
Expand Down
4 changes: 2 additions & 2 deletions src/server/verification/verificationAPI.js
Expand Up @@ -85,7 +85,7 @@ const setup = (app: Router, verifier: VerificationAPI, storage: StorageAPI) => {
if (result.isVerified) {
log.debug('Whitelisting new user', user)
await Promise.all([
AdminWallet.whitelistUser(user.gdAddress),
AdminWallet.whitelistUser(user.gdAddress, user.profilePublickey),
storage
.updateUser({ identifier: user.loggedInAs, isVerified: true })
.then(updatedUser => log.debug('updatedUser:', updatedUser))
Expand Down Expand Up @@ -256,7 +256,7 @@ const setup = (app: Router, verifier: VerificationAPI, storage: StorageAPI) => {

//allow topping once a day
await storage.updateUser({ identifier: user.loggedInAs, lastTopWallet: new Date().toISOString() })
let txRes = await AdminWallet.topWallet(user.gdAddress)
let txRes = await AdminWallet.topWallet(user.gdAddress, user.lastTopWallet)
.then(tx => {
log.debug('topping wallet tx', { walletaddress: user.gdAddress, tx })
return { ok: 1 }
Expand Down

0 comments on commit 113e772

Please sign in to comment.