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

Binance Smart Chain support #2659

Merged
merged 31 commits into from
Dec 5, 2022
Merged

Binance Smart Chain support #2659

merged 31 commits into from
Dec 5, 2022

Conversation

kkosiorowska
Copy link
Contributor

@kkosiorowska kkosiorowska commented Nov 22, 2022

Ref #2569

This PR adds initial Binance Smart Chain support.

UI

Screenshot 2022-11-24 at 15 46 27

Screenshot 2022-11-28 at 13 04 48

Screenshot 2022-11-24 at 15 55 40

Screenshot 2022-11-28 at 12 59 47

To Test
Set SUPPORT_BINANCE_SMART_CHAIN to true.

  • Check that Binance Smart Chain is available on the network list to choose.
  • Choose Binance Smart Chain and try to send and receive BNB coin.
  • Try to connect to dapp.

Latest build: extension-builds-2659 (as of Mon, 05 Dec 2022 09:42:41 GMT).

Copy link
Contributor

@greg-nagy greg-nagy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good so far :)

Excitedly waiting for this to move out of Draft :)

Note: since you have added swap endpoints you might want to add a token list as well.

@@ -87,7 +99,7 @@ export const BTC: NetworkBaseAsset = {
},
}

export const BASE_ASSETS = [ETH, BTC, MATIC, RBTC, OPTIMISTIC_ETH, AVAX]
export const BASE_ASSETS = [ETH, BTC, MATIC, RBTC, OPTIMISTIC_ETH, AVAX, BNB]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love to see this list expand :)

@0xDaedalus
Copy link
Contributor

Small nit - description is wrong

This PR adds initial Avalanche support.

@kkosiorowska kkosiorowska marked this pull request as ready for review November 28, 2022 14:00
@kkosiorowska
Copy link
Contributor Author

@jagodarybacka @0xDaedalus @hyphenized @greg-nagy

I have a small problem with a BSC chain. The problem is related to the number of decimals for the selected chain. What I mean is that when we want to get the price point in the selectAssetPricePoint selector (code) instead of finding the pricedAsset for BSC we take the first one found which is for ETH. This results in the wrong price point. See the photo below.

Screenshot 2022-11-25 at 11 18 05

I made some changes to take the price point for the right chain. After the changes, I was able to display the data correctly in the swap and wallet views. I did some tests and noticed that the data is displayed correctly however in the overview view we don't find the price point for ETH for the BSC chain. The total balance is lower which looks strange. I'm not sure if this is the correct way to go and if this solution is acceptable. Please take a look at my solution What do you think about it?

Screenshot 2022-11-28 at 13 04 48

Copy link
Contributor

@Shadowfiend Shadowfiend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of small thoughts.

Comment on lines 188 to 190
if (assetDecimals > decimals) {
amount /= BigInt(`1${"0".repeat(assetDecimals - decimals)}`)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convertFixedPoint should be your friend here I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion!

let { amount } = combinedAssetAmount

if (acc[assetSymbol]?.asset) {
const { decimals } = acc[assetSymbol].asset as FungibleAsset
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This forced cast seems dangerous, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change it and make it simpler.

@0xDaedalus
Copy link
Contributor

0xDaedalus commented Nov 28, 2022

I think that explicitly passing around the network an asset is related to is an acceptable solution here - but I also wonder if we would benefit from attaching an asset's network to the asset object itself - meaning we make homeNetwork a required attribute on AnyAsset.

I believe we have everything we need to do this and it feels like a reasonable enough approach. I guess something I'd like to get people's opinion on is if the following mental model makes sense:

  • tokens with the same symbol and contract code on different networks are different assets
  • tokens bridged across multiple networks (like USDC) are different assets even though they represent the same underlying
  • therefore - an asset can only belong to one network - and must have a homeNetwork.

@jagodarybacka
Copy link
Contributor

From the user perspective I agree with @0xDaedalus points - different network should mean it is a different asset even if symbol, name, contract are the same.

I can't find more in Figma but I see that @VladUXUI tried adding network icon to assets. We can think about what is the best way to display it (sum similar assets balance across networks, group them together but show balance on each chain separately...).
image

This would need some more work on the Portfolio page so I think it's not in the BSC scope.

Copy link
Contributor

@jagodarybacka jagodarybacka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick comment + I think it doesn't look the best multiline, can we make the label here shorter or maybe add ellipsis? cc @VladUXUI

image

@@ -23,6 +24,10 @@ export const scanWebsite = {
[GOERLI.chainID]: { title: "Etherscan", url: "https://goerli.etherscan.io/" },
[ARBITRUM_ONE.chainID]: { title: "Arbiscan", url: "https://arbiscan.io/" },
[AVALANCHE.chainID]: { title: "Snowtrace", url: "https://snowtrace.io/" },
[BINANCE_SMART_CHAIN.chainID]: {
title: "Bscscan",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title: "Bscscan",
title: "BscScan",

Copy link
Contributor

@hyphenized hyphenized left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superb work @kkosiorowska 💯! We're so close to shipping this!

I think it's a good idea to include the network in the asset, adding chainId should be enough but this change might be more involved than just fixing the price point handling. Left some comments on the issue.

@@ -142,6 +154,7 @@ export const CHAIN_ID_TO_RPC_URLS: {
[ARBITRUM_NOVA.chainID]: ["https://nova.arbitrum.io/rpc "],
[GOERLI.chainID]: ["https://ethereum-goerli-rpc.allthatnode.com"],
[AVALANCHE.chainID]: ["https://api.avax.network/ext/bc/C/rpc"],
[BINANCE_SMART_CHAIN.chainID]: ["https://rpc.ankr.com/bsc"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add the official one as well https://bsc-dataseed.binance.org

Comment on lines 190 to 196
if (targetDecimals > decimals) {
amount = convertFixedPoint(
combinedAssetAmount?.amount,
decimals,
targetDecimals
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be converting here from newAmountDecimals to existingDecimals right? Otherwise the balance is wrong after the object spread below replaces the existing asset (and it's decimals).

Suggested change
if (targetDecimals > decimals) {
amount = convertFixedPoint(
combinedAssetAmount?.amount,
decimals,
targetDecimals
)
}
if (newDecimals !== existingDecimals) {
amount = convertFixedPoint(amount, newDecimals, existingDecimals)
}

Then we can change the existing logic to not overwrite the existing asset

  if (acc[assetSymbol]) {
        acc[assetSymbol].amount += amount
      } else {
        acc[assetSymbol] = {
          ...combinedAssetAmount,
        }
      }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

const pricedAsset = assets.find(
[
selectAssetsState,
selectAssetSymbol,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pass the asset object here instead of the symbol and chainId separately. I suspect we were using the symbol because it's easier to find an existing price point, but, that's now causing issues for assets with different decimals because this is a best-effort match. For base network assets, it should be enough to find a price point with the symbol alone.

If we pass the asset here we should be able to match by the symbol and use convertFixedPoint to transform the pricePoint amount to be more accurate for the wanted asset. We can improve this to take into account BaseNetworkAssets and SmartContractFungible assets too but I think it would be better to address this in another issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you that maybe we should pass on the whole asset here. However, we will probably want to expand the assets to include the home network. Let's fix it in another issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is def strange here. I think creating a new ticket was a good call for this work.

@kkosiorowska
Copy link
Contributor Author

@jagodarybacka @0xDaedalus @hyphenized @greg-nagy

I created a separate task to distinguish assets. Please take a look at it and let me know if you agree. #2709

@VladUXUI
Copy link
Contributor

VladUXUI commented Dec 5, 2022

Quick comment + I think it doesn't look the best multiline, can we make the label here shorter or maybe add ellipsis? cc @VladUXUI

image

This might be a noob question, but as far as i know it's been called BNB Chain for some time now, since BSC merged with BNB? So maybe let's call it BNB Chain?
https://www.binance.com/en/blog/ecosystem/introducing-bnb-chain-the-evolution-of-binance-smart-chain-421499824684903436

@VladUXUI
Copy link
Contributor

VladUXUI commented Dec 5, 2022

@0xDaedalus and @jagodarybacka good call-out.
Yes each asset should have an network icon attached to it.
Idea is that if you have the same asset on different chains, we would show you the total + a breakdown per chain as an expandable menu.
The network icons for assets have been designed for some time, but i did not touch the "same asset on multiple chains" yet

Because Today and Tomorrow are sort of full, i think we can go ahead and show each asset separately with network icon. Even if they are same asset on different network.
We can improve the UX of this in the future

const pricedAsset = assets.find(
[
selectAssetsState,
selectAssetSymbol,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is def strange here. I think creating a new ticket was a good call for this work.

@@ -82,7 +82,8 @@ const computeCombinedAssetAmountsData = (
assets: AssetsState,
mainCurrencySymbol: string,
currentNetwork: EVMNetwork,
hideDust: boolean
hideDust: boolean,
useCurrentNetwork = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API naming is misleading here because it comes from the selector api.
Here it means every network every asset sum or current network every asset sum

I would have used something like onlyForCurrentNetwork

Copy link
Contributor

@greg-nagy greg-nagy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking good, and this is a big win 👸🚀

let's fix the getAssetPricePoint, chainID weirdness in #2709

@greg-nagy greg-nagy merged commit b9abfe6 into main Dec 5, 2022
@greg-nagy greg-nagy deleted the add-binance-smart-chain branch December 5, 2022 17:32
@greg-nagy greg-nagy mentioned this pull request Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants