• R/O
  • HTTP
  • SSH
  • HTTPS

Bytom-Dapp-Demo: Commit

A simple deposit and profit bytom dapp demo


Commit MetaInfo

Revision9c58c8af99598c680f8d166f496f79f69958faf6 (tree)
Zeit2019-04-01 18:07:09
AutorZhiting Lin <zlin035@uott...>
CommiterZhiting Lin

Log Message

update and seperate the code in to submitContract

Ändern Zusammenfassung

Diff

--- a/contracts/configure.json.js
+++ b/contracts/configure.json.js
@@ -1,26 +1,26 @@
11 var config = {
22 "solonet": {
3- "depositProgram": "2091194ddbf3614cafbadb1274c33e61afd4d5044c6ec4c30f8202980199c30083160014c800033d5e94de5f22e23a6d3cbeaed87b55bd640600204aa9d101050010a5d4e8203310d9951697418af3cdbe7a9cdde1dc49bb5439503dacb33828d6c9ef5af5a24dfc01567a64f5010000c358797ca153795579a19a6957790400e1f5059653790400e1f505967c00a07c00a09a69c358797c9f91616429010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec169515879c2515a79c16952c3597994c251005a79895979895879895779895679895579890274787e008901c07ec16963f0010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec16951c3c2515a79c16963fc010000567a567aae7cac747800c0",
4- "profitProgram": "2091194ddbf3614cafbadb1274c33e61afd4d5044c6ec4c30f8202980199c30083160014c800033d5e94de5f22e23a6d3cbeaed87b55bd640600204aa9d101050010a5d4e820666f298d34806a6db0528c3b3d081bc00fa58aa393e7c42f90d67eb7db2a524f4c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac747800c0",
5- "assetDeposited": "3310d9951697418af3cdbe7a9cdde1dc49bb5439503dacb33828d6c9ef5af5a2",
6- "assetBill": "666f298d34806a6db0528c3b3d081bc00fa58aa393e7c42f90d67eb7db2a524f",
3+ "depositProgram": "207c171cafa127bb405295e37cdbfbddbf2d39079872bd2f5cd95fb21a07147ec51600143705562ee78b51846b7aec9e2eadc5dcfef5c08f0600204aa9d101050010a5d4e820bf19b9726f8229cc3a894af3a93caea8b05c0605a5ebba21018f6a2cc85e02084dfc01567a64f5010000c358797ca153795579a19a6957790400e1f5059653790400e1f505967c00a07c00a09a69c358797c9f91616429010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec169515879c2515a79c16952c3597994c251005a79895979895879895779895679895579890274787e008901c07ec16963f0010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec16951c3c2515a79c16963fc010000567a567aae7cac747800c0",
4+ "profitProgram": "207c171cafa127bb405295e37cdbfbddbf2d39079872bd2f5cd95fb21a07147ec51600143705562ee78b51846b7aec9e2eadc5dcfef5c08f0600204aa9d101050010a5d4e82034d60be23a1858ed0d1f9c62a4a18185c2b35c068ca9e948d91fd62e7d47a8aa4c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac747800c0",
5+ "assetDeposited": "bf19b9726f8229cc3a894af3a93caea8b05c0605a5ebba21018f6a2cc85e0208",
6+ "assetBill": "34d60be23a1858ed0d1f9c62a4a18185c2b35c068ca9e948d91fd62e7d47a8aa",
77 "totalAmountBill": 1000000000000,
88 "totalAmountCapital": 2000000000000,
99 "dueBlockHeight": "",
1010 "expireBlockHeight": "",
11- "banker": "0014c800033d5e94de5f22e23a6d3cbeaed87b55bd64",
11+ "banker": "00143705562ee78b51846b7aec9e2eadc5dcfef5c08f",
1212 "gas": 0.4
1313 },
1414 "testnet":{
15- "depositProgram": "20f39af759065598406ca988f0dd79af9175dd7adcbe019317a2d605578b1597ac1600147211ec12410ce8bd0d71cab0a29be3ea61c71eb103c8260203da240203da2402060080f420e6b50600407a10f35a2000d38a1c946e8cba1a69493240f281cd925002a43b81f516c4391b5fb2ffdacd4d4302597a64370200005479cda069c35b790400e1f5059600a05c797ba19a53795579a19a695a790400e1f5059653790400e1f505967800a07800a09a6955797b957c9600a069c35b797c9f9161645b010000005b79c2547951005e79895d79895c79895b7989597989587989537a894ca4587a64980000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c967600a069c3787c9f91616481000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696393000000005b795479515b79c16951c3c2515d79c16963a4000000557acd9f69577a577aae7cac890274787e008901c07ec169515b79c2515d79c16952c35c7994c251005d79895c79895b79895a79895979895879895779895679895579890274787e008901c07ec1696332020000005b79c2547951005e79895d79895c79895b7989597989587989537a894ca4587a64980000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c967600a069c3787c9f91616481000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696393000000005b795479515b79c16951c3c2515d79c16963a4000000557acd9f69577a577aae7cac890274787e008901c07ec16951c3c2515d79c1696343020000547acd9f69587a587aae7cac747800c0",
16- "profitProgram": "20f39af759065598406ca988f0dd79af9175dd7adcbe019317a2d605578b1597ac1600147211ec12410ce8bd0d71cab0a29be3ea61c71eb103c8260203da2402060080f420e6b50600407a10f35a20f855baf98778a892bad0371f5afca845191824dc8584585d566fbbc8ef1f304c4ca4587a64980000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c967600a069c3787c9f91616481000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696393000000005b795479515b79c16951c3c2515d79c16963a4000000557acd9f69577a577aae7cac747800c0",
15+ "depositProgram": "20f39af759065598406ca988f0dd79af9175dd7adcbe019317a2d605578b1597ac1600147211ec12410ce8bd0d71cab0a29be3ea61c71eb1060040e59c30120600a0724e18092000d38a1c946e8cba1a69493240f281cd925002a43b81f516c4391b5fb2ffdacd4dfc01567a64f5010000c358797ca153795579a19a6957790400e1f5059653790400e1f505967c00a07c00a09a69c358797c9f91616429010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec169515879c2515a79c16952c3597994c251005a79895979895879895779895679895579890274787e008901c07ec16963f0010000005879c2547951005b79895a7989597989587989537a894c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac890274787e008901c07ec16951c3c2515a79c16963fc010000567a567aae7cac747800c0",
16+ "profitProgram": "20f39af759065598406ca988f0dd79af9175dd7adcbe019317a2d605578b1597ac1600147211ec12410ce8bd0d71cab0a29be3ea61c71eb1060040e59c30120600a0724e180920f855baf98778a892bad0371f5afca845191824dc8584585d566fbbc8ef1f304c4c9a567a649300000057790400e1f5059653790400e1f505967800a07800a09a5a7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f9161647c0000000059795479515979c1695178c2515b79c16952c3527994c251005b79895a79895979895879895779895679890274787e008901c07ec169638e0000000059795479515979c16951c3c2515b79c169639a000000567a567aae7cac747800c0",
1717 "assetDeposited": "00d38a1c946e8cba1a69493240f281cd925002a43b81f516c4391b5fb2ffdacd",
1818 "assetBill": "f855baf98778a892bad0371f5afca845191824dc8584585d566fbbc8ef1f304c",
19- "totalAmountBill": 100000000000000,
20- "totalAmountCapital": 200000000000000,
21- "dueBlockHeight": 140506,
22- "expireBlockHeight": 140506,
19+ "totalAmountBill": 10000000000000,
20+ "totalAmountCapital": 20000000000000,
21+ "dueBlockHeight": 0,
22+ "expireBlockHeight": 0,
2323 "banker": "00147211ec12410ce8bd0d71cab0a29be3ea61c71eb1",
2424 "gas": 0.4
2525 }
26-}
\ No newline at end of file
26+}
--- a/src/components/layout/account/action.js
+++ b/src/components/layout/account/action.js
@@ -1,4 +1,4 @@
1-import { listAddress } from '../../bytom'
1+import { listAddress } from '../../util/api'
22
33
44 import GetContractArgs from "../../constants";
--- a/src/components/layout/account/index.jsx
+++ b/src/components/layout/account/index.jsx
@@ -5,7 +5,7 @@ import { connect } from "react-redux"
55 import jdenticon from "jdenticon"
66 import action from "./action";
77 import GetContractArgs from "../../constants";
8-import { listBalances } from '../../bytom'
8+import { listBalances } from '../../util/api'
99
1010 class Account extends Component {
1111 constructor(props) {
--- a/src/components/layout/profit/action.js
+++ b/src/components/layout/profit/action.js
@@ -1,25 +1,41 @@
11 import {
22 spendUTXOAction, spendWalletAction, controlProgramAction, controlAddressAction,
3- updateBalances, updateUtxo, listDappUTXO, contractArguments
4-} from '../../bytom'
3+ contractArguments
4+} from '../../util/bytomAction'
5+import {
6+ updateBalances, listDappUTXO
7+} from '../../util/api'
8+import { submitContract } from '../../util/submitContract'
59 import GetContractArgs from '../../constants'
610 import { matchesUTXO } from '../../filter'
711 import BigNumber from 'bignumber.js'
812
913 export function FixedLimitProfit(account, amountBill, saver) {
14+ const object = {
15+ address: saver,
16+ amount: amountBill,
17+ parameter: [saver, amountBill, account]
18+ }
19+ return submitContract(listDepositUTXO, createContractTransaction, updateDatatbaseBalance, object)
20+}
21+
22+function listDepositUTXO() {
23+ return listDappUTXO({
24+ "program": GetContractArgs().profitProgram,
25+ "asset": GetContractArgs().assetDeposited,
26+ "sort": {
27+ "by":"amount",
28+ "order":"desc"
29+ }
30+ })
31+}
32+
33+function createContractTransaction(resp, amountBill, saver) {
1034 return new Promise((resolve, reject) => {
11- return listDappUTXO({
12- "program": GetContractArgs().profitProgram,
13- "asset": GetContractArgs().assetDeposited,
14- "sort": {
15- "by":"amount",
16- "order":"desc"
17- }
18- }).then(resp => {
1935 if(resp.length === 0) {
20- throw 'Empty UTXO info, it might be that the utxo is locked. Please retry after 60s.'
36+ reject( 'Empty UTXO info, it might be that the utxo is locked. Please retry after 60s.')
2137 }else if(amountBill < 100000000){
22- throw 'Please enter an amount bigger or equal than 100000000.'
38+ reject( 'Please enter an amount bigger or equal than 100000000.')
2339 }
2440
2541 const radio = BigNumber( GetContractArgs().radio )
@@ -31,18 +47,18 @@ export function FixedLimitProfit(account, amountBill, saver) {
3147 const utxo = result.hash
3248
3349 if(profitAmount > capitalAmount) {
34- throw 'input amount must be smaller or equal to ' + capitalAmount/radio.toNumber() + '.'
35- }else{
50+ reject( 'input amount must be smaller or equal to ' + capitalAmount/radio.toNumber() + '.')
51+ }else {
3652 const input = []
3753 const output = []
3854
39- const sAmountBill = BigNumber(amountBill).div( 100000000 )
40- const sTotalAmountBill = BigNumber(GetContractArgs().totalAmountBill).div( 100000000 )
41- const multiplyResult = BigNumber( GetContractArgs().totalAmountCapital).multipliedBy( sAmountBill )
42- const gain = multiplyResult.div( sTotalAmountBill ).toNumber()
55+ const sAmountBill = BigNumber(amountBill).div(100000000)
56+ const sTotalAmountBill = BigNumber(GetContractArgs().totalAmountBill).div(100000000)
57+ const multiplyResult = BigNumber(GetContractArgs().totalAmountCapital).multipliedBy(sAmountBill)
58+ const gain = multiplyResult.div(sTotalAmountBill).toNumber()
4359
44- if( multiplyResult.isGreaterThan( 9223372036854775807 ) ){
45- throw 'The entered amount is too big, please reduce the amount.'
60+ if (multiplyResult.isGreaterThan(9223372036854775807)) {
61+ reject( 'The entered amount is too big, please reduce the amount.')
4662 }
4763
4864 const args = contractArguments(amountBill, saver)
@@ -50,56 +66,42 @@ export function FixedLimitProfit(account, amountBill, saver) {
5066 input.push(spendUTXOAction(utxo))
5167 input.push(spendWalletAction(amountBill, GetContractArgs().assetBill))
5268
53- if( gain < capitalAmount ){
54- output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker ))
69+ if (gain < capitalAmount) {
70+ output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker))
5571 output.push(controlAddressAction(gain, capitalAsset, saver))
5672 output.push(controlProgramAction((BigNumber(capitalAmount).minus(gain)).toNumber(), capitalAsset, GetContractArgs().profitProgram))
57- }else{
58- output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker ))
73+ } else {
74+ output.push(controlProgramAction(amountBill, GetContractArgs().assetBill, GetContractArgs().banker))
5975 output.push(controlAddressAction(capitalAmount, capitalAsset, saver))
6076 }
6177
62- updateUtxo({"hash": utxo})
63- .then(()=>{
64- window.bytom.advancedTransfer(input, output, GetContractArgs().gas*10000000, args, 1)
65- .then((resp) => {
66- if(resp.action === 'reject'){
67- reject('user reject the request')
68- }else if(resp.action === 'success'){
69- const transactionHash = resp.message.result.data.transaction_hash
70- updateBalances({
71- "tx_id": transactionHash,
72- "address": saver,
73- "asset": GetContractArgs().assetDeposited,
74- "amount": profitAmount
75- }).then(()=>{
76- updateBalances({
77- "tx_id": transactionHash,
78- "address": account.address,
79- "asset": GetContractArgs().assetBill,
80- "amount": -amountBill
81- }).then(()=>{
82- resolve()
83- }).catch(err => {
84- throw err
85- })
86- }).catch(err => {
87- throw err
88- })
89- }
90- })
91- .catch(err => {
92- throw err
93- })
94- })
95- .catch(err => {
96- throw err
97- })
78+ resolve({
79+ input,
80+ output,
81+ args,
82+ utxo
83+ })
9884 }
99- }).catch(err => {
100- reject(err)
101- })
10285 })
10386 }
10487
105-
88+function updateDatatbaseBalance(resp, saver, amountBill, account){
89+ const transactionHash = resp.message.result.data.transaction_hash
90+ const radio = BigNumber( GetContractArgs().radio )
91+ const profitAmount = radio.multipliedBy(amountBill).toNumber()
92+ return updateBalances({
93+ "tx_id": transactionHash,
94+ "address": saver,
95+ "asset": GetContractArgs().assetDeposited,
96+ "amount": profitAmount
97+ }).then(()=>{
98+ return updateBalances({
99+ "tx_id": transactionHash,
100+ "address": account.address,
101+ "asset": GetContractArgs().assetBill,
102+ "amount": -amountBill
103+ })
104+ }).catch(err => {
105+ throw err
106+ })
107+}
--- a/src/components/layout/profit/index.jsx
+++ b/src/components/layout/profit/index.jsx
@@ -44,19 +44,27 @@ class Profit extends React.Component {
4444 const account = this.state.account
4545 const address = event.target.address.value
4646
47+ this.refs.btn.setAttribute("disabled", "disabled")
48+ this.setState({
49+ error:'',
50+ msg: ''
51+ })
52+
4753 FixedLimitProfit(account, amount, address)
4854 .then(()=> {
55+ this.refs.btn.removeAttribute("disabled");
4956 this.setState({
5057 error:'',
5158 msg:`Submit success!!! you spent ${amount} bill asset, and gain ${BigNumber(amount).multipliedBy(GetContractArgs().radio).toNumber()} deposit asset.`
5259 })
5360 }).catch(err => {
54- this.setState({
55- error: err,
56- msg:''
61+ this.refs.btn.removeAttribute("disabled");
62+ this.setState({
63+ error: err,
64+ msg:''
65+ })
5766 })
58- })
59- }
67+ }
6068
6169 render() {
6270 return (
@@ -88,7 +96,7 @@ class Profit extends React.Component {
8896 onChange={this.handleInputChange} />
8997 </div>
9098 <p>Fee: {GetContractArgs().gas} BTM</p>
91- <button type="submit" className="btn btn-primary">Profit to address</button>
99+ <button ref='btn' type="submit" className="btn btn-primary">Profit to address</button>
92100 {this.state.msg && <div className="alert alert-success mt-4" role="alert">
93101 {this.state.msg}
94102 </div>}
--- a/src/components/layout/save/action.js
+++ b/src/components/layout/save/action.js
@@ -1,92 +1,94 @@
11 import {
22 spendUTXOAction, spendWalletAction, controlProgramAction,
3- controlAddressAction, listDappUTXO, updateUtxo, updateBalances,
4- contractArguments
5-} from '../../bytom'
3+ controlAddressAction, contractArguments
4+} from '../../util/bytomAction'
5+import { listDappUTXO, updateBalances
6+} from '../../util/api'
67 import GetContractArgs from '../../constants'
78 import { matchesUTXO } from '../../filter'
9+import { submitContract } from '../../util/submitContract'
810 import BigNumber from 'bignumber.js'
911
1012 export function FixedLimitDeposit(amount, address) {
11- return new Promise((resolve, reject) => {
12- return listDappUTXO({
13- "program": GetContractArgs().depositProgram,
14- "asset": GetContractArgs().assetBill,
15- "sort": {
16- "by":"amount",
17- "order":"desc"
18- }
19- }).then(resp => {
20- const limit = GetContractArgs().radio * 100000000
21- if(resp.length === 0) {
22- throw 'Empty UTXO info, it might be that the utxo is locked. Please retry after 60s.'
23- }else if(amount < limit){
24- throw `Please enter an amount bigger or equal than ${limit}.`
25- }
13+ const object = {
14+ address: address,
15+ amount: amount,
16+ parameter: [amount, address]
17+ }
18+ return submitContract(listDepositUTXO, createContractTransaction, updateDatatbaseBalance, object)
19+}
20+
21+function listDepositUTXO() {
22+ return listDappUTXO({
23+ "program": GetContractArgs().depositProgram,
24+ "asset": GetContractArgs().assetBill,
25+ "sort": {
26+ "by":"amount",
27+ "order":"desc"
28+ }
29+ })
30+}
2631
27- const result = matchesUTXO(resp, amount)
28- const billAmount = result.amount
29- const billAsset = result.asset
30- const utxo = result.hash
32+function createContractTransaction(resp, amount, address){
33+ return new Promise((resolve, reject) => {
34+ //utxo pre calculation
35+ const limit = GetContractArgs().radio * 100000000
36+ if (resp.length === 0) {
37+ reject( 'Empty UTXO info, it might be that the utxo is locked. Please retry after 60s.')
38+ } else if (amount < limit) {
39+ reject( `Please enter an amount bigger or equal than ${limit}.`)
40+ }
3141
32- if(amount > billAmount){
33- throw 'input amount must be smaller or equal to ' + billAmount +'.'
34- }else{
35- const input = []
36- const output = []
42+ const result = matchesUTXO(resp, amount)
43+ const billAmount = result.amount
44+ const billAsset = result.asset
45+ const utxo = result.hash
3746
38- const args = contractArguments(amount, address)
47+ //contract calculation
48+ if (amount > billAmount) {
49+ reject('input amount must be smaller or equal to ' + billAmount + '.')
50+ } else {
51+ const input = []
52+ const output = []
3953
40- input.push(spendUTXOAction(utxo))
41- input.push(spendWalletAction(amount, GetContractArgs().assetDeposited))
54+ const args = contractArguments(amount, address)
4255
43- if(amount < billAmount){
44- output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
45- output.push(controlAddressAction(amount, billAsset, address))
46- output.push(controlProgramAction((BigNumber(billAmount).minus(BigNumber(amount))).toNumber(), billAsset, GetContractArgs().depositProgram))
47- }else{
48- output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
49- output.push(controlAddressAction(billAmount, billAsset, address))
50- }
56+ input.push(spendUTXOAction(utxo))
57+ input.push(spendWalletAction(amount, GetContractArgs().assetDeposited))
5158
52- updateUtxo({"hash": utxo})
53- .then(()=>{
54- window.bytom.advancedTransfer(input, output, GetContractArgs().gas*10000000, args, 1)
55- .then((resp) => {
56- if(resp.action === 'reject'){
57- reject('user reject the request')
58- }else if(resp.action === 'success'){
59- updateBalances({
60- "tx_id": resp.message.result.data.transaction_hash,
61- address,
62- "asset": GetContractArgs().assetDeposited,
63- "amount": -amount
64- }).then(()=>{
65- updateBalances({
66- "tx_id": resp.message.result.data.transaction_hash,
67- address,
68- "asset": GetContractArgs().assetBill,
69- "amount": amount
70- }).then(()=>{
71- resolve()
72- }).catch(err => {
73- throw err
74- })
75- }).catch(err => {
76- throw err
77- })
78- }
79- })
80- .catch(err => {
81- throw err
82- })
83- })
84- .catch(err => {
85- throw err
86- })
59+ if (amount < billAmount) {
60+ output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
61+ output.push(controlAddressAction(amount, billAsset, address))
62+ output.push(controlProgramAction((BigNumber(billAmount).minus(BigNumber(amount))).toNumber(), billAsset, GetContractArgs().depositProgram))
63+ } else {
64+ output.push(controlProgramAction(amount, GetContractArgs().assetDeposited, GetContractArgs().profitProgram))
65+ output.push(controlAddressAction(billAmount, billAsset, address))
8766 }
88- }).catch(err => {
89- reject(err)
67+
68+ resolve({
69+ input,
70+ output,
71+ args,
72+ utxo
73+ })
74+ }
75+ })
76+}
77+
78+function updateDatatbaseBalance(resp, amount, address){
79+ return updateBalances({
80+ "tx_id": resp.message.result.data.transaction_hash,
81+ address,
82+ "asset": GetContractArgs().assetDeposited,
83+ "amount": -amount
84+ }).then(()=>{
85+ return updateBalances({
86+ "tx_id": resp.message.result.data.transaction_hash,
87+ address,
88+ "asset": GetContractArgs().assetBill,
89+ "amount": amount
9090 })
91+ }).catch(err => {
92+ throw err
9193 })
9294 }
--- a/src/components/layout/save/index.jsx
+++ b/src/components/layout/save/index.jsx
@@ -43,13 +43,21 @@ class Save extends React.Component {
4343 const account = this.state.account
4444 const address = account.address
4545
46+ this.refs.btn.setAttribute("disabled", "disabled")
47+ this.setState({
48+ error:'',
49+ msg: ''
50+ })
51+
4652 FixedLimitDeposit(amount, address)
4753 .then(()=> {
54+ this.refs.btn.removeAttribute("disabled");
4855 this.setState({
4956 error:'',
5057 msg:`Submit success!!! you spent ${amount} deposite asset,and gain ${amount} bill asset.`
5158 })
5259 }).catch(err => {
60+ this.refs.btn.removeAttribute("disabled");
5361 this.setState({
5462 error:err,
5563 msg: ''
@@ -78,7 +86,7 @@ class Save extends React.Component {
7886 onChange={this.handleInputChange} />
7987 </div>
8088 <p>Fee: {GetContractArgs().gas} BTM</p>
81- <button type="submit" className="btn btn-primary">Spend Asset</button>
89+ <button ref="btn" type="submit" className="btn btn-primary">Spend Asset</button>
8290
8391 {this.state.msg && <div className="alert alert-success mt-4" role="alert">
8492 {this.state.msg}
--- a/src/components/bytom.js
+++ b/src/components/util/api.js
@@ -1,61 +1,13 @@
1-import axios from 'axios'
2-
3-export function spendUTXOAction(utxo){
4- return {
5- "type": "spend_utxo",
6- "output_id": utxo
7- }
8-}
9-
10-export function contractArguments(amount, address){
11- return [
12- {
13- "type": "integer",
14- "value": amount
15- },
16- {
17- "type": "address",
18- "value": address
19- },
20- {
21- "type": "data",
22- "value": ""
23- }
24- ]
25-}
26-
27-export function spendWalletAction(amount, asset){
28- return {
29- "amount": amount,
30- "asset": asset,
31- "type": "spend_wallet"
32- }
33-}
34-
35-export function controlProgramAction(amount, asset, program){
36- return {
37- "amount": amount,
38- "asset": asset,
39- "control_program": program,
40- "type": "control_program"
41- }
42-}
43-
44-export function controlAddressAction(amount, asset, address){
45- return {
46- "amount": amount,
47- "asset": asset,
48- "address": address,
49- "type": "control_address"
50- }
51-}
1+import axios from "axios/index";
522
3+//Api call using bytomAPI sdk
534 export function listAddress(guid)
545 {
556 return bytomAPI.sdk.accounts.listAddressUseServer(guid)
567 .then(resp => resp[0])
578 }
589
10+//Api call from Buffer server
5911 export function listDappUTXO(params)
6012 {
6113 let url
@@ -120,4 +72,4 @@ function post(url, params){
12072 throw response.data.msg
12173 }
12274 })
123-}
75+}
\ No newline at end of file
--- /dev/null
+++ b/src/components/util/bytomAction.js
@@ -0,0 +1,51 @@
1+export function spendUTXOAction(utxo){
2+ return {
3+ "type": "spend_utxo",
4+ "output_id": utxo
5+ }
6+}
7+
8+export function contractArguments(amount, address){
9+ return [
10+ {
11+ "type": "integer",
12+ "value": amount
13+ },
14+ {
15+ "type": "address",
16+ "value": address
17+ },
18+ {
19+ "type": "data",
20+ "value": ""
21+ }
22+ ]
23+}
24+
25+export function spendWalletAction(amount, asset){
26+ return {
27+ "amount": amount,
28+ "asset": asset,
29+ "type": "spend_wallet"
30+ }
31+}
32+
33+export function controlProgramAction(amount, asset, program){
34+ return {
35+ "amount": amount,
36+ "asset": asset,
37+ "control_program": program,
38+ "type": "control_program"
39+ }
40+}
41+
42+export function controlAddressAction(amount, asset, address){
43+ return {
44+ "amount": amount,
45+ "asset": asset,
46+ "address": address,
47+ "type": "control_address"
48+ }
49+}
50+
51+
--- /dev/null
+++ b/src/components/util/submitContract.js
@@ -0,0 +1,54 @@
1+import GetContractArgs from "../constants";
2+import { updateUtxo } from './api'
3+
4+export function submitContract(listDepositUTXO, createContractTransaction, updateDatatbaseBalance, object) {
5+ const address = object.address
6+ const amount = object.amount
7+ const updateParameters = object.parameter
8+
9+ return new Promise((resolve, reject) => {
10+ //list available utxo
11+ return listDepositUTXO().then(resp => {
12+
13+ //create the Contract Transaction
14+ return createContractTransaction(resp, amount, address).then(object =>{
15+ const input = object.input
16+ const output = object.output
17+ const args = object.args
18+
19+ const utxo = object.utxo
20+
21+ //Lock UTXO
22+ return updateUtxo({"hash": utxo})
23+ .then(()=>{
24+
25+ //Transactions
26+ window.bytom.advancedTransfer(input, output, GetContractArgs().gas*100000000, args, 1)
27+ .then((resp) => {
28+ if(resp.action === 'reject'){
29+ reject('user reject the request')
30+ }else if(resp.action === 'success'){
31+
32+ //Update Balance
33+ return updateDatatbaseBalance(resp, ...updateParameters).then(()=>{
34+ resolve()
35+ }).catch(err => {
36+ throw err
37+ })
38+ }
39+ })
40+ .catch(err => {
41+ throw err
42+ })
43+ })
44+ .catch(err => {
45+ throw err
46+ })
47+ }).catch(err => {
48+ throw err
49+ })
50+ }).catch(err => {
51+ reject(err)
52+ })
53+ })
54+}
Show on old repository browser