Skip to content

Commit

Permalink
Merge pull request #323 from kennycud/master
Browse files Browse the repository at this point in the history
Multi Trade Support
  • Loading branch information
crowetic authored Sep 6, 2024
2 parents f887bc6 + 3fc0281 commit a4a5af8
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 13 deletions.
16 changes: 16 additions & 0 deletions core/src/plugins/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const signArbitraryTransaction = api.signArbitraryTransaction
const signArbitraryWithFeeTransaction = api.signArbitraryWithFeeTransaction
const tradeBotCreateRequest = api.tradeBotCreateRequest
const tradeBotRespondRequest = api.tradeBotRespondRequest
const tradeBotRespondMultipleRequest = api.tradeBotRespondMultipleRequest
const signTradeBotTxn = api.signTradeBotTxn
const deleteTradeOffer = api.deleteTradeOffer
const cancelAllOffers = api.cancelAllOffers
Expand Down Expand Up @@ -396,6 +397,21 @@ export const routes = {
return response
},

tradeBotRespondMultipleRequest: async (req) => {
let response

try {
response = await tradeBotRespondMultipleRequest(req.data)
} catch (e) {
console.error(e)
console.error(e.message)

response = e.message
}

return response
},

deleteTradeOffer: async (req) => {
let response

Expand Down
16 changes: 16 additions & 0 deletions core/src/tradebot/trade-bot-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const createTransaction = api.createTransaction
const processTransaction = api.processTransaction
const tradeBotCreateRequest = api.tradeBotCreateRequest
const tradeBotRespondRequest = api.tradeBotRespondRequest
const tradeBotRespondMultipleRequest = api.tradeBotRespondMultipleRequest
const signTradeBotTxn = api.signTradeBotTxn
const deleteTradeOffer = api.deleteTradeOffer
const cancelAllOffers = api.cancelAllOffers
Expand Down Expand Up @@ -163,6 +164,21 @@ export const routes = {
return response
},

tradeBotRespondMultipleRequest: async (req) => {
let response

try {
response = await tradeBotRespondMultipleRequest(req.data)
} catch (e) {
console.error(e)
console.error(e.message)

response = e.message
}

return response
},

deleteTradeOffer: async (req) => {
let response

Expand Down
2 changes: 1 addition & 1 deletion crypto/api/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { request } from './fetch-request'
export { transactionTypes as transactions } from './transactions/transactions'
export { processTransaction, processTransactionVersion2, createTransaction, computeChatNonce, signChatTransaction, signArbitraryTransaction, signArbitraryWithFeeTransaction } from './createTransaction'
export { tradeBotCreateRequest, tradeBotRespondRequest, signTradeBotTxn, deleteTradeOffer, sendBtc, sendLtc, sendDoge, sendDgb, sendRvn, sendArrr } from './tradeRequest'
export { tradeBotCreateRequest, tradeBotRespondRequest, tradeBotRespondMultipleRequest, signTradeBotTxn, deleteTradeOffer, sendBtc, sendLtc, sendDoge, sendDgb, sendRvn, sendArrr } from './tradeRequest'
export { cancelAllOffers } from './transactions/trade-portal/tradeoffer/cancelAllOffers'
16 changes: 16 additions & 0 deletions crypto/api/tradeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import TradeBotRespondRequest from './transactions/trade-portal/tradebot/TradeBo
import signTradeBotTransaction from './transactions/trade-portal/tradebot/signTradeBotTransaction'
import DeleteTradeOffer from './transactions/trade-portal/tradeoffer/DeleteTradeOffer'
import { request } from './fetch-request'
import TradeBotRespondMultipleRequest from "./transactions/trade-portal/tradebot/TradeBotRespondMultipleRequest";

// TradeBotCreateRequest
export const tradeBotCreateRequest = (requestObject) => {
Expand Down Expand Up @@ -35,6 +36,21 @@ export const tradeBotRespondRequest = (requestObject) => {
})
}

// TradeBotRespondRequest
export const tradeBotRespondMultipleRequest = (requestObject) => {
const txn = new TradeBotRespondMultipleRequest().createTransaction(requestObject)
const myNode = window.parent.reduxStore.getState().app.nodeConfig.knownNodes[window.parent.reduxStore.getState().app.nodeConfig.node]

return request(`/crosschain/tradebot/respondmultiple?apiKey=${myNode.apiKey}`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(txn)
})
}

// Sign Trade Transactions
export const signTradeBotTxn = (unsignedTxn, keyPair) => {
return signTradeBotTransaction(unsignedTxn, keyPair)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* CrossChain - TradeBot Respond Multiple Request (Buy Action)
*
* These are special types of transactions (JSON ENCODED)
*/

export default class TradeBotRespondMultipleRequest {
constructor() {
// ...
}

createTransaction(txnReq) {
this.addresses(txnReq.addresses)
this.foreignKey(txnReq.foreignKey)
this.receivingAddress(txnReq.receivingAddress)

return this.txnRequest()
}

addresses(addresses) {
this._addresses = addresses
}

foreignKey(foreignKey) {
this._foreignKey = foreignKey
}

receivingAddress(receivingAddress) {
this._receivingAddress = receivingAddress
}

txnRequest() {
return {
addresses: this._addresses,
foreignKey: this._foreignKey,
receivingAddress: this._receivingAddress
}
}
}
94 changes: 82 additions & 12 deletions plugins/plugins/core/trade-portal/trade-portal.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import '@polymer/paper-spinner/paper-spinner-lite.js'
import '@vaadin/grid'
import '@vaadin/grid/vaadin-grid-sorter'
import '@vaadin/password-field'
import '@vaadin/grid/vaadin-grid-selection-column.js'

// Multi language support
import { get, registerTranslateConfig, translate, use } from '../../../../core/translate'
Expand Down Expand Up @@ -498,6 +499,10 @@ class TradePortal extends LitElement {

this._openOrdersGrid = this.shadowRoot.getElementById('openOrdersGrid')

this._openOrdersGrid.addEventListener("selected-items-changed", function(event){
_this.fillBuyFormMultiple(event.detail.value);
});

this._openOrdersGrid.querySelector('#priceColumn').headerRenderer = function (root) {
const priceString = get("tradepage.tchange9")
root.innerHTML = '<vaadin-grid-sorter path="price" direction="asc">' + priceString + ' (' + _this.listedCoins.get(_this.selectedCoin).coinCode + ')</vaadin-grid-sorter>'
Expand Down Expand Up @@ -733,6 +738,25 @@ class TradePortal extends LitElement {
`
}

coinGridSelection() {

if(this.selectedCoin == 'PIRATECHAIN') {
return html``
}
else {
return html`
<vaadin-grid-selection-column></vaadin-grid-selection-column>
`
}
}

clickHandler(data) {

if(this.selectedCoin == 'PIRATECHAIN') {
this.fillBuyFormSingle(data)
}
}

openTradesTemplate() {
return html`
<div class="open-trades myhover">
Expand All @@ -751,13 +775,14 @@ class TradePortal extends LitElement {
aria-label="Open Orders"
.items="${this.listedCoins.get(this.selectedCoin).openFilteredOrders}"
>
${this.coinGridSelection()}
<vaadin-grid-column
auto-width
resizable
header="${translate("tradepage.tchange8")} (QORT)"
id="qortAmountColumn"
.renderer=${(root, column, data) => {
render(html`<span style="cursor: pointer;" @click="${() => this.fillBuyForm(data)}">${this.round(data.item.qortAmount)}</span>`, root)
render(html`<span style="cursor: pointer;" @click="${() => this.clickHandler(data)}">${this.round(data.item.qortAmount)}</span>`, root)
}}
>
</vaadin-grid-column>
Expand All @@ -767,7 +792,7 @@ class TradePortal extends LitElement {
header="${translate("tradepage.tchange9")} (${this.listedCoins.get(this.selectedCoin).coinCode})"
id="priceColumn"
.renderer=${(root, column, data) => {
render(html`<span style="cursor: pointer;" @click="${() => this.fillBuyForm(data)}">${this.round(data.item.price)}</span>`, root)
render(html`<span style="cursor: pointer;" @click="${() => this.clickHandler(data)}">${this.round(data.item.price)}</span>`, root)
}}
>
</vaadin-grid-column>
Expand All @@ -777,7 +802,7 @@ class TradePortal extends LitElement {
header="${translate("tradepage.tchange10")} (${this.listedCoins.get(this.selectedCoin).coinCode})"
id="foreignAmountColumn"
.renderer=${(root, column, data) => {
render(html`<span style="cursor: pointer;" @click="${() => this.fillBuyForm(data)}">${data.item.foreignAmount}</span>`, root)
render(html`<span style="cursor: pointer;" @click="${() => this.clickHandler(data)}">${data.item.foreignAmount}</span>`, root)
}}
>
</vaadin-grid-column>
Expand All @@ -787,7 +812,7 @@ class TradePortal extends LitElement {
header="${translate("tradepage.tchange13")}"
id="qortalCreatorColumn"
.renderer=${(root, column, data) => {
render(html`<span style="cursor: pointer;" @click="${() => this.fillBuyForm(data)}">${data.item.qortalCreator}</span>`, root)
render(html`<span style="cursor: pointer;" @click="${() => this.clickHandler(data)}">${data.item.qortalCreator}</span>`, root)
}}
>
</vaadin-grid-column>
Expand Down Expand Up @@ -1546,19 +1571,52 @@ class TradePortal extends LitElement {
this.isLoadingOpenTrades = false
}

reSelectOpenOrders() {
const qortalATAddressesString = this.shadowRoot.getElementById('qortalAtAddress').value;
const qortalATAddresses = qortalATAddressesString.split(',');
const itemsToSelect = this.tradesPresenceCleaned.filter((order) => qortalATAddresses.includes(order.qortalAtAddress));
this._openOrdersGrid.selectedItems = [...itemsToSelect];
this.fillBuyFormMultiple(itemsToSelect);
}

async reRenderMyOpenOrders() {
this.requestUpdate()
await this.updateComplete
this.isLoadingMyOpenOrders = false
}

fillBuyForm(sellerRequest) {
fillBuyFormSingle(sellerRequest) {
this.shadowRoot.getElementById('buyAmountInput').value = parseFloat(sellerRequest.item.qortAmount)
this.shadowRoot.getElementById('buyPriceInput').value = this.round(parseFloat(sellerRequest.item.foreignAmount) / parseFloat(sellerRequest.item.qortAmount))
this.shadowRoot.getElementById('buyTotalInput').value = parseFloat(sellerRequest.item.foreignAmount)
this.shadowRoot.getElementById('qortalAtAddress').value = sellerRequest.item.qortalAtAddress
const buyFunds = this.round(parseFloat(sellerRequest.item.foreignAmount))
this.fillBuyForm(buyFunds)
}

fillBuyFormMultiple(sellerRequests) {
let qortalATAddresses = [];
let qortAmount = 0;
let foreignAmount = 0;

sellerRequests.forEach((item, index) => {
qortalATAddresses.push(item.qortalAtAddress);
qortAmount += parseFloat(item.qortAmount);
foreignAmount += parseFloat(item.foreignAmount);
})

this.shadowRoot.getElementById('buyAmountInput').value = qortAmount
this.shadowRoot.getElementById('buyPriceInput').value = qortAmount > 0 ? this.round(foreignAmount / qortAmount) : 0
this.shadowRoot.getElementById('buyTotalInput').value = foreignAmount
this.shadowRoot.getElementById('qortalAtAddress').value = qortalATAddresses

const buyFunds = this.round(foreignAmount)
this.fillBuyForm(buyFunds)
}

fillBuyForm(buyFunds) {
const haveFunds = this.round(parseFloat(this.listedCoins.get(this.selectedCoin).balance))

if (Number(haveFunds) > Number(buyFunds)) {
this.buyBtnDisable = false
this.autoBuyWarning = false
Expand All @@ -1568,7 +1626,6 @@ class TradePortal extends LitElement {
this.autoBuyWarning = true
this.displayTabContent('buy')
}

}

processOfferingTrade(offer) {
Expand Down Expand Up @@ -2330,11 +2387,21 @@ class TradePortal extends LitElement {
}

const makeRequest = async () => {
return await parentEpml.request('tradeBotRespondRequest', {
atAddress: qortalAtAddress,
foreignKey: _foreignKey,
receivingAddress: this.selectedAddress.address
})

if( this.selectedCoin == 'PIRATECHAIN') {
return await parentEpml.request('tradeBotRespondRequest', {
atAddress: qortalAtAddress,
foreignKey: _foreignKey,
receivingAddress: this.selectedAddress.address
})
}
else {
return await parentEpml.request('tradeBotRespondMultipleRequest', {
addresses: qortalAtAddress.split(','),
foreignKey: _foreignKey,
receivingAddress: this.selectedAddress.address
})
}
}

const manageResponse = (response) => {
Expand Down Expand Up @@ -2648,6 +2715,9 @@ class TradePortal extends LitElement {
filterPresenceList()
this.listedCoins.get(message.data.relatedCoin).openFilteredOrders = this.tradesPresenceCleaned
this.reRenderOpenFilteredOrders()
if( this.selectedCoin != 'PIRATECHAIN') {
this.reSelectOpenOrders()
}
return null
default:
break
Expand Down Expand Up @@ -2836,4 +2906,4 @@ class TradePortal extends LitElement {
}
}

window.customElements.define('trade-portal', TradePortal)
window.customElements.define('trade-portal', TradePortal)

0 comments on commit a4a5af8

Please sign in to comment.