From fdb0bd398b32ba0b51899ea127de16c7c72b63f0 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Thu, 31 Aug 2023 08:39:48 +0200 Subject: [PATCH 01/17] -wip- initial commit implementing viem --- package-lock.json | 160 +++++++++++++++++- packages/config/src/configs/moonbaseBeta.ts | 2 +- packages/sdk/package.json | 3 +- packages/sdk/src/contract/contract.factory.ts | 3 +- .../sdk/src/contract/contract.interfaces.ts | 3 +- .../sdk/src/contract/contracts/Erc20/Erc20.ts | 45 ++++- .../src/contract/contracts/Xtokens/Xtokens.ts | 79 +++++++-- .../src/getTransferData/getDestinationData.ts | 3 +- .../sdk/src/getTransferData/getSourceData.ts | 8 +- .../getTransferData/getTransferData.utils.ts | 3 +- packages/sdk/src/sdk.interfaces.ts | 4 +- 11 files changed, 277 insertions(+), 36 deletions(-) diff --git a/package-lock.json b/package-lock.json index 41e67807..1727c639 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4983,6 +4983,62 @@ } ] }, + "node_modules/@scure/bip32": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@scure/bip32/-/bip32-1.3.0.tgz", + "integrity": "sha512-bcKpo1oj54hGholplGLpqPHRbIsnbixFtc06nwuNM5/dwSXOq/AAYoIBRsBmnZJSdfeNW5rnff7NTAz3ZCqR9Q==", + "funding": [ + { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + ], + "dependencies": { + "@noble/curves": "~1.0.0", + "@noble/hashes": "~1.3.0", + "@scure/base": "~1.1.0" + } + }, + "node_modules/@scure/bip32/node_modules/@noble/curves": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.0.0.tgz", + "integrity": "sha512-2upgEu0iLiDVDZkNLeFV2+ht0BAVgQnEmCk6JsOch9Rp8xfkMCbvbAZlA2pBHQc73dbl+vFOXfqkf4uemdn0bw==", + "funding": [ + { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + ], + "dependencies": { + "@noble/hashes": "1.3.0" + } + }, + "node_modules/@scure/bip32/node_modules/@noble/hashes": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.0.tgz", + "integrity": "sha512-ilHEACi9DwqJB0pw7kv+Apvh50jiiSyR/cQ3y4W7lOR5mhvn/50FLUfsnfJz0BDZtl/RR16kXvptiv6q1msYZg==", + "funding": [ + { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + ] + }, + "node_modules/@scure/bip39": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@scure/bip39/-/bip39-1.2.0.tgz", + "integrity": "sha512-SX/uKq52cuxm4YFXWFaVByaSHJh2w3BnokVSeUJVCv6K7WulT9u2BuNRBhuFl8vAuYnzx9bEu9WgpcNYTrYieg==", + "funding": [ + { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + ], + "dependencies": { + "@noble/hashes": "~1.3.0", + "@scure/base": "~1.1.0" + } + }, "node_modules/@sinclair/typebox": { "version": "0.27.8", "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz", @@ -5794,6 +5850,14 @@ "@types/node": "*" } }, + "node_modules/@types/ws": { + "version": "8.5.5", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.5.tgz", + "integrity": "sha512-lwhs8hktwxSjf9UaZ9tG5M03PGogvFaH8gUgLNbN9HKIg0dvv6q+gkSuJ8HN4/VbyxkuLzCjlN7GquQ0gUJfIg==", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/yargs": { "version": "17.0.24", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.24.tgz", @@ -6148,6 +6212,29 @@ "integrity": "sha512-OkqtOLPkR7oqWLrsgRKhzyLZVFLnNLfEF3DMXH+Rpn1fMNMDq/fOY9pXt55B+flBc62saN73CfOTy3hMSVZFTA==", "peer": true }, + "node_modules/abitype": { + "version": "0.9.3", + "resolved": "https://registry.npmjs.org/abitype/-/abitype-0.9.3.tgz", + "integrity": "sha512-dz4qCQLurx97FQhnb/EIYTk/ldQ+oafEDUqC0VVIeQS1Q48/YWt/9YNfMmp9SLFqN41ktxny3c8aYxHjmFIB/w==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/wagmi-dev" + } + ], + "peerDependencies": { + "typescript": ">=5.0.4", + "zod": "^3 >=3.19.1" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + }, + "zod": { + "optional": true + } + } + }, "node_modules/acorn": { "version": "8.8.2", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.2.tgz", @@ -9621,6 +9708,14 @@ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "dev": true }, + "node_modules/isomorphic-ws": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/isomorphic-ws/-/isomorphic-ws-5.0.0.tgz", + "integrity": "sha512-muId7Zzn9ywDsyXgTIafTry2sV3nySZeUDe6YedVd1Hvuuep5AsIlqK+XefWpYTyJG5e503F2xIuT2lcU6rCSw==", + "peerDependencies": { + "ws": "*" + } + }, "node_modules/istanbul-lib-coverage": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.0.tgz", @@ -16105,7 +16200,7 @@ "version": "5.1.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.3.tgz", "integrity": "sha512-XH627E9vkeqhlZFQuL+UsyAXEnibT0kWR2FWONlr4sTjvxyJYnyefgrkyECLzM5NenmKzRAy2rR/OlYLA1HkZw==", - "dev": true, + "devOptional": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -16241,6 +16336,66 @@ "spdx-expression-parse": "^3.0.0" } }, + "node_modules/viem": { + "version": "1.9.2", + "resolved": "https://registry.npmjs.org/viem/-/viem-1.9.2.tgz", + "integrity": "sha512-a4l502Sl4ytYOT6a77ezDCEEgztR+wF8ZZRLAEVcrQawFFIiAWXGPrXVPT8MQmDF00idPMKPON8Ayz34Ft3NLw==", + "dependencies": { + "@adraffy/ens-normalize": "1.9.0", + "@noble/curves": "1.1.0", + "@noble/hashes": "1.3.0", + "@scure/bip32": "1.3.0", + "@scure/bip39": "1.2.0", + "@types/ws": "^8.5.4", + "abitype": "0.9.3", + "isomorphic-ws": "5.0.0", + "ws": "8.12.0" + }, + "peerDependencies": { + "typescript": ">=5.0.4" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, + "node_modules/viem/node_modules/@adraffy/ens-normalize": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/@adraffy/ens-normalize/-/ens-normalize-1.9.0.tgz", + "integrity": "sha512-iowxq3U30sghZotgl4s/oJRci6WPBfNO5YYgk2cIOMCHr3LeGPcsZjCEr+33Q4N+oV3OABDAtA+pyvWjbvBifQ==" + }, + "node_modules/viem/node_modules/@noble/hashes": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.0.tgz", + "integrity": "sha512-ilHEACi9DwqJB0pw7kv+Apvh50jiiSyR/cQ3y4W7lOR5mhvn/50FLUfsnfJz0BDZtl/RR16kXvptiv6q1msYZg==", + "funding": [ + { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + ] + }, + "node_modules/viem/node_modules/ws": { + "version": "8.12.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.12.0.tgz", + "integrity": "sha512-kU62emKIdKVeEIOIKVegvqpXMSTAMLJozpHZaJNDYqBjzlSYXQGviYwN1osDLJ9av68qHd4a2oSjd7yD4pacig==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/walker": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/walker/-/walker-1.0.8.tgz", @@ -16847,7 +17002,8 @@ "@moonbeam-network/xcm-config": "1.1.0", "@moonbeam-network/xcm-types": "1.0.1", "@moonbeam-network/xcm-utils": "1.0.2", - "big.js": "^6.2.1" + "big.js": "^6.2.1", + "viem": "^1.6.0" }, "peerDependencies": { "@polkadot/api": "^10.9.1", diff --git a/packages/config/src/configs/moonbaseBeta.ts b/packages/config/src/configs/moonbaseBeta.ts index 1900a417..ef9cbadd 100644 --- a/packages/config/src/configs/moonbaseBeta.ts +++ b/packages/config/src/configs/moonbaseBeta.ts @@ -29,7 +29,7 @@ export const moonbaseBetaConfig = new ChainConfig({ balance: BalanceBuilder().substrate().assets().account(), destination: moonbaseAlpha, destinationFee: { - amount: 0.0002, + amount: 0.002, asset: dev, balance: BalanceBuilder().substrate().system().account(), }, diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 8019cde4..cf5b665c 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -51,7 +51,8 @@ "@moonbeam-network/xcm-config": "1.1.0", "@moonbeam-network/xcm-types": "1.0.1", "@moonbeam-network/xcm-utils": "1.0.2", - "big.js": "^6.2.1" + "big.js": "^6.2.1", + "viem": "^1.6.0" }, "peerDependencies": { "@polkadot/api": "^10.9.1", diff --git a/packages/sdk/src/contract/contract.factory.ts b/packages/sdk/src/contract/contract.factory.ts index 250fdd56..0854d63e 100644 --- a/packages/sdk/src/contract/contract.factory.ts +++ b/packages/sdk/src/contract/contract.factory.ts @@ -1,5 +1,6 @@ import { ContractConfig } from '@moonbeam-network/xcm-builder'; import { Signer } from 'ethers'; +import { WalletClient } from 'viem'; import { BalanceContractInterface, TransferContractInterface, @@ -8,7 +9,7 @@ import { Erc20, Xtokens } from './contracts'; export function createContract( config: ContractConfig, - signer: Signer, + signer: Signer | WalletClient, ): TransferContractInterface | BalanceContractInterface { if (config.module === 'Erc20') { return new Erc20(config, signer); diff --git a/packages/sdk/src/contract/contract.interfaces.ts b/packages/sdk/src/contract/contract.interfaces.ts index 018e6626..1b432fa6 100644 --- a/packages/sdk/src/contract/contract.interfaces.ts +++ b/packages/sdk/src/contract/contract.interfaces.ts @@ -1,11 +1,12 @@ import type { TransactionResponse } from '@ethersproject/abstract-provider'; +import { WriteContractReturnType } from 'viem'; export interface BaseContractInterface { readonly address: string; } export interface TransferContractInterface extends BaseContractInterface { - transfer(): Promise; + transfer(): Promise; getFee(amount: bigint): Promise; } diff --git a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts index d9b41070..8c292418 100644 --- a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts +++ b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts @@ -1,5 +1,8 @@ import { ContractConfig } from '@moonbeam-network/xcm-builder'; import { Contract, Signer } from 'ethers'; +import { PublicClient, WalletClient, createPublicClient, http } from 'viem'; +// TODO mjm implement correctly +import { moonbaseAlpha } from 'viem/chains'; import { BalanceContractInterface } from '../../contract.interfaces'; import abi from './Erc20ABI.json'; @@ -8,26 +11,54 @@ export class Erc20 implements BalanceContractInterface { readonly #config: ContractConfig; - readonly #contract: Contract; + readonly #contract: Contract | undefined; - constructor(config: ContractConfig, signer: Signer) { + readonly #client: PublicClient; + + readonly #walletClient: WalletClient | undefined; + + constructor(config: ContractConfig, signer: Signer | WalletClient) { if (!config.address) { throw new Error('Contract address is required'); } this.address = config.address; this.#config = config; - this.#contract = new Contract(this.address, abi, signer); + this.#contract = + signer instanceof Signer + ? new Contract(this.address, abi, signer) + : undefined; + this.#client = createPublicClient({ + chain: moonbaseAlpha, + transport: http(), + }); + this.#walletClient = signer instanceof Signer ? undefined : signer; } async getBalance(): Promise { - const balance = await this.#contract.balanceOf(...this.#config.args); - - return balance.toBigInt(); + if (this.#contract) { + const balance = await this.#contract.balanceOf(...this.#config.args); + return balance.toBigInt(); + } + return (await this.#client.readContract({ + abi, + account: this.#walletClient?.account, + address: this.address as `0x${string}`, + args: this.#config.args, + functionName: 'balanceOf', + })) as bigint; } async getDecimals(): Promise { - const decimals = await this.#contract.decimals(); + const decimals = this.#contract + ? await this.#contract.decimals(...this.#config.args) + : this.#client.readContract({ + abi, + account: this.#walletClient?.account, + address: this.address as `0x${string}`, + args: [], + functionName: 'decimals', + }); return decimals; } diff --git a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts index 85e17851..b5cd8cf6 100644 --- a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts +++ b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts @@ -1,26 +1,59 @@ import type { TransactionResponse } from '@ethersproject/abstract-provider'; import { ContractConfig } from '@moonbeam-network/xcm-builder'; import { Contract, Signer } from 'ethers'; +import { + PublicClient, + WalletClient, + WriteContractReturnType, + createPublicClient, + http, +} from 'viem'; import { TransferContractInterface } from '../../contract.interfaces'; import abi from './XtokensABI.json'; export class Xtokens implements TransferContractInterface { - readonly address: string = '0x0000000000000000000000000000000000000804'; + readonly address = '0x0000000000000000000000000000000000000804'; readonly #config: ContractConfig; - readonly #contract: Contract; + readonly #contract: Contract | undefined; - readonly #signer: Signer; + // TODO mjm rename to EthersSigner + readonly #signer: Signer | WalletClient; - constructor(config: ContractConfig, signer: Signer) { + // TODO mjm use this or use the signer? + readonly #walletClient: WalletClient | undefined; + + readonly #client: PublicClient; + + constructor(config: ContractConfig, signer: Signer | WalletClient) { this.#config = config; this.#signer = signer; - this.#contract = new Contract(this.address, abi, signer); + this.#walletClient = signer instanceof Signer ? undefined : signer; + this.#contract = + signer instanceof Signer + ? new Contract(this.address, abi, signer) + : undefined; + this.#client = createPublicClient({ + // TODO + transport: http('https://rpc.api.moonbeam.network'), + }); } - async transfer(): Promise { - return this.#contract[this.#config.func](...this.#config.args); + async transfer(): Promise { + if (this.#signer instanceof Signer) { + return this.#contract?.[this.#config.func](...this.#config.args); + } + + const { request } = await this.#client.simulateContract({ + // TODO compact this + abi, + account: this.#signer.account, + address: this.address, + args: this.#config.args, + functionName: this.#config.func, + }); + return this.#signer.writeContract(request); } async getFee(amount: bigint): Promise { @@ -33,13 +66,20 @@ export class Xtokens implements TransferContractInterface { * Or if you try to send 0 as amount. */ try { - const estimatedGas = ( - await this.#contract.estimateGas[this.#config.func]( - ...this.#config.args, - ) - ).toBigInt(); + const estimatedGas = this.#contract + ? ( + await this.#contract.estimateGas[this.#config.func]( + ...this.#config.args, + ) + ).toBigInt() + : await this.#client.estimateContractGas({ + abi, + account: (this.#signer as WalletClient).account || '0x', // TODO mjm throw error here + address: this.address, + args: this.#config.args, + functionName: this.#config.func, + }); const gasPrice = await this.getGasPrice(); - return estimatedGas * gasPrice; } catch (error) { // eslint-disable-next-line no-console @@ -52,10 +92,15 @@ export class Xtokens implements TransferContractInterface { } private async getGasPrice() { - const { gasPrice, maxPriorityFeePerGas } = await this.#signer.getFeeData(); + if (this.#signer instanceof Signer) { + const { gasPrice, maxPriorityFeePerGas } = + await this.#signer.getFeeData(); + + return ( + (gasPrice?.toBigInt() || 0n) + (maxPriorityFeePerGas?.toBigInt() || 0n) + ); + } - return ( - (gasPrice?.toBigInt() || 0n) + (maxPriorityFeePerGas?.toBigInt() || 0n) - ); + return this.#client.getGasPrice(); } } diff --git a/packages/sdk/src/getTransferData/getDestinationData.ts b/packages/sdk/src/getTransferData/getDestinationData.ts index 0c018776..2cb4558d 100644 --- a/packages/sdk/src/getTransferData/getDestinationData.ts +++ b/packages/sdk/src/getTransferData/getDestinationData.ts @@ -4,6 +4,7 @@ import { TransferConfig } from '@moonbeam-network/xcm-config'; import { AssetAmount } from '@moonbeam-network/xcm-types'; import { toBigInt } from '@moonbeam-network/xcm-utils'; import { Signer as EthersSigner } from 'ethers'; +import { WalletClient } from 'viem'; import { PolkadotService } from '../polkadot'; import { DestinationChainTransferData } from '../sdk.interfaces'; import { getBalance, getDecimals, getMin } from './getTransferData.utils'; @@ -11,7 +12,7 @@ import { getBalance, getDecimals, getMin } from './getTransferData.utils'; export interface GetDestinationDataParams { transferConfig: TransferConfig; destinationAddress: string; - ethersSigner: EthersSigner; + ethersSigner: EthersSigner | WalletClient; polkadot: PolkadotService; } diff --git a/packages/sdk/src/getTransferData/getSourceData.ts b/packages/sdk/src/getTransferData/getSourceData.ts index 60298f19..ca4e0bda 100644 --- a/packages/sdk/src/getTransferData/getSourceData.ts +++ b/packages/sdk/src/getTransferData/getSourceData.ts @@ -9,6 +9,7 @@ import { AssetAmount } from '@moonbeam-network/xcm-types'; import { convertDecimals } from '@moonbeam-network/xcm-utils'; import Big from 'big.js'; import { Signer as EthersSigner } from 'ethers'; +import { WalletClient } from 'viem'; import { TransferContractInterface, createContract } from '../contract'; import { PolkadotService } from '../polkadot'; import { SourceChainTransferData } from '../sdk.interfaces'; @@ -18,7 +19,7 @@ export interface GetSourceDataParams { transferConfig: TransferConfig; destinationAddress: string; destinationFee: AssetAmount; - ethersSigner: EthersSigner; + ethersSigner: EthersSigner | WalletClient; polkadot: PolkadotService; sourceAddress: string; } @@ -107,6 +108,7 @@ export async function getSourceData({ palletInstance: chain.getAssetPalletInstance(asset), source: chain, }); + const contract = config.contract?.build({ address: destinationAddress, amount: balance, @@ -179,7 +181,7 @@ export interface GetFeeParams { balance: bigint; contract?: ContractConfig; decimals: number; - ethersSigner?: EthersSigner; + ethersSigner?: EthersSigner | WalletClient; extrinsic?: ExtrinsicConfig; polkadot: PolkadotService; sourceAddress: string; @@ -213,7 +215,7 @@ export async function getContractFee( balance: bigint, config: ContractConfig, decimals: number, - ethersSigner: EthersSigner, + ethersSigner: EthersSigner | WalletClient, ): Promise { const contract = createContract( config, diff --git a/packages/sdk/src/getTransferData/getTransferData.utils.ts b/packages/sdk/src/getTransferData/getTransferData.utils.ts index 19df16b7..4d3b9e19 100644 --- a/packages/sdk/src/getTransferData/getTransferData.utils.ts +++ b/packages/sdk/src/getTransferData/getTransferData.utils.ts @@ -3,13 +3,14 @@ import { AssetConfig } from '@moonbeam-network/xcm-config'; import { Asset } from '@moonbeam-network/xcm-types'; import { toBigInt } from '@moonbeam-network/xcm-utils'; import { Signer as EthersSigner } from 'ethers'; +import { WalletClient } from 'viem'; import { BalanceContractInterface, createContract } from '../contract'; import { PolkadotService } from '../polkadot'; export interface GetFeeBalancesParams { address: string; config: AssetConfig; - ethersSigner: EthersSigner; + ethersSigner: EthersSigner | WalletClient; polkadot: PolkadotService; asset?: Asset; } diff --git a/packages/sdk/src/sdk.interfaces.ts b/packages/sdk/src/sdk.interfaces.ts index 82927000..22a3ec26 100644 --- a/packages/sdk/src/sdk.interfaces.ts +++ b/packages/sdk/src/sdk.interfaces.ts @@ -2,9 +2,11 @@ import { AnyChain, AssetAmount } from '@moonbeam-network/xcm-types'; import type { Signer as PolkadotSigner } from '@polkadot/api/types'; import type { IKeyringPair } from '@polkadot/types/types'; import type { Signer as EthersSigner } from 'ethers'; +import { WalletClient } from 'viem'; export interface Signers { - ethersSigner: EthersSigner; + // TODO rename + ethersSigner: EthersSigner | WalletClient; polkadotSigner: PolkadotSigner | IKeyringPair; } From 7917cb4854c2484e881aaff70c4de73c7ff08b58 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Thu, 31 Aug 2023 10:27:11 +0200 Subject: [PATCH 02/17] -wip- --- packages/sdk/src/contract/contracts/Erc20/Erc20.ts | 14 +++++++------- .../sdk/src/contract/contracts/Xtokens/Xtokens.ts | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts index 8c292418..92359670 100644 --- a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts +++ b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts @@ -2,7 +2,6 @@ import { ContractConfig } from '@moonbeam-network/xcm-builder'; import { Contract, Signer } from 'ethers'; import { PublicClient, WalletClient, createPublicClient, http } from 'viem'; // TODO mjm implement correctly -import { moonbaseAlpha } from 'viem/chains'; import { BalanceContractInterface } from '../../contract.interfaces'; import abi from './Erc20ABI.json'; @@ -15,6 +14,7 @@ export class Erc20 implements BalanceContractInterface { readonly #client: PublicClient; + // TODO mjm name signer? readonly #walletClient: WalletClient | undefined; constructor(config: ContractConfig, signer: Signer | WalletClient) { @@ -24,15 +24,15 @@ export class Erc20 implements BalanceContractInterface { this.address = config.address; this.#config = config; - this.#contract = - signer instanceof Signer - ? new Contract(this.address, abi, signer) - : undefined; + if (signer instanceof Signer) { + this.#contract = new Contract(this.address, abi, signer); + } else { + this.#walletClient = signer; + } this.#client = createPublicClient({ - chain: moonbaseAlpha, + chain: this.#walletClient?.chain, transport: http(), }); - this.#walletClient = signer instanceof Signer ? undefined : signer; } async getBalance(): Promise { diff --git a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts index b5cd8cf6..014d1023 100644 --- a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts +++ b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts @@ -35,8 +35,8 @@ export class Xtokens implements TransferContractInterface { ? new Contract(this.address, abi, signer) : undefined; this.#client = createPublicClient({ - // TODO - transport: http('https://rpc.api.moonbeam.network'), + chain: this.#walletClient?.chain, + transport: http(), }); } @@ -46,7 +46,7 @@ export class Xtokens implements TransferContractInterface { } const { request } = await this.#client.simulateContract({ - // TODO compact this + // TODO mjm compact this abi, account: this.#signer.account, address: this.address, From b04703bdcae02f0498e1e44c948a3923535ba3f8 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Thu, 31 Aug 2023 13:12:26 +0200 Subject: [PATCH 03/17] -wip- --- .../sdk/src/contract/contract.interfaces.ts | 15 ++++- packages/sdk/src/contract/contract.utils.ts | 25 ++++++++ .../sdk/src/contract/contracts/Erc20/Erc20.ts | 47 ++++++++------ .../src/contract/contracts/Xtokens/Xtokens.ts | 61 +++++++++---------- 4 files changed, 94 insertions(+), 54 deletions(-) create mode 100644 packages/sdk/src/contract/contract.utils.ts diff --git a/packages/sdk/src/contract/contract.interfaces.ts b/packages/sdk/src/contract/contract.interfaces.ts index 1b432fa6..8f6142f2 100644 --- a/packages/sdk/src/contract/contract.interfaces.ts +++ b/packages/sdk/src/contract/contract.interfaces.ts @@ -1,5 +1,6 @@ import type { TransactionResponse } from '@ethersproject/abstract-provider'; -import { WriteContractReturnType } from 'viem'; +import { Contract, Signer } from 'ethers'; +import { PublicClient, WalletClient, WriteContractReturnType } from 'viem'; export interface BaseContractInterface { readonly address: string; @@ -14,3 +15,15 @@ export interface BalanceContractInterface extends BaseContractInterface { getBalance(): Promise; getDecimals(): Promise; } + +export type ContractClient = EthersClient | ViemClient; + +export interface EthersClient { + contract: Contract; + signer: Signer; +} + +export interface ViemClient { + publicClient: PublicClient; + walletClient: WalletClient; +} diff --git a/packages/sdk/src/contract/contract.utils.ts b/packages/sdk/src/contract/contract.utils.ts new file mode 100644 index 00000000..2f50a4d8 --- /dev/null +++ b/packages/sdk/src/contract/contract.utils.ts @@ -0,0 +1,25 @@ +import { Signer as EthersSigner } from 'ethers'; +import { WalletClient } from 'viem'; +import { + ContractClient, + EthersClient, + ViemClient, +} from './contract.interfaces'; + +type Signer = EthersSigner | WalletClient; + +export function isEthersSigner(signer: Signer): signer is EthersSigner { + return 'provider' in signer; +} + +export function isWalletClient(signer: Signer): signer is WalletClient { + return 'chain' in signer; +} + +export function isEthersClient(client: ContractClient): client is EthersClient { + return 'contract' in client; +} + +export function isViemClient(client: ContractClient): client is ViemClient { + return 'publicClient' in client; +} diff --git a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts index 92359670..6ab34702 100644 --- a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts +++ b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts @@ -1,8 +1,12 @@ import { ContractConfig } from '@moonbeam-network/xcm-builder'; import { Contract, Signer } from 'ethers'; -import { PublicClient, WalletClient, createPublicClient, http } from 'viem'; +import { WalletClient, createPublicClient, http } from 'viem'; // TODO mjm implement correctly -import { BalanceContractInterface } from '../../contract.interfaces'; +import { + BalanceContractInterface, + ContractClient, +} from '../../contract.interfaces'; +import { isEthersClient, isEthersSigner } from '../../contract.utils'; import abi from './Erc20ABI.json'; export class Erc20 implements BalanceContractInterface { @@ -10,9 +14,7 @@ export class Erc20 implements BalanceContractInterface { readonly #config: ContractConfig; - readonly #contract: Contract | undefined; - - readonly #client: PublicClient; + readonly #client: ContractClient; // TODO mjm name signer? readonly #walletClient: WalletClient | undefined; @@ -24,23 +26,28 @@ export class Erc20 implements BalanceContractInterface { this.address = config.address; this.#config = config; - if (signer instanceof Signer) { - this.#contract = new Contract(this.address, abi, signer); - } else { - this.#walletClient = signer; - } - this.#client = createPublicClient({ - chain: this.#walletClient?.chain, - transport: http(), - }); + + this.#client = isEthersSigner(signer) + ? { contract: new Contract(this.address, abi, signer), signer } + : { + publicClient: createPublicClient({ + chain: signer.chain, + transport: http(), + }), + walletClient: signer, + }; } async getBalance(): Promise { - if (this.#contract) { - const balance = await this.#contract.balanceOf(...this.#config.args); + if (isEthersClient(this.#client)) { + const balance = await this.#client.contract.balanceOf( + ...this.#config.args, + ); return balance.toBigInt(); } - return (await this.#client.readContract({ + + return (await this.#client.publicClient.readContract({ + // TODO mjm extract abi, account: this.#walletClient?.account, address: this.address as `0x${string}`, @@ -50,9 +57,9 @@ export class Erc20 implements BalanceContractInterface { } async getDecimals(): Promise { - const decimals = this.#contract - ? await this.#contract.decimals(...this.#config.args) - : this.#client.readContract({ + const decimals = isEthersClient(this.#client) + ? await this.#client.contract.decimals() + : this.#client.publicClient.readContract({ abi, account: this.#walletClient?.account, address: this.address as `0x${string}`, diff --git a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts index 014d1023..09fa9dd9 100644 --- a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts +++ b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts @@ -2,13 +2,16 @@ import type { TransactionResponse } from '@ethersproject/abstract-provider'; import { ContractConfig } from '@moonbeam-network/xcm-builder'; import { Contract, Signer } from 'ethers'; import { - PublicClient, WalletClient, WriteContractReturnType, createPublicClient, http, } from 'viem'; -import { TransferContractInterface } from '../../contract.interfaces'; +import { + ContractClient, + TransferContractInterface, +} from '../../contract.interfaces'; +import { isEthersClient, isEthersSigner } from '../../contract.utils'; import abi from './XtokensABI.json'; export class Xtokens implements TransferContractInterface { @@ -16,44 +19,36 @@ export class Xtokens implements TransferContractInterface { readonly #config: ContractConfig; - readonly #contract: Contract | undefined; - - // TODO mjm rename to EthersSigner - readonly #signer: Signer | WalletClient; - - // TODO mjm use this or use the signer? - readonly #walletClient: WalletClient | undefined; - - readonly #client: PublicClient; + readonly #client: ContractClient; constructor(config: ContractConfig, signer: Signer | WalletClient) { this.#config = config; - this.#signer = signer; - this.#walletClient = signer instanceof Signer ? undefined : signer; - this.#contract = - signer instanceof Signer - ? new Contract(this.address, abi, signer) - : undefined; - this.#client = createPublicClient({ - chain: this.#walletClient?.chain, - transport: http(), - }); + + this.#client = isEthersSigner(signer) + ? { contract: new Contract(this.address, abi, signer), signer } + : { + publicClient: createPublicClient({ + chain: signer.chain, + transport: http(), + }), + walletClient: signer, + }; } async transfer(): Promise { - if (this.#signer instanceof Signer) { - return this.#contract?.[this.#config.func](...this.#config.args); + if (isEthersClient(this.#client)) { + return this.#client.contract[this.#config.func](...this.#config.args); } - const { request } = await this.#client.simulateContract({ + const { request } = await this.#client.publicClient.simulateContract({ // TODO mjm compact this abi, - account: this.#signer.account, + account: this.#client.walletClient.account, address: this.address, args: this.#config.args, functionName: this.#config.func, }); - return this.#signer.writeContract(request); + return this.#client.walletClient.writeContract(request); } async getFee(amount: bigint): Promise { @@ -66,15 +61,15 @@ export class Xtokens implements TransferContractInterface { * Or if you try to send 0 as amount. */ try { - const estimatedGas = this.#contract + const estimatedGas = isEthersClient(this.#client) ? ( - await this.#contract.estimateGas[this.#config.func]( + await this.#client.contract.estimateGas[this.#config.func]( ...this.#config.args, ) ).toBigInt() - : await this.#client.estimateContractGas({ + : await this.#client.publicClient.estimateContractGas({ abi, - account: (this.#signer as WalletClient).account || '0x', // TODO mjm throw error here + account: this.#client.walletClient.account || '0x', // TODO mjm throw error here address: this.address, args: this.#config.args, functionName: this.#config.func, @@ -92,15 +87,15 @@ export class Xtokens implements TransferContractInterface { } private async getGasPrice() { - if (this.#signer instanceof Signer) { + if (isEthersClient(this.#client)) { const { gasPrice, maxPriorityFeePerGas } = - await this.#signer.getFeeData(); + await this.#client.signer.getFeeData(); return ( (gasPrice?.toBigInt() || 0n) + (maxPriorityFeePerGas?.toBigInt() || 0n) ); } - return this.#client.getGasPrice(); + return this.#client.publicClient.getGasPrice(); } } From 2a2a64fdc7ca341209b2c04a8cca7b9a4a41d872 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Mon, 4 Sep 2023 23:28:02 +0200 Subject: [PATCH 04/17] change dependencies --- package-lock.json | 5 +++-- package.json | 1 + packages/sdk/package.json | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 594a143f..ada007cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,6 +32,7 @@ "eslint-plugin-import": "^2.27.5", "eslint-plugin-jest": "^27.2.1", "eslint-plugin-prettier": "^4.2.1", + "ethers": "^5.7.2", "glob": "^10.3.3", "husky": "^8.0.3", "jest": "^29.6.1", @@ -17003,14 +17004,14 @@ "@moonbeam-network/xcm-types": "1.0.1", "@moonbeam-network/xcm-utils": "1.0.2", "big.js": "^6.2.1", + "ethers": "^5.7.2", "viem": "^1.6.0" }, "peerDependencies": { "@polkadot/api": "^10.9.1", "@polkadot/api-augment": "^10.9.1", "@polkadot/types": "^10.9.1", - "@polkadot/util": "^12.3.2", - "ethers": "^5.7.2" + "@polkadot/util": "^12.3.2" } }, "packages/types": { diff --git a/package.json b/package.json index 30812518..a49299ba 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "eslint-plugin-import": "^2.27.5", "eslint-plugin-jest": "^27.2.1", "eslint-plugin-prettier": "^4.2.1", + "ethers": "^5.7.2", "glob": "^10.3.3", "husky": "^8.0.3", "jest": "^29.6.1", diff --git a/packages/sdk/package.json b/packages/sdk/package.json index d34410bc..70fd24e9 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -52,13 +52,13 @@ "@moonbeam-network/xcm-types": "1.0.1", "@moonbeam-network/xcm-utils": "1.0.2", "big.js": "^6.2.1", + "ethers": "^5.7.2", "viem": "^1.6.0" }, "peerDependencies": { "@polkadot/api": "^10.9.1", "@polkadot/api-augment": "^10.9.1", "@polkadot/types": "^10.9.1", - "@polkadot/util": "^12.3.2", - "ethers": "^5.7.2" + "@polkadot/util": "^12.3.2" } } From 0412e42b5b45136d1e80c39c5a19d08e96eb3b31 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Tue, 5 Sep 2023 00:43:04 +0200 Subject: [PATCH 05/17] rename types and fix interfaces --- examples/sdk-simple/index.ts | 10 +++--- packages/sdk/README.md | 4 +-- .../sdk/src/contract/contract.interfaces.ts | 4 +-- packages/sdk/src/contract/contract.utils.ts | 3 +- .../sdk/src/contract/contracts/Erc20/Erc20.ts | 10 ++---- .../src/contract/contracts/Xtokens/Xtokens.ts | 35 ++++++++++++------- .../src/getTransferData/getDestinationData.ts | 12 +++---- .../sdk/src/getTransferData/getSourceData.ts | 33 ++++++++--------- .../src/getTransferData/getTransferData.ts | 21 +++++------ .../getTransferData/getTransferData.utils.ts | 19 ++++------ packages/sdk/src/sdk.interfaces.ts | 7 ++-- packages/sdk/src/sdk.ts | 4 +-- 12 files changed, 76 insertions(+), 86 deletions(-) diff --git a/examples/sdk-simple/index.ts b/examples/sdk-simple/index.ts index 688fae8b..5d9cf2b1 100644 --- a/examples/sdk-simple/index.ts +++ b/examples/sdk-simple/index.ts @@ -13,7 +13,7 @@ const provider = new ethers.providers.WebSocketProvider(moonbeam.ws, { chainId: moonbeam.id, name: moonbeam.name, }); -const ethersSigner = new ethers.Wallet(moonbeamPrivateKey, provider); +const signer = new ethers.Wallet(moonbeamPrivateKey, provider); // Polkadot Signer =========================================================== @@ -65,7 +65,7 @@ export async function fromPolkadot() { console.log('\nTransfer from Polkadot to Moonbeam\n'); const data = await Sdk().getTransferData({ - destinationAddress: ethersSigner.address, + destinationAddress: signer.address, destinationKeyOrChain: moonbeam, keyOrAsset: dot, polkadotSigner: pair, @@ -93,8 +93,8 @@ export async function fromMoonbeam() { .asset(dot) .source(moonbeam) .destination(polkadot) - .accounts(ethersSigner.address, pair.address, { - ethersSigner, + .accounts(signer.address, pair.address, { + signer, }); logBalances(data); @@ -114,7 +114,7 @@ async function main() { console.warn = () => null; console.clear(); - console.log(`\nMoonbeam address: ${ethersSigner.address}.`); + console.log(`\nMoonbeam address: ${signer.address}.`); console.log(`Polkadot address: ${pair.address}.`); await fromPolkadot(); diff --git a/packages/sdk/README.md b/packages/sdk/README.md index 7b6bfe2e..e8728c7a 100644 --- a/packages/sdk/README.md +++ b/packages/sdk/README.md @@ -47,7 +47,7 @@ const dataViaAssetsMethod = await assets() .source('INSERT_SOURCE_CHAIN') .destination('INSERT_DESTINATION_CHAIN') .accounts('INSERT_SOURCE_ADDRESS', 'INSERT_DESTINATION_ADDRESS', { - ethersSigner?: 'INSERT_ETHERS_SIGNER', + signer?: 'INSERT_ETHERS_SIGNER', polkadotSigner?: 'INSERT_POLKADOT_SIGNER', }); @@ -55,7 +55,7 @@ const dataViaAssetsMethod = await assets() const dataViaGetTransferDataMethod = await getTransferData({ destinationAddress: 'INSERT_DESTINATION_ADDRESS', destinationKeyOrChain: 'INSERT_DESTINATION_CHAIN', - ethersSigner?: 'INSERT_ETHERS_SIGNER', + signer?: 'INSERT_ETHERS_SIGNER', keyOrAsset: 'INSERT_ASSET', polkadotSigner?: 'INSERT_POLKADOT_SIGNER', sourceAddress: 'INSERT_SOURCE_ADDRESS', diff --git a/packages/sdk/src/contract/contract.interfaces.ts b/packages/sdk/src/contract/contract.interfaces.ts index 8f6142f2..2b08e16c 100644 --- a/packages/sdk/src/contract/contract.interfaces.ts +++ b/packages/sdk/src/contract/contract.interfaces.ts @@ -1,5 +1,5 @@ import type { TransactionResponse } from '@ethersproject/abstract-provider'; -import { Contract, Signer } from 'ethers'; +import { Contract, Signer as EthersSigner } from 'ethers'; import { PublicClient, WalletClient, WriteContractReturnType } from 'viem'; export interface BaseContractInterface { @@ -20,7 +20,7 @@ export type ContractClient = EthersClient | ViemClient; export interface EthersClient { contract: Contract; - signer: Signer; + signer: EthersSigner; } export interface ViemClient { diff --git a/packages/sdk/src/contract/contract.utils.ts b/packages/sdk/src/contract/contract.utils.ts index 2f50a4d8..896d683a 100644 --- a/packages/sdk/src/contract/contract.utils.ts +++ b/packages/sdk/src/contract/contract.utils.ts @@ -1,13 +1,12 @@ import { Signer as EthersSigner } from 'ethers'; import { WalletClient } from 'viem'; +import { Signer } from '../sdk.interfaces'; import { ContractClient, EthersClient, ViemClient, } from './contract.interfaces'; -type Signer = EthersSigner | WalletClient; - export function isEthersSigner(signer: Signer): signer is EthersSigner { return 'provider' in signer; } diff --git a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts index 6ab34702..9f9f4f4f 100644 --- a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts +++ b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts @@ -1,7 +1,6 @@ import { ContractConfig } from '@moonbeam-network/xcm-builder'; import { Contract, Signer } from 'ethers'; import { WalletClient, createPublicClient, http } from 'viem'; -// TODO mjm implement correctly import { BalanceContractInterface, ContractClient, @@ -16,9 +15,6 @@ export class Erc20 implements BalanceContractInterface { readonly #client: ContractClient; - // TODO mjm name signer? - readonly #walletClient: WalletClient | undefined; - constructor(config: ContractConfig, signer: Signer | WalletClient) { if (!config.address) { throw new Error('Contract address is required'); @@ -26,7 +22,6 @@ export class Erc20 implements BalanceContractInterface { this.address = config.address; this.#config = config; - this.#client = isEthersSigner(signer) ? { contract: new Contract(this.address, abi, signer), signer } : { @@ -47,9 +42,8 @@ export class Erc20 implements BalanceContractInterface { } return (await this.#client.publicClient.readContract({ - // TODO mjm extract abi, - account: this.#walletClient?.account, + account: this.#client.walletClient.account, address: this.address as `0x${string}`, args: this.#config.args, functionName: 'balanceOf', @@ -61,7 +55,7 @@ export class Erc20 implements BalanceContractInterface { ? await this.#client.contract.decimals() : this.#client.publicClient.readContract({ abi, - account: this.#walletClient?.account, + account: this.#client.walletClient.account, address: this.address as `0x${string}`, args: [], functionName: 'decimals', diff --git a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts index 09fa9dd9..f0c33aee 100644 --- a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts +++ b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts @@ -10,6 +10,7 @@ import { import { ContractClient, TransferContractInterface, + ViemClient, } from '../../contract.interfaces'; import { isEthersClient, isEthersSigner } from '../../contract.utils'; import abi from './XtokensABI.json'; @@ -41,7 +42,6 @@ export class Xtokens implements TransferContractInterface { } const { request } = await this.#client.publicClient.simulateContract({ - // TODO mjm compact this abi, account: this.#client.walletClient.account, address: this.address, @@ -51,6 +51,25 @@ export class Xtokens implements TransferContractInterface { return this.#client.walletClient.writeContract(request); } + async getEthersContractEstimatedGas(contract: Contract): Promise { + return ( + await contract.estimateGas[this.#config.func](...this.#config.args) + ).toBigInt(); + } + + async getClientEstimatedGas(client: ViemClient): Promise { + if (!client.walletClient.account) { + return 0n; + } + return client.publicClient.estimateContractGas({ + abi, + account: client.walletClient.account, + address: this.address, + args: this.#config.args, + functionName: this.#config.func, + }); + } + async getFee(amount: bigint): Promise { if (amount === 0n) { return 0n; @@ -62,18 +81,8 @@ export class Xtokens implements TransferContractInterface { */ try { const estimatedGas = isEthersClient(this.#client) - ? ( - await this.#client.contract.estimateGas[this.#config.func]( - ...this.#config.args, - ) - ).toBigInt() - : await this.#client.publicClient.estimateContractGas({ - abi, - account: this.#client.walletClient.account || '0x', // TODO mjm throw error here - address: this.address, - args: this.#config.args, - functionName: this.#config.func, - }); + ? await this.getEthersContractEstimatedGas(this.#client.contract) + : await this.getClientEstimatedGas(this.#client); const gasPrice = await this.getGasPrice(); return estimatedGas * gasPrice; } catch (error) { diff --git a/packages/sdk/src/getTransferData/getDestinationData.ts b/packages/sdk/src/getTransferData/getDestinationData.ts index 2cb4558d..f48373e2 100644 --- a/packages/sdk/src/getTransferData/getDestinationData.ts +++ b/packages/sdk/src/getTransferData/getDestinationData.ts @@ -3,23 +3,21 @@ import { FeeConfigBuilder } from '@moonbeam-network/xcm-builder'; import { TransferConfig } from '@moonbeam-network/xcm-config'; import { AssetAmount } from '@moonbeam-network/xcm-types'; import { toBigInt } from '@moonbeam-network/xcm-utils'; -import { Signer as EthersSigner } from 'ethers'; -import { WalletClient } from 'viem'; import { PolkadotService } from '../polkadot'; -import { DestinationChainTransferData } from '../sdk.interfaces'; +import { DestinationChainTransferData, Signer } from '../sdk.interfaces'; import { getBalance, getDecimals, getMin } from './getTransferData.utils'; export interface GetDestinationDataParams { transferConfig: TransferConfig; destinationAddress: string; - ethersSigner: EthersSigner | WalletClient; + signer: Signer; polkadot: PolkadotService; } export async function getDestinationData({ transferConfig, destinationAddress, - ethersSigner, + signer, polkadot, }: GetDestinationDataParams): Promise { const { @@ -32,16 +30,16 @@ export async function getDestinationData({ decimals: await getDecimals({ address: destinationAddress, config, - ethersSigner, polkadot, + signer, }), }); const balance = await getBalance({ address: destinationAddress, config, - ethersSigner, polkadot, + signer, }); const min = await getMin(config, polkadot); diff --git a/packages/sdk/src/getTransferData/getSourceData.ts b/packages/sdk/src/getTransferData/getSourceData.ts index ca4e0bda..6ac99fc5 100644 --- a/packages/sdk/src/getTransferData/getSourceData.ts +++ b/packages/sdk/src/getTransferData/getSourceData.ts @@ -8,18 +8,16 @@ import { FeeAssetConfig, TransferConfig } from '@moonbeam-network/xcm-config'; import { AssetAmount } from '@moonbeam-network/xcm-types'; import { convertDecimals } from '@moonbeam-network/xcm-utils'; import Big from 'big.js'; -import { Signer as EthersSigner } from 'ethers'; -import { WalletClient } from 'viem'; import { TransferContractInterface, createContract } from '../contract'; import { PolkadotService } from '../polkadot'; -import { SourceChainTransferData } from '../sdk.interfaces'; +import { Signer, SourceChainTransferData } from '../sdk.interfaces'; import { getBalance, getDecimals, getMin } from './getTransferData.utils'; export interface GetSourceDataParams { transferConfig: TransferConfig; destinationAddress: string; destinationFee: AssetAmount; - ethersSigner: EthersSigner | WalletClient; + signer: Signer; polkadot: PolkadotService; sourceAddress: string; } @@ -28,7 +26,7 @@ export async function getSourceData({ transferConfig, destinationAddress, destinationFee, - ethersSigner, + signer, polkadot, sourceAddress, }: GetSourceDataParams): Promise { @@ -42,8 +40,8 @@ export async function getSourceData({ decimals: await getDecimals({ address: destinationAddress, config, - ethersSigner, polkadot, + signer, }), }); const zeroFeeAmount = config.fee?.asset @@ -53,8 +51,8 @@ export async function getSourceData({ address: destinationAddress, asset: config.fee.asset, config, - ethersSigner, polkadot, + signer, }), }) : zeroAmount; @@ -65,8 +63,8 @@ export async function getSourceData({ address: destinationAddress, asset: config.destinationFee.asset, config, - ethersSigner, polkadot, + signer, }), }) : zeroAmount; @@ -74,8 +72,8 @@ export async function getSourceData({ const balance = await getBalance({ address: sourceAddress, config, - ethersSigner, polkadot, + signer, }); const feeBalance = await getFeeBalances({ @@ -121,9 +119,9 @@ export async function getSourceData({ balance, contract, decimals: zeroFeeAmount.decimals, - ethersSigner, extrinsic, polkadot, + signer, sourceAddress, }); @@ -181,7 +179,7 @@ export interface GetFeeParams { balance: bigint; contract?: ContractConfig; decimals: number; - ethersSigner?: EthersSigner | WalletClient; + signer?: Signer; extrinsic?: ExtrinsicConfig; polkadot: PolkadotService; sourceAddress: string; @@ -191,17 +189,17 @@ export async function getFee({ balance, contract, decimals, - ethersSigner, + signer, extrinsic, polkadot, sourceAddress, }: GetFeeParams): Promise { if (contract) { - if (!ethersSigner) { + if (!signer) { throw new Error('Ethers signer must be provided'); } - return getContractFee(balance, contract, decimals, ethersSigner); + return getContractFee(balance, contract, decimals, signer); } if (extrinsic) { @@ -215,12 +213,9 @@ export async function getContractFee( balance: bigint, config: ContractConfig, decimals: number, - ethersSigner: EthersSigner | WalletClient, + signer: Signer, ): Promise { - const contract = createContract( - config, - ethersSigner, - ) as TransferContractInterface; + const contract = createContract(config, signer) as TransferContractInterface; const fee = await contract.getFee(balance); return convertDecimals(fee, 18, decimals); diff --git a/packages/sdk/src/getTransferData/getTransferData.ts b/packages/sdk/src/getTransferData/getTransferData.ts index 7ecef59f..8b263604 100644 --- a/packages/sdk/src/getTransferData/getTransferData.ts +++ b/packages/sdk/src/getTransferData/getTransferData.ts @@ -1,4 +1,5 @@ /* eslint-disable @typescript-eslint/no-use-before-define */ +import type { TransactionResponse } from '@ethersproject/abstract-provider'; import { TransferConfig } from '@moonbeam-network/xcm-config'; import { AssetAmount } from '@moonbeam-network/xcm-types'; import { convertDecimals, toBigInt } from '@moonbeam-network/xcm-utils'; @@ -21,12 +22,12 @@ export interface GetTransferDataParams extends Partial { export async function getTransferData({ destinationAddress, - ethersSigner, + signer, polkadotSigner, sourceAddress, transferConfig, }: GetTransferDataParams): Promise { - if (!ethersSigner) { + if (!signer) { throw new Error('Ethers signer must be provided'); } @@ -37,8 +38,8 @@ export async function getTransferData({ const destination = await getDestinationData({ destinationAddress, - ethersSigner, polkadot: destPolkadot, + signer, transferConfig, }); @@ -50,8 +51,8 @@ export async function getTransferData({ const source = await getSourceData({ destinationAddress, destinationFee, - ethersSigner, polkadot: srcPolkadot, + signer, sourceAddress, transferConfig, }); @@ -82,8 +83,8 @@ export async function getTransferData({ async swap() { return getTransferData({ destinationAddress: sourceAddress, - ethersSigner, polkadotSigner, + signer, sourceAddress: destinationAddress, transferConfig: { ...transferConfig, @@ -119,15 +120,15 @@ export async function getTransferData({ }); if (contract) { - if (!ethersSigner) { + if (!signer) { throw new Error('Ethers signer must be provided'); } - return ( - createContract(contract, ethersSigner) as TransferContractInterface - ) + return (createContract(contract, signer) as TransferContractInterface) .transfer() - .then((tx) => tx.hash); + .then((tx) => + typeof tx === 'object' ? (tx as TransactionResponse).hash : tx, + ); } if (extrinsic) { diff --git a/packages/sdk/src/getTransferData/getTransferData.utils.ts b/packages/sdk/src/getTransferData/getTransferData.utils.ts index 4d3b9e19..56babffe 100644 --- a/packages/sdk/src/getTransferData/getTransferData.utils.ts +++ b/packages/sdk/src/getTransferData/getTransferData.utils.ts @@ -2,15 +2,14 @@ import { CallType, SubstrateQueryConfig } from '@moonbeam-network/xcm-builder'; import { AssetConfig } from '@moonbeam-network/xcm-config'; import { Asset } from '@moonbeam-network/xcm-types'; import { toBigInt } from '@moonbeam-network/xcm-utils'; -import { Signer as EthersSigner } from 'ethers'; -import { WalletClient } from 'viem'; import { BalanceContractInterface, createContract } from '../contract'; import { PolkadotService } from '../polkadot'; +import { Signer } from '../sdk.interfaces'; export interface GetFeeBalancesParams { address: string; config: AssetConfig; - ethersSigner: EthersSigner | WalletClient; + signer: Signer; polkadot: PolkadotService; asset?: Asset; } @@ -18,7 +17,7 @@ export interface GetFeeBalancesParams { export async function getBalance({ address, config, - ethersSigner, + signer, polkadot, }: GetFeeBalancesParams) { const cfg = config.balance.build({ @@ -30,10 +29,7 @@ export async function getBalance({ return polkadot.query(cfg as SubstrateQueryConfig); } - const contract = createContract( - cfg, - ethersSigner, - ) as BalanceContractInterface; + const contract = createContract(cfg, signer) as BalanceContractInterface; return contract.getBalance(); } @@ -42,7 +38,7 @@ export async function getDecimals({ address, asset, config, - ethersSigner, + signer, polkadot, }: GetFeeBalancesParams) { const cfg = config.balance.build({ @@ -54,10 +50,7 @@ export async function getDecimals({ return polkadot.getAssetDecimals(asset || config.asset); } - const contract = createContract( - cfg, - ethersSigner, - ) as BalanceContractInterface; + const contract = createContract(cfg, signer) as BalanceContractInterface; return contract.getDecimals(); } diff --git a/packages/sdk/src/sdk.interfaces.ts b/packages/sdk/src/sdk.interfaces.ts index 22a3ec26..d084cdd3 100644 --- a/packages/sdk/src/sdk.interfaces.ts +++ b/packages/sdk/src/sdk.interfaces.ts @@ -1,12 +1,13 @@ import { AnyChain, AssetAmount } from '@moonbeam-network/xcm-types'; import type { Signer as PolkadotSigner } from '@polkadot/api/types'; import type { IKeyringPair } from '@polkadot/types/types'; -import type { Signer as EthersSigner } from 'ethers'; +import { Signer as EthersSigner } from 'ethers'; import { WalletClient } from 'viem'; +export type Signer = EthersSigner | WalletClient; + export interface Signers { - // TODO rename - ethersSigner: EthersSigner | WalletClient; + signer: Signer; polkadotSigner: PolkadotSigner | IKeyringPair; } diff --git a/packages/sdk/src/sdk.ts b/packages/sdk/src/sdk.ts index 794d45ba..529352d0 100644 --- a/packages/sdk/src/sdk.ts +++ b/packages/sdk/src/sdk.ts @@ -52,7 +52,7 @@ export function Sdk(options?: SdkOptions) { async getTransferData({ destinationAddress, destinationKeyOrChain, - ethersSigner, + signer, keyOrAsset, polkadotSigner, sourceAddress, @@ -60,7 +60,7 @@ export function Sdk(options?: SdkOptions) { }: SdkTransferParams): Promise { return gtd({ destinationAddress, - ethersSigner, + signer, polkadotSigner, sourceAddress, transferConfig: ConfigBuilder(configService) From 71c976c52b47cbaabf746f441a7b7f51fec51ca4 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Tue, 5 Sep 2023 02:16:38 +0200 Subject: [PATCH 06/17] change signer property name and update viem --- package-lock.json | 166 ++++++++---------- package.json | 6 +- packages/sdk/package.json | 2 +- packages/sdk/src/contract/contract.utils.ts | 6 +- .../src/getTransferData/getDestinationData.ts | 10 +- .../sdk/src/getTransferData/getSourceData.ts | 33 ++-- .../src/getTransferData/getTransferData.ts | 20 ++- .../getTransferData/getTransferData.utils.ts | 12 +- packages/sdk/src/sdk.interfaces.ts | 4 +- packages/sdk/src/sdk.ts | 4 +- 10 files changed, 129 insertions(+), 134 deletions(-) diff --git a/package-lock.json b/package-lock.json index ada007cc..3aaf4505 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,8 +15,7 @@ "@polkadot/api-augment": "^10.9.1", "@polkadot/types": "^10.9.1", "@polkadot/util": "^12.3.2", - "@polkadot/util-crypto": "^12.3.2", - "ethers": "^5.7.2" + "@polkadot/util-crypto": "^12.3.2" }, "devDependencies": { "@changesets/changelog-github": "^0.4.8", @@ -42,7 +41,8 @@ "ts-node": "^10.9.1", "tsup": "^6.7.0", "turbo": "^1.9.9", - "typescript": "^5.0.4" + "typescript": "^5.0.4", + "viem": "^1.10.3" } }, "examples/sdk-simple": { @@ -4985,59 +4985,58 @@ ] }, "node_modules/@scure/bip32": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/@scure/bip32/-/bip32-1.3.0.tgz", - "integrity": "sha512-bcKpo1oj54hGholplGLpqPHRbIsnbixFtc06nwuNM5/dwSXOq/AAYoIBRsBmnZJSdfeNW5rnff7NTAz3ZCqR9Q==", - "funding": [ - { - "type": "individual", - "url": "https://paulmillr.com/funding/" - } - ], + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@scure/bip32/-/bip32-1.3.2.tgz", + "integrity": "sha512-N1ZhksgwD3OBlwTv3R6KFEcPojl/W4ElJOeCZdi+vuI5QmTFwLq3OFf2zd2ROpKvxFdgZ6hUpb0dx9bVNEwYCA==", "dependencies": { - "@noble/curves": "~1.0.0", - "@noble/hashes": "~1.3.0", - "@scure/base": "~1.1.0" + "@noble/curves": "~1.2.0", + "@noble/hashes": "~1.3.2", + "@scure/base": "~1.1.2" + }, + "funding": { + "url": "https://paulmillr.com/funding/" } }, "node_modules/@scure/bip32/node_modules/@noble/curves": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.0.0.tgz", - "integrity": "sha512-2upgEu0iLiDVDZkNLeFV2+ht0BAVgQnEmCk6JsOch9Rp8xfkMCbvbAZlA2pBHQc73dbl+vFOXfqkf4uemdn0bw==", - "funding": [ - { - "type": "individual", - "url": "https://paulmillr.com/funding/" - } - ], + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.2.0.tgz", + "integrity": "sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==", "dependencies": { - "@noble/hashes": "1.3.0" + "@noble/hashes": "1.3.2" + }, + "funding": { + "url": "https://paulmillr.com/funding/" } }, "node_modules/@scure/bip32/node_modules/@noble/hashes": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.0.tgz", - "integrity": "sha512-ilHEACi9DwqJB0pw7kv+Apvh50jiiSyR/cQ3y4W7lOR5mhvn/50FLUfsnfJz0BDZtl/RR16kXvptiv6q1msYZg==", - "funding": [ - { - "type": "individual", - "url": "https://paulmillr.com/funding/" - } - ] + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.2.tgz", + "integrity": "sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==", + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/@scure/bip32/node_modules/@scure/base": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@scure/base/-/base-1.1.3.tgz", + "integrity": "sha512-/+SgoRjLq7Xlf0CWuLHq2LUZeL/w65kfzAPG5NH9pcmBhs+nunQTn4gvdwgMTIXnt9b2C/1SeL2XiysZEyIC9Q==", + "funding": { + "url": "https://paulmillr.com/funding/" + } }, "node_modules/@scure/bip39": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/@scure/bip39/-/bip39-1.2.0.tgz", - "integrity": "sha512-SX/uKq52cuxm4YFXWFaVByaSHJh2w3BnokVSeUJVCv6K7WulT9u2BuNRBhuFl8vAuYnzx9bEu9WgpcNYTrYieg==", - "funding": [ - { - "type": "individual", - "url": "https://paulmillr.com/funding/" - } - ], + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@scure/bip39/-/bip39-1.2.1.tgz", + "integrity": "sha512-Z3/Fsz1yr904dduJD0NpiyRHhRYHdcnyh73FZWiV+/qhWi83wNJ3NWolYqCEN+ZWsUz2TWwajJggcRE9r1zUYg==", "dependencies": { "@noble/hashes": "~1.3.0", "@scure/base": "~1.1.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" } }, "node_modules/@sinclair/typebox": { @@ -6214,9 +6213,9 @@ "peer": true }, "node_modules/abitype": { - "version": "0.9.3", - "resolved": "https://registry.npmjs.org/abitype/-/abitype-0.9.3.tgz", - "integrity": "sha512-dz4qCQLurx97FQhnb/EIYTk/ldQ+oafEDUqC0VVIeQS1Q48/YWt/9YNfMmp9SLFqN41ktxny3c8aYxHjmFIB/w==", + "version": "0.9.8", + "resolved": "https://registry.npmjs.org/abitype/-/abitype-0.9.8.tgz", + "integrity": "sha512-puLifILdm+8sjyss4S+fsUN09obiT1g2YW6CtcQF+QDzxR0euzgEB29MZujC6zMk2a6SVmtttq1fc6+YFA7WYQ==", "funding": [ { "type": "github", @@ -16338,19 +16337,19 @@ } }, "node_modules/viem": { - "version": "1.9.2", - "resolved": "https://registry.npmjs.org/viem/-/viem-1.9.2.tgz", - "integrity": "sha512-a4l502Sl4ytYOT6a77ezDCEEgztR+wF8ZZRLAEVcrQawFFIiAWXGPrXVPT8MQmDF00idPMKPON8Ayz34Ft3NLw==", - "dependencies": { - "@adraffy/ens-normalize": "1.9.0", - "@noble/curves": "1.1.0", - "@noble/hashes": "1.3.0", - "@scure/bip32": "1.3.0", - "@scure/bip39": "1.2.0", - "@types/ws": "^8.5.4", - "abitype": "0.9.3", + "version": "1.10.3", + "resolved": "https://registry.npmjs.org/viem/-/viem-1.10.3.tgz", + "integrity": "sha512-7L35k0JmxTa7xIx1s8lCsTLGrTEUXTYnLppZ56EVBjtvE2OKSfDjL8No0Fnedu6Ye0zi/VX2BQC3arhpKOVIlw==", + "dependencies": { + "@adraffy/ens-normalize": "1.9.4", + "@noble/curves": "1.2.0", + "@noble/hashes": "1.3.2", + "@scure/bip32": "1.3.2", + "@scure/bip39": "1.2.1", + "@types/ws": "^8.5.5", + "abitype": "0.9.8", "isomorphic-ws": "5.0.0", - "ws": "8.12.0" + "ws": "8.13.0" }, "peerDependencies": { "typescript": ">=5.0.4" @@ -16362,39 +16361,30 @@ } }, "node_modules/viem/node_modules/@adraffy/ens-normalize": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/@adraffy/ens-normalize/-/ens-normalize-1.9.0.tgz", - "integrity": "sha512-iowxq3U30sghZotgl4s/oJRci6WPBfNO5YYgk2cIOMCHr3LeGPcsZjCEr+33Q4N+oV3OABDAtA+pyvWjbvBifQ==" + "version": "1.9.4", + "resolved": "https://registry.npmjs.org/@adraffy/ens-normalize/-/ens-normalize-1.9.4.tgz", + "integrity": "sha512-UK0bHA7hh9cR39V+4gl2/NnBBjoXIxkuWAPCaY4X7fbH4L/azIi7ilWOCjMUYfpJgraLUAqkRi2BqrjME8Rynw==" }, - "node_modules/viem/node_modules/@noble/hashes": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.0.tgz", - "integrity": "sha512-ilHEACi9DwqJB0pw7kv+Apvh50jiiSyR/cQ3y4W7lOR5mhvn/50FLUfsnfJz0BDZtl/RR16kXvptiv6q1msYZg==", - "funding": [ - { - "type": "individual", - "url": "https://paulmillr.com/funding/" - } - ] + "node_modules/viem/node_modules/@noble/curves": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.2.0.tgz", + "integrity": "sha512-oYclrNgRaM9SsBUBVbb8M6DTV7ZHRTKugureoYEncY5c65HOmRzvSiTE3y5CYaPYJA/GVkrhXEoF0M3Ya9PMnw==", + "dependencies": { + "@noble/hashes": "1.3.2" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } }, - "node_modules/viem/node_modules/ws": { - "version": "8.12.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.12.0.tgz", - "integrity": "sha512-kU62emKIdKVeEIOIKVegvqpXMSTAMLJozpHZaJNDYqBjzlSYXQGviYwN1osDLJ9av68qHd4a2oSjd7yD4pacig==", + "node_modules/viem/node_modules/@noble/hashes": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.2.tgz", + "integrity": "sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==", "engines": { - "node": ">=10.0.0" - }, - "peerDependencies": { - "bufferutil": "^4.0.1", - "utf-8-validate": ">=5.0.2" + "node": ">= 16" }, - "peerDependenciesMeta": { - "bufferutil": { - "optional": true - }, - "utf-8-validate": { - "optional": true - } + "funding": { + "url": "https://paulmillr.com/funding/" } }, "node_modules/walker": { @@ -17005,7 +16995,7 @@ "@moonbeam-network/xcm-utils": "1.0.2", "big.js": "^6.2.1", "ethers": "^5.7.2", - "viem": "^1.6.0" + "viem": "^1.10.3" }, "peerDependencies": { "@polkadot/api": "^10.9.1", diff --git a/package.json b/package.json index a49299ba..32c6d66d 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,7 @@ "@polkadot/api-augment": "^10.9.1", "@polkadot/types": "^10.9.1", "@polkadot/util": "^12.3.2", - "@polkadot/util-crypto": "^12.3.2", - "ethers": "^5.7.2" + "@polkadot/util-crypto": "^12.3.2" }, "devDependencies": { "@changesets/changelog-github": "^0.4.8", @@ -65,7 +64,8 @@ "ts-node": "^10.9.1", "tsup": "^6.7.0", "turbo": "^1.9.9", - "typescript": "^5.0.4" + "typescript": "^5.0.4", + "viem": "^1.10.3" }, "lint-staged": { "*.{js,ts}": "eslint --cache --fix --max-warnings=0", diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 70fd24e9..28b89055 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -53,7 +53,7 @@ "@moonbeam-network/xcm-utils": "1.0.2", "big.js": "^6.2.1", "ethers": "^5.7.2", - "viem": "^1.6.0" + "viem": "^1.10.3" }, "peerDependencies": { "@polkadot/api": "^10.9.1", diff --git a/packages/sdk/src/contract/contract.utils.ts b/packages/sdk/src/contract/contract.utils.ts index 896d683a..0c68c8ec 100644 --- a/packages/sdk/src/contract/contract.utils.ts +++ b/packages/sdk/src/contract/contract.utils.ts @@ -1,17 +1,17 @@ import { Signer as EthersSigner } from 'ethers'; import { WalletClient } from 'viem'; -import { Signer } from '../sdk.interfaces'; +import { EvmSigner } from '../sdk.interfaces'; import { ContractClient, EthersClient, ViemClient, } from './contract.interfaces'; -export function isEthersSigner(signer: Signer): signer is EthersSigner { +export function isEthersSigner(signer: EvmSigner): signer is EthersSigner { return 'provider' in signer; } -export function isWalletClient(signer: Signer): signer is WalletClient { +export function isWalletClient(signer: EvmSigner): signer is WalletClient { return 'chain' in signer; } diff --git a/packages/sdk/src/getTransferData/getDestinationData.ts b/packages/sdk/src/getTransferData/getDestinationData.ts index f48373e2..076792b0 100644 --- a/packages/sdk/src/getTransferData/getDestinationData.ts +++ b/packages/sdk/src/getTransferData/getDestinationData.ts @@ -4,20 +4,20 @@ import { TransferConfig } from '@moonbeam-network/xcm-config'; import { AssetAmount } from '@moonbeam-network/xcm-types'; import { toBigInt } from '@moonbeam-network/xcm-utils'; import { PolkadotService } from '../polkadot'; -import { DestinationChainTransferData, Signer } from '../sdk.interfaces'; +import { DestinationChainTransferData, EvmSigner } from '../sdk.interfaces'; import { getBalance, getDecimals, getMin } from './getTransferData.utils'; export interface GetDestinationDataParams { transferConfig: TransferConfig; destinationAddress: string; - signer: Signer; + evmSigner: EvmSigner; polkadot: PolkadotService; } export async function getDestinationData({ transferConfig, destinationAddress, - signer, + evmSigner, polkadot, }: GetDestinationDataParams): Promise { const { @@ -30,16 +30,16 @@ export async function getDestinationData({ decimals: await getDecimals({ address: destinationAddress, config, + evmSigner, polkadot, - signer, }), }); const balance = await getBalance({ address: destinationAddress, config, + evmSigner, polkadot, - signer, }); const min = await getMin(config, polkadot); diff --git a/packages/sdk/src/getTransferData/getSourceData.ts b/packages/sdk/src/getTransferData/getSourceData.ts index 6ac99fc5..3e9b2eb6 100644 --- a/packages/sdk/src/getTransferData/getSourceData.ts +++ b/packages/sdk/src/getTransferData/getSourceData.ts @@ -10,14 +10,14 @@ import { convertDecimals } from '@moonbeam-network/xcm-utils'; import Big from 'big.js'; import { TransferContractInterface, createContract } from '../contract'; import { PolkadotService } from '../polkadot'; -import { Signer, SourceChainTransferData } from '../sdk.interfaces'; +import { EvmSigner, SourceChainTransferData } from '../sdk.interfaces'; import { getBalance, getDecimals, getMin } from './getTransferData.utils'; export interface GetSourceDataParams { transferConfig: TransferConfig; destinationAddress: string; destinationFee: AssetAmount; - signer: Signer; + evmSigner: EvmSigner; polkadot: PolkadotService; sourceAddress: string; } @@ -26,7 +26,7 @@ export async function getSourceData({ transferConfig, destinationAddress, destinationFee, - signer, + evmSigner, polkadot, sourceAddress, }: GetSourceDataParams): Promise { @@ -40,8 +40,8 @@ export async function getSourceData({ decimals: await getDecimals({ address: destinationAddress, config, + evmSigner, polkadot, - signer, }), }); const zeroFeeAmount = config.fee?.asset @@ -51,8 +51,8 @@ export async function getSourceData({ address: destinationAddress, asset: config.fee.asset, config, + evmSigner, polkadot, - signer, }), }) : zeroAmount; @@ -63,8 +63,8 @@ export async function getSourceData({ address: destinationAddress, asset: config.destinationFee.asset, config, + evmSigner, polkadot, - signer, }), }) : zeroAmount; @@ -72,8 +72,8 @@ export async function getSourceData({ const balance = await getBalance({ address: sourceAddress, config, + evmSigner, polkadot, - signer, }); const feeBalance = await getFeeBalances({ @@ -119,9 +119,9 @@ export async function getSourceData({ balance, contract, decimals: zeroFeeAmount.decimals, + evmSigner, extrinsic, polkadot, - signer, sourceAddress, }); @@ -179,7 +179,7 @@ export interface GetFeeParams { balance: bigint; contract?: ContractConfig; decimals: number; - signer?: Signer; + evmSigner?: EvmSigner; extrinsic?: ExtrinsicConfig; polkadot: PolkadotService; sourceAddress: string; @@ -189,17 +189,17 @@ export async function getFee({ balance, contract, decimals, - signer, + evmSigner, extrinsic, polkadot, sourceAddress, }: GetFeeParams): Promise { if (contract) { - if (!signer) { - throw new Error('Ethers signer must be provided'); + if (!evmSigner) { + throw new Error('EVM Signer must be provided'); } - return getContractFee(balance, contract, decimals, signer); + return getContractFee(balance, contract, decimals, evmSigner); } if (extrinsic) { @@ -213,9 +213,12 @@ export async function getContractFee( balance: bigint, config: ContractConfig, decimals: number, - signer: Signer, + evmSigner: EvmSigner, ): Promise { - const contract = createContract(config, signer) as TransferContractInterface; + const contract = createContract( + config, + evmSigner, + ) as TransferContractInterface; const fee = await contract.getFee(balance); return convertDecimals(fee, 18, decimals); diff --git a/packages/sdk/src/getTransferData/getTransferData.ts b/packages/sdk/src/getTransferData/getTransferData.ts index 8b263604..90b49456 100644 --- a/packages/sdk/src/getTransferData/getTransferData.ts +++ b/packages/sdk/src/getTransferData/getTransferData.ts @@ -22,13 +22,13 @@ export interface GetTransferDataParams extends Partial { export async function getTransferData({ destinationAddress, - signer, + evmSigner, polkadotSigner, sourceAddress, transferConfig, }: GetTransferDataParams): Promise { - if (!signer) { - throw new Error('Ethers signer must be provided'); + if (!evmSigner) { + throw new Error('EVM Signer must be provided'); } const [destPolkadot, srcPolkadot] = await PolkadotService.createMulti([ @@ -38,8 +38,8 @@ export async function getTransferData({ const destination = await getDestinationData({ destinationAddress, + evmSigner, polkadot: destPolkadot, - signer, transferConfig, }); @@ -51,8 +51,8 @@ export async function getTransferData({ const source = await getSourceData({ destinationAddress, destinationFee, + evmSigner, polkadot: srcPolkadot, - signer, sourceAddress, transferConfig, }); @@ -83,8 +83,8 @@ export async function getTransferData({ async swap() { return getTransferData({ destinationAddress: sourceAddress, + evmSigner, polkadotSigner, - signer, sourceAddress: destinationAddress, transferConfig: { ...transferConfig, @@ -120,11 +120,13 @@ export async function getTransferData({ }); if (contract) { - if (!signer) { - throw new Error('Ethers signer must be provided'); + if (!evmSigner) { + throw new Error('EVM Signer must be provided'); } - return (createContract(contract, signer) as TransferContractInterface) + return ( + createContract(contract, evmSigner) as TransferContractInterface + ) .transfer() .then((tx) => typeof tx === 'object' ? (tx as TransactionResponse).hash : tx, diff --git a/packages/sdk/src/getTransferData/getTransferData.utils.ts b/packages/sdk/src/getTransferData/getTransferData.utils.ts index 56babffe..b1af6a52 100644 --- a/packages/sdk/src/getTransferData/getTransferData.utils.ts +++ b/packages/sdk/src/getTransferData/getTransferData.utils.ts @@ -4,12 +4,12 @@ import { Asset } from '@moonbeam-network/xcm-types'; import { toBigInt } from '@moonbeam-network/xcm-utils'; import { BalanceContractInterface, createContract } from '../contract'; import { PolkadotService } from '../polkadot'; -import { Signer } from '../sdk.interfaces'; +import { EvmSigner } from '../sdk.interfaces'; export interface GetFeeBalancesParams { address: string; config: AssetConfig; - signer: Signer; + evmSigner: EvmSigner; polkadot: PolkadotService; asset?: Asset; } @@ -17,7 +17,7 @@ export interface GetFeeBalancesParams { export async function getBalance({ address, config, - signer, + evmSigner, polkadot, }: GetFeeBalancesParams) { const cfg = config.balance.build({ @@ -29,7 +29,7 @@ export async function getBalance({ return polkadot.query(cfg as SubstrateQueryConfig); } - const contract = createContract(cfg, signer) as BalanceContractInterface; + const contract = createContract(cfg, evmSigner) as BalanceContractInterface; return contract.getBalance(); } @@ -38,7 +38,7 @@ export async function getDecimals({ address, asset, config, - signer, + evmSigner, polkadot, }: GetFeeBalancesParams) { const cfg = config.balance.build({ @@ -50,7 +50,7 @@ export async function getDecimals({ return polkadot.getAssetDecimals(asset || config.asset); } - const contract = createContract(cfg, signer) as BalanceContractInterface; + const contract = createContract(cfg, evmSigner) as BalanceContractInterface; return contract.getDecimals(); } diff --git a/packages/sdk/src/sdk.interfaces.ts b/packages/sdk/src/sdk.interfaces.ts index d084cdd3..4618a5bd 100644 --- a/packages/sdk/src/sdk.interfaces.ts +++ b/packages/sdk/src/sdk.interfaces.ts @@ -4,10 +4,10 @@ import type { IKeyringPair } from '@polkadot/types/types'; import { Signer as EthersSigner } from 'ethers'; import { WalletClient } from 'viem'; -export type Signer = EthersSigner | WalletClient; +export type EvmSigner = EthersSigner | WalletClient; export interface Signers { - signer: Signer; + evmSigner: EvmSigner; polkadotSigner: PolkadotSigner | IKeyringPair; } diff --git a/packages/sdk/src/sdk.ts b/packages/sdk/src/sdk.ts index 529352d0..4c944341 100644 --- a/packages/sdk/src/sdk.ts +++ b/packages/sdk/src/sdk.ts @@ -52,7 +52,7 @@ export function Sdk(options?: SdkOptions) { async getTransferData({ destinationAddress, destinationKeyOrChain, - signer, + evmSigner, keyOrAsset, polkadotSigner, sourceAddress, @@ -60,7 +60,7 @@ export function Sdk(options?: SdkOptions) { }: SdkTransferParams): Promise { return gtd({ destinationAddress, - signer, + evmSigner, polkadotSigner, sourceAddress, transferConfig: ConfigBuilder(configService) From be94e2d31a4aa067ce6940cf4fe3f05db3981e13 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Tue, 5 Sep 2023 02:24:49 +0200 Subject: [PATCH 07/17] fix example --- examples/sdk-simple/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/sdk-simple/index.ts b/examples/sdk-simple/index.ts index 5d9cf2b1..087c16d1 100644 --- a/examples/sdk-simple/index.ts +++ b/examples/sdk-simple/index.ts @@ -13,7 +13,7 @@ const provider = new ethers.providers.WebSocketProvider(moonbeam.ws, { chainId: moonbeam.id, name: moonbeam.name, }); -const signer = new ethers.Wallet(moonbeamPrivateKey, provider); +const evmSigner = new ethers.Wallet(moonbeamPrivateKey, provider); // Polkadot Signer =========================================================== @@ -65,7 +65,7 @@ export async function fromPolkadot() { console.log('\nTransfer from Polkadot to Moonbeam\n'); const data = await Sdk().getTransferData({ - destinationAddress: signer.address, + destinationAddress: evmSigner.address, destinationKeyOrChain: moonbeam, keyOrAsset: dot, polkadotSigner: pair, @@ -93,8 +93,8 @@ export async function fromMoonbeam() { .asset(dot) .source(moonbeam) .destination(polkadot) - .accounts(signer.address, pair.address, { - signer, + .accounts(evmSigner.address, pair.address, { + evmSigner, }); logBalances(data); @@ -114,7 +114,7 @@ async function main() { console.warn = () => null; console.clear(); - console.log(`\nMoonbeam address: ${signer.address}.`); + console.log(`\nMoonbeam address: ${evmSigner.address}.`); console.log(`Polkadot address: ${pair.address}.`); await fromPolkadot(); From 1ce9cde4d91db976cd4bcef9682574d9721a02dd Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Tue, 5 Sep 2023 03:33:38 +0200 Subject: [PATCH 08/17] change Readme property --- packages/sdk/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/sdk/README.md b/packages/sdk/README.md index e8728c7a..136a0a66 100644 --- a/packages/sdk/README.md +++ b/packages/sdk/README.md @@ -47,7 +47,7 @@ const dataViaAssetsMethod = await assets() .source('INSERT_SOURCE_CHAIN') .destination('INSERT_DESTINATION_CHAIN') .accounts('INSERT_SOURCE_ADDRESS', 'INSERT_DESTINATION_ADDRESS', { - signer?: 'INSERT_ETHERS_SIGNER', + evmSigner?: 'INSERT_EVM_SIGNER', polkadotSigner?: 'INSERT_POLKADOT_SIGNER', }); @@ -55,7 +55,7 @@ const dataViaAssetsMethod = await assets() const dataViaGetTransferDataMethod = await getTransferData({ destinationAddress: 'INSERT_DESTINATION_ADDRESS', destinationKeyOrChain: 'INSERT_DESTINATION_CHAIN', - signer?: 'INSERT_ETHERS_SIGNER', + evmSigner?: 'INSERT_EVM_SIGNER', keyOrAsset: 'INSERT_ASSET', polkadotSigner?: 'INSERT_POLKADOT_SIGNER', sourceAddress: 'INSERT_SOURCE_ADDRESS', From 7685a7a860b5654f7ea191b93aefc2c4bed56de1 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Tue, 5 Sep 2023 16:03:53 +0200 Subject: [PATCH 09/17] accept ethersSigner --- packages/sdk/src/sdk.interfaces.ts | 3 ++- packages/sdk/src/sdk.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/sdk/src/sdk.interfaces.ts b/packages/sdk/src/sdk.interfaces.ts index 4618a5bd..33377d9a 100644 --- a/packages/sdk/src/sdk.interfaces.ts +++ b/packages/sdk/src/sdk.interfaces.ts @@ -7,7 +7,8 @@ import { WalletClient } from 'viem'; export type EvmSigner = EthersSigner | WalletClient; export interface Signers { - evmSigner: EvmSigner; + ethersSigner?: EthersSigner; + evmSigner?: EvmSigner; polkadotSigner: PolkadotSigner | IKeyringPair; } diff --git a/packages/sdk/src/sdk.ts b/packages/sdk/src/sdk.ts index 4c944341..c7f0d0c1 100644 --- a/packages/sdk/src/sdk.ts +++ b/packages/sdk/src/sdk.ts @@ -52,6 +52,7 @@ export function Sdk(options?: SdkOptions) { async getTransferData({ destinationAddress, destinationKeyOrChain, + ethersSigner, evmSigner, keyOrAsset, polkadotSigner, @@ -60,7 +61,7 @@ export function Sdk(options?: SdkOptions) { }: SdkTransferParams): Promise { return gtd({ destinationAddress, - evmSigner, + evmSigner: evmSigner ?? ethersSigner, polkadotSigner, sourceAddress, transferConfig: ConfigBuilder(configService) From cdbdf67e2c9d56a0d07dde718265708ed1d9bb8f Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Tue, 5 Sep 2023 16:18:00 +0200 Subject: [PATCH 10/17] allow ethers signer as input --- packages/sdk/src/sdk.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/sdk/src/sdk.ts b/packages/sdk/src/sdk.ts index c7f0d0c1..6969c4d1 100644 --- a/packages/sdk/src/sdk.ts +++ b/packages/sdk/src/sdk.ts @@ -35,10 +35,11 @@ export function Sdk(options?: SdkOptions) { ): Promise { return gtd({ ...options, - ...signers, destinationAddress, + evmSigner: signers?.evmSigner ?? signers?.ethersSigner, sourceAddress, transferConfig: destination(destKeyOrChain).build(), + polkadotSigner: signers?.polkadotSigner, }); }, }; From 7b52e15705f88c6bb47d9ecbca71ee0a3227baeb Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Tue, 5 Sep 2023 16:45:24 +0200 Subject: [PATCH 11/17] add changeset --- .changeset/four-gifts-try.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/four-gifts-try.md diff --git a/.changeset/four-gifts-try.md b/.changeset/four-gifts-try.md new file mode 100644 index 00000000..3ff6c667 --- /dev/null +++ b/.changeset/four-gifts-try.md @@ -0,0 +1,6 @@ +--- +'@moonbeam-network/xcm-config': minor +'@moonbeam-network/xcm-sdk': minor +--- + +Allow viem Wallet Client as signer From 66dfa720f65b3b3f7fffbd59fe16d2778597c2ab Mon Sep 17 00:00:00 2001 From: mmaurello <93129175+mmaurello@users.noreply.github.com> Date: Wed, 6 Sep 2023 18:50:49 +0200 Subject: [PATCH 12/17] Update packages/sdk/src/sdk.interfaces.ts Co-authored-by: elmar --- packages/sdk/src/sdk.interfaces.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/sdk/src/sdk.interfaces.ts b/packages/sdk/src/sdk.interfaces.ts index 33377d9a..b68aea34 100644 --- a/packages/sdk/src/sdk.interfaces.ts +++ b/packages/sdk/src/sdk.interfaces.ts @@ -7,6 +7,9 @@ import { WalletClient } from 'viem'; export type EvmSigner = EthersSigner | WalletClient; export interface Signers { + /** + * @deprecated ethersSigner - is deprecated and will be removed in v2, use evmSigner instead + */ ethersSigner?: EthersSigner; evmSigner?: EvmSigner; polkadotSigner: PolkadotSigner | IKeyringPair; From 881aaaf1f0e2589023b0c004caa102958ccf142d Mon Sep 17 00:00:00 2001 From: mmaurello <93129175+mmaurello@users.noreply.github.com> Date: Wed, 6 Sep 2023 18:52:58 +0200 Subject: [PATCH 13/17] Update packages/sdk/src/contract/contracts/Erc20/Erc20.ts Co-authored-by: elmar --- packages/sdk/src/contract/contracts/Erc20/Erc20.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts index 9f9f4f4f..f0256e9d 100644 --- a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts +++ b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts @@ -15,7 +15,7 @@ export class Erc20 implements BalanceContractInterface { readonly #client: ContractClient; - constructor(config: ContractConfig, signer: Signer | WalletClient) { + constructor(config: ContractConfig, signer: EvmSigner) { if (!config.address) { throw new Error('Contract address is required'); } From 7231b1cf23d782ff2832be6c8f1006694a623fb3 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Thu, 7 Sep 2023 02:10:48 +0200 Subject: [PATCH 14/17] implement viem contract instance --- packages/sdk/src/contract/contract.utils.ts | 19 ++--- .../sdk/src/contract/contracts/Erc20/Erc20.ts | 64 +++++++------- .../src/contract/contracts/Xtokens/Xtokens.ts | 83 ++++++++++--------- 3 files changed, 83 insertions(+), 83 deletions(-) diff --git a/packages/sdk/src/contract/contract.utils.ts b/packages/sdk/src/contract/contract.utils.ts index 0c68c8ec..3a275b74 100644 --- a/packages/sdk/src/contract/contract.utils.ts +++ b/packages/sdk/src/contract/contract.utils.ts @@ -1,11 +1,6 @@ -import { Signer as EthersSigner } from 'ethers'; -import { WalletClient } from 'viem'; +import { Contract, Signer as EthersSigner } from 'ethers'; +import { Abi, GetContractReturnType, WalletClient } from 'viem'; import { EvmSigner } from '../sdk.interfaces'; -import { - ContractClient, - EthersClient, - ViemClient, -} from './contract.interfaces'; export function isEthersSigner(signer: EvmSigner): signer is EthersSigner { return 'provider' in signer; @@ -15,10 +10,8 @@ export function isWalletClient(signer: EvmSigner): signer is WalletClient { return 'chain' in signer; } -export function isEthersClient(client: ContractClient): client is EthersClient { - return 'contract' in client; -} - -export function isViemClient(client: ContractClient): client is ViemClient { - return 'publicClient' in client; +export function isEthersContract( + contract: Contract | GetContractReturnType, +): contract is Contract { + return 'signer' in contract; } diff --git a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts index f0256e9d..a7ddc7dd 100644 --- a/packages/sdk/src/contract/contracts/Erc20/Erc20.ts +++ b/packages/sdk/src/contract/contracts/Erc20/Erc20.ts @@ -1,19 +1,30 @@ import { ContractConfig } from '@moonbeam-network/xcm-builder'; -import { Contract, Signer } from 'ethers'; -import { WalletClient, createPublicClient, http } from 'viem'; +import { Contract } from 'ethers'; import { - BalanceContractInterface, - ContractClient, -} from '../../contract.interfaces'; -import { isEthersClient, isEthersSigner } from '../../contract.utils'; + GetContractReturnType, + PublicClient, + WalletClient, + createPublicClient, + getContract, + http, +} from 'viem'; +import { EvmSigner } from '../../../sdk.interfaces'; +import { BalanceContractInterface } from '../../contract.interfaces'; +import { isEthersContract, isEthersSigner } from '../../contract.utils'; import abi from './Erc20ABI.json'; +type Erc20Contract = GetContractReturnType< + typeof abi, + PublicClient, + WalletClient +>; + export class Erc20 implements BalanceContractInterface { readonly address: string; readonly #config: ContractConfig; - readonly #client: ContractClient; + readonly #contract: Contract | Erc20Contract; constructor(config: ContractConfig, signer: EvmSigner) { if (!config.address) { @@ -22,44 +33,33 @@ export class Erc20 implements BalanceContractInterface { this.address = config.address; this.#config = config; - this.#client = isEthersSigner(signer) - ? { contract: new Contract(this.address, abi, signer), signer } - : { + this.#contract = isEthersSigner(signer) + ? new Contract(this.address, abi, signer) + : getContract({ + abi, + address: this.address as `0x${string}`, publicClient: createPublicClient({ chain: signer.chain, transport: http(), }), walletClient: signer, - }; + }); } async getBalance(): Promise { - if (isEthersClient(this.#client)) { - const balance = await this.#client.contract.balanceOf( - ...this.#config.args, - ); + if (isEthersContract(this.#contract)) { + const balance = await this.#contract.balanceOf(...this.#config.args); return balance.toBigInt(); } - - return (await this.#client.publicClient.readContract({ - abi, - account: this.#client.walletClient.account, - address: this.address as `0x${string}`, - args: this.#config.args, - functionName: 'balanceOf', - })) as bigint; + return this.#contract.read.balanceOf( + this.#config.args, + ) as unknown as bigint; } async getDecimals(): Promise { - const decimals = isEthersClient(this.#client) - ? await this.#client.contract.decimals() - : this.#client.publicClient.readContract({ - abi, - account: this.#client.walletClient.account, - address: this.address as `0x${string}`, - args: [], - functionName: 'decimals', - }); + const decimals = isEthersContract(this.#contract) + ? await this.#contract.decimals() + : this.#contract.read.decimals(); return decimals; } diff --git a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts index f0c33aee..fb93eea2 100644 --- a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts +++ b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts @@ -1,54 +1,59 @@ import type { TransactionResponse } from '@ethersproject/abstract-provider'; import { ContractConfig } from '@moonbeam-network/xcm-builder'; -import { Contract, Signer } from 'ethers'; +import { Contract } from 'ethers'; import { + GetContractReturnType, + PublicClient, WalletClient, WriteContractReturnType, createPublicClient, + getContract, http, } from 'viem'; -import { - ContractClient, - TransferContractInterface, - ViemClient, -} from '../../contract.interfaces'; -import { isEthersClient, isEthersSigner } from '../../contract.utils'; +import { EvmSigner } from '../../../sdk.interfaces'; +import { TransferContractInterface } from '../../contract.interfaces'; +import { isEthersContract, isEthersSigner } from '../../contract.utils'; import abi from './XtokensABI.json'; +type XtokensContract = GetContractReturnType< + typeof abi, + PublicClient, + WalletClient +>; + export class Xtokens implements TransferContractInterface { readonly address = '0x0000000000000000000000000000000000000804'; readonly #config: ContractConfig; - readonly #client: ContractClient; + readonly #contract: Contract | XtokensContract; + + readonly #signer: EvmSigner; - constructor(config: ContractConfig, signer: Signer | WalletClient) { + constructor(config: ContractConfig, signer: EvmSigner) { this.#config = config; - this.#client = isEthersSigner(signer) - ? { contract: new Contract(this.address, abi, signer), signer } - : { + this.#signer = signer; + + this.#contract = isEthersSigner(signer) + ? new Contract(this.address, abi, signer) + : getContract({ + abi, + address: this.address, publicClient: createPublicClient({ chain: signer.chain, transport: http(), }), walletClient: signer, - }; + }); } async transfer(): Promise { - if (isEthersClient(this.#client)) { - return this.#client.contract[this.#config.func](...this.#config.args); + if (isEthersContract(this.#contract)) { + return this.#contract[this.#config.func](...this.#config.args); } - const { request } = await this.#client.publicClient.simulateContract({ - abi, - account: this.#client.walletClient.account, - address: this.address, - args: this.#config.args, - functionName: this.#config.func, - }); - return this.#client.walletClient.writeContract(request); + return this.#contract.write[this.#config.func](...this.#config.args); } async getEthersContractEstimatedGas(contract: Contract): Promise { @@ -57,17 +62,14 @@ export class Xtokens implements TransferContractInterface { ).toBigInt(); } - async getClientEstimatedGas(client: ViemClient): Promise { - if (!client.walletClient.account) { + async getViemContractEstimatedGas( + contract: XtokensContract, + ): Promise { + if (!contract) { return 0n; } - return client.publicClient.estimateContractGas({ - abi, - account: client.walletClient.account, - address: this.address, - args: this.#config.args, - functionName: this.#config.func, - }); + + return contract.estimateGas[this.#config.func](this.#config.args as any); } async getFee(amount: bigint): Promise { @@ -80,9 +82,9 @@ export class Xtokens implements TransferContractInterface { * Or if you try to send 0 as amount. */ try { - const estimatedGas = isEthersClient(this.#client) - ? await this.getEthersContractEstimatedGas(this.#client.contract) - : await this.getClientEstimatedGas(this.#client); + const estimatedGas = isEthersContract(this.#contract) + ? await this.getEthersContractEstimatedGas(this.#contract) + : await this.getViemContractEstimatedGas(this.#contract); const gasPrice = await this.getGasPrice(); return estimatedGas * gasPrice; } catch (error) { @@ -96,15 +98,20 @@ export class Xtokens implements TransferContractInterface { } private async getGasPrice() { - if (isEthersClient(this.#client)) { + if (isEthersSigner(this.#signer)) { const { gasPrice, maxPriorityFeePerGas } = - await this.#client.signer.getFeeData(); + await this.#signer.getFeeData(); return ( (gasPrice?.toBigInt() || 0n) + (maxPriorityFeePerGas?.toBigInt() || 0n) ); } - return this.#client.publicClient.getGasPrice(); + const publicClient = createPublicClient({ + chain: this.#signer.chain, + transport: http(), + }); + + return publicClient.getGasPrice(); } } From a6553da9cc9883a395f8a10e454d2675047c34f3 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Thu, 7 Sep 2023 02:22:46 +0200 Subject: [PATCH 15/17] fix args --- packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts index fb93eea2..3e4eee0a 100644 --- a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts +++ b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts @@ -53,7 +53,7 @@ export class Xtokens implements TransferContractInterface { return this.#contract[this.#config.func](...this.#config.args); } - return this.#contract.write[this.#config.func](...this.#config.args); + return this.#contract.write[this.#config.func](this.#config.args); } async getEthersContractEstimatedGas(contract: Contract): Promise { From 0fec36e8aa41d8fb794e664995cadc935a1f97a8 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Thu, 7 Sep 2023 02:27:23 +0200 Subject: [PATCH 16/17] fix type --- packages/sdk/src/contract/contract.factory.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/sdk/src/contract/contract.factory.ts b/packages/sdk/src/contract/contract.factory.ts index 0854d63e..a90a76d7 100644 --- a/packages/sdk/src/contract/contract.factory.ts +++ b/packages/sdk/src/contract/contract.factory.ts @@ -1,6 +1,5 @@ import { ContractConfig } from '@moonbeam-network/xcm-builder'; -import { Signer } from 'ethers'; -import { WalletClient } from 'viem'; +import { EvmSigner } from '../sdk.interfaces'; import { BalanceContractInterface, TransferContractInterface, @@ -9,7 +8,7 @@ import { Erc20, Xtokens } from './contracts'; export function createContract( config: ContractConfig, - signer: Signer | WalletClient, + signer: EvmSigner, ): TransferContractInterface | BalanceContractInterface { if (config.module === 'Erc20') { return new Erc20(config, signer); From a8d16965273784ab57a0684f75adebfbf0737751 Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Thu, 7 Sep 2023 03:25:09 +0200 Subject: [PATCH 17/17] put ethers and viem as peer dependencies --- package-lock.json | 8 ++++---- packages/sdk/package.json | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3aaf4505..81f900fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16993,15 +16993,15 @@ "@moonbeam-network/xcm-config": "1.2.0", "@moonbeam-network/xcm-types": "1.0.1", "@moonbeam-network/xcm-utils": "1.0.2", - "big.js": "^6.2.1", - "ethers": "^5.7.2", - "viem": "^1.10.3" + "big.js": "^6.2.1" }, "peerDependencies": { "@polkadot/api": "^10.9.1", "@polkadot/api-augment": "^10.9.1", "@polkadot/types": "^10.9.1", - "@polkadot/util": "^12.3.2" + "@polkadot/util": "^12.3.2", + "ethers": "^5.7.2", + "viem": "^1.10.3" } }, "packages/types": { diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 28b89055..3b3954b8 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -51,14 +51,14 @@ "@moonbeam-network/xcm-config": "1.2.0", "@moonbeam-network/xcm-types": "1.0.1", "@moonbeam-network/xcm-utils": "1.0.2", - "big.js": "^6.2.1", - "ethers": "^5.7.2", - "viem": "^1.10.3" + "big.js": "^6.2.1" }, "peerDependencies": { "@polkadot/api": "^10.9.1", "@polkadot/api-augment": "^10.9.1", "@polkadot/types": "^10.9.1", - "@polkadot/util": "^12.3.2" + "@polkadot/util": "^12.3.2", + "ethers": "^5.7.2", + "viem": "^1.10.3" } }