Skip to content

Commit

Permalink
Merge pull request #238 from skalenetwork/add-send-tx-func
Browse files Browse the repository at this point in the history
Add sendTx func, add more exports
  • Loading branch information
dmytrotkk authored Dec 6, 2023
2 parents 236fb43 + 8179128 commit 231c72a
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 14 deletions.
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.2",
"version": "2.0.3",
"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
6 changes: 4 additions & 2 deletions src/core/community_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
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
9 changes: 4 additions & 5 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,14 +147,13 @@ async function _networkSwitch(
}

export async function enforceNetwork(
provider: Provider,
chainId: bigint,
walletClient: WalletClient,
switchNetwork: (chainId: number | bigint) => Promise<Chain | undefined>,
switchNetwork: (chainId: number | undefined) => Promise<Chain | undefined>,
skaleNetwork: SkaleNetwork,
chainName: string
): Promise<bigint> {
const currentChainId = await walletClient.getChainId()
const { chainId } = await provider.getNetwork()
log(
`Current chainId: ${currentChainId}, required chainId: ${chainId}, required network: ${chainName} `
)
Expand Down
49 changes: 49 additions & 0 deletions src/core/transactions.ts
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

/**
* @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<TxResponse> {
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 }
}
}
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
}

0 comments on commit 231c72a

Please sign in to comment.