Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

support PlatON(LAT) #1943

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@ledgerhq/hw-app-xrp": "6.3.0",
"@ledgerhq/hw-transport": "6.3.0",
"@ledgerhq/hw-transport-http": "6.3.0",
"@ledgerhq/live-common": "20.11.0",
"@ledgerhq/live-common": "https://github.com/PlatONnetwork/ledger-live-common/releases/download/v20.11.0-platon/ledgerhq-live-common-v20.11.0.tgz",
"@ledgerhq/logs": "6.2.0",
"@ledgerhq/react-native-hid": "6.3.0",
"@ledgerhq/react-native-hw-transport-ble": "6.3.0",
Expand Down
1 change: 1 addition & 0 deletions src/const/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const ScreenName = {
EditAccountUnits: "EditAccountUnits",
EditDeviceName: "EditDeviceName",
EthereumCustomFees: "EthereumCustomFees",
PlatONCustomFees: "PlatONCustomFees",
EthereumEditGasLimit: "EthereumEditGasLimit",
Exchange: "Exchange",
ExchangeBuy: "ExchangeBuy",
Expand Down
1 change: 1 addition & 0 deletions src/families/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from "./tron";
export * from "./cosmos";
export * from "./algorand";
export * from "./polkadot";
export * from "./platon";
141 changes: 141 additions & 0 deletions src/families/platon/EditFeeUnitEthereum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// @flow
import React, { useCallback } from "react";
import { BigNumber } from "bignumber.js";
import { View, StyleSheet } from "react-native";
import { useTranslation } from "react-i18next";
import Slider from "react-native-slider";
import { useTheme } from "@react-navigation/native";
import type { Account, AccountLike } from "@ledgerhq/live-common/lib/types";
import type { Transaction } from "@ledgerhq/live-common/lib/families/platon/types";
import {
reverseRangeIndex,
projectRangeIndex,
} from "@ledgerhq/live-common/lib/range";
import { getMainAccount } from "@ledgerhq/live-common/lib/account";
import LText from "../../components/LText";
import CurrencyUnitValue from "../../components/CurrencyUnitValue";

const GasSlider = React.memo(({ value, onChange, range }: *) => {
const { colors } = useTheme();
const index = reverseRangeIndex(range, value);
const setValueIndex = useCallback(
i => onChange(projectRangeIndex(range, i)),
[range, onChange],
);

return (
<Slider
value={index}
step={1}
onValueChange={setValueIndex}
minimumValue={0}
maximumValue={range.steps - 1}
thumbTintColor={colors.live}
minimumTrackTintColor={colors.live}
/>
);
});

type Props = {
account: AccountLike,
parentAccount: ?Account,
transaction: Transaction,
gasPrice: BigNumber,
onChange: Function,
range: any,
};

export default function EditFeeUnitEthereum({
account,
parentAccount,
transaction,
gasPrice,
onChange,
range,
}: Props) {
const { colors } = useTheme();
const { t } = useTranslation();

const mainAccount = getMainAccount(account, parentAccount);

const feeCustomUnit = transaction.feeCustomUnit;

const onChangeF = useCallback(
value => {
onChange(value);
},
[onChange],
);

const { networkInfo } = transaction;
if (!networkInfo) return null;
const { gasPrice: serverGas } = networkInfo;

return (
<View>
<View style={[styles.sliderContainer, { backgroundColor: colors.card }]}>
<View style={styles.gasPriceHeader}>
<LText style={styles.gasPriceLabel} semiBold>
{t("send.summary.gasPrice")}
</LText>
<View
style={[styles.gasPrice, { backgroundColor: colors.lightLive }]}
>
<LText style={[styles.currencyUnitText, { color: colors.live }]}>
<CurrencyUnitValue
unit={feeCustomUnit || mainAccount.unit}
value={gasPrice}
/>
</LText>
</View>
</View>
<View style={styles.container}>
<GasSlider
defaultGas={serverGas}
value={gasPrice}
range={range}
onChange={onChangeF}
/>
<View style={styles.textContainer}>
<LText color="grey" style={styles.currencyUnitText}>
{t("common.slow")}
</LText>
<LText color="grey" style={styles.currencyUnitText}>
{t("common.fast")}
</LText>
</View>
</View>
</View>
</View>
);
}

const styles = StyleSheet.create({
sliderContainer: {
paddingLeft: 0,
},
gasPriceHeader: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
},
gasPriceLabel: {
fontSize: 20,
},
gasPrice: {
paddingVertical: 4,
paddingHorizontal: 8,
},
container: {
flexDirection: "column",
justifyContent: "center",
},
textContainer: {
flexDirection: "row",
justifyContent: "space-between",
},
currencyUnitText: {
fontSize: 14,
textTransform: "capitalize",
},
});
100 changes: 100 additions & 0 deletions src/families/platon/EthereumFeesStrategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* @flow */
import React, { useCallback, useState, useMemo, useEffect } from "react";

import { useFeesStrategy } from "@ledgerhq/live-common/lib/families/platon/react";
import { getAccountBridge } from "@ledgerhq/live-common/lib/bridge";

import type { Account, AccountLike } from "@ledgerhq/live-common/lib/types";
import type { Transaction } from "@ledgerhq/live-common/lib/families/platon/types";
import { getGasLimit } from "@ledgerhq/live-common/lib/families/platon/transaction";
import type { RouteParams } from "../../screens/SendFunds/04-Summary";
import { ScreenName } from "../../const";
import SelectFeesStrategy from "../../components/SelectFeesStrategy";

type Props = {
account: AccountLike,
parentAccount: ?Account,
transaction: Transaction,
navigation: any,
route: { params: RouteParams },
setTransaction: Function,
};

const getCustomStrategy = transaction => {
if (transaction.feesStrategy === "custom") {
return {
label: "custom",
forceValueLabel: null,
amount: transaction.gasPrice,
displayedAmount: transaction.gasPrice.multipliedBy(
getGasLimit(transaction),
),
userGasLimit: getGasLimit(transaction),
};
}

return null;
};

export default function EthereumFeesStrategy({
account,
parentAccount,
transaction,
setTransaction,
navigation,
route,
}: Props) {
const defaultStrategies = useFeesStrategy(transaction);
const [customStrategy, setCustomStrategy] = useState(
getCustomStrategy(transaction),
);
const strategies = useMemo(
() =>
customStrategy
? [...defaultStrategies, customStrategy]
: defaultStrategies,
[defaultStrategies, customStrategy],
);

useEffect(() => {
const newCustomStrategy = getCustomStrategy(transaction);
if (newCustomStrategy) {
setCustomStrategy(newCustomStrategy);
}
}, [transaction, setCustomStrategy]);

const onFeesSelected = useCallback(
({ amount, label, userGasLimit }) => {
const bridge = getAccountBridge(account, parentAccount);

setTransaction(
bridge.updateTransaction(transaction, {
gasPrice: amount,
feesStrategy: label,
userGasLimit: userGasLimit || transaction.userGasLimit,
}),
);
},
[setTransaction, account, parentAccount, transaction],
);

const openCustomFees = useCallback(() => {
navigation.navigate(ScreenName.PlatONCustomFees, {
...route.params,
accountId: account.id,
parentId: parentAccount && parentAccount.id,
transaction,
});
}, [navigation, route.params, account.id, parentAccount, transaction]);

return (
<SelectFeesStrategy
strategies={strategies}
onStrategySelect={onFeesSelected}
onCustomFeesPress={openCustomFees}
account={account}
parentAccount={parentAccount}
transaction={transaction}
/>
);
}
Loading