Skip to content

Commit

Permalink
Adding support for lending account (flexible earn).
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomanuel committed Apr 7, 2023
1 parent 1378cd7 commit 0072cf7
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 29 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ jobs:
filepath=_build/$filename
tmpfile=_build/tmp.gs
mkdir _build
find . -type f \( -iname "*.gs" ! -iname "main.gs" ! -iname "tmp.gs" \) -exec cat > $tmpfile {} +
cat main.gs > $filepath
find . -type f \( -iname "*.gs" ! -iname "config.gs" ! -iname "main.gs" ! -iname "tmp.gs" \) -exec cat > $tmpfile {} +
cat config.gs > $filepath
printf "\n\n/////////////////////////////////////\n\n" >> $filepath
cat main.gs >> $filepath
printf "\n\n/////////////////////////////////////\n\n" >> $filepath
cat $tmpfile >> $filepath
echo "$filename generated!"
Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ dev: push

generate:
@rm -f BINANCE-ALL.gs
@find . -type f \( -iname "*.gs" ! -iname "main.gs" ! -iname "tmp.gs" \) -exec cat > tmp.gs {} +
@cat main.gs > BINANCE-ALL.gs
@find . -type f \( -iname "*.gs" ! -iname "config.gs" ! -iname "main.gs" ! -iname "tmp.gs" \) -exec cat > tmp.gs {} +
@cat config.gs > BINANCE-ALL.gs
@printf "\n\n/////////////////////////////////////\n\n" >> BINANCE-ALL.gs
@cat main.gs >> BINANCE-ALL.gs
@printf "\n\n/////////////////////////////////////\n\n" >> BINANCE-ALL.gs
@cat tmp.gs >> BINANCE-ALL.gs
@rm -f tmp.gs
Expand Down
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ By using the `BINANCE()` formula in your spreadsheet, you can get data fetched f
* Current market [prices](#operation-prices-public)
* Historical market [OHLCV](#operation-history-public)
* Last [24h stats](#operation-stats24h-public)
* Total [account assets](#operation-account-private) from Binance wallets (SPOT + CROSS + ISOLATED + FUTURES + SUB-ACCOUNTS)
* Total [account assets](#operation-account-private) from Binance wallets (SPOT + LENDING + CROSS + ISOLATED + FUTURES + SUB-ACCOUNTS)
* All current [open orders](#operation-ordersopen-private) (SPOT + CROSS + ISOLATED + FUTURES)
* Latest [done/finished orders](#operation-ordersdone-private) (SPOT + CROSS + ISOLATED + FUTURES)
* Historical [orders table](#operation-orderstable-private) (SPOT + CROSS + ISOLATED + FUTURES)
Expand Down Expand Up @@ -154,8 +154,9 @@ Some operations are **private**, meaning they **do require a Binance API key** t
* `=BINANCE("stats/24h", A1:A3, "ticker: BTC, headers: false")` Optionally you can give more options like not returning table headers.

### Operation: `"account"` (private)
`=BINANCE("account")` will return total account assets from Binance wallets (SPOT + CROSS + ISOLATED + FUTURES + SUB-ACCOUNTS).
`=BINANCE("account")` will return total account assets from Binance wallets (SPOT + LENDING + CROSS + ISOLATED + FUTURES + SUB-ACCOUNTS).
* `=BINANCE("account", "spot")` Display assets summary for SPOT wallet.
* `=BINANCE("account", "lending")` Display assets summary for LENDING (Flexible Earn) wallet.
* `=BINANCE("account", "cross")` Display assets summary for CROSS MARGIN wallet.
* If you don't use this wallet (0 assets there), you should disable it from "Binance->Wallets" main menu.
* `=BINANCE("account", "isolated")` Display assets summary for ISOLATED MARGIN wallet.
Expand Down Expand Up @@ -297,11 +298,9 @@ I have several ideas for new features, so much more could come!
You can send any token through the **Binance Smart Chain** (BSC/BEP20) to the address:
`0x1d047bc3e46ce0351fd0c44fc2a2029512e87a97`

But you can also use:
* **[BTC] BTC:** `1FsN54WNibhhPhRt4vnAPRGgzaVeeFvEnM`
* **[BTC] SegWit:** `bc1qanxn2ycp9em50hj5p7en6wxe962zj4umqvs7q9`
* **[ETH] ERC20:** `0x1d047bc3e46ce0351fd0c44fc2a2029512e87a97`
* **[LTC] LTC:** `LZ8URuChzyuuy272isMCrts7R7UKtwnj6a`
But you can also use these networks:
* **BTC:** `bc1qanxn2ycp9em50hj5p7en6wxe962zj4umqvs7q9`
* **ETH:** `0x1d047bc3e46ce0351fd0c44fc2a2029512e87a97`

---

Expand Down
21 changes: 16 additions & 5 deletions misc/config.gs → config.gs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
/**
* App config params.
* Binance to Google Sheets!
* Diego Manuel - diegomanuel@gmail.com
* https://github.com/diegomanuel/binance-to-google-sheets
* https://github.com/diegomanuel/binance-to-google-sheets-proxy
*/

let DEBUG = false;
const VERSION = "v0.5.3";
const REPO_URL = "https://github.com/diegomanuel/binance-to-google-sheets";
/**
* Add-on config params.
* You should only need to adjust the `USE_PROXY` value!
*/

// Proxy usage
const USE_PROXY = false;
// See: https://github.com/diegomanuel/binance-to-google-sheets-proxy
//const USE_PROXY = "https://btgs-proxy.setupme.io"

// Other settings
let DEBUG = false; // If enabled, will output more logs to the AppScript console
const VERSION = "v0.5.4";
const REPO_URL = "https://github.com/diegomanuel/binance-to-google-sheets";
const SPOT_API_URL = "https://api.binance.com";
const FUTURES_API_URL = "https://fapi.binance.com";
const DELIVERY_API_URL = "https://dapi.binance.com";
const TICKER_AGAINST = "USDT";
const TICKER_AGAINST = "USDT"; // The default base cryptocurrency to build a full ticker
const REQUEST_RETRY_MAX_ATTEMPTS = 10; // Max number of attempts when the API responses with status != 200
const REQUEST_RETRY_DELAY = 1000; // Delay between API calls when it fails in milliseconds
8 changes: 0 additions & 8 deletions main.gs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/**
* Binance to Google Sheets!
* Diego Manuel - diegomanuel@gmail.com
* https://github.com/diegomanuel/binance-to-google-sheets
* https://github.com/diegomanuel/binance-to-google-sheets-proxy
*/


/**
* Main formula wrapper to call supported operations.
*
Expand Down
36 changes: 36 additions & 0 deletions misc/wallet.gs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ function BinWallet(OPTIONS) {
isEnabled,
getAssets,
getSpotAssets,
getLendingAssets,
getCrossAssets,
getIsolatedAssets,
getFuturesAssets,
getDeliveryAssets,
getSubAccountAssets,
setSpotAssets,
setLendingAssets,
setCrossAssets,
setIsolatedAssets,
setFuturesAssets,
Expand All @@ -23,6 +25,7 @@ function BinWallet(OPTIONS) {
getIsolatedPairs,
setIsolatedPairs,
parseSpotAsset,
parseLendingAsset,
parseCrossMarginAsset,
parseIsolatedMarginAsset,
parseFuturesAsset,
Expand All @@ -48,6 +51,13 @@ function BinWallet(OPTIONS) {
return getAssets("spot", symbol);
}

/**
* Returns the account wallet assets for LENDING (flexible earn)
*/
function getLendingAssets(symbol) {
return getAssets("lending", symbol);
}

/**
* Returns the account wallet assets for CROSS MARGIN
*/
Expand Down Expand Up @@ -96,6 +106,13 @@ function BinWallet(OPTIONS) {
return setAssetsData("spot", data);
}

/**
* Sets account wallet data for LENDING (flexible earn)
*/
function setLendingAssets(data) {
return setAssetsData("lending", data);
}

/**
* Sets account wallet data for CROSS MARGIN
*/
Expand Down Expand Up @@ -178,6 +195,21 @@ function BinWallet(OPTIONS) {
};
}

function parseLendingAsset(asset) {
const amount = parseFloat(asset.amount);
const netBTC = parseFloat(asset.amountInBTC);
return {
symbol: asset.asset,
free: 0,
locked: amount,
borrowed: 0,
interest: 0,
total: amount,
net: amount,
netBTC: netBTC
};
}

function parseCrossMarginAsset(asset) {
const free = parseFloat(asset.free);
const locked = parseFloat(asset.locked);
Expand Down Expand Up @@ -288,6 +320,10 @@ function BinWallet(OPTIONS) {
totals = Object.keys(spot).reduce(function(acc, symbol) {
return _accAssetHelper(acc, symbol, spot[symbol]);
}, totals);
const lending = getLendingAssets();
totals = Object.keys(lending).reduce(function(acc, symbol) {
return _accAssetHelper(acc, "LEND"+symbol, lending[symbol]);
}, totals);
if (isEnabled("cross")) {
const cross = getCrossAssets();
totals = Object.keys(cross).reduce(function(acc, symbol) {
Expand Down
32 changes: 32 additions & 0 deletions tasks/do-account-info.gs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function BinDoAccountInfo() {
const bw = BinWallet();

run("spot", opts);
run("lending", opts);
if (bw.isEnabled("cross")) {
run("cross", opts);
}
Expand Down Expand Up @@ -117,6 +118,9 @@ function BinDoAccountInfo() {
if (type === "spot") {
return br.get("api/v3/account", "", "");
}
if (type === "lending") {
return br.get("sapi/v1/lending/union/account", "", "");
}
if (type === "cross") {
return br.get("sapi/v1/margin/account", "", "");
}
Expand Down Expand Up @@ -158,6 +162,9 @@ function BinDoAccountInfo() {
if (type === "spot") {
return parseSpot(data, show_headers);
}
if (type === "lending") {
return parseLending(data, show_headers);
}
if (type === "cross") {
return parseCrossMargin(data, show_headers);
}
Expand Down Expand Up @@ -226,6 +233,31 @@ function BinDoAccountInfo() {
return [...general, ...sorted];
}

function parseLending(data, show_headers) {
const wallet = BinWallet();
const header = ["Asset", "Amount", "Amount BTC"];

const assets = [];
const balances = (data.positionAmountVos || []).reduce(function(rows, a) {
const asset = wallet.parseLendingAsset(a);
if (asset.total > 0) { // Only return assets with balance
assets.push(asset);
rows.push([
asset.symbol,
asset.net,
asset.netBTC
]);
}
return rows;
}, []);

// Save assets to wallet
wallet.setLendingAssets(assets);

const sorted = BinUtils().sortResults(balances);
return show_headers ? [header, ...sorted] : sorted;
}

function parseCrossMargin(data, show_headers) {
const wallet = BinWallet();
const header1 = ["Account Type", "Trade Enabled", "Transfer Enabled", "Borrow Enabled", "Margin Level", "Total BTC Asset", "Total BTC Liability", "Total BTC Net Asset", "Last Update"];
Expand Down
8 changes: 3 additions & 5 deletions ui/menu.gs
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,9 @@ function showDonate() {
"You can send any token through the Binance Smart Chain (BSC/BEP20) to:\n"+
"0x1d047bc3e46ce0351fd0c44fc2a2029512e87a97\n"+
"\n"+
"But you can also use:\n"+
"[BTC] BTC: 1FsN54WNibhhPhRt4vnAPRGgzaVeeFvEnM\n"+
"[BTC] SegWit: bc1qanxn2ycp9em50hj5p7en6wxe962zj4umqvs7q9\n"+
"[ETH] ERC20: 0x1d047bc3e46ce0351fd0c44fc2a2029512e87a97\n"+
"[LTC] LTC: LZ8URuChzyuuy272isMCrts7R7UKtwnj6a\n"+
"But you can also use these networks:\n"+
"BTC: bc1qanxn2ycp9em50hj5p7en6wxe962zj4umqvs7q9\n"+
"ETH: 0x1d047bc3e46ce0351fd0c44fc2a2029512e87a97\n"+
"\n"+
"------------------------------------------------\n"+
"Don't you have a Binance account yet?\n"+
Expand Down

0 comments on commit 0072cf7

Please sign in to comment.