Skip to content

Commit

Permalink
Merge pull request #274 from AschPlatform/2018-09-13/#278
Browse files Browse the repository at this point in the history
2018 09 13/#278
  • Loading branch information
sqfasd authored Oct 17, 2018
2 parents 0b18f9b + 319d627 commit 18719ec
Show file tree
Hide file tree
Showing 16 changed files with 693 additions and 28 deletions.
7 changes: 2 additions & 5 deletions src/contract/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ function isUniq(arr) {
module.exports = {
async transfer(amount, recipient) {
if (!recipient) return 'Invalid recipient'
// Verify amount should be positive integer
// if (!Number.isInteger(amount) || amount <= 0) return 'Amount should be positive integer'
app.validate('amount', String(amount))

amount = Number(amount)
Expand All @@ -50,8 +48,8 @@ module.exports = {

let recipientAccount
// Validate recipient is valid address
if (recipient && (app.util.address.isNormalAddress(recipient) ||
app.util.address.isGroupAddress(recipient))) {
if (recipient && (app.util.address.isNormalAddress(recipient)
|| app.util.address.isGroupAddress(recipient))) {
recipientAccount = await app.sdb.load('Account', recipient)
if (recipientAccount) {
app.sdb.increase('Account', { xas: amount }, { address: recipientAccount.address })
Expand Down Expand Up @@ -113,7 +111,6 @@ module.exports = {

async lock(height, amount) {
if (!Number.isInteger(height) || height <= 0) return 'Height should be positive integer'
// if (!Number.isInteger(amount) || amount <= 0) return 'Amount should be positive integer'

height = Number(height)
amount = Number(amount)
Expand Down
1 change: 0 additions & 1 deletion src/contract/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ module.exports = {
if (!recipient) return 'Invalid recipient'
if (!chainName) return 'Invalid chain name'
if (!currency) return 'Invalid currency'
// if (!Number.isInteger(amount) || amount <= 0) return 'Amount should be positive integer'
app.validate('amount', String(amount))

const chain = await app.sdb.findOne('Chain', { condition: { name: chainName } })
Expand Down
102 changes: 102 additions & 0 deletions src/contract/exchange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
async function doExchange(sourceCurrency, targetCurrency, bancor, result, context) {
const senderId = context.sender.address
// const exchangeFee = Math.floor(result.targetAmount * 0.001)
const exchangeFee = Math.ceil(result.targetAmount.toNumber() * 0.001)
const realTargetAmount = result.targetAmount.minus(exchangeFee)
// decrease source, increase target
if (sourceCurrency === 'XAS') {
app.sdb.increase('Account', { xas: -result.sourceAmount }, { address: senderId })
app.balances.increase(app.councilAddress, targetCurrency, exchangeFee)
app.balances.increase(senderId, targetCurrency, realTargetAmount.toNumber())
}
if (targetCurrency === 'XAS') {
app.balances.decrease(senderId, sourceCurrency, result.sourceAmount)
app.sdb.increase('Account', { xas: exchangeFee }, { address: app.councilAddress })
app.sdb.increase('Account', { xas: realTargetAmount.toNumber() }, { address: senderId })
}
let sourcePrecision = 0
let targetPrecision = 0
let type
let price
const bancorObj = bancor.getBancorInfo()
if (sourceCurrency === bancorObj.money) {
type = 'BUY'
price = result.targetAmount.toNumber() / result.sourceAmount.toNumber()
sourcePrecision = bancorObj.moneyPrecision
targetPrecision = bancorObj.stockPrecision
} else {
type = 'SELL'
price = result.sourceAmount.toNumber() / result.targetAmount.toNumber()
sourcePrecision = bancorObj.stockPrecision
targetPrecision = bancorObj.moneyPrecision
}
// 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(10),
sourceAmount: result.sourceAmount.toString(10),
})
}

module.exports = {
async exchangeByTarget(sourceCurrency, targetCurrency, targetAmount, bancorInfo) {
app.validate('amount', String(targetAmount))
const senderId = this.sender.address
const bancor = await app.util.bancor
.create(bancorInfo.money, bancorInfo.stock, bancorInfo.owner)
const simulateResult = await bancor.exchangeByTarget(sourceCurrency,
targetCurrency, targetAmount, false)
// Check source account has sufficient balance to handle the exchange
if (sourceCurrency === 'XAS') {
if (simulateResult.sourceAmount.gt(app.util.bignumber(String(this.sender.xas)))) return 'Insufficient balance'
} else {
const balance = app.balances.get(senderId, sourceCurrency)
if (balance.lt(simulateResult.sourceAmount)) return 'Insufficient balance'
}
if (!bancor) return 'Bancor is not ready'
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(app.util.bignumber(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
},

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.toNumber())
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
},
}
94 changes: 91 additions & 3 deletions src/contract/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ module.exports = {
async deposit(gateway, address, currency, amount, oid) {
if (!gateway) return 'Invalid gateway name'
if (!currency) return 'Invalid currency'
// if (!Number.isInteger(amount) || amount <= 0) return 'Amount should be positive integer'
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 @@ -106,6 +107,11 @@ module.exports = {
if (deposit.confirmations > count / 2 && !deposit.processed) {
deposit.processed = 1
app.balances.increase(gatewayAccount.address, currency, amount)
const gwCurrency = await app.sdb.load('GatewayCurrency', { gateway, symbol: currency })
if (!gwCurrency) return 'No gateway currency found'
const quantity = app.util.bignumber(gwCurrency.quantity).plus(amount).toString(10)
app.sdb.update('GatewayCurrency', { quantity }, { gateway, symbol: currency })
// app.sdb.increase('GatewayCurrency', { quantity: amount }, { gateway, symbol: currency })
}
app.sdb.update('GatewayDeposit', deposit, dipositKey)
}
Expand All @@ -115,8 +121,6 @@ module.exports = {
async withdrawal(address, gateway, currency, amount, fee) {
if (!gateway) return 'Invalid gateway name'
if (!currency) return 'Invalid currency'
// if (!Number.isInteger(amount) || amount <= 0) return 'Amount should be positive integer'
// if (!Number.isInteger(fee) || fee <= 0) return 'Fee should be positive integer'
app.validate('amount', fee)
app.validate('amount', amount)

Expand All @@ -129,6 +133,12 @@ module.exports = {
if (!app.gateway.isValidAddress(gateway, address)) return 'Invalid withdrawal address'

app.balances.decrease(this.sender.address, currency, amount)

const gwCurrency = await app.sdb.load('GatewayCurrency', { gateway, symbol: currency })
if (!gwCurrency) return 'No gateway currency found'
const quantity = app.util.bignumber(gwCurrency.quantity).minus(amount).toString(10)
app.sdb.update('GatewayCurrency', { quantity }, { gateway, symbol: currency })
// app.sdb.increase('GatewayCurrency', { quantity: -amount }, { gateway, symbol: currency })
const seq = Number(app.autoID.increment('gate_withdrawal_seq'))

app.sdb.create('GatewayWithdrawal', {
Expand Down Expand Up @@ -225,4 +235,82 @@ module.exports = {
app.sdb.update('GatewayWithdrawal', { oid }, { tid: wid })
return null
},

async depositBail(gatewayName, amount) {
app.validate('amount', String(amount))
amount = app.util.bignumber(amount).toNumber()
const senderId = this.sender.address
const addr = app.util.address.generateLockedAddress(senderId)
const lockAccount = app.sdb.createOrLoad('Account', { xas: 0, address: addr, name: null })
if ((lockAccount.entity.xas + amount) < app.util.constants.initialDeposit) return `Deposit amount should be greater than ${app.util.constants.initialDeposit - lockAccount.entity.xas}`
const m = await app.util.gateway.getGatewayMember(gatewayName, senderId)
if (!m) return 'Please register as a gateway member before deposit bail'
if (amount > this.sender.xas) return 'Insufficient balance'

app.sdb.increase('Account', { xas: -amount }, { address: senderId })
app.sdb.increase('Account', { xas: amount }, { address: addr })
return null
},

async withdrawalBail(gatewayName, amount) {
app.validate('amount', String(amount))
amount = app.util.bignumber(amount).toNumber()
const senderId = this.sender.address
const gw = await app.sdb.findOne('Gateway', { condition: { name: gatewayName } })
if (!gw) return 'Gateway not found'
const m = await app.util.gateway.getGatewayMember(gatewayName, senderId)
if (!m) return 'Not a gateway member'
const addr = app.util.address.generateLockedAddress(senderId)
const lockAccount = await app.sdb.load('Account', addr)
if (!lockAccount) return 'No bail was found'
if (m.elected === 0 && amount > lockAccount.xas) return 'Withdrawl amount exceeds balance'
if (m.elected === 0) {
app.sdb.increase('Account', { xas: amount }, { address: senderId })
app.sdb.increase('Account', { xas: -amount }, { address: addr })
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 && threshold.ratio > app.util.constants.supplyCriteria) {
const canBeWithdrawl = await app.util.gateway
.getMaximumBailWithdrawl(gatewayName, senderId)
if (amount > canBeWithdrawl) return 'Withdrawl amount exceeds balance'
if (amount > (lockAccount.xas - app.util.constants.initialDeposit)) return 'Withdrawl amount exceeds balance'
app.sdb.increase('Account', { xas: amount }, { address: senderId })
app.sdb.increase('Account', { xas: -amount }, { address: addr })
} else {
return 'Withdrawl amount exceeds balance'
}
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)).toNumber()
const ratio = userAmount / app.util.bignumber(gwCurrency[0].quantity).toNumber()
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 = Math.floor(ratio * memberLockedAccount.xas)
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,
senderId, app.storeClaimedAddr)
} else {
return 'Gateway was not revoked'
}
return null
},
}
2 changes: 0 additions & 2 deletions src/contract/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ module.exports = {
const senderId = this.sender.address
const requestTrs = await app.sdb.findOne('Transaction', { condition: { id: targetId } })
if (!requestTrs) return 'Request transaction not found'

// if (requestTrs.mode !== app.TransactionMode.REQUEST) return 'Invalid transaction mode'
if (!app.util.transactionMode.isRequestMode(requestTrs.mode)) return 'Invalid transaction mode'

const requestTrsState = await app.sdb.load('TransactionStatu', { tid: targetId })
Expand Down
Loading

0 comments on commit 18719ec

Please sign in to comment.