Skip to content

Commit b44a986

Browse files
committed
Fixing signer issue for getFee
1 parent 13c0655 commit b44a986

File tree

5 files changed

+27
-34
lines changed

5 files changed

+27
-34
lines changed

packages/sdk/src/contract/contract.factory.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ContractConfig } from '@moonbeam-network/xcm-builder';
22
import { AnyChain } from '@moonbeam-network/xcm-types';
3-
import { EvmSigner } from '../sdk.interfaces';
43
import {
54
BalanceContractInterface,
65
TransferContractInterface,
@@ -10,21 +9,20 @@ import { Erc20, Xtokens } from './contracts';
109
export function createContract(
1110
chain: AnyChain,
1211
config: ContractConfig,
13-
signer?: EvmSigner,
1412
): TransferContractInterface | BalanceContractInterface {
15-
if (config.module === 'Erc20' && chain.isEvmParachain()) {
13+
if (!chain.isEvmParachain()) {
14+
throw new Error('Chain is not an EVM parachain');
15+
}
16+
17+
if (config.module === 'Erc20') {
1618
return new Erc20(config, chain);
1719
}
1820

1921
if (config.module === 'Xtokens') {
20-
if (!signer) {
21-
throw new Error('Signer is required for Xtokens contract');
22-
}
23-
2422
const address =
2523
'contracts' in chain ? chain?.contracts?.Xtokens : undefined;
2624

27-
return new Xtokens(config, signer, address);
25+
return new Xtokens(config, chain, address);
2826
}
2927

3028
throw new Error(`Contract ${config.module} not found`);

packages/sdk/src/contract/contract.interfaces.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Hash, PublicClient, WalletClient } from 'viem';
2+
import { EvmSigner } from '../sdk.interfaces';
23

34
export interface BaseContractInterface {
45
readonly address: string;
56
}
67

78
export interface TransferContractInterface extends BaseContractInterface {
8-
transfer(): Promise<Hash>;
9-
getFee(amount: bigint): Promise<bigint>;
9+
transfer(signer: EvmSigner): Promise<Hash>;
10+
getFee(amount: bigint, address: string): Promise<bigint>;
1011
}
1112

1213
export interface BalanceContractInterface extends BaseContractInterface {

packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { ContractConfig } from '@moonbeam-network/xcm-builder';
2-
import {
3-
Address,
4-
Hash,
5-
PublicClient,
6-
WalletClient,
7-
createPublicClient,
8-
http,
9-
} from 'viem';
2+
import { Address, Hash, PublicClient, createPublicClient, http } from 'viem';
3+
import { EvmParachain } from '@moonbeam-network/xcm-types';
104
import { EvmSigner } from '../../../sdk.interfaces';
115
import { TransferContractInterface } from '../../contract.interfaces';
126
import { XTOKENS_ABI } from './XtokensABI';
@@ -15,39 +9,36 @@ export class Xtokens implements TransferContractInterface {
159
readonly defaultMoonChainAddress =
1610
'0x0000000000000000000000000000000000000804';
1711

18-
readonly address: Address;
12+
readonly address: string;
1913

2014
readonly #config: ContractConfig;
2115

2216
readonly #publicClient: PublicClient;
2317

24-
readonly #walletClient: WalletClient;
25-
26-
constructor(config: ContractConfig, signer: EvmSigner, address?: Address) {
18+
constructor(config: ContractConfig, chain: EvmParachain, address?: string) {
2719
this.#config = config;
2820
this.address = address ?? this.defaultMoonChainAddress;
29-
this.#walletClient = signer;
3021
this.#publicClient = createPublicClient({
31-
chain: signer.chain,
22+
chain: chain.getViemChain(),
3223
transport: http(),
3324
});
3425
}
3526

36-
async transfer(): Promise<Hash> {
27+
async transfer(signer: EvmSigner): Promise<Hash> {
3728
const { request } = await this.#publicClient.simulateContract({
3829
abi: XTOKENS_ABI,
39-
account: this.#walletClient.account,
40-
address: this.address,
30+
account: signer.account,
31+
address: this.address as Address,
4132
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4233
args: this.#config.args as any,
4334
functionName: this.#config.func as 'transfer',
4435
});
45-
const hash = await this.#walletClient.writeContract(request);
36+
const hash = await signer.writeContract(request);
4637

4738
return hash;
4839
}
4940

50-
async getFee(amount: bigint): Promise<bigint> {
41+
async getFee(amount: bigint, address: string): Promise<bigint> {
5142
if (amount === 0n) {
5243
return 0n;
5344
}
@@ -59,8 +50,8 @@ export class Xtokens implements TransferContractInterface {
5950
try {
6051
const gas = await this.#publicClient.estimateContractGas({
6152
abi: XTOKENS_ABI,
62-
account: this.#walletClient.account,
63-
address: this.address,
53+
account: address as Address,
54+
address: this.address as Address,
6455
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6556
args: this.#config.args as any,
6657
functionName: this.#config.func as 'transfer',

packages/sdk/src/getTransferData/getSourceData.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export async function getFee({
295295
}
296296

297297
return getContractFee({
298+
address: sourceAddress,
298299
balance,
299300
chain: chain as EvmParachain,
300301
config: contract,
@@ -323,18 +324,20 @@ export async function getFee({
323324
}
324325

325326
export async function getContractFee({
327+
address,
326328
balance,
327329
config,
328330
decimals,
329331
chain,
330332
}: {
333+
address: string;
331334
balance: bigint;
332335
config: ContractConfig;
333336
decimals: number;
334337
chain: EvmParachain;
335338
}): Promise<bigint> {
336339
const contract = createContract(chain, config) as TransferContractInterface;
337-
const fee = await contract.getFee(balance);
340+
const fee = await contract.getFee(balance, address);
338341

339342
return convertDecimals(fee, 18, decimals);
340343
}

packages/sdk/src/getTransferData/getTransferData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ export async function getTransferData({
123123
}
124124

125125
return (
126-
createContract(chain, contract, signer) as TransferContractInterface
127-
).transfer();
126+
createContract(chain, contract) as TransferContractInterface
127+
).transfer(signer);
128128
}
129129

130130
if (extrinsic) {

0 commit comments

Comments
 (0)