Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds send max #206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -938,14 +938,20 @@ button {
margin-top: 20px;
}

#sendBalance {
#sendBalance{
position: absolute;
right: 0;
top: 0;
color: #999999;
font-size: 14px;
}

#sendBalance:hover {
text-decoration: underline;
text-underline-offset: 4px;
cursor: pointer;
}


#sendConfirm .addr, #signConfirm .addr {
font-family: monospace;
Expand Down Expand Up @@ -1272,4 +1278,4 @@ button {
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
}
28 changes: 17 additions & 11 deletions src/js/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cell>, estimateFee: () => Promise<*>}}> transfer object
*/
async sign(request, keyPair) {
async sign(request, keyPair, sendAll) {
/** @type {number} */
const seqno = await this.getMySeqno();

Expand All @@ -503,7 +503,7 @@ class Controller {
toAddress: message.toAddress,
amount: message.amount,
payload: message.comment,
sendMode: 3,
sendMode: sendAll ? 128 : 3,
stateInit: message.stateInit
}
})
Expand Down Expand Up @@ -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<BN>} 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,
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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<Cell | null>} successfully sent BoC
*/
async send(request, privateKeyBase64, totalAmount) {
async send(request, privateKeyBase64, totalAmount, sendAll) {
try {
let query;

Expand All @@ -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);
Expand All @@ -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} */
Expand Down
11 changes: 11 additions & 0 deletions src/js/view/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down Expand Up @@ -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
Expand Down