Skip to content

Commit

Permalink
Merge pull request #2 from ety001/feature_0313
Browse files Browse the repository at this point in the history
Feature 0313 -- v0.0.7
  • Loading branch information
ety001 authored Mar 15, 2018
2 parents bb39bce + 8f696f6 commit 40fe07e
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 63 deletions.
31 changes: 28 additions & 3 deletions src/components/common/OrderList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@
<md-table-cell>
<span>{{ order.amount | fixNumCustom(7) }}</span>
</md-table-cell>
<md-table-cell></md-table-cell>
<md-table-cell>
<a href="javascript:void(0);" v-on:click="cancel(order.id)">{{ $t('myorder.cancel') }}</a>
</md-table-cell>
</md-table-row>
</md-table-body>
<md-table-body v-else>
Expand All @@ -165,13 +167,15 @@ import Api from '@/lib/Api';
export default {
data() {
return {
pair: null,
selectedPair: '',
selectedPairStr: '',
selectedPairBaseAsset: '',
selectedPairCounterAsset: '',
orderbookInterval: null,
orderbook: null,
myOrderList: [],
pairOrderObj: {},
};
},
computed: {
Expand All @@ -182,12 +186,14 @@ export default {
watch: {
selectedPair(newPair) {
const pair = this.parseExchangeKey(newPair);
this.pair = pair;
this.selectedPairStr = `${pair.baseAsset}/${pair.counterAsset}`;
this.selectedPairBaseAsset = pair.baseAsset;
this.selectedPairCounterAsset = pair.counterAsset;
window.Sconsole(['selectedPair', pair]);
clearInterval(this.orderbookInterval);
this.updateOrderBook(pair);
this.updateMyOrderList(pair);
this.orderbookInterval = setInterval(() => {
this.updateOrderBook(pair);
this.updateMyOrderList(pair);
Expand Down Expand Up @@ -236,7 +242,7 @@ export default {
Api.getOrderBook(window.server, sellingAsset, buyingAsset, (res) => {
this.orderbook = res;
}, (errRes) => {
window.Sconsole(['updateOrderbook fail', errRes]);
window.Sconsole(['updateOrderbook fail', errRes], 'msg');
});
},
updateMyOrderList(pair) {
Expand All @@ -255,6 +261,7 @@ export default {
Api.getOffers(window.server, this.$store.getters.privateKey, (res) => {
if (res.records.length > 0) {
const tmpMyOrder = [];
const tmpPairOrder = {};
res.records.forEach((record) => {
if (JSON.stringify(record.buying) === JSON.stringify(sellingAsset)
&& JSON.stringify(record.selling) === JSON.stringify(buyingAsset)) {
Expand All @@ -264,6 +271,7 @@ export default {
price: 1 / record.price, // record.price = baseAsset / counterAsset
amount: record.price * record.amount, // record.amount = counterAsset
});
tmpPairOrder[record.id] = record;
}
if (JSON.stringify(record.selling) === JSON.stringify(sellingAsset)
&& JSON.stringify(record.buying) === JSON.stringify(buyingAsset)) {
Expand All @@ -273,14 +281,31 @@ export default {
price: record.price, // counterAsset / baseAsset
amount: record.amount, // baseAsset
});
tmpPairOrder[record.id] = record;
}
});
this.myOrderList = tmpMyOrder;
this.pairOrderObj = tmpPairOrder;
}
}, (errRes) => {
window.Sconsole(['updateMyList fail', errRes]);
window.Sconsole(['updateMyList fail', errRes], 'msg');
});
},
cancel(orderId) {
if (this.pairOrderObj[orderId]) {
this.$store.commit('updateSnackmsg', this.$i18n.translate('myorder.cancel_msg'));
Api.cancelOrder(
window.server,
this.$store.getters.privateKey,
this.pairOrderObj[orderId],
(transResult) => {
window.Sconsole(['cancel order result', transResult]);
this.updateMyOrderList(this.pair);
}, (errRes) => {
window.Sconsole(['cancel order err', errRes, errRes.message], 'msg');
});
}
},
},
};
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/WalletInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export default {
this.assetIssuer = '';
},
(errRes) => {
window.Sconsole(['addTrustline fail', errRes]);
window.Sconsole(['addTrustline fail', errRes], 'msg');
this.$store.commit('updateIsloading', false);
this.$store.commit('updateSnackmsg', 'wallet.add_trustline_fail');
this.assetCode = '';
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/WalletInfoItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default {
this.$store.commit('updateSnackmsg', 'wallet.remove_trustline_succ');
},
(errRes) => {
window.Sconsole(['removeTrustline fail', errRes]);
window.Sconsole(['removeTrustline fail', errRes], 'msg');
this.$store.commit('updateIsloading', false);
this.$store.commit('updateSnackmsg', 'wallet.remove_trustline_fail');
},
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"sell": "Sell",
"price": "Price{pair}",
"cancel": "Cancel",
"amount": "Amount{asset}"
"amount": "Amount{asset}",
"cancel_msg": "Order canceling, please wait ...."
}
}
3 changes: 2 additions & 1 deletion src/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"sell": "卖出",
"price": "价格{pair}",
"cancel": "撤单",
"amount": "数量{asset}"
"amount": "数量{asset}",
"cancel_msg": "订单取消中, 请等待生效..."
}
}
69 changes: 45 additions & 24 deletions src/lib/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,17 @@ export default {
amount: Number(amount).toFixed(7), // The total amount you're selling
price : Number(price).toFixed(7) // The exchange rate ratio (selling / buying)
})
let tx = new StellarSdk.TransactionBuilder(account).addOperation(op).build();
// console.log(tx);
const tx = new StellarSdk.TransactionBuilder(account).addOperation(op).build();
tx.sign(keyPair);
// lock
store.commit('updateOrderLock', {skey, lock: true});
server.submitTransaction(tx)
.then(function(transactionResult) {
window.Sconsole(['transactionResult', transactionResult]);
// unlock
store.commit('updateOrderLock', {skey, lock: false});
return null;
}).catch((err) => {
// unlock
store.commit('updateOrderLock', {skey, lock: false});
cbErr(err);
});
return null;
}).then(function(e) {
window.Sconsole([e]);
return null;
})
.catch(function(e) {
return server.submitTransaction(tx);
}).then(function(transactionResult) {
window.Sconsole(['transactionResult', transactionResult]);
// unlock
store.commit('updateOrderLock', {skey, lock: false});
return;
}).catch(function(e) {
// unlock
store.commit('updateOrderLock', {skey, lock: false});
cbErr(e);
Expand All @@ -167,6 +156,39 @@ export default {
window.Sconsole(['lock', lockStatus]);
}
},
cancelOrder: function(server, privateKey, order, cb, cbErr) {
const keyPair = StellarSdk.Keypair.fromSecret(privateKey);
let buying, selling;
if (order.buying.asset_type === 'native') {
buying = StellarSdk.Asset.native();
} else {
buying = new StellarSdk.Asset(order.buying.asset_code, order.buying.asset_issuer);
}
if (order.selling.asset_type === 'native') {
selling = StellarSdk.Asset.native();
} else {
selling = new StellarSdk.Asset(order.selling.asset_code, order.selling.asset_issuer);
}
server.loadAccount(keyPair.publicKey())
.then((account) => {
const op = StellarSdk.Operation.manageOffer({
selling: selling,
buying: buying,
amount: '0',
price : Number(order.price).toFixed(7),
offerId: order.id,
});
const tx = new StellarSdk.TransactionBuilder(account).addOperation(op).build();
tx.sign(keyPair);
return server.submitTransaction(tx);
}).then(function(transactionResult) {
window.Sconsole(['cancel transaction result', transactionResult]);
cb(transactionResult);
return;
}).catch(function(e) {
cbErr(e);
});
},
findOrder: function (offers, pair, cb) {
const buyOrders = offers.filter((detail) => {
const buyingAsset = {
Expand Down Expand Up @@ -213,7 +235,7 @@ export default {
const skey = `${pair.baseAsset}_${pair.baseIssuer}`;
const tmp = store.getters.maxes.filter(detail => detail.skey === skey);
if (tmp.length > 0) {
return tmp[0].max;
return Number(tmp[0].max);
} else {
return 0;
}
Expand All @@ -226,7 +248,7 @@ export default {
const skey = `${pair.counterAsset}_${pair.counterIssuer}`;
const tmp = store.getters.maxes.filter(detail => detail.skey === skey);
if (tmp.length > 0) {
return tmp[0].max;
return Number(tmp[0].max);
} else {
return 0;
}
Expand Down Expand Up @@ -269,7 +291,7 @@ export default {
const skey = `${pair.baseAsset}_${pair.baseIssuer}`;
const res = store.getters.exchangeVals.filter(detail => detail.skey === skey);
if (res.length > 0) {
return res[0].exchangeVal;
return Number(res[0].exchangeVal);
} else {
return 0;
}
Expand All @@ -281,7 +303,7 @@ export default {
const skey = `${pair.counterAsset}_${pair.counterIssuer}`;
const res = store.getters.exchangeVals.filter(detail => detail.skey === skey);
if (res.length > 0) {
return res[0].exchangeVal;
return Number(res[0].exchangeVal);
} else {
return 0;
}
Expand All @@ -298,7 +320,6 @@ export default {
buying = {asset_code: pair.counterAsset, asset_issuer: pair.counterIssuer};
}
this.getOrderBook(server, selling, buying, (res) => {
console.log(res, selling, buying);
const bids = res.bids; // buy 'buying' from these orders
const asks = res.asks; // sell 'buying' from these orders
if (t === 'base') {
Expand Down
Loading

0 comments on commit 40fe07e

Please sign in to comment.