From 6ba9c5ad805b4febf51b43f60bbedb17c99a8cfa Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Fri, 3 Jan 2020 20:09:01 +0100 Subject: [PATCH] les: missing API functions --- internal/web3ext/web3ext.go | 25 +++++++++++++++++++++++++ les/api.go | 4 ++-- les/tokensale.go | 18 +++++++++++++++--- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index f10013569e..7ac7762e45 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -868,6 +868,31 @@ web3._extend({ call: 'lespay_connection', params: 6 }), + new web3._extend.Method({ + name: 'deposit', + call: 'lespay_deposit', + params: 4 + }), + new web3._extend.Method({ + name: 'buyTokens', + call: 'lespay_buyTokens', + params: 6 + }), + new web3._extend.Method({ + name: 'getBalance', + call: 'lespay_getBalance', + params: 2 + }), + new web3._extend.Method({ + name: 'info', + call: 'lespay_info', + params: 2 + }), + new web3._extend.Method({ + name: 'remoteInfo', + call: 'lespay_remoteInfo', + params: 3 + }), ], properties: [ diff --git a/les/api.go b/les/api.go index 4efd2ed6ac..0fb5b5c096 100644 --- a/les/api.go +++ b/les/api.go @@ -473,8 +473,8 @@ func (api *PrivateLespayAPI) Deposit(ctx context.Context, remote bool, node stri return } -func (api *PrivateLespayAPI) BuyTokens(ctx context.Context, remote bool, node string, maxSpend, minReceive uint64, spendAll bool) (results tsBuyTokensResults, err error) { - params := tsBuyTokensParams{maxSpend, minReceive, spendAll} +func (api *PrivateLespayAPI) BuyTokens(ctx context.Context, remote bool, node string, maxSpend, minReceive uint64, relative, spendAll bool) (results tsBuyTokensResults, err error) { + params := tsBuyTokensParams{maxSpend, minReceive, relative, spendAll} enc, _ := rlp.EncodeToBytes(¶ms) var resEnc []byte resEnc, err = api.makeCall(ctx, remote, node, append([]byte{tsBuyTokens}, enc...)) diff --git a/les/tokensale.go b/les/tokensale.go index dda97ac4a4..310b23e181 100644 --- a/les/tokensale.go +++ b/les/tokensale.go @@ -326,7 +326,7 @@ func (t *tokenSale) deposit(id enode.ID, paymentModule string, proofOfPayment [] return } -func (t *tokenSale) buyTokens(id enode.ID, maxSpend, minReceive uint64, spendAll bool) (pcBalance, tokenBalance, spend, receive uint64, success bool) { +func (t *tokenSale) buyTokens(id enode.ID, maxSpend, minReceive uint64, relative, spendAll bool) (pcBalance, tokenBalance, spend, receive uint64, success bool) { t.lock.Lock() defer t.lock.Unlock() @@ -336,6 +336,18 @@ func (t *tokenSale) buyTokens(id enode.ID, maxSpend, minReceive uint64, spendAll if err := rlp.DecodeBytes([]byte(pb.meta), &meta); err == nil { pcBalance = meta.pcBalance } + if relative { + if pcBalance > maxSpend { + maxSpend = pcBalance - maxSpend + } else { + maxSpend = 0 + } + if minReceive > tokenBalance { + minReceive -= tokenBalance + } else { + minReceive = 0 + } + } if maxSpend > pcBalance { maxSpend = pcBalance @@ -476,7 +488,7 @@ type ( } tsBuyTokensParams struct { MaxSpend, MinReceive uint64 - SpendAll bool + Relative, SpendAll bool } tsBuyTokensResults struct { PcBalance, TokenBalance, Spend, Receive uint64 @@ -537,7 +549,7 @@ func (t *tokenSale) runCommand(cmd []byte, id enode.ID, freeID string) []byte { ) if err := rlp.DecodeBytes(cmd[1:], ¶ms); err == nil { results.PcBalance, results.TokenBalance, results.Spend, results.Receive, results.Success = - t.buyTokens(id, params.MaxSpend, params.MinReceive, params.SpendAll) + t.buyTokens(id, params.MaxSpend, params.MinReceive, params.Relative, params.SpendAll) res, _ = rlp.EncodeToBytes(&results) } case tsConnection: