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

2.0.2 Hotifx Beta #232

Merged
merged 12 commits into from
Jan 8, 2024
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@skalenetwork/metaport",
"version": "2.0.1",
"version": "2.0.3",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note -> Skipped 2.0.2

"description": "SKALE Metaport Widget",
"keywords": [
"skale",
Expand Down
3 changes: 2 additions & 1 deletion src/components/AddToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ export default function AddToken(props: {
props.mpc.config
)
const iconUrl = getIconUrl(props.token)
const { chainId } = await props.ima.provider.getNetwork()
try {
await enforceNetwork(
props.ima.provider,
chainId,
walletClient,
switchNetworkAsync,
props.mpc.config.skaleNetwork,
Expand Down
7 changes: 4 additions & 3 deletions src/core/actions/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,16 @@ export class Action {
): Promise<MainnetChain | SChain> {
let chain: MainnetChain | SChain
this.updateState('switch')
const chainId = await enforceNetwork(
provider,
const { chainId } = await provider.getNetwork()
const updChainId = await enforceNetwork(
chainId,
this.walletClient,
this._switchNetwork,
this.mpc.config.skaleNetwork,
chainName ?? this.chainName1
)
const signer = walletClientToSigner(this.walletClient)
if (isMainnetChainId(chainId, this.mpc.config.skaleNetwork)) {
if (isMainnetChainId(updChainId, this.mpc.config.skaleNetwork)) {
chain = new MainnetChain(signer.provider, getMainnetAbi(this.mpc.config.skaleNetwork))
} else {
chain = new SChain(signer.provider, IMA_ABIS.schain)
Expand Down
7 changes: 4 additions & 3 deletions src/core/community_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export async function getCommunityPoolData(
return communityPoolData
}


export async function withdraw(
mpc: MetaportCore,
walletClient: WalletClient,
Expand All @@ -119,8 +118,9 @@ export async function withdraw(
setLoading('withdraw')
try {
log(`Withdrawing from community pool: ${chainName}, amount: ${amount}`)
const { chainId } = await mpc.mainnet().provider.getNetwork()
await enforceNetwork(
mpc.mainnet().provider,
chainId,
walletClient,
switchNetwork,
mpc.config.skaleNetwork,
Expand Down Expand Up @@ -159,8 +159,9 @@ export async function recharge(
log(`Recharging community pool: ${chainName}, amount: ${amount}`)

const sChain = mpc.schain(chainName)
const { chainId } = await mpc.mainnet().provider.getNetwork()
await enforceNetwork(
mpc.mainnet().provider,
chainId,
walletClient,
switchNetwork,
mpc.config.skaleNetwork,
Expand Down
35 changes: 35 additions & 0 deletions src/core/interfaces/Transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license
* SKALE Metaport
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
* @file Transactions.ts
* @copyright SKALE Labs 2023-Present
*/

import { type TransactionResponse } from 'ethers';

export interface TxResponse {
status: boolean
err: TxResponseError | undefined
response: TransactionResponse | undefined
}

export interface TxResponseError {
name: string
msg: string
}
1 change: 1 addition & 0 deletions src/core/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export * from './CommunityPoolData'
export * from './TokenMetadata'
export * from './ActionStateUpdate'
export * from './ActionState'
export * from './Transactions'

export type AddressType = `0x${string}`

Expand Down
18 changes: 9 additions & 9 deletions src/core/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import debug from 'debug'
import { MainnetChain, SChain, TimeoutException } from '@skalenetwork/ima-js'
import { JsonRpcProvider, Provider } from 'ethers'
import { JsonRpcProvider } from 'ethers'

import { WalletClient } from 'viem'
import { Chain } from '@wagmi/core'
Expand Down Expand Up @@ -138,7 +138,7 @@ async function waitForNetworkChange(
async function _networkSwitch(
chainId: number | bigint,
currentChainId: number | bigint,
switchNetwork: (chainId: number | bigint) => Promise<Chain | undefined>
switchNetwork: (chainId: number | undefined) => Promise<Chain | undefined>
): Promise<void> {
const chain = await switchNetwork(Number(chainId))
if (!chain) {
Expand All @@ -147,21 +147,21 @@ async function _networkSwitch(
}

export async function enforceNetwork(
provider: Provider,
walletClient: WalletClient,
switchNetwork: (chainId: number | bigint) => Promise<Chain | undefined>,
chainId: bigint,
walletClient: any,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Usage of cast to WalletClient to create local instance _walletClient on line 156 starts here.

Ideal Cleanup

  • Type any-> WalletClient
  • Remove use of _walletClient unless it specifically uses a local scoped version?

switchNetwork: (chainId: number | undefined) => Promise<Chain | undefined>,
skaleNetwork: SkaleNetwork,
chainName: string
): Promise<bigint> {
const currentChainId = walletClient.chain.id
const { chainId } = await provider.getNetwork()
const _walletClient = walletClient as WalletClient
const currentChainId = await _walletClient.getChainId()
log(
`Current chainId: ${currentChainId}, required chainId: ${chainId}, required network: ${chainName} `
)
if (currentChainId !== Number(chainId)) {
log(`Switching network to ${chainId}...`)
if (chainId !== 1n && chainId !== 5n) {
await walletClient.addChain({ chain: constructWagmiChain(skaleNetwork, chainName) })
await _walletClient.addChain({ chain: constructWagmiChain(skaleNetwork, chainName) })
}
try {
// tmp fix for coinbase wallet
Expand All @@ -170,7 +170,7 @@ export async function enforceNetwork(
await sleep(DEFAULT_SLEEP)
_networkSwitch(chainId, currentChainId, switchNetwork)
}
await waitForNetworkChange(walletClient, currentChainId, chainId)
await waitForNetworkChange(_walletClient, currentChainId, chainId)
log(`Network switched to ${chainId}...`)
}
return chainId
Expand Down
51 changes: 51 additions & 0 deletions src/core/transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license
* SKALE Metaport
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
* @file transactions.ts
* @copyright SKALE Labs 2023-Present
*/

import { type TransactionResponse } from 'ethers';
import { TRANSFER_ERROR_MSG } from './constants'
import { TxResponse } from './interfaces'


export async function sendTransaction(func: any, args: any[]): Promise<TxResponse> {
try {
const response: TransactionResponse = await func(...args)
await response.wait()
return { status: true, err: undefined, response: response }
} catch (err) {
console.error(err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ideally should not print errors in production unless via different debug channel.

const msg = err.message
let name
if (err.code && err.code === 'ACTION_REJECTED') {
name = 'Transaction signing was rejected'
} else {
name = TRANSFER_ERROR_MSG
}
if (err.info && err.info.error && err.info.error.data && err.info.error.data.message) {
name = err.info.error.data.message
}
if (err.shortMessage) {
name = err.shortMessage
}
return { status: false, err: { name, msg }, response: undefined }
}
}
17 changes: 15 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ import MetaportCore from './core/metaport'
import { chainBg } from './core/metadata'
import { BASE_EXPLORER_URLS, GRAY_BG } from './core/constants'
import { toWei, fromWei } from './core/convertation'
import { ERC_ABIS } from './core/contracts'
import { sendTransaction } from './core/transactions'

import { getWidgetTheme as getMetaportTheme } from './core/themes'

import { useAccount as useWagmiAccount } from 'wagmi'
import {
useAccount as useWagmiAccount,
useWalletClient as useWagmiWalletClient,
useSwitchNetwork as useWagmiSwitchNetwork
} from 'wagmi'
import { ConnectButton as RainbowConnectButton } from '@rainbow-me/rainbowkit'

import { PROXY_ENDPOINTS } from './core/network'
import { PROXY_ENDPOINTS, enforceNetwork } from './core/network'
import { walletClientToSigner } from './core/ethers'

export {
Metaport,
Expand Down Expand Up @@ -82,11 +89,17 @@ export {
dataclasses,
getMetaportTheme,
useWagmiAccount,
useWagmiWalletClient,
useWagmiSwitchNetwork,
walletClientToSigner,
sendTransaction,
PROXY_ENDPOINTS,
BASE_EXPLORER_URLS,
CHAINS_META,
GRAY_BG,
ERC_ABIS,
chainBg,
getChainAlias,
enforceNetwork,
RainbowConnectButton
}
4 changes: 4 additions & 0 deletions src/metadata/faucet.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"wan-red-ain": {
"address": "0xb51a0E538c76C82e76757dc6D5a3938136C03c0C",
"func": "0x0c11dedd"
},
"green-giddy-denebola": {
"address": "0x5a6869ef5b81DCb58EBF51b8F893c31f5AFE3Fa8",
"func": "0x0c11dedd"
}
},
"staging": {
Expand Down
4 changes: 2 additions & 2 deletions src/metadata/proxy.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"mainnet": "mainnet.skalenodes.com",
"staging": "staging-v3.skalenodes.com",
"legacy": "legacy-proxy.skalenodes.com/",
"regression": "https://regression-proxy.skalenodes.com/",
"legacy": "legacy-proxy.skalenodes.com",
"regression": "regression-proxy.skalenodes.com",
"qatestnet": "new-testnet-proxy.skalenodes.com"
}
Loading