Skip to content

Commit

Permalink
Merge pull request #212 from skalenetwork/transfer-bugs-hotfix
Browse files Browse the repository at this point in the history
Improve network switching, fix transfer bugs
  • Loading branch information
dmytrotkk authored Oct 30, 2023
2 parents bbd6da8 + 9d54534 commit 0d36f6d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
* @dmitry-tk
* @dmytrotkk
*.md @skalenetwork/docowners
/docs/ @skalenetwork/docowners
8 changes: 7 additions & 1 deletion src/components/AddToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ export default function AddToken(props: {
)
const iconUrl = getIconUrl(props.token)
try {
await enforceNetwork(props.ima.provider, walletClient, switchNetworkAsync)
await enforceNetwork(
props.ima.provider,
walletClient,
switchNetworkAsync,
props.mpc.config.skaleNetwork,
props.destChainName
)
const wasAdded = await window.ethereum.request({
method: 'wallet_watchAsset',
params: {
Expand Down
3 changes: 2 additions & 1 deletion src/components/MetaportProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ export default function MetaportProvider(props: {
<RainbowKitProvider
coolMode
appInfo={{
appName: 'SKALE Metaport'
appName: 'SKALE Metaport',
learnMoreUrl: 'https://portal.skale.space/other/faq'
}}
showRecentTransactions={true}
chains={chains}
Expand Down
2 changes: 1 addition & 1 deletion src/components/TokenListSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function TokenListSection(props: {
className={cmn.fullWidth}
onClick={() => handle(props.tokens[key])}
>
<div className={cls(cmn.flex, cmn.flexcv, cmn.fullWidth, cmn.mtop5, cmn.mbott5)}>
<div className={cls(cmn.flex, cmn.flexcv, cmn.fullWidth, cmn.mtop10, cmn.mbott10)}>
<div className={cls(cmn.flex, cmn.flexc, cmn.mleft10)}>
<TokenIcon
tokenSymbol={props.tokens[key]?.meta.symbol}
Expand Down
27 changes: 12 additions & 15 deletions src/core/actions/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { LOADING_BUTTON_TEXT } from './actionState'
import { isMainnet } from '../helper'

import { IMA_ABIS } from '../contracts'
import { isMainnetChainId, getMainnetAbi } from '../network'
import { isMainnetChainId, getMainnetAbi, enforceNetwork } from '../network'

import { walletClientToSigner } from '../ethers'

Expand Down Expand Up @@ -81,7 +81,7 @@ export class Action {
setAmountErrorMessage: React.Dispatch<React.SetStateAction<string>>
setBtnText: (btnText: string) => void

_switchNetwork: (chainId: number | bigint) => Chain | undefined
_switchNetwork: (chainId: number | bigint) => Promise<Chain | undefined>

constructor(
mpc: MetaportCore,
Expand All @@ -93,7 +93,7 @@ export class Action {
token: TokenData,
setAmountErrorMessage: (amountErrorMessage: string) => void,
setBtnText: (btnText: string) => void,
switchNetwork: (chainId: number | bigint) => Chain | undefined,
switchNetwork: (chainId: number | bigint) => Promise<Chain | undefined>,
walletClient: WalletClient
) {
this.mpc = mpc
Expand Down Expand Up @@ -183,21 +183,18 @@ export class Action {
async getConnectedChain(
provider: Provider,
customAbiTokenType?: CustomAbiTokenType,
destChainName?: string
destChainName?: string,
chainName?: string
): Promise<MainnetChain | SChain> {
let chain: MainnetChain | SChain
this.updateState('switch')
const currentChainId = this.walletClient.chain.id
const { chainId } = await provider.getNetwork()
log(`Current chainId: ${currentChainId}, required chainId: ${chainId} `)
if (currentChainId !== Number(chainId)) {
log(`Switching network to ${chainId}...`)
const chain = await this._switchNetwork(Number(chainId))
if (!chain) {
throw new Error(`Failed to switch from ${currentChainId} to ${chainId} `)
}
log(`Network switched to ${chainId}...`)
}
const chainId = await enforceNetwork(
provider,
this.walletClient,
this._switchNetwork,
this.mpc.config.skaleNetwork,
chainName ?? this.chainName1
)
const signer = walletClientToSigner(this.walletClient)
if (isMainnetChainId(chainId, this.mpc.config.skaleNetwork)) {
chain = new MainnetChain(signer.provider, getMainnetAbi(this.mpc.config.skaleNetwork))
Expand Down
3 changes: 2 additions & 1 deletion src/core/actions/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ export class UnWrapERC20S extends Action {
const sChain = (await this.getConnectedChain(
this.sChain2.provider,
CustomAbiTokenType.erc20wrap,
this.chainName1
this.chainName1,
this.chainName2
)) as SChain
this.updateState('unwrap')
let tx
Expand Down
7 changes: 6 additions & 1 deletion src/core/actions/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ export class UnlockEthM extends Action {

async execute() {
this.updateState('init')
const mainnet = (await this.getConnectedChain(this.mainnet.provider)) as MainnetChain
const mainnet = (await this.getConnectedChain(
this.mainnet.provider,
undefined,
undefined,
this.chainName2
)) as MainnetChain
this.updateState('unlock')
const tx = await mainnet.eth.getMyEth({ address: this.address })
const block = await this.mainnet.provider.getBlock(tx.blockNumber)
Expand Down
18 changes: 13 additions & 5 deletions src/core/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import proxyEndpoints from '../metadata/proxy.json'
import { MAINNET_CHAIN_NAME } from './constants'
import { IMA_ADDRESSES, IMA_ABIS } from './contracts'
import { SkaleNetwork } from './interfaces'
import { constructWagmiChain } from './wagmi_network'

export { proxyEndpoints as PROXY_ENDPOINTS }

Expand Down Expand Up @@ -117,17 +118,24 @@ export function initSChain(network: SkaleNetwork, chainName: string): SChain {
export async function enforceNetwork(
provider: Provider,
walletClient: WalletClient,
switchNetwork: (chainId: number | bigint) => Promise<Chain | undefined>
): Promise<void> {
switchNetwork: (chainId: number | bigint) => Promise<Chain | undefined>,
skaleNetwork: SkaleNetwork,
chainName: string
): Promise<bigint> {
const currentChainId = walletClient.chain.id
const { chainId } = await provider.getNetwork()
log(`Current chainId: ${currentChainId}, required chainId: ${chainId} `)
if (currentChainId !== Number(chainId)) {
log(`Switching network to ${chainId}...`)
const chain = await switchNetwork(Number(chainId))
if (!chain) {
throw new Error(`Failed to switch from ${currentChainId} to ${chainId} `)
if (chainId !== 1n && chainId !== 5n) {
await walletClient.addChain({ chain: constructWagmiChain(skaleNetwork, chainName) })
} else {
const chain = await switchNetwork(Number(chainId))
if (!chain) {
throw new Error(`Failed to switch from ${currentChainId} to ${chainId} `)
}
}
log(`Network switched to ${chainId}...`)
}
return chainId
}
2 changes: 1 addition & 1 deletion src/styles/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $sk-paper-color: RGB(136 135 135 / 15%);
$sk-gray-background-color: rgba(161, 161, 161, 0.2);


$sk-btn-height: 47px;
$sk-btn-height: 52px;

$sk-disabled-dark: rgba(255, 255, 255, 0.5);
$sk-disabled-light: rgba(0, 0, 0, 0.38);
Expand Down
4 changes: 2 additions & 2 deletions src/styles/styles.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ button {
line-height: 1.6 !important;
letter-spacing: 0.02857em !important;
font-weight: 600 !important;
padding-top: 0.8em !important;
padding-bottom: 0.8em !important;
padding-top: 0.9em !important;
padding-bottom: 0.9em !important;
min-height: $sk-btn-height !important;
border-radius: $sk-border-radius !important;
box-shadow: none !important;
Expand Down

0 comments on commit 0d36f6d

Please sign in to comment.