From: Zhiting Lin Date: Tue, 2 Jun 2020 06:50:47 +0000 (+0800) Subject: update cross chain v3 api X-Git-Tag: 2.2.1~4 X-Git-Url: http://git.osdn.net/view?p=bytom%2FByone.git;a=commitdiff_plain;h=42e71a62ca445fe7857f67d7abe506fce1fe0b15 update cross chain v3 api --- diff --git a/src/models/account.js b/src/models/account.js index ce32a98..bd4eda1 100644 --- a/src/models/account.js +++ b/src/models/account.js @@ -59,10 +59,10 @@ account.listVapor = function(guid) { return retPromise } -account.balance = function(guid) { +account.balance = function(address) { let retPromise = new Promise((resolve, reject) => { bytom.accounts - .listAddressUseServer(guid) + .listAddressUseServer(address) .then(address => { let balances = address.balances || [] let votes = address.votes || [] diff --git a/src/models/transaction.js b/src/models/transaction.js index dacba6a..35b64f9 100644 --- a/src/models/transaction.js +++ b/src/models/transaction.js @@ -46,10 +46,10 @@ transaction.build = function(address, to, asset, amount, fee, confirmations) { return retPromise; }; -transaction.buildCrossChain = function(guid, to, asset, amount, confirmations) { +transaction.buildCrossChain = function(address, to, asset, amount, confirmations) { let retPromise = new Promise((resolve, reject) => { bytom.transaction - .buildCrossChain(guid, to, asset, Number(amount), confirmations) + .buildCrossChain(address, to, asset, amount.toString(), confirmations) .then(res => { resolve(res); }) diff --git a/src/views/assetList.vue b/src/views/assetList.vue index b6348ed..0bf5d64 100644 --- a/src/views/assetList.vue +++ b/src/views/assetList.vue @@ -283,8 +283,8 @@ export default { const balanceObject = transaction.balances .filter(b => b.asset.assetId === assetID); - const filterInput = _.find(transaction.inputs, function(o) { return o.type =='veto'; }) - const filterOutput = _.find(transaction.outputs, function(o) { return o.type =='vote'; }) + const filterInput = transaction.types.includes('veto') + const filterOutput = transaction.types.includes('vote') if(filterInput){ transaction.type = 'veto' @@ -297,12 +297,20 @@ export default { transaction.pubkey = filterOutput.vote transaction.vAmount = Num.formatNue(outAmount,8) transaction.type = 'vote' - }else if(_.find(transaction.outputs, function(o) { return o.type =='crosschain_output'; })){ + }else if(transaction.types.includes('out_crosschain')){ transaction.type = 'crossChain' - transaction.cDirection ='Vapor -> Bytom' - }else if(_.find(transaction.inputs, function(o) { return o.type =='crosschain_input'; })){ + if(this.netType === 'vapor'){ + transaction.cDirection ='Vapor -> Bytom' + }else{ + transaction.cDirection ='Bytom -> Vapor' + } + }else if(transaction.types.includes('in_crosschain')){ transaction.type = 'crossChain' - transaction.cDirection ='Bytom -> Vapor' + if(this.netType === 'vapor'){ + transaction.cDirection ='Bytom -> Vapor' + }else{ + transaction.cDirection ='Vapor -> Bytom' + } } const inputAddresses = transaction.inputs diff --git a/src/views/sendTransaction/crossChainTransaction.vue b/src/views/sendTransaction/crossChainTransaction.vue index d7877ee..5699e85 100644 --- a/src/views/sendTransaction/crossChainTransaction.vue +++ b/src/views/sendTransaction/crossChainTransaction.vue @@ -141,14 +141,14 @@ @@ -189,9 +189,12 @@ import { Number as Num } from "@/utils/Number" import _ from 'lodash'; const currencyInPrice = { - inCny: 'cny_price', - inUsd: 'usd_price', - inBtc:'btc_price' + in_cny: 'cnyPrice', + in_usd: 'usdPrice', + in_btc:'btcPrice', + inCny: 'cnyPrice', + inUsd: 'usdPrice', + inBtc:'btcPrice' } export default { @@ -201,9 +204,11 @@ export default { data() { return { selectAsset: { - asset: BTM, - symbol: "BTM", - decimals:8 + asset:{ + assetId: BTM, + symbol: "BTM", + decimals:8 + } }, show: false, guid: null, @@ -229,9 +234,16 @@ export default { return this.currentAccount.vpBalances } }, - unit() { - return this.selectAsset.symbol; - }, + address(){ + if(this.transaction.type === 'toVapor'){ + return this.currentAccount.address + }else{ + return this.currentAccount.vpAddress + } + }, + unit() { + return this.selectAsset.asset.symbol; + }, bytomBalance: function () { let balance, balances if(this.transaction.type === 'toVapor'){ @@ -241,26 +253,8 @@ export default { } if(balances && balances.length >0 ){ - if( this.selectAsset.asset === BTM && this.transaction.type === 'toBytom' ){ - const balanceObject = balances.filter(b => b.asset === BTM)[0] - balance = balanceObject.balance - - let vote = 0, lock = 0 - - const votes = this.currentAccount.votes - - if (votes && votes.length > 0) { - vote = _.sumBy(votes, 'total') - lock = _.sumBy(votes, 'locked') - } - - balance = Num.formatNue((balance - vote - lock), balanceObject.decimals) - }else{ - - const balanceObject = balances.filter(b => b.asset === this.selectAsset.asset)[0] - balance = Num.formatNue(balanceObject.balance, balanceObject.decimals) - } - + const balanceObject = balances.filter(b => b.asset.assetId === this.selectAsset.asset.assetId)[0] + balance = Num.formatNue(balanceObject.availableBalance, balanceObject.decimals) } if(this.transaction.type === 'toVapor'){ @@ -295,11 +289,11 @@ export default { } }); - account.balance(newGuid).then(balances => { + account.balance(this.address).then(balances => { let balance = 0.00 if(balances.length >0 ) { const balanceObject = balances.filter(b => b.asset === BTM)[0] - balance = balanceObject.balance / Math.pow(10, balanceObject.decimals) + balance = balanceObject.balance } this.accountBalance = balance; }).catch(error => { @@ -324,77 +318,93 @@ export default { return Num.formatCurrency(num, this.currency) }, assetChange: function (val) { - if(val.asset !== this.selectAsset.asset){ - this.transaction.asset = val.asset; - const balances = this.currentAccount.balances + if(val.asset.assetId !== this.selectAsset.asset.assetId){ + this.transaction.asset = val.asset.assetId; + const balances = this.assets let balance = 0.00 if(balances.length >0 ) { - const balanceObject = balances.filter(b => b.asset === val.asset)[0] - balance = Num.formatNue(balanceObject.balance, balanceObject.decimals) + const balanceObject = balances.filter(b => b.asset.assetId === val.asset.assetId)[0] + balance = Num.formatNue(balanceObject.availableBalance, 0) } this.accountBalance = balance; - transaction.asset(val.asset).then(ret => { + transaction.asset(val.asset.assetId).then(ret => { this.selectAsset = Object.assign(ret,val) this.transaction.cost = Number(ret[currencyInPrice[this.currency]] * this.transaction.amount).toFixed(2); }); } }, send: function () { - if (this.transaction.amount <= 0) { - this.$dialog.show({ - body: this.$t("transfer.noneBTM") - }); - return; - } - - let loader = this.$loading.show({ - // Optional parameters - container: null, - canCancel: true, - onCancel: this.onCancel + if (this.transaction.amount <= 0) { + this.$dialog.show({ + body: this.$t("transfer.noneBTM") }); + return; + } - // Bytom => Vapor - if(this.transaction.type === 'toVapor'){ - transaction.chainStatus().then((resp)=>{ - const address = resp.federation_address - account.setupNet(`${this.net}`) - this.transaction.to = address - transaction.build(this.account.guid, address, this.transaction.asset, Num.convertToNue(this.transaction.amount,this.selectAsset.decimals), this.transaction.confirmations).then(result => { - loader.hide(); - if(!this.transaction.fee){ - this.transaction.fee = Number( _.sumBy(result, 'fee')); - } - this.$router.push({ name: 'transfer-confirm', params: { account: this.account, transaction: this.transaction, rawData: result,assetAlias: this.selectAsset.symbol, type: this.$route.query.type } }) - }).catch(error => { - loader.hide(); - this.$dialog.show({ - body: getLang(error.message) - }); - }); - }) - } - - // Vapor => Bytom - else{ - const address = this.account.address - account.setupNet(`${this.net}vapor`) + let loader = this.$loading.show({ + // Optional parameters + container: null, + canCancel: true, + onCancel: this.onCancel + }); + + // Bytom => Vapor + if (this.transaction.type === 'toVapor') { + transaction.chainStatus().then((resp) => { + const address = resp.federationAddress + account.setupNet(`${this.net}`) this.transaction.to = address - transaction.buildCrossChain(this.account.guid, address, this.transaction.asset, Num.convertToNue(this.transaction.amount,this.selectAsset.decimals), this.transaction.confirmations).then(result => { - loader.hide(); - if(!this.transaction.fee){ - this.transaction.fee = Number( _.sumBy(result, 'fee') / 100000000); + transaction.build(this.address, address, this.transaction.asset, this.transaction.amount, this.transaction.confirmations).then(result => { + loader.hide(); + if (!this.transaction.fee) { + this.transaction.fee = Number( _.sumBy(result, 'tx.fee')); } - this.$router.push({ name: 'transfer-confirm', params: { account: this.account, transaction: this.transaction, rawData: result, assetAlias: this.selectAsset.symbol, type: this.$route.query.type } }) + this.$router.push({name: 'transfer-confirm', + params: { + account: this.account, + transaction: this.transaction, + rawData: result, + assetAlias: this.selectAsset.asset.symbol, + type: this.$route.query.type + } + }) }).catch(error => { - loader.hide(); - this.$dialog.show({ - body: getLang(error.message) - }); + loader.hide(); + this.$dialog.show({ + body: getLang(error.message) + }); }); - } + }) + } + // Vapor => Bytom + else { + const toAddress = this.account.address + account.setupNet(`${this.net}vapor`) + this.transaction.to = toAddress + transaction.buildCrossChain(this.address, toAddress, this.transaction.asset, this.transaction.amount, this.transaction.confirmations).then(result => { + loader.hide(); + if (!this.transaction.fee) { + this.transaction.fee = Number( _.sumBy(result, 'tx.fee')); + } + this.$router.push({name: 'transfer-confirm', + params: { + account: this.account, + transaction: this.transaction, + rawData: result, + assetAlias: this.selectAsset.asset.symbol, + type: this.$route.query.type + } + }) + }).catch(error => { + loader.hide(); + this.$dialog.show({ + body: getLang(error.message) + }); + }); + } } + }, mounted() { //detect injection if(this.$route.query.type === 'popup'){ @@ -422,13 +432,13 @@ export default { this.account = this.currentAccount } - const currentAsset = this.currentAccount.balances[0] + const currentAsset = this.currentAccount.balances[0] - if(currentAsset){ - transaction.asset(currentAsset.asset).then(ret => { - this.selectAsset = Object.assign(ret,currentAsset) - }); - } + if(currentAsset){ + transaction.asset(currentAsset.asset.assetId).then(ret => { + this.selectAsset = Object.assign(ret,currentAsset) + }); + } } }; diff --git a/src/views/sendTransaction/transferConfirm.vue b/src/views/sendTransaction/transferConfirm.vue index a9681a5..02ecb14 100644 --- a/src/views/sendTransaction/transferConfirm.vue +++ b/src/views/sendTransaction/transferConfirm.vue @@ -249,9 +249,14 @@ export default { onCancel: this.onCancel }); - // rawData, password - const address = this.netType === 'vapor'? this.account.vpAddress: this.account.address; - console.log(this.rawData) + let address + if (this.transaction.type === 'toVapor'){ + address = this.account.address + }else if(this.transaction.type === 'toBytom'){ + address = this.account.vpAddress + }else{ + address = this.netType === 'vapor'? this.account.vpAddress: this.account.address; + } Promise.all(this.rawData.map( (rawdata) => transaction.transfer(this.account.guid, rawdata, this.password, address))) .then(ret => {