Skip to content

Commit

Permalink
Merge branch 'release/0.14.0' into master-cere
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Navoichyk committed Jun 17, 2022
2 parents d451b02 + 983f70a commit 5a62c03
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ REACT_APP_SUPPORT_URL="https://t.me/ceretroubleshooting"
## These are only required if using Sentry for error logging
REACT_APP_SENTRY_DSN_URL="https://3d39fa05d843482eb93377947b435b5b@o318183.ingest.sentry.io/5987177"
SENTRY_AUTH_TOKEN="db67ae12222811ecabd2564b6436d30d"
REACT_APP_SENTRY_RELEASE="0.13.0"
REACT_APP_SENTRY_RELEASE="0.14.0"

REACT_APP_MAINTENANCE=false
REACT_APP_CONNECT_WITH_SUBSTRATE_WALLET_ENABLED=true
Expand Down
2 changes: 1 addition & 1 deletion .env.prod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ REACT_APP_SUPPORT_URL="https://t.me/ceretroubleshooting"
## These are only required if using Sentry for error logging
REACT_APP_SENTRY_DSN_URL="https://3d39fa05d843482eb93377947b435b5b@o318183.ingest.sentry.io/5987177"
SENTRY_AUTH_TOKEN="db67ae12222811ecabd2564b6436d30d"
REACT_APP_SENTRY_RELEASE="0.13.0"
REACT_APP_SENTRY_RELEASE="0.14.0"

REACT_APP_MAINTENANCE=false
REACT_APP_CONNECT_WITH_SUBSTRATE_WALLET_ENABLED=true
Expand Down
2 changes: 1 addition & 1 deletion .env.stage
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ REACT_APP_SUPPORT_URL="https://t.me/ceretroubleshooting"
## These are only required if using Sentry for error logging
REACT_APP_SENTRY_DSN_URL="https://3d39fa05d843482eb93377947b435b5b@o318183.ingest.sentry.io/5987177"
SENTRY_AUTH_TOKEN="db67ae12222811ecabd2564b6436d30d"
REACT_APP_SENTRY_RELEASE="0.13.0"
REACT_APP_SENTRY_RELEASE="0.14.0"

REACT_APP_MAINTENANCE=false
REACT_APP_CONNECT_WITH_SUBSTRATE_WALLET_ENABLED=true
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- ...

## v0.14.0

- Added liquidity enough check before transfer starts

## v0.13.0

- Implemented fallback mechanism for Polygon, Ethereum to Cere, Cere to Polygon transfer flows to be able to update transaction status window in the case blockchain response didn't reach client with GA integration
Expand Down
4 changes: 3 additions & 1 deletion config/chainbridge-runtime-config.cere.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ window.__RUNTIME_CONFIG__ = {
transferFunctionName: "transferNative",
typesFileName: "bridgeTypes.json",
existentialDepositPlusNetworkFee: "1.03",
bridgeAccountId: "5EYCAe5g7bGpFHagwe26HiRHdHdE3hobrwV6hq1UD2BPAiZb",
tokens: [
{
address: "cere-native",
name: cereTokenName,
symbol: cereTokenSymbol,
imageUri: "CEREIcon",
resourceId: "cere-native",
resourceId:
"0x000000000000000000000000000000c76ebe4a02bbc34786d860b355f5a5ce00",
decimals: cereTokenDecimals,
},
],
Expand Down
4 changes: 3 additions & 1 deletion config/chainbridge-runtime-config.cere.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ window.__RUNTIME_CONFIG__ = {
transferFunctionName: "transferNative",
typesFileName: "bridgeTypes.json",
existentialDepositPlusNetworkFee: "1.03",
bridgeAccountId: "5EYCAe5g7bGpFHagwe26HiRHdHdE3hobrwV6hq1UD2BPAiZb",
tokens: [
{
address: "cere-native",
name: cereTokenName,
symbol: cereTokenSymbol,
imageUri: "CEREIcon",
resourceId: "cere-native",
resourceId:
"0x000000000000000000000000000000c76ebe4a02bbc34786d860b355f5a5ce00",
decimals: cereTokenDecimals,
},
],
Expand Down
4 changes: 3 additions & 1 deletion config/chainbridge-runtime-config.cere.stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ window.__RUNTIME_CONFIG__ = {
transferFunctionName: "transferNative",
typesFileName: "bridgeTypes.json",
existentialDepositPlusNetworkFee: "1.03",
bridgeAccountId: "5EYCAe5g7bGpFHagwe26HiRHdHdE3hobrwV6hq1UD2BPAiZb",
tokens: [
{
address: "cere-native",
name: cereTokenName,
symbol: cereTokenSymbol,
imageUri: "CEREIcon",
resourceId: "cere-native",
resourceId:
"0x000000000000000000000000000000c76ebe4a02bbc34786d860b355f5a5ce00",
decimals: cereTokenDecimals,
},
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@Cerebellum-Network/chainbridge-ui",
"version": "0.13.0",
"version": "0.14.0",
"private": true,
"dependencies": {
"@babel/core": "^7.12.3",
Expand Down
15 changes: 14 additions & 1 deletion src/Components/Pages/TransferPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,15 @@ const useStyles = makeStyles(({ constants, palette }: ITheme) =>
borderRight: 0,
},
},
"& [class*='standardIcon']": {
backgroundColor: "white",
height: "30px",
},
},
"& span:last-child.error": {
position: "absolute",
width: "calc(100% + 62px)",
lineHeight: "1em",
},
},
maxButton: {
Expand Down Expand Up @@ -470,7 +475,7 @@ const TransferPage = () => {
})
.test(
"Bridge Supplies",
"Not enough tokens on the destination chain. Please contact support.",
"Max bridging amount exceeded. Please contact support.",
async (value) => {
if (checkSupplies && destinationChainConfig && value) {
const supplies = await checkSupplies(
Expand Down Expand Up @@ -770,6 +775,14 @@ const TransferPage = () => {
>
Cere Homepage
</NavLink>
<NavLink
style={{ textDecoration: "none" }}
className={classes.footerText}
to={{ pathname: process.env.REACT_APP_SUPPORT_URL }}
target="_blank"
>
Support
</NavLink>
<NavLink
style={{ textDecoration: "none" }}
className={classes.footerText}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export const EVMDestinationAdaptorProvider = ({
});
break;
case 3:
console.log("Transfer Completed Check!!!");
if (transactionStatus === "Transfer Completed") return;
setTransactionStatus("Transfer Completed");
setTransferTxHash(tx.transactionHash);
Expand Down
61 changes: 34 additions & 27 deletions src/Contexts/Adaptors/EVMAdaptors/EVMHomeAdaptorProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useCallback, useEffect, useState } from "react";
import {
chainbridgeConfig,
EvmBridgeConfig,
SubstrateBridgeConfig,
TokenConfig,
} from "../../../chainbridgeConfig";
import { Erc20DetailedFactory } from "../../../Contracts/Erc20DetailedFactory";
Expand All @@ -16,7 +17,9 @@ import { IHomeBridgeProviderProps } from "../interfaces";
import { HomeBridgeContext } from "../../HomeBridgeContext";
import { parseUnits } from "ethers/lib/utils";
import { decodeAddress } from "@polkadot/util-crypto";
import { hasTokenSupplies, getPriceCompatibility } from "./helpers";
import { getPriceCompatibility } from "./helpers";
import { createApi, hasTokenSupplies } from "../SubstrateApis/ChainBridgeAPI";
import { ApiPromise } from "@polkadot/api";

export const EVMHomeAdaptorProvider = ({
children,
Expand Down Expand Up @@ -72,6 +75,7 @@ export const EVMHomeAdaptorProvider = ({

const {
homeChainConfig,
destinationChainConfig,
setTransactionStatus,
setDepositNonce,
handleSetHomeChain,
Expand Down Expand Up @@ -99,6 +103,24 @@ export const EVMHomeAdaptorProvider = ({
const [wrapTokenConfig, setWrapperConfig] = useState<TokenConfig | undefined>(
undefined
);
const [api, setApi] = useState<ApiPromise | undefined>();
const [initialising, setInitialising] = useState(false);
const [walletSelected, setWalletSelected] = useState(false);

useEffect(() => {
if (destinationChainConfig?.type !== "Substrate" || initialising || api)
return;
setInitialising(true);
createApi(
destinationChainConfig.rpcUrl,
destinationChainConfig.rpcFallbackUrls
)
.then((api) => {
setApi(api);
setInitialising(false);
})
.catch(console.error);
}, [destinationChainConfig, initialising]);

useEffect(() => {
if (network) {
Expand All @@ -110,8 +132,6 @@ export const EVMHomeAdaptorProvider = ({
}
}, [handleSetHomeChain, homeChains, network, setNetworkId]);

const [initialising, setInitialising] = useState(false);
const [walletSelected, setWalletSelected] = useState(false);
useEffect(() => {
if (initialising || homeBridge || !onboard) return;
console.log("starting init");
Expand Down Expand Up @@ -262,35 +282,22 @@ export const EVMHomeAdaptorProvider = ({
}, [wallet, network, onboard]);

const handleCheckSupplies = useCallback(
async (
amount: number,
tokenAddress: string,
destinationChainId: number
) => {
if (homeChainConfig) {
const destinationChain = chainbridgeConfig.chains.find(
(c) => c.chainId === destinationChainId
async (amount: number) => {
if (destinationChainConfig?.type === "Substrate") {
return await hasTokenSupplies(
api as ApiPromise,
(destinationChainConfig as SubstrateBridgeConfig).bridgeAccountId,
amount,
destinationChainConfig.decimals
);
const token = homeChainConfig.tokens.find(
(token) => token.address === tokenAddress
} else {
console.warn(
`Liquidity check is skipping. The destination chain type ${destinationChainConfig?.type} is unknown. Please check it.`
);

if (destinationChain?.type === "Ethereum" && token) {
const hasSupplies = await hasTokenSupplies(
destinationChain,
tokens,
token,
amount,
tokenAddress
);
if (!hasSupplies) {
return false;
}
}
return true;
}
},
[homeChainConfig, tokens]
[destinationChainConfig, api]
);

const deposit = useCallback(
Expand Down
48 changes: 37 additions & 11 deletions src/Contexts/Adaptors/SubstrateApis/ChainBridgeAPI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ export const submitDeposit = (
return api.tx[subChainConfig.transferPalletName][
subChainConfig.transferFunctionName
](
new BigNumber(amount)
.multipliedBy(base.pow(new BigNumber(subChainConfig.decimals)))
.toString(10),
getBNFromDecimalAmount(amount, subChainConfig.decimals).toString(),
recipient,
dstChainId
);
Expand All @@ -64,12 +62,14 @@ export const getBridgeProposalVotes = async (
decimalAmount: number
): Promise<GetBridgeProsalVotesRes | undefined> => {
const destinationChainConfig = getСhainConfig(destinationChainId);
const decimals = new BigNumber(destinationChainConfig.decimals);

const call = api.registry.createType("Call", {
args: [
decodeAddress(recipient),
await decimalToBalance(api, decimalAmount, decimals),
await decimalToBalance(
api,
decimalAmount,
destinationChainConfig.decimals
),
],
callIndex: api.tx.erc20.transfer.callIndex,
});
Expand All @@ -83,10 +83,36 @@ export const getBridgeProposalVotes = async (

export const decimalToBalance = async (
api: ApiPromise,
decimalAmount: number,
decimals: BigNumber
amount: number,
decimals: number
) => {
const amount = new BigNumber(decimalAmount);
const balance = amount.multipliedBy(base.pow(decimals));
return api.registry.createType("Balance", balance.toString());
const amountBN = getBNFromDecimalAmount(amount, decimals);
return api.registry.createType("Balance", amountBN.toString());
};

export const getBalance = async (
api: ApiPromise,
address: string
): Promise<BigNumber> => {
return new BigNumber(
(await api.query.system.account(address)).data.free.toString()
);
};

export const hasTokenSupplies = async (
api: ApiPromise,
address: string,
amount: number,
decimals: number
): Promise<boolean> => {
const balance = await getBalance(api, address);
const amountBN = getBNFromDecimalAmount(amount, decimals);
return amountBN.isLessThan(balance);
};

export const getBNFromDecimalAmount = (
decimalAmount: number,
decimals: number
): BigNumber => {
return new BigNumber(decimalAmount).multipliedBy(base.pow(decimals));
};
38 changes: 35 additions & 3 deletions src/Contexts/Adaptors/SubstrateHomeAdaptor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import { Tokens } from "@chainsafe/web3-context/dist/context/tokensReducer";
import { BigNumber as BN } from "bignumber.js";
import { VoidFn } from "@polkadot/api/types";
import { utils } from "ethers";
import { SubstrateBridgeConfig } from "../../chainbridgeConfig";
import { SubstrateBridgeConfig, getСhainConfig } from "../../chainbridgeConfig";
import { toFixedWithoutRounding } from "../../Utils/Helpers";
import { hasTokenSupplies } from "./EVMAdaptors/helpers";

export const SubstrateHomeAdaptorProvider = ({
children,
Expand Down Expand Up @@ -152,7 +153,7 @@ export const SubstrateHomeAdaptorProvider = ({
)
);
setTokens({
[homeChainConfig.tokens[0].symbol || "TOKEN"]: {
[homeChainConfig.tokens[0].address || "TOKEN"]: {
decimals:
homeChainConfig.tokens[0].decimals ?? homeChainConfig.decimals,
balance: transferableBalance,
Expand Down Expand Up @@ -297,6 +298,37 @@ export const SubstrateHomeAdaptorProvider = ({
[api, setDepositNonce, setTransactionStatus, address, homeChainConfig]
);

const handleCheckSupplies = useCallback(
async (
amount: number,
tokenAddress: string,
destinationChainId: number
) => {
if (homeChainConfig) {
const destinationChainConfig = getСhainConfig(destinationChainId);
const token = homeChainConfig.tokens.find(
(token) => token.address === tokenAddress
);

if (destinationChainConfig?.type === "Ethereum" && token) {
return await hasTokenSupplies(
destinationChainConfig,
tokens,
token,
amount,
tokenAddress
);
} else {
console.warn(
`Liquidity check is skipping. The destination chain type ${destinationChainConfig?.type} is unknown. Please check it.`
);
return true;
}
}
},
[homeChainConfig, tokens]
);

// Required for adaptor however not needed for substrate
const wrapToken = async (value: number): Promise<string> => {
return "Not implemented";
Expand Down Expand Up @@ -333,7 +365,7 @@ export const SubstrateHomeAdaptorProvider = ({
nativeTokenBalance: 0,
accounts: accounts,
selectAccount: selectAccount,
handleCheckSupplies: undefined,
handleCheckSupplies,
}}
>
{children}
Expand Down
1 change: 1 addition & 0 deletions src/chainbridgeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type SubstrateBridgeConfig = BridgeConfig & {
transferFunctionName: string;
typesFileName: string;
existentialDepositPlusNetworkFee: number;
bridgeAccountId: string;
};

export type ChainbridgeConfig = {
Expand Down

0 comments on commit 5a62c03

Please sign in to comment.