Skip to content

Commit

Permalink
Merge pull request #230 from etherspot/develop
Browse files Browse the repository at this point in the history
Release 0.26.11
  • Loading branch information
poocart authored Oct 26, 2023
2 parents d4c238d + 1e58848 commit c08d402
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 178 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@etherspot/react-transaction-buidler",
"version": "0.26.10",
"version": "0.26.11",
"description": "Etherspot React component.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
25 changes: 11 additions & 14 deletions src/components/TransactionBlock/HoneySwapLPTransactionBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { debounce } from 'lodash';
import styled, { useTheme } from 'styled-components';
import { AccountStates, AccountTypes, BridgingQuote, CrossChainServiceProvider, ExchangeOffer } from 'etherspot';
import { AccountStates, AccountTypes, ExchangeOffer } from 'etherspot';
import { Route } from '@lifi/sdk';
import { BigNumber, ethers, utils } from 'ethers';
import { BigNumber, ethers } from 'ethers';

// Types
import { IHoneySwapLPBlock, IKlimaStakingTransactionBlock } from '../../types/transactionBlock';
import { IHoneySwapLPBlock } from '../../types/transactionBlock';

// Components
import { useEtherspot, useTransactionBuilder } from '../../hooks';
import AccountSwitchInput from '../AccountSwitchInput';
import NetworkAssetSelectInput from '../NetworkAssetSelectInput';
import { CombinedRoundedImages, RoundedImage } from '../Image';
import { CombinedRoundedImages } from '../Image';
import TextInput from '../TextInput';
import { Pill } from '../Text';
import Text from '../Text/Text';
import SelectInput from '../SelectInput';
import { SelectOption } from '../SelectInput/SelectInput';

Expand All @@ -24,22 +23,20 @@ import { IAssetWithBalance } from '../../providers/EtherspotContextProvider';

// utils
import { formatAmountDisplay, formatAssetAmountInput, formatMaxAmount, isEtherspotPrime } from '../../utils/common';
import { addressesEqual, isValidAmount, isValidEthereumAddress } from '../../utils/validation';
import { isValidAmount, isValidEthereumAddress } from '../../utils/validation';
import { Theme } from '../../utils/theme';
import { Chain, CHAIN_ID, supportedChains, klimaAsset } from '../../utils/chain';
import { Chain, CHAIN_ID, supportedChains } from '../../utils/chain';

// constants
import { bridgeServiceIdToDetails } from '../../utils/bridge';
import { DestinationWalletEnum } from '../../enums/wallet.enum';
import { GNOSIS_USDC_CONTRACT_ADDRESS } from '../../constants/assetConstants';

// hooks
import useAssetPriceUsd from '../../hooks/useAssetPriceUsd';
import { BiCheck } from 'react-icons/bi';
import { getNativeAssetPriceInUsd } from '../../services/coingecko';
import RouteOption from '../RouteOption/RouteOption';
import { GNOSIS_USDC_CONTRACT_ADDRESS } from '../../constants/assetConstants';
import HoneySwapRoute from '../HoneySwapRoute/HoneySwapRoute';

// services
import { getNativeAssetPriceInUsd } from '../../services/coingecko';

export interface IHoneySwapLPTransactionBlockValues {
fromChainId?: number;
fromAssetAddress?: string;
Expand Down Expand Up @@ -98,7 +95,7 @@ const HoneySwapLPTransactionBlock = ({
const { smartWalletOnly, providerAddress, accountAddress, sdk, getSdkForChainId, etherspotMode } = useEtherspot();
const [amount, setAmount] = useState<string>('');
const [selectedFromAsset, setSelectedFromAsset] = useState<IAssetWithBalance | null>(null);
const [selectedAccountType, setSelectedAccountType] = useState<string>(AccountTypes.Contract);
const [selectedAccountType, setSelectedAccountType] = useState<string>(AccountTypes.Key);
const [selectedFromNetwork, setSelectedFromNetwork] = useState<Chain | null>(null);
const [receiveAmount, setReceiveAmount] = useState<string>('');
const [routeToUSDC, setRouteToUSDC] = useState<Route[]>([]);
Expand Down
81 changes: 57 additions & 24 deletions src/components/TransactionBlock/PlrStakingV2TransactionBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import { Chain, supportedChains, CHAIN_ID } from '../../utils/chain';
import { swapServiceIdToDetails } from '../../utils/swap';
import { Theme } from '../../utils/theme';
import { bridgeServiceIdToDetails } from '../../utils/bridge';
import { getPlrAssetForChainId, plrStakedAssetEthereumMainnet } from '../../utils/asset';
import { getPlrAssetForChainId, stkPlrAsset } from '../../utils/asset';

// constants
import {
PLR_ADDRESS_PER_CHAIN,
PLR_STAKING_ADDRESS_ETHEREUM_MAINNET,
STKPLR_ADDRESS_ETHEREUM_MAINNET,
} from '../../constants/assetConstants';

interface ICrossChainSwap {
Expand Down Expand Up @@ -174,6 +174,7 @@ const PlrStakingV2TransactionBlock = ({
addressPlrBalancePerChain,
setAddressPlrBalancePerChain,
] = useState<{ [address: string]: IPlrBalancePerChain }>({});
const [stkPlrBalance, setStkPlrBalance] = useState<BigNumber | undefined>(undefined);

const hasEnoughPlrToStake = useMemo(() => [providerAddress, accountAddress].some((address) => {
if (!address || !addressPlrBalancePerChain?.[address]?.[CHAIN_ID.ETHEREUM_MAINNET]) return false;
Expand Down Expand Up @@ -206,16 +207,23 @@ const PlrStakingV2TransactionBlock = ({
addressPlrBalancePerChain?.[balanceAddress]?.[CHAIN_ID.ETHEREUM_MAINNET] as BigNumber
);

if (isEnoughPlrBalanceToStake(addressPlrBalancePerChain?.[providerAddress]?.[CHAIN_ID.ETHEREUM_MAINNET])) {
setSelectedFromNetwork(ethereumMainnetChain);
if (isEnoughPlrBalanceToStake(addressPlrBalancePerChain?.[balanceAddress]?.[CHAIN_ID.ETHEREUM_MAINNET])
&& (!selectedFromAsset || addressesEqual(selectedFromAsset?.address, plrAsset.address))) {
if (!selectedFromNetwork) {
setSelectedFromNetwork(ethereumMainnetChain);
}
if (!selectedFromAsset) {
setSelectedFromAsset(plrAsset);
}
setSelectedToNetwork(ethereumMainnetChain);
setSelectedFromAsset(plrAsset);
setSelectedToAsset(plrStakedAssetEthereumMainnet);
} else {
setSelectedToAsset(stkPlrAsset);
} else if (!addressesEqual(selectedFromAsset?.address, plrAsset.address)) {
setSelectedToNetwork(ethereumMainnetChain);
setSelectedToAsset(plrAsset);
}
}, [
selectedFromAsset,
selectedFromNetwork,
addressPlrBalancePerChain,
selectedAccountType,
providerAddress,
Expand Down Expand Up @@ -419,7 +427,7 @@ const PlrStakingV2TransactionBlock = ({
route,
}
} else if (selectedFromNetwork?.chainId === selectedToNetwork?.chainId
&& !addressesEqual(selectedFromAsset?.address, PLR_STAKING_ADDRESS_ETHEREUM_MAINNET)) {
&& !addressesEqual(selectedFromAsset?.address, stkPlrAsset.address)) {
const offer = availableOffers?.find((availableOffer) => availableOffer.provider === selectedOffer?.value);
swap = {
type: 'SAME_CHAIN_SWAP',
Expand Down Expand Up @@ -477,10 +485,14 @@ const PlrStakingV2TransactionBlock = ({

const plrAddressForChain = PLR_ADDRESS_PER_CHAIN[chain.chainId];

const tokensForBalance = chain.chainId === CHAIN_ID.ETHEREUM_MAINNET
? [plrAddressForChain, STKPLR_ADDRESS_ETHEREUM_MAINNET] // query for sktPLR balance on ethereum mainnet
: [plrAddressForChain];

try {
const { items: balances } = await sdk.getAccountBalances({
account: address,
tokens: [plrAddressForChain],
tokens: tokensForBalance,
chainId: chain.chainId,
});

Expand All @@ -497,8 +509,14 @@ const PlrStakingV2TransactionBlock = ({
[chain.chainId]: plrBalance,
},
}));

const stkPlrBalance = balances
.find((balance) => addressesEqual(STKPLR_ADDRESS_ETHEREUM_MAINNET, balance.token))
?.balance;

setStkPlrBalance(stkPlrBalance ?? undefined);
} catch (e) {
//
console.warn('Failed to get token balances', tokensForBalance, e);
}
}));
}));
Expand Down Expand Up @@ -571,12 +589,14 @@ const PlrStakingV2TransactionBlock = ({
return sum + walletSum;
}, 0), [addressPlrBalancePerChain]);

const isStakingAssetSelected = selectedToAsset?.address === plrStakedAssetEthereumMainnet.address;
const isStakingAssetSelected = addressesEqual(selectedToAsset?.address, stkPlrAsset.address);

const assetToSelectDisabled = !selectedFromNetwork
|| !selectedFromAsset
|| isStakingAssetSelected;

const hasStkPlrBalance = !!stkPlrBalance && stkPlrBalance.gt(0);

return (
<>
{!hideTitle && <Title>Pillar Validator Staking</Title>}
Expand All @@ -593,21 +613,34 @@ const PlrStakingV2TransactionBlock = ({
{hasEnoughPlrToStake && <>You can stake your PLR tokens.</>}
</Text>
<HorizontalLine />
{hasStkPlrBalance && (
<Text size={14}>
You have
<Highlighted
color={theme.color?.text?.blockParagraphHighlightSecondary}
>
&nbsp;{formatAmountDisplay(ethers.utils.formatUnits(stkPlrBalance, 18))} stkPLR
</Highlighted>
&nbsp;tokens:
</Text>
)}
{plrTokensSum > 0 && (
<>
<Text size={14}>
You have
<Highlighted
color={
hasEnoughPlrCrossChainToStake
? theme.color?.text?.blockParagraphHighlightSecondary
: theme.color?.text?.errorMessage
}
>
&nbsp;{formatAmountDisplay(plrTokensSum)} PLR
</Highlighted>
&nbsp;tokens:
</Text>
{!hasStkPlrBalance && (
<Text size={14}>
You have
<Highlighted
color={
hasEnoughPlrCrossChainToStake
? theme.color?.text?.blockParagraphHighlightSecondary
: theme.color?.text?.errorMessage
}
>
&nbsp;{formatAmountDisplay(plrTokensSum)} PLR
</Highlighted>
&nbsp;tokens:
</Text>
)}
{supportedChains
.filter((chain) => chainIdsWithPlrTokens.includes(chain.chainId))
.map((chain) => {
Expand Down
3 changes: 2 additions & 1 deletion src/constants/assetConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ export const PLR_DAO_CONTRACT_PER_CHAIN = {
[TESTNET_CHAIN_ID.POLYGON]: '0x23690E5981996cDC5eFc1e126ce9377B9876C95e',
};

// TODO: to be added once available, contract said to be deployed once QA passes UI
export const PLR_STAKING_ADDRESS_ETHEREUM_MAINNET = '0x4fa3d9Cf11Dc94e5E0f3BCCa980aA8FB3a0d27f3';

export const STKPLR_ADDRESS_ETHEREUM_MAINNET = '0xdfc4575b3cec99d756f45ed22289fa3f1fc530d7';
3 changes: 0 additions & 3 deletions src/providers/TransactionBuilderContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,6 @@ const TransactionBuilderContextProvider = ({
!crossChainActionClick
) {
if (transactionBlocks?.length === 0 && crossChainActions.length === 0) {
setShowWalletBlock(!mappedDefaultTransactionBlocks?.length && !hideWalletBlock);
if (mappedDefaultTransactionBlocks?.length) setTransactionBlocks(mappedDefaultTransactionBlocks);
}
}
Expand All @@ -1131,9 +1130,7 @@ const TransactionBuilderContextProvider = ({
crossChainActions,
isSubmitting,
crossChainActionClick,
hideWalletBlock,
mappedDefaultTransactionBlocks,
showWalletBlock,
]);

return (
Expand Down
12 changes: 7 additions & 5 deletions src/utils/asset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { BigNumber } from 'ethers';
import { IAssetWithBalance } from '../providers/EtherspotContextProvider';
import { CHAIN_ID, MAINNET_CHAIN_ID, TESTNET_CHAIN_ID } from './chain';
import { PLR_ADDRESS_PER_CHAIN } from '../constants/assetConstants';
import {
PLR_ADDRESS_PER_CHAIN,
STKPLR_ADDRESS_ETHEREUM_MAINNET
} from '../constants/assetConstants';

export const plrDaoAsset: IAssetWithBalance = {
address: '0xa6b37fc85d870711c56fbcb8afe2f8db049ae774',
Expand Down Expand Up @@ -52,9 +55,8 @@ export const getPlrAssetForChainId = (chainId: number, balance: BigNumber = BigN
balanceWorthUsd: null,
});

// TODO: replace with actual values once deployed to mainnet, deployment said to happen after qa passes
export const plrStakedAssetEthereumMainnet: IAssetWithBalance = {
address: '0xa6b37fc85d870711c56fbcb8afe2f8db049ae774',
export const stkPlrAsset = {
address: STKPLR_ADDRESS_ETHEREUM_MAINNET,
chainId: CHAIN_ID.ETHEREUM_MAINNET,
name: 'Staked Pillar',
symbol: 'stkPLR',
Expand All @@ -63,4 +65,4 @@ export const plrStakedAssetEthereumMainnet: IAssetWithBalance = {
balance: BigNumber.from(0),
assetPriceUsd: null,
balanceWorthUsd: null,
};
}
Loading

0 comments on commit c08d402

Please sign in to comment.