Skip to content

Commit

Permalink
Merge pull request #283 from AschPlatform/asch1.5-without-bancor
Browse files Browse the repository at this point in the history
Asch1.5 without bancor
  • Loading branch information
sqfasd authored Jan 9, 2019
2 parents f5b3769 + 1f98fc3 commit 29466c7
Show file tree
Hide file tree
Showing 8 changed files with 4 additions and 611 deletions.
118 changes: 0 additions & 118 deletions src/contract/exchange.js

This file was deleted.

49 changes: 0 additions & 49 deletions src/contract/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ module.exports = {
async deposit(gateway, address, currency, amount, oid) {
if (!gateway) return 'Invalid gateway name'
if (!currency) return 'Invalid currency'
const threshold = await app.util.gateway.getThreshold(gateway, this.sender.address)
if (threshold.ratio > 0 && threshold.ratio < app.util.constants.frozenCriteria) return `Bail is not enough, please withdrawl ${currency} asap`
app.validate('amount', amount)

if (!await app.sdb.exists('GatewayCurrency', { symbol: currency })) return 'Currency not supported'
Expand Down Expand Up @@ -250,8 +248,6 @@ module.exports = {
const m = await app.util.gateway.getGatewayMember(gatewayName, senderId)
if (!m) return 'Please register as a gateway member before deposit bail'
if (amount.gt(String(this.sender.xas))) return 'Insufficient balance'
const threshold = await app.util.gateway.getThreshold(gatewayName, senderId)
if (amount.lt(threshold.needSupply)) return `Deposit amount should be greater than ${threshold.needSupply}`

app.sdb.increase('Account', { xas: -amount.toNumber() }, { address: senderId })
app.sdb.increase('Account', { xas: amount.toNumber() }, { address: addr })
Expand All @@ -276,52 +272,7 @@ module.exports = {
return null
}

if (gw.revoked === 1 && m.elected === 1) return 'Gateway is revoked, withdrawal can be processed by claim proposal'
if (gw.revoked === 2 && m.elected === 1) return 'Gateway is in claim status, withdrawl bail is not permitted'
const threshold = await app.util.gateway.getThreshold(gatewayName, senderId)
if (m.elected === 1) {
let canBeWithdrawl = 0
if (threshold.ratio > 0) {
canBeWithdrawl = await app.util.gateway.getMaximumBailWithdrawl(gatewayName, senderId)
} else if (threshold.ratio === 0) {
canBeWithdrawl = lockAccount.xas - app.util.constants.initialDeposit
}
if (amount.gt(canBeWithdrawl)) return 'Withdrawl amount exceeds balance'
if (amount.gt(lockAccount.xas - app.util.constants.initialDeposit)) return 'Withdrawl amount exceeds balance'
app.sdb.increase('Account', { xas: amount.toNumber() }, { address: senderId })
app.sdb.increase('Account', { xas: -amount.toNumber() }, { address: addr })
}
return null
},

async claim(gatewayName) {
const limit = 1
const gateway = await app.sdb.load('Gateway', gatewayName)
const senderId = this.sender.address
if (!gateway) return 'Gateway not found'
if (gateway.revoked === 1) return 'No claim proposal was activated'
const gwCurrency = await app.sdb.findAll('GatewayCurrency', { condition: { gateway: gatewayName }, limit })
if (gateway.revoked === 2) {
const members = await app.util.gateway.getElectedGatewayMember(gatewayName)
const userAmount = app.util
.bignumber(app.balances.get(senderId, gwCurrency[0].symbol))
const ratio = userAmount.div(app.util.bignumber(gwCurrency[0].quantity))
const totalClaim = ratio.times(gwCurrency[0].claimAmount)
const allBailAmount = await app.util.gateway.getAllBailAmount(gatewayName)
const claimRatio = totalClaim.div(allBailAmount)
for (let i = 0; i < members.length; i++) {
const lockedAddr = app.util.address.generateLockedAddress(members[i].address)
const memberLockedAccount = await app.sdb.load('Account', lockedAddr)
const needClaim = claimRatio.times(memberLockedAccount.xas).toNumber()
if (needClaim === 0) continue
app.sdb.increase('Account', { xas: -needClaim }, { address: lockedAddr })
app.sdb.increase('Account', { xas: needClaim }, { address: senderId })
}
app.balances.transfer(gwCurrency[0].symbol, userAmount.toString(),
senderId, app.storeClaimedAddr)
} else {
return 'Gateway was not revoked'
}
return null
},
}
151 changes: 4 additions & 147 deletions src/contract/proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const VALID_TOPICS = [
'gateway_init',
'gateway_update_member',
'gateway_revoke',
'gateway_claim',
'bancor_init',
'update_net_energy_per_xas',
'update_net_energy_per_pledged_xas',
'update_gasprice',
Expand Down Expand Up @@ -68,9 +66,7 @@ async function doGatewayUpdateMember(params, context) {
const addr = app.util.address.generateLockedAddress(params.to)
const account = await app.sdb.findOne('Account', { condition: { address: addr } })
if (!account) throw new Error(`No bail was found for new gateway member ${params.to}`)
const needsBail = await app.util.gateway.getNeedsBail(params.gateway)
if (account.xas < app.util.constants.initialDeposit
|| needsBail.gt(String(account.xas))) {
if (account.xas < app.util.constants.initialDeposit) {
throw new Error(`New member's bail is not enough for gateway member ${params.to}`)
}

Expand All @@ -93,94 +89,17 @@ async function doGatewayRevoke(params) {
app.sdb.update('GatewayCurrency', { revoked: 1 }, { gateway: params.gateway, symbol: gwCurrency[i].symbol })
}
}
}

async function doGatewayClaim(params) {
app.sdb.lock(`gateway@${params.gateway}`)
const gateway = await app.sdb.load('Gateway', params.gateway)
if (!gateway) throw new Error('Gateway was not found')
const members = await app.util.gateway.getAllGatewayMember(params.gateway)
const evilMembers = params.evilMembers
const goodMembers = members.filter((m) => {
for (let i = 0; i < evilMembers.length; i++) {
if (evilMembers[i] === m.address) {
return false
}
}
return true
})

for (let i = 0; i < goodMembers.length; i++) {
const member = await app.util.gateway.getGatewayMember(params.gateway, goodMembers[i].address)
for (let i = 0; i < members.length; i++) {
const member = members[i]
const addr = app.util.address.generateLockedAddress(member.address)
const exists = await app.sdb.load('Account', { address: addr })
if (exists) {
app.sdb.increase('Account', { xas: member.bail }, { address: member.address })
app.sdb.increase('Account', { xas: -member.bail }, { address: addr })
}
}
let claimAmount = app.util.bignumber(0)
for (let i = 0; i < evilMembers.length; i++) {
const member = await app.util.gateway.getGatewayMember(params.gateway, evilMembers[i])
claimAmount = claimAmount.plus(member.bail)
}

gateway.revoked = 2
app.sdb.update('Gateway', { revoked: 2 }, { name: params.gateway })
const gwCurrency = await app.sdb.findAll('GatewayCurrency', { condition: { gateway: params.gateway } })
if (gwCurrency.length > 0) {
for (let i = 0; i < gwCurrency.length; i++) {
app.sdb.update('GatewayCurrency', { revoked: 2, claimAmount: claimAmount.toString() }, { gateway: params.gateway, symbol: gwCurrency[i].symbol })
}
}
}

async function doBancorInit(params, context) {
const address = params.owner
const stockBalance = app.util.bignumber(params.stockBalance)
const moneyBalance = app.util.bignumber(params.moneyBalance)
app.sdb.lock(`bancor@${address}`)
const account = await app.sdb.findOne('Account', { condition: { address } })
if (params.stock === 'XAS') {
const balance = await app.balances.get(address, params.money)
if (stockBalance.gt(String(account.xas))) throw new Error('Stock balance is not enough')
if (balance.lt(moneyBalance)) throw new Error('Money balance is not enough')
} else if (params.money === 'XAS') {
const balance = await app.balances.get(address, params.stock)
if (moneyBalance.gt(String(account.xas))) throw new Error('Money balance is not enough')
if (balance.lt(stockBalance)) throw new Error('Stock balance is not enough')
} else {
const sBalance = await app.balances.get(address, params.stock)
const mBalance = await app.balances.get(address, params.money)
if (mBalance.lt(moneyBalance)) throw new Error('Money balance is not enough')
if (sBalance.lt(stockBalance)) throw new Error('Stock balance is not enough')
}
app.sdb.create('Bancor', {
id: Number(app.autoID.increment('bancor_id')),
owner: address,
stock: params.stock,
money: params.money,
supply: params.supply,
stockBalance: params.stockBalance,
stockPrecision: params.stockPrecision,
moneyBalance: params.moneyBalance,
moneyPrecision: params.moneyPrecision,
stockCw: params.stockCw,
moneyCw: params.moneyCw,
relay: params.relay,
name: params.name,
timestamp: context.trs.timestamp,
})
if (params.money === 'XAS') {
app.balances.decrease(address, params.stock, params.stockBalance)
app.sdb.increase('Account', { xas: -params.moneyBalance }, { address })
} else if (params.stock === 'XAS') {
app.balances.decrease(address, params.money, params.moneyBalance)
app.sdb.increase('Account', { xas: -params.stockBalance }, { address })
} else {
app.balances.decrease(address, params.stock, params.stockBalance)
app.balances.decrease(address, params.money, params.moneyBalance)
}
}

async function doUpdateNetEnergyPerXAS(params) {
Expand Down Expand Up @@ -284,9 +203,7 @@ async function validateGatewayUpdateMember(content/* , context */) {
const addr = app.util.address.generateLockedAddress(content.to)
const account = await app.sdb.findOne('Account', { condition: { address: addr } })
if (!account) throw new Error(`No bail was found for new gateway member ${content.to}`)
const needsBail = await app.util.gateway.getNeedsBail(content.gateway)
if (account.xas < app.util.constants.initialDeposit
|| needsBail.gt(String(account.xas))) {
if (account.xas < app.util.constants.initialDeposit) {
throw new Error(`New member's bail is not enough for gateway member ${content.to}`)
}
}
Expand All @@ -297,58 +214,6 @@ async function validateGatewayContent(content/* , context */) {
if (gateway.revoked) throw new Error('Gateway is already revoked')
}

async function validateGatewayClaim(content/* , context */) {
const gateway = await app.sdb.findOne('Gateway', { condition: { name: content.gateway } })
if (!gateway) throw new Error('Gateway not found')
if (!gateway.revoked) throw new Error('Gateway is not revoked')
if (gateway.revoked === 2) throw new Error('Gateway is already claimed')
const members = await app.util.gateway.getElectedGatewayMember(content.gateway)
const evilMembers = content.evilMembers
for (let i = 0; i < evilMembers.length; i++) {
const m = await app.sdb.findOne('GatewayMember', { condition: { address: evilMembers[i], gateway: content.gateway } })
if (!m) {
throw new Error('Evil member is not gateway member')
}
if (m.elected === 0) {
throw new Error('Evil member should be elected gateway member')
}
}
if (evilMembers.length < (Math.floor(members.length / 2) + 1)) {
throw new Error(`Evil member count should be greater than ${Math.floor(members.length / 2) + 1}`)
}
}

async function validateBancorContent(content/* , context */) {
app.validate('amount', content.stockBalance)
app.validate('amount', content.moneyBalance)
if (content.relay !== 0) {
app.validate('amount', content.supply)
if (content.stockCw !== 1) throw new Error('Stock cw should be 1')
if (content.moneyCw !== 1) throw new Error('Money cw should be 1')
}
const stockBalance = app.util.bignumber(content.stockBalance)
const moneyBalance = app.util.bignumber(content.moneyBalance)
const address = content.owner
if (content.money === content.stock) throw new Error('Money and stock cannot be same')
const bancor = await app.sdb.findOne('Bancor', { condition: { owner: address, stock: content.stock, money: content.money } })
if (bancor) throw new Error('Bancor exists')
const account = await app.sdb.findOne('Account', { condition: { address } })
if (content.stock === 'XAS') {
const balance = await app.balances.get(address, content.money)
if (stockBalance.gt(String(account.xas))) throw new Error('Stock balance is not enough')
if (balance.lt(content.moneyBalance)) throw new Error('Money balance is not enough')
} else if (content.money === 'XAS') {
const balance = await app.balances.get(address, content.stock)
if (moneyBalance.gt(String(account.xas))) throw new Error('Money balance is not enough')
if (balance.lt(stockBalance)) throw new Error('Stock balance is not enough')
} else {
const sBalance = await app.balances.get(address, content.stock)
const mBalance = await app.balances.get(address, content.money)
if (mBalance.lt(moneyBalance)) throw new Error('Money balance is not enough')
if (sBalance.lt(stockBalance)) throw new Error('Stock balance is not enough')
}
}

async function validateNetEnergyPerXAS(content/* , context */) {
app.validate('amount', String(content.netPerXAS))
app.validate('amount', String(content.energyPerXAS))
Expand Down Expand Up @@ -397,10 +262,6 @@ module.exports = {
await validateGatewayUpdateMember(content, this)
} else if (topic === 'gateway_revoke') {
await validateGatewayContent(content, this)
} else if (topic === 'gateway_claim') {
await validateGatewayClaim(content, this)
} else if (topic === 'bancor_init') {
await validateBancorContent(content, this)
} else if (topic === 'update_net_energy_per_xas') {
await validateNetEnergyPerXAS(content, this)
} else if (topic === 'update_net_energy_per_pledged_xas') {
Expand Down Expand Up @@ -469,10 +330,6 @@ module.exports = {
await doGatewayUpdateMember(content, this)
} else if (topic === 'gateway_revoke') {
await doGatewayRevoke(content, this)
} else if (topic === 'gateway_claim') {
await doGatewayClaim(content, this)
} else if (topic === 'bancor_init') {
await doBancorInit(content, this)
} else if (topic === 'update_net_energy_per_xas') {
await doUpdateNetEnergyPerXAS(content, this)
} else if (topic === 'update_net_energy_per_pledged_xas') {
Expand Down
Loading

0 comments on commit 29466c7

Please sign in to comment.