From 775c381a37b699fabe0c8881d980f9c0963b71b2 Mon Sep 17 00:00:00 2001 From: Dmytro Date: Tue, 14 Nov 2023 12:28:48 +0000 Subject: [PATCH 1/6] Fix chainId in enforce network --- package.json | 2 +- src/core/network.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 074ce02..1a53786 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@skalenetwork/metaport", - "version": "2.0.1", + "version": "2.0.2", "description": "SKALE Metaport Widget", "keywords": [ "skale", diff --git a/src/core/network.ts b/src/core/network.ts index 7461f8b..588f2dd 100644 --- a/src/core/network.ts +++ b/src/core/network.ts @@ -153,7 +153,7 @@ export async function enforceNetwork( skaleNetwork: SkaleNetwork, chainName: string ): Promise { - const currentChainId = walletClient.chain.id + const currentChainId = await walletClient.getChainId() const { chainId } = await provider.getNetwork() log( `Current chainId: ${currentChainId}, required chainId: ${chainId}, required network: ${chainName} ` From 88f590f781561e2cf48f9ddbd5a54c323864da8e Mon Sep 17 00:00:00 2001 From: Dmytro Date: Tue, 14 Nov 2023 16:23:26 +0000 Subject: [PATCH 2/6] Hotfix: add Nebula faucet --- src/core/community_pool.ts | 1 - src/metadata/faucet.json | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/community_pool.ts b/src/core/community_pool.ts index 8a1a461..cdff10c 100644 --- a/src/core/community_pool.ts +++ b/src/core/community_pool.ts @@ -104,7 +104,6 @@ export async function getCommunityPoolData( return communityPoolData } - export async function withdraw( mpc: MetaportCore, walletClient: WalletClient, diff --git a/src/metadata/faucet.json b/src/metadata/faucet.json index e87a19e..3c7476e 100644 --- a/src/metadata/faucet.json +++ b/src/metadata/faucet.json @@ -23,6 +23,10 @@ "wan-red-ain": { "address": "0xb51a0E538c76C82e76757dc6D5a3938136C03c0C", "func": "0x0c11dedd" + }, + "green-giddy-denebola": { + "address": "0x5a6869ef5b81DCb58EBF51b8F893c31f5AFE3Fa8", + "func": "0x0c11dedd" } }, "staging": { From 81791283be13722d9d5be6744ce42bb95cf16dbb Mon Sep 17 00:00:00 2001 From: Dmytro Date: Wed, 6 Dec 2023 19:49:00 +0000 Subject: [PATCH 3/6] Add sendTx func, add more exports --- package.json | 2 +- src/components/AddToken.tsx | 3 +- src/core/actions/action.ts | 7 +++-- src/core/community_pool.ts | 6 ++-- src/core/interfaces/Transactions.ts | 35 +++++++++++++++++++++ src/core/interfaces/index.ts | 1 + src/core/network.ts | 9 +++--- src/core/transactions.ts | 49 +++++++++++++++++++++++++++++ src/index.ts | 17 ++++++++-- 9 files changed, 115 insertions(+), 14 deletions(-) create mode 100644 src/core/interfaces/Transactions.ts create mode 100644 src/core/transactions.ts diff --git a/package.json b/package.json index 1a53786..ab28ccf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@skalenetwork/metaport", - "version": "2.0.2", + "version": "2.0.3", "description": "SKALE Metaport Widget", "keywords": [ "skale", diff --git a/src/components/AddToken.tsx b/src/components/AddToken.tsx index 5bad728..4892391 100644 --- a/src/components/AddToken.tsx +++ b/src/components/AddToken.tsx @@ -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, diff --git a/src/core/actions/action.ts b/src/core/actions/action.ts index 09723bb..ebe633e 100644 --- a/src/core/actions/action.ts +++ b/src/core/actions/action.ts @@ -188,15 +188,16 @@ export class Action { ): Promise { 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) diff --git a/src/core/community_pool.ts b/src/core/community_pool.ts index cdff10c..6df70b0 100644 --- a/src/core/community_pool.ts +++ b/src/core/community_pool.ts @@ -118,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, @@ -158,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, diff --git a/src/core/interfaces/Transactions.ts b/src/core/interfaces/Transactions.ts new file mode 100644 index 0000000..e1d1df7 --- /dev/null +++ b/src/core/interfaces/Transactions.ts @@ -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 . + */ + +/** + * @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 +} \ No newline at end of file diff --git a/src/core/interfaces/index.ts b/src/core/interfaces/index.ts index df24428..129715b 100644 --- a/src/core/interfaces/index.ts +++ b/src/core/interfaces/index.ts @@ -33,6 +33,7 @@ export * from './CommunityPoolData' export * from './TokenMetadata' export * from './ActionStateUpdate' export * from './ActionState' +export * from './Transactions' export type AddressType = `0x${string}` diff --git a/src/core/network.ts b/src/core/network.ts index 588f2dd..bed4aff 100644 --- a/src/core/network.ts +++ b/src/core/network.ts @@ -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' @@ -138,7 +138,7 @@ async function waitForNetworkChange( async function _networkSwitch( chainId: number | bigint, currentChainId: number | bigint, - switchNetwork: (chainId: number | bigint) => Promise + switchNetwork: (chainId: number | undefined) => Promise ): Promise { const chain = await switchNetwork(Number(chainId)) if (!chain) { @@ -147,14 +147,13 @@ async function _networkSwitch( } export async function enforceNetwork( - provider: Provider, + chainId: bigint, walletClient: WalletClient, - switchNetwork: (chainId: number | bigint) => Promise, + switchNetwork: (chainId: number | undefined) => Promise, skaleNetwork: SkaleNetwork, chainName: string ): Promise { const currentChainId = await walletClient.getChainId() - const { chainId } = await provider.getNetwork() log( `Current chainId: ${currentChainId}, required chainId: ${chainId}, required network: ${chainName} ` ) diff --git a/src/core/transactions.ts b/src/core/transactions.ts new file mode 100644 index 0000000..5693d6c --- /dev/null +++ b/src/core/transactions.ts @@ -0,0 +1,49 @@ +/** + * @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 . + */ + +/** + * @file transactions.ts + * @copyright SKALE Labs 2023-Present + */ + +import { TRANSFER_ERROR_MSG } from './constants' +import { TxResponse } from './interfaces' + + +export async function sendTransaction(func: any, args: any[]): Promise { + try { + const response = await func(...args) + return { status: true, err: undefined, response: response } + } catch (err) { + console.error(err) + 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 } + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 0719241..7595b14 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -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 } From f2cfb2666057a584fd18b7bb3effd836fe0b17a4 Mon Sep 17 00:00:00 2001 From: Dmytro Date: Mon, 11 Dec 2023 11:37:01 +0000 Subject: [PATCH 4/6] update send tx function --- src/core/transactions.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/transactions.ts b/src/core/transactions.ts index 5693d6c..72b27e8 100644 --- a/src/core/transactions.ts +++ b/src/core/transactions.ts @@ -21,13 +21,15 @@ * @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 { try { - const response = await func(...args) + const response: TransactionResponse = await func(...args) + await response.wait() return { status: true, err: undefined, response: response } } catch (err) { console.error(err) From 4e47f4150ae9491aacabd5cf144733f4f8cc81e4 Mon Sep 17 00:00:00 2001 From: Dmytro Date: Mon, 11 Dec 2023 16:24:47 +0000 Subject: [PATCH 5/6] Update enforce netork function signature --- src/core/network.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/core/network.ts b/src/core/network.ts index bed4aff..2c5bd06 100644 --- a/src/core/network.ts +++ b/src/core/network.ts @@ -148,19 +148,20 @@ async function _networkSwitch( export async function enforceNetwork( chainId: bigint, - walletClient: WalletClient, + walletClient: any, switchNetwork: (chainId: number | undefined) => Promise, skaleNetwork: SkaleNetwork, chainName: string ): Promise { - const currentChainId = await walletClient.getChainId() + 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 @@ -169,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 From 36fe87397c5c922da05b4f37714c8904d4c0511c Mon Sep 17 00:00:00 2001 From: Dmytro Date: Mon, 11 Dec 2023 16:39:12 +0000 Subject: [PATCH 6/6] Fix proxy URLs --- src/metadata/proxy.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/metadata/proxy.json b/src/metadata/proxy.json index 6769b17..5344ffc 100644 --- a/src/metadata/proxy.json +++ b/src/metadata/proxy.json @@ -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" } \ No newline at end of file