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

Support/filecoin evm #7535

Merged
Merged
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
4 changes: 4 additions & 0 deletions apps/ledger-live-desktop/static/i18n/en/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -5518,6 +5518,10 @@
"InvalidRecipientForTokenTransfer": {
"title": "Invalid recipient for token transfer, supported account types for token transfer: [f0, f4, 0x]"
},
"FilecoinFeeEstimationFailed": {
"title": "Sorry, fee estimation failed",
"description": "Fee estimation failed on the network. Check inputs, transaction most likely to fail"
},
"NotEnoughBalance": {
"title": "Sorry, insufficient funds",
"description": "Please make sure the account has enough funds."
Expand Down
4 changes: 4 additions & 0 deletions apps/ledger-live-mobile/src/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,10 @@
"InvalidRecipientForTokenTransfer": {
"title": "Invalid recipient for token transfer, supported account types for token transfer: [f0, f4, 0x]"
},
"FilecoinFeeEstimationFailed": {
"title": "Sorry, fee estimation failed",
"description": "Fee estimation failed on the network. Check inputs, transaction most likely to fail"
},
"FirmwareNotRecognized": {
"title": "Invalid Provider",
"description": "You have to change \"My Ledger provider\" setting. To change it, open Ledger Live \"Settings\", select \"Experimental features\", and then select a different provider."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ERC20Transfer,
ERC20BalanceResponse,
} from "./types";
import { FilecoinFeeEstimationFailed } from "../../errors";

const getFilecoinURL = (path?: string): string => {
const baseUrl = getEnv("API_FILECOIN_ENDPOINT");
Expand Down Expand Up @@ -70,8 +71,13 @@ export const fetchBalances = async (addr: string): Promise<BalanceResponse> => {

export const fetchEstimatedFees = makeLRUCache(
async (request: EstimatedFeesRequest): Promise<EstimatedFeesResponse> => {
const data = await send<EstimatedFeesResponse>(`/fees/estimate`, request);
return data; // TODO Validate if the response fits this interface
try {
const data = await send<EstimatedFeesResponse>(`/fees/estimate`, request);
return data; // TODO Validate if the response fits this interface
} catch (e: any) {
log("error", "filecoin fetchEstimatedFees", e);
throw new FilecoinFeeEstimationFailed();
}
},
request => `${request.from}-${request.to}`,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ export async function buildTokenAccounts(

const operations = txns
.flatMap(txn => erc20TxnToOperation(txn, filAddr, tokenAccountId, token.units[0]))
.flat();
.flat()
.sort((a, b) => b.date.getTime() - a.date.getTime());

if (operations.length === 0 && bnBalance.isZero()) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ export const mapTxToOps =
const hasFailed = status !== TxStatus.Ok;

if (isSending) {
const type = value.eq(0) ? "FEES" : "OUT";
ops.push({
id: encodeOperationId(accountId, hash, "OUT"),
id: encodeOperationId(accountId, hash, type),
hash,
type: "OUT",
type,
value: value.plus(feeToUse),
fee: feeToUse,
blockHeight: tx.height,
Expand All @@ -113,10 +114,11 @@ export const mapTxToOps =
}

if (isReceiving) {
const type = value.eq(0) ? "FEES" : "IN";
ops.push({
id: encodeOperationId(accountId, hash, "IN"),
id: encodeOperationId(accountId, hash, type),
hash,
type: "IN",
type,
value,
fee: feeToUse,
blockHeight: tx.height,
Expand Down Expand Up @@ -200,7 +202,9 @@ export const getAccountShape: GetAccountShape = async info => {
subAccounts: tokenAccounts,
balance: new BigNumber(balance.total_balance),
spendableBalance: new BigNumber(balance.spendable_balance),
operations: flatMap(processTxs(rawTxs), mapTxToOps(accountId, info)),
operations: flatMap(processTxs(rawTxs), mapTxToOps(accountId, info)).sort(
(a, b) => b.date.getTime() - a.date.getTime(),
),
blockHeight: blockHeight.current_block_identifier.index,
};

Expand Down
5 changes: 5 additions & 0 deletions libs/ledger-live-common/src/families/filecoin/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ import { createCustomErrorClass } from "@ledgerhq/errors";
export const InvalidRecipientForTokenTransfer = createCustomErrorClass(
"InvalidRecipientForTokenTransfer",
);

/*
* When the fee estimation endpoint fails
*/
export const FilecoinFeeEstimationFailed = createCustomErrorClass("FilecoinFeeEstimationFailed");
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ export const estimateMaxSpendable: AccountBridge<Transaction>["estimateMaxSpenda
const amount = transaction?.amount;

const validatedContractAddress = validateAddress(subAccount?.token.contractAddress ?? "");
if (!validatedContractAddress.isValid) {
if (tokenAccountTxn && !validatedContractAddress.isValid) {
throw invalidAddressErr;
}
const contractAddress = validatedContractAddress.parsedAddress.toString();
const contractAddress =
tokenAccountTxn && validatedContractAddress.isValid
? validatedContractAddress.parsedAddress.toString()
: "";
const finalRecipient = tokenAccountTxn ? contractAddress : recipient;

// If token transfer, the evm payload is required to estimate fees
Expand Down Expand Up @@ -103,5 +106,5 @@ export const estimateMaxSpendable: AccountBridge<Transaction>["estimateMaxSpenda
}
// log("debug", "[estimateMaxSpendable] finish fn");

return balance;
return balance.gt(0) ? balance : new BigNumber(0);
};
Loading