forked from LedgerHQ/ledger-live
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(fil): apply feature updates post refac
- Loading branch information
Showing
10 changed files
with
555 additions
and
489 deletions.
There are no files selected for viewing
423 changes: 10 additions & 413 deletions
423
libs/ledger-live-common/src/families/filecoin/bridge/account.ts
Large diffs are not rendered by default.
Oops, something went wrong.
208 changes: 208 additions & 0 deletions
208
libs/ledger-live-common/src/families/filecoin/bridge/utils/dist/utils.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
exports.__esModule = true; | ||
exports.getSubAccount = exports.getAccountShape = exports.getTxToBroadcast = exports.getAddress = exports.mapTxToOps = exports.processTxs = exports.getUnit = void 0; | ||
var logs_1 = require("@ledgerhq/logs"); | ||
var currencies_1 = require("../../../../currencies"); | ||
var bignumber_js_1 = require("bignumber.js"); | ||
var types_1 = require("./types"); | ||
var api_1 = require("./api"); | ||
var account_1 = require("../../../../account"); | ||
var operation_1 = require("../../../../operation"); | ||
var flatMap_1 = require("lodash/flatMap"); | ||
var tokenAccounts_1 = require("./erc20/tokenAccounts"); | ||
exports.getUnit = function () { return currencies_1.getCryptoCurrencyById("filecoin").units[0]; }; | ||
exports.processTxs = function (txs) { | ||
// Group all tx types related to same tx cid into the same object | ||
var txsByTxCid = txs.reduce(function (txsByTxCidResult, currentTx) { | ||
var txCid = currentTx.hash, txType = currentTx.type; | ||
var txByType = txsByTxCidResult[txCid] || {}; | ||
switch (txType) { | ||
case "Send": | ||
case "InvokeContract": | ||
case "Fee": | ||
txByType[txType] = currentTx; | ||
break; | ||
default: | ||
logs_1.log("warn", "tx type [" + txType + "] on tx cid [" + txCid + "] was not recognized."); | ||
break; | ||
} | ||
txsByTxCidResult[txCid] = txByType; | ||
return txsByTxCidResult; | ||
}, {}); | ||
// Once all tx types have been grouped, we want to find | ||
var processedTxs = []; | ||
for (var txCid in txsByTxCid) { | ||
var item = txsByTxCid[txCid]; | ||
var feeTx = item.Fee; | ||
var mainTx = void 0; | ||
if ("Send" in item) { | ||
mainTx = item.Send; | ||
} | ||
else if ("InvokeContract" in item) { | ||
mainTx = item.InvokeContract; | ||
} | ||
else { | ||
logs_1.log("warn", "unexpected tx type, tx with cid [" + txCid + "] and payload [" + JSON.stringify(item) + "]"); | ||
} | ||
if (!mainTx) { | ||
if (feeTx) { | ||
logs_1.log("warn", "feeTx [" + feeTx.hash + "] found without a mainTx linked to it."); | ||
} | ||
continue; | ||
} | ||
if (feeTx) { | ||
mainTx.fee = feeTx.amount; | ||
} | ||
processedTxs.push(mainTx); | ||
} | ||
return processedTxs; | ||
}; | ||
exports.mapTxToOps = function (accountId, _a) { | ||
var address = _a.address; | ||
return function (tx) { | ||
var to = tx.to, from = tx.from, hash = tx.hash, timestamp = tx.timestamp, amount = tx.amount, fee = tx.fee, status = tx.status; | ||
var ops = []; | ||
var date = new Date(timestamp * 1000); | ||
var value = currencies_1.parseCurrencyUnit(exports.getUnit(), amount.toString()); | ||
var feeToUse = currencies_1.parseCurrencyUnit(exports.getUnit(), (fee || 0).toString()); | ||
var isSending = address === from; | ||
var isReceiving = address === to; | ||
var hasFailed = status !== types_1.TxStatus.Ok; | ||
if (isSending) { | ||
ops.push({ | ||
id: operation_1.encodeOperationId(accountId, hash, "OUT"), | ||
hash: hash, | ||
type: "OUT", | ||
value: value.plus(feeToUse), | ||
fee: feeToUse, | ||
blockHeight: tx.height, | ||
blockHash: null, | ||
accountId: accountId, | ||
senders: [from], | ||
recipients: [to], | ||
date: date, | ||
extra: {}, | ||
hasFailed: hasFailed | ||
}); | ||
} | ||
if (isReceiving) { | ||
ops.push({ | ||
id: operation_1.encodeOperationId(accountId, hash, "IN"), | ||
hash: hash, | ||
type: "IN", | ||
value: value, | ||
fee: feeToUse, | ||
blockHeight: tx.height, | ||
blockHash: null, | ||
accountId: accountId, | ||
senders: [from], | ||
recipients: [to], | ||
date: date, | ||
extra: {}, | ||
hasFailed: hasFailed | ||
}); | ||
} | ||
return ops; | ||
}; | ||
}; | ||
exports.getAddress = function (a) { return ({ address: a.freshAddress, derivationPath: a.freshAddressPath }); }; | ||
exports.getTxToBroadcast = function (operation, signature, rawData) { | ||
var sender = rawData.sender, recipient = rawData.recipient, gasLimit = rawData.gasLimit, gasFeeCap = rawData.gasFeeCap, gasPremium = rawData.gasPremium, method = rawData.method, version = rawData.version, nonce = rawData.nonce, signatureType = rawData.signatureType, params = rawData.params, value = rawData.value; | ||
return { | ||
message: { | ||
version: version, | ||
method: method, | ||
nonce: nonce, | ||
params: params !== null && params !== void 0 ? params : "", | ||
to: recipient, | ||
from: sender, | ||
gaslimit: gasLimit.toNumber(), | ||
gaspremium: gasPremium.toString(), | ||
gasfeecap: gasFeeCap.toString(), | ||
value: value | ||
}, | ||
signature: { | ||
type: signatureType, | ||
data: signature | ||
} | ||
}; | ||
}; | ||
exports.getAccountShape = function (info) { return __awaiter(void 0, void 0, void 0, function () { | ||
var address, currency, derivationMode, accountId, blockHeight, balance, rawTxs, tokenAccounts, result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
address = info.address, currency = info.currency, derivationMode = info.derivationMode; | ||
accountId = account_1.encodeAccountId({ | ||
type: "js", | ||
version: "2", | ||
currencyId: currency.id, | ||
xpubOrAddress: address, | ||
derivationMode: derivationMode | ||
}); | ||
return [4 /*yield*/, api_1.fetchBlockHeight()]; | ||
case 1: | ||
blockHeight = _a.sent(); | ||
return [4 /*yield*/, api_1.fetchBalances(address)]; | ||
case 2: | ||
balance = _a.sent(); | ||
return [4 /*yield*/, api_1.fetchTxs(address)]; | ||
case 3: | ||
rawTxs = _a.sent(); | ||
return [4 /*yield*/, tokenAccounts_1.buildTokenAccounts(address, accountId, info.initialAccount)]; | ||
case 4: | ||
tokenAccounts = _a.sent(); | ||
result = { | ||
id: accountId, | ||
subAccounts: tokenAccounts, | ||
balance: new bignumber_js_1.BigNumber(balance.total_balance), | ||
spendableBalance: new bignumber_js_1.BigNumber(balance.spendable_balance), | ||
operations: flatMap_1["default"](exports.processTxs(rawTxs), exports.mapTxToOps(accountId, info)), | ||
blockHeight: blockHeight.current_block_identifier.index | ||
}; | ||
return [2 /*return*/, result]; | ||
} | ||
}); | ||
}); }; | ||
exports.getSubAccount = function (account, tx) { | ||
var subAccount = tx.subAccountId && account.subAccounts | ||
? account.subAccounts.find(function (sa) { return sa.id === tx.subAccountId; }) | ||
: null; | ||
return subAccount; | ||
}; |
Oops, something went wrong.