From 25694ccc7d379e52a75e9129025183c940e46cfc Mon Sep 17 00:00:00 2001 From: lampamazaza Date: Fri, 10 May 2024 12:13:08 +0800 Subject: [PATCH] feat: adds send max --- src/css/main.css | 10 ++++++++-- src/js/Controller.js | 28 +++++++++++++++++----------- src/js/view/View.js | 11 +++++++++++ 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/css/main.css b/src/css/main.css index 85899fc..4481d89 100644 --- a/src/css/main.css +++ b/src/css/main.css @@ -938,7 +938,7 @@ button { margin-top: 20px; } -#sendBalance { +#sendBalance{ position: absolute; right: 0; top: 0; @@ -946,6 +946,12 @@ button { font-size: 14px; } +#sendBalance:hover { + text-decoration: underline; + text-underline-offset: 4px; + cursor: pointer; +} + #sendConfirm .addr, #signConfirm .addr { font-family: monospace; @@ -1272,4 +1278,4 @@ button { -webkit-transform: rotate(45deg); -ms-transform: rotate(45deg); transform: rotate(45deg); -} \ No newline at end of file +} diff --git a/src/js/Controller.js b/src/js/Controller.js index 66ce48b..7211b5c 100644 --- a/src/js/Controller.js +++ b/src/js/Controller.js @@ -487,7 +487,7 @@ class Controller { * @param keyPair {nacl.KeyPair | null} null if estimates fee, keyPair if real sending * @return Promise<{{send: () => Promise<*>, getQuery: () => Promise, estimateFee: () => Promise<*>}}> transfer object */ - async sign(request, keyPair) { + async sign(request, keyPair, sendAll) { /** @type {number} */ const seqno = await this.getMySeqno(); @@ -503,7 +503,7 @@ class Controller { toAddress: message.toAddress, amount: message.amount, payload: message.comment, - sendMode: 3, + sendMode: sendAll ? 128 : 3, stateInit: message.stateInit } }) @@ -881,9 +881,10 @@ class Controller { /** * @param request {{expireAt?: number, messages: [{amount: BN, toAddress: string, comment?: string | Uint8Array | Cell, needEncryptComment: boolean, stateInit?: Cell}]}} + * @param sendAll {boolean} * @return {Promise} total fees in nanotons */ - async getFees(request) { + async getFees(request, sendALl) { /** @type {{expireAt?: number, messages: [{amount: BN, toAddress: string, comment?: string | Uint8Array | Cell, needEncryptComment: boolean, stateInit?: Cell}]}} */ const tempRequest = { expireAt: request.expireAt, @@ -908,7 +909,7 @@ class Controller { }); } - const query = await this.sign(tempRequest, null); + const query = await this.sign(tempRequest, null, sendALl); const all_fees = await query.estimateFee(); const fees = all_fees.source_fees; const in_fwd_fee = new BN(fees.in_fwd_fee); // External processing fee @@ -1043,16 +1044,17 @@ class Controller { } let fee; + let sendAll = this.balance.eq(totalAmount) try { - fee = await this.getFees(request); + fee = await this.getFees(request, sendAll); } catch (e) { console.error(e); this.sendToView('sendCheckFailed', {message: 'API request error'}); return null; } - if (this.balance.sub(fee).lt(totalAmount)) { + if (!sendAll && this.balance.sub(fee).lt(totalAmount)) { this.sendToView('sendCheckCantPayFee', {fee}); return null; } @@ -1070,7 +1072,7 @@ class Controller { fee: fee.toString() }, needQueue); - const sentBoc = await this.send(request, null, totalAmount); + const sentBoc = await this.send(request, null, totalAmount, sendAll); if (sentBoc) { dAppPromise.resolve(sentBoc); @@ -1093,7 +1095,7 @@ class Controller { } const privateKeyBase64 = await Controller.wordsToPrivateKey(words); - const sentBoc = await this.send(request, privateKeyBase64, totalAmount); + const sentBoc = await this.send(request, privateKeyBase64, totalAmount, sendAll); this.onCancelAction = null; @@ -1127,9 +1129,10 @@ class Controller { * @param request {{expireAt?: number, messages: [{amount: BN, toAddress: string, comment?: string | Uint8Array | Cell, needEncryptComment: boolean, stateInit?: Cell}]}} * @param privateKeyBase64 {string | null} null if Ledger * @param totalAmount {BN} + * @param sendAll {boolean} * @return {Promise} successfully sent BoC */ - async send(request, privateKeyBase64, totalAmount) { + async send(request, privateKeyBase64, totalAmount, sendAll) { try { let query; @@ -1152,6 +1155,10 @@ class Controller { await this.createLedger((await storage.getItem('ledgerTransportType')) || 'hid'); } + if(sendAll) { + message.sendMode = 128 + } + let addressFormat = 0; const toAddress = new Address(message.toAddress); @@ -1177,8 +1184,7 @@ class Controller { } else { const keyPair = nacl.sign.keyPair.fromSeed(TonWeb.utils.base64ToBytes(privateKeyBase64)); - query = await this.sign(request, keyPair); - + query = await this.sign(request, keyPair, sendAll); } /** @type {Cell | null} */ diff --git a/src/js/view/View.js b/src/js/view/View.js index 2ca03ca..5a9f4a7 100644 --- a/src/js/view/View.js +++ b/src/js/view/View.js @@ -374,6 +374,7 @@ class View { $('#transaction_closeBtn').addEventListener('click', () => this.closePopup()); $('#connectLedger_cancelBtn').addEventListener('click', () => this.closePopup()); + $('#sendBalance').addEventListener('click', () => this.setMaxAmount()); $('#send_btn').addEventListener('click', (e) => { /** @type {string} */ @@ -1268,6 +1269,16 @@ class View { this.port.postMessage({method, params}); } } + /** + * Set max amount to send input field + */ + setMaxAmount(){ + if(this.balance !== null) { + const input = $('#amountInput') + input.value = fromNano(this.balance.toString()); + input.classList.remove('error'); + } + } /** * Receive message from Controller.js