From: Zhiting Lin Date: Fri, 15 Feb 2019 07:54:47 +0000 (+0800) Subject: matches Arguement styles, and add the error handle for dapp request X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=3a3ecec829f69b65dde21da7c76e582c59dba502;p=bytom%2FBytom-Dapp-Demo.git matches Arguement styles, and add the error handle for dapp request --- diff --git a/src/components/bytom.js b/src/components/bytom.js index 5c0d072..f8318c1 100644 --- a/src/components/bytom.js +++ b/src/components/bytom.js @@ -1,32 +1,29 @@ import axios from 'axios' -export function spendUTXOAction(utxo, amount, address){ +export function spendUTXOAction(utxo){ return { "type": "spend_utxo", - "output_id": utxo, - "arguments": [ - { - "type": "integer", - "raw_data": { - "value": amount - } - }, - { - "type": "address", - "raw_data": { - "value": address - } - }, - { - "type": "data", - "raw_data": { - "value": "" - } - } - ] + "output_id": utxo } } +export function contractArguments(amount, address){ + return [ + { + "type": "integer", + "value": amount + }, + { + "type": "address", + "value": address + }, + { + "type": "data", + "value": "" + } + ] +} + export function spendWalletAction(amount, asset){ return { "amount": amount, @@ -53,28 +50,10 @@ export function controlAddressAction(amount, asset, address){ } } -export function listUTXO(params) +export function listAddress(guid) { - const url = "/api/api/v1/btm/q/list-utxos" - return axios({ - method: 'post', - url, - data: params - }).then(response => { - return response.data.result.data[0]; - }) -} - -export function listAddress(params) -{ - const url = "/api/api/v1/btm//account/list-addresses" - return axios({ - method: 'post', - url, - data: params - }).then(response => { - return response.data.result.data[0]; - }) + return bytomAPI.sdk.accounts.listAddressUseServer(guid) + .then(resp => resp[0]) } export function listDappUTXO(params) @@ -85,7 +64,12 @@ export function listDappUTXO(params) url, data: params }).then(response => { - return response.data.result.data[0]; + if(response.data.code === 200){ + return response.data.result.data[0]; + } + else { + throw response.data.msg + } }) } @@ -97,7 +81,11 @@ export function updateBase(params) url, data: params }).then(response => { - return response.data.result; + if(response.data.code === 200){ + return response.data.result; + }else { + throw response.data.msg + } }) } @@ -109,7 +97,11 @@ export function updateUtxo(params) url, data: params }).then(response => { - return response.data.result; + if(response.data.code === 200){ + return response.data.result; + }else{ + throw response.data.msg + } }) } @@ -121,7 +113,11 @@ export function updateBalances(params) url, data: params }).then(response => { - return response.data.result; + if(response.data.code === 200){ + return response.data.result; + }else{ + throw response.data.msg + } }) } @@ -133,6 +129,10 @@ export function listBalances(params) url, data: params }).then(response => { - return response.data.result; + if(response.data.code === 200){ + return response.data.result; + }else{ + throw response.data.msg + } }) } diff --git a/src/components/layout/account/action.js b/src/components/layout/account/action.js index cb3604b..5fd8756 100644 --- a/src/components/layout/account/action.js +++ b/src/components/layout/account/action.js @@ -5,7 +5,7 @@ import { assetDeposited, assetBill } from "../../constants"; const updateBalances = (guid = '') => { return (dispatch) => { - return listAddress({guid}) + return listAddress(guid) .then((resp)=>{ const balances = resp.balances const billBalance = balances.filter(balance => balance.asset === assetBill) diff --git a/src/components/layout/profit/action.js b/src/components/layout/profit/action.js index 85d3a6e..938b36a 100644 --- a/src/components/layout/profit/action.js +++ b/src/components/layout/profit/action.js @@ -1,6 +1,6 @@ import { spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction, - updateBase, updateBalances, updateUtxo, listDappUTXO + updateBase, updateBalances, updateUtxo, listDappUTXO, contractArguments } from '../../bytom' import {profitProgram, assetDeposited, assetBill, gas, banker, totalAmountBill, totalAmountCapital} from "../../constants"; @@ -25,7 +25,9 @@ export function FixedLimitProfit(account, amountBill, saver) { const sTotalAmountBill = totalAmountBill/100000000 const gain = totalAmountCapital*sAmountBill/sTotalAmountBill - input.push(spendUTXOAction(utxo, amountBill, saver)) + const args = contractArguments(amountBill, saver) + + input.push(spendUTXOAction(utxo)) input.push(spendWalletAction(amountBill, assetBill)) if(amountBill < capitalAmount){ @@ -37,7 +39,7 @@ export function FixedLimitProfit(account, amountBill, saver) { output.push(controlAddressAction(capitalAmount, capitalAsset, saver)) } - window.bytom.advancedTransfer(account, input, output, gas*10000000) + window.bytom.advancedTransfer(account, input, output, gas*10000000, args) .then((resp) => { if(resp.action === 'reject'){ reject('user reject the request') @@ -55,9 +57,16 @@ export function FixedLimitProfit(account, amountBill, saver) { "amount": -amountBill }).then(()=>{ resolve() + }).catch(err => { + throw err }) + }).catch(err => { + throw err }) }) + .catch(err => { + throw err + }) } }) .catch(err => { diff --git a/src/components/layout/save/action.js b/src/components/layout/save/action.js index b385e25..159a1bf 100644 --- a/src/components/layout/save/action.js +++ b/src/components/layout/save/action.js @@ -1,6 +1,7 @@ import { spendUTXOAction, spendWalletAction, controlProgramAction, - controlAddressAction, listDappUTXO, updateBase, updateUtxo ,updateBalances + controlAddressAction, listDappUTXO, updateBase, updateUtxo ,updateBalances, + contractArguments } from '../../bytom' import { depositProgram, profitProgram, assetDeposited, assetBill, gas @@ -22,7 +23,9 @@ export function FixedLimitDeposit(account, amount, address) { const input = [] const output = [] - input.push(spendUTXOAction(utxo, amount, address)) + const args = contractArguments(amount, address) + + input.push(spendUTXOAction(utxo)) input.push(spendWalletAction(amount, assetDeposited)) if(amount < billAmount){ @@ -34,7 +37,7 @@ export function FixedLimitDeposit(account, amount, address) { output.push(controlAddressAction(billAmount, billAsset, address)) } - window.bytom.advancedTransfer(account, input, output, gas*10000000) + window.bytom.advancedTransfer(account, input, output, gas*10000000, args) .then((resp) => { if(resp.action === 'reject'){ reject('user reject the request') @@ -52,9 +55,16 @@ export function FixedLimitDeposit(account, amount, address) { "amount": amount }).then(()=>{ resolve() + }).catch(err => { + throw err }) + }).catch(err => { + throw err }) }) + .catch(err => { + throw err + }) } }) .catch(err => {