Skip to content

Commit

Permalink
feat(bancor):remove bancor from asch1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
chengenguan committed Jan 9, 2019
1 parent f5b3769 commit ae7ddc7
Show file tree
Hide file tree
Showing 8 changed files with 604 additions and 593 deletions.
226 changes: 113 additions & 113 deletions src/contract/exchange.js
Original file line number Diff line number Diff line change
@@ -1,118 +1,118 @@
async function doExchange(sourceCurrency, targetCurrency, bancor, result, context) {
const senderId = context.sender.address
const exchangeFee = result.targetAmount.times(0.001).round()
const realTargetAmount = result.targetAmount.minus(exchangeFee)
// decrease source, increase target
if (sourceCurrency === 'XAS') {
app.sdb.increase('Account', { xas: -result.sourceAmount.toNumber() }, { address: senderId })
app.balances.increase(app.councilAddress, targetCurrency, exchangeFee.toString())
app.balances.increase(senderId, targetCurrency, realTargetAmount.toString())
} else if (targetCurrency === 'XAS') {
app.balances.decrease(senderId, sourceCurrency, result.sourceAmount.toString())
app.sdb.increase('Account', { xas: exchangeFee.toNumber() }, { address: app.councilAddress })
app.sdb.increase('Account', { xas: realTargetAmount.toNumber() }, { address: senderId })
} else {
app.balances.decrease(senderId, sourceCurrency, result.sourceAmount.toString())
app.balances.increase(app.councilAddress, targetCurrency, exchangeFee.toString())
app.balances.increase(senderId, targetCurrency, realTargetAmount.toString())
}
let sourcePrecision = 0
let targetPrecision = 0
let type = ''
let price = 0.0
const bancorObj = bancor.getBancorInfo()
if (sourceCurrency === bancorObj.money) {
type = 'BUY'
sourcePrecision = bancorObj.moneyPrecision
targetPrecision = bancorObj.stockPrecision
app.logger.debug(`====> exchange: BUY, price = ${result.targetAmount.toString()} / 10 ^ ${targetPrecision} / (${result.sourceAmount.toString()} / 10 ^ ${sourcePrecision})`)
price = Number(result.targetAmount.div(10 ** Number(targetPrecision))
.div(result.sourceAmount.div(10 ** Number(sourcePrecision)))
.toFixed(sourcePrecision).toString())
} else {
type = 'SELL'
sourcePrecision = bancorObj.stockPrecision
targetPrecision = bancorObj.moneyPrecision
app.logger.debug(`====> exchange: SELL, price = ${result.sourceAmount.toString()} / 10 ^ ${sourcePrecision} / (${result.targetAmount.toString()} / 10 ^ ${targetPrecision})`)
price = Number(result.sourceAmount.div(10 ** Number(sourcePrecision))
.div(result.targetAmount.div(10 ** Number(targetPrecision)))
.toFixed(targetPrecision).toString())
}
// Record exchange transactions
app.sdb.create('BancorExchange', {
id: context.trs.id,
address: senderId,
timestamp: app.util.slots.getTime(),
type,
owner: bancorObj.owner,
source: sourceCurrency,
sourcePrecision,
target: targetCurrency,
targetPrecision,
price,
targetAmount: result.targetAmount.toString(),
sourceAmount: result.sourceAmount.toString(),
})
}
// async function doExchange(sourceCurrency, targetCurrency, bancor, result, context) {
// const senderId = context.sender.address
// const exchangeFee = result.targetAmount.times(0.001).round()
// const realTargetAmount = result.targetAmount.minus(exchangeFee)
// // decrease source, increase target
// if (sourceCurrency === 'XAS') {
// app.sdb.increase('Account', { xas: -result.sourceAmount.toNumber() }, { address: senderId })
// app.balances.increase(app.councilAddress, targetCurrency, exchangeFee.toString())
// app.balances.increase(senderId, targetCurrency, realTargetAmount.toString())
// } else if (targetCurrency === 'XAS') {
// app.balances.decrease(senderId, sourceCurrency, result.sourceAmount.toString())
// app.sdb.increase('Account', { xas: exchangeFee.toNumber() }, { address: app.councilAddress })
// app.sdb.increase('Account', { xas: realTargetAmount.toNumber() }, { address: senderId })
// } else {
// app.balances.decrease(senderId, sourceCurrency, result.sourceAmount.toString())
// app.balances.increase(app.councilAddress, targetCurrency, exchangeFee.toString())
// app.balances.increase(senderId, targetCurrency, realTargetAmount.toString())
// }
// let sourcePrecision = 0
// let targetPrecision = 0
// let type = ''
// let price = 0.0
// const bancorObj = bancor.getBancorInfo()
// if (sourceCurrency === bancorObj.money) {
// type = 'BUY'
// sourcePrecision = bancorObj.moneyPrecision
// targetPrecision = bancorObj.stockPrecision
// app.logger.debug(`====> exchange: BUY, price = ${result.targetAmount.toString()} / 10 ^ ${targetPrecision} / (${result.sourceAmount.toString()} / 10 ^ ${sourcePrecision})`)
// price = Number(result.targetAmount.div(10 ** Number(targetPrecision))
// .div(result.sourceAmount.div(10 ** Number(sourcePrecision)))
// .toFixed(sourcePrecision).toString())
// } else {
// type = 'SELL'
// sourcePrecision = bancorObj.stockPrecision
// targetPrecision = bancorObj.moneyPrecision
// app.logger.debug(`====> exchange: SELL, price = ${result.sourceAmount.toString()} / 10 ^ ${sourcePrecision} / (${result.targetAmount.toString()} / 10 ^ ${targetPrecision})`)
// price = Number(result.sourceAmount.div(10 ** Number(sourcePrecision))
// .div(result.targetAmount.div(10 ** Number(targetPrecision)))
// .toFixed(targetPrecision).toString())
// }
// // Record exchange transactions
// app.sdb.create('BancorExchange', {
// id: context.trs.id,
// address: senderId,
// timestamp: app.util.slots.getTime(),
// type,
// owner: bancorObj.owner,
// source: sourceCurrency,
// sourcePrecision,
// target: targetCurrency,
// targetPrecision,
// price,
// targetAmount: result.targetAmount.toString(),
// sourceAmount: result.sourceAmount.toString(),
// })
// }

module.exports = {
async exchangeByTarget(sourceCurrency, targetCurrency, targetAmount, bancorInfo) {
app.validate('amount', String(targetAmount))
const senderId = this.sender.address
const bancorSimulate = await app.util.bancor
.create(bancorInfo.money, bancorInfo.stock, bancorInfo.owner)
if (!bancorSimulate) return 'Bancor is not ready'
const simulateResult = await bancorSimulate.exchangeByTarget(sourceCurrency,
targetCurrency, targetAmount, false)
// module.exports = {
// async exchangeByTarget(sourceCurrency, targetCurrency, targetAmount, bancorInfo) {
// app.validate('amount', String(targetAmount))
// const senderId = this.sender.address
// const bancorSimulate = await app.util.bancor
// .create(bancorInfo.money, bancorInfo.stock, bancorInfo.owner)
// if (!bancorSimulate) return 'Bancor is not ready'
// const simulateResult = await bancorSimulate.exchangeByTarget(sourceCurrency,
// targetCurrency, targetAmount, false)

// Check source account has sufficient balance to handle the exchange
if (sourceCurrency === 'XAS') {
if (simulateResult.sourceAmount.gt(String(this.sender.xas))) return 'Insufficient balance'
} else if (targetCurrency === 'XAS') {
const bigAmount = app.util.bignumber(targetAmount)
if (bigAmount.gt(String(this.sender.xas))) return 'Insufficient balance'
} else {
const sourceBalance = await app.balances.get(senderId, sourceCurrency)
const targetBalance = await app.balances.get(senderId, targetCurrency)
if (sourceBalance.gt(simulateResult.sourceAmount)) return `Insufficient balance for ${sourceCurrency}`
if (targetBalance.gt(targetAmount)) return `Insufficient balance for ${targetCurrency}`
}
const bancor = await app.util.bancor
.create(bancorInfo.money, bancorInfo.stock, bancorInfo.owner)
const result = await bancor.exchangeByTarget(sourceCurrency, targetCurrency, targetAmount, true)
await doExchange(sourceCurrency, targetCurrency, bancor, result, this)
return null
},
// // Check source account has sufficient balance to handle the exchange
// if (sourceCurrency === 'XAS') {
// if (simulateResult.sourceAmount.gt(String(this.sender.xas))) return 'Insufficient balance'
// } else if (targetCurrency === 'XAS') {
// const bigAmount = app.util.bignumber(targetAmount)
// if (bigAmount.gt(String(this.sender.xas))) return 'Insufficient balance'
// } else {
// const sourceBalance = await app.balances.get(senderId, sourceCurrency)
// const targetBalance = await app.balances.get(senderId, targetCurrency)
// if (sourceBalance.gt(simulateResult.sourceAmount)) return `Insufficient balance for ${sourceCurrency}`
// if (targetBalance.gt(targetAmount)) return `Insufficient balance for ${targetCurrency}`
// }
// const bancor = await app.util.bancor
// .create(bancorInfo.money, bancorInfo.stock, bancorInfo.owner)
// const result = await bancor.exchangeByTarget(sourceCurrency, targetCurrency, targetAmount, true)
// await doExchange(sourceCurrency, targetCurrency, bancor, result, this)
// return null
// },

async exchangeBySource(sourceCurrency, targetCurrency, sourceAmount, bancorInfo) {
app.validate('amount', String(sourceAmount))
const senderId = this.sender.address
// Check source account has sufficient balance to handle the exchange
if (sourceCurrency === 'XAS') {
if (app.util.bignumber(sourceAmount).gt(String(this.sender.xas))) return 'Insufficient balance'
} else {
const balance = app.balances.get(senderId, sourceCurrency)
if (balance.lt(sourceAmount)) return 'Insufficient balance'
}
// async exchangeBySource(sourceCurrency, targetCurrency, sourceAmount, bancorInfo) {
// app.validate('amount', String(sourceAmount))
// const senderId = this.sender.address
// // Check source account has sufficient balance to handle the exchange
// if (sourceCurrency === 'XAS') {
// if (app.util.bignumber(sourceAmount).gt(String(this.sender.xas))) return 'Insufficient balance'
// } else {
// const balance = app.balances.get(senderId, sourceCurrency)
// if (balance.lt(sourceAmount)) return 'Insufficient balance'
// }

const bancor = await app.util.bancor
.create(bancorInfo.money, bancorInfo.stock, bancorInfo.owner)
if (!bancor) return 'Bancor is not ready'
const result = await bancor.exchangeBySource(sourceCurrency, targetCurrency, sourceAmount, true)
await doExchange(sourceCurrency, targetCurrency, bancor, result, this)
return null
},
// const bancor = await app.util.bancor
// .create(bancorInfo.money, bancorInfo.stock, bancorInfo.owner)
// if (!bancor) return 'Bancor is not ready'
// const result = await bancor.exchangeBySource(sourceCurrency, targetCurrency, sourceAmount, true)
// await doExchange(sourceCurrency, targetCurrency, bancor, result, this)
// return null
// },

async burnXAS() {
const bancor = await app.util.bancor.create('BCH', 'XAS')
if (!bancor) return 'Bancor is not ready'
const balance = await app.balances.get(app.repurchaseAddr, 'BCH')
const result = await bancor.exchangeBySource('BCH', 'XAS', balance, true)
app.balances.decrease(app.repurchaseAddr, 'BCH', result.sourceAmount.toString())
if (app.buringPoolAddr) {
app.sdb.createOrLoad('Account', { xas: 0, address: app.buringPoolAddr, name: null })
app.sdb.increase('Account', { xas: result.targetAmount.toNumber() }, { address: app.buringPoolAddr })
}
return null
},
}
// async burnXAS() {
// const bancor = await app.util.bancor.create('BCH', 'XAS')
// if (!bancor) return 'Bancor is not ready'
// const balance = await app.balances.get(app.repurchaseAddr, 'BCH')
// const result = await bancor.exchangeBySource('BCH', 'XAS', balance, true)
// app.balances.decrease(app.repurchaseAddr, 'BCH', result.sourceAmount.toString())
// if (app.buringPoolAddr) {
// app.sdb.createOrLoad('Account', { xas: 0, address: app.buringPoolAddr, name: null })
// app.sdb.increase('Account', { xas: result.targetAmount.toNumber() }, { address: app.buringPoolAddr })
// }
// return null
// },
// }
98 changes: 49 additions & 49 deletions src/contract/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ 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`
// 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 +250,8 @@ 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}`
// 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 +276,52 @@ 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 })
}
// 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
},
// 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
// },
}
Loading

0 comments on commit ae7ddc7

Please sign in to comment.