From af927214884f4ceb21524f773c46ab436d6e9d0b Mon Sep 17 00:00:00 2001 From: Richard Kenigs Date: Wed, 29 May 2024 07:10:05 +0200 Subject: [PATCH] --wip-- [skip ci] --- packages/config/src/chains.ts | 3 ++ packages/sdk/src/contract/contract.factory.ts | 25 +++++++++++-- .../src/contract/contracts/Xtokens/Xtokens.ts | 10 ++++-- .../sdk/src/getTransferData/getSourceData.ts | 36 ++++++++++++++----- .../src/getTransferData/getTransferData.ts | 16 ++++++++- .../types/src/chain/parachain/EvmParachain.ts | 11 +++++- 6 files changed, 87 insertions(+), 14 deletions(-) diff --git a/packages/config/src/chains.ts b/packages/config/src/chains.ts index 024f385a..bbd688aa 100644 --- a/packages/config/src/chains.ts +++ b/packages/config/src/chains.ts @@ -1279,6 +1279,9 @@ export const peaqEvmAlphanet = new EvmParachain({ metadataId: 0, }, ], + contracts: { + Xtokens: '0x0000000000000000000000000000000000000803', + }, ecosystem: Ecosystem.AlphanetRelay, genesisHash: '0x2dfcd5c560f6db1667cbc2bc3791dfd337f88f400af6de39b1b8638ee7af6ed4', diff --git a/packages/sdk/src/contract/contract.factory.ts b/packages/sdk/src/contract/contract.factory.ts index 2fae09ff..a8ac438e 100644 --- a/packages/sdk/src/contract/contract.factory.ts +++ b/packages/sdk/src/contract/contract.factory.ts @@ -10,14 +10,35 @@ import { Erc20Public } from './contracts/Erc20/Erc20Public'; export function createContractWithSigner( config: ContractConfig, + chain: EvmParachain, signer: EvmSigner, ): TransferContractInterface | BalanceContractInterface { if (config.module === 'Erc20') { return new Erc20(config, signer); } + console.log( + '\x1b[34m████████████████████▓▓▒▒░ contract.factory.ts:20 ░▒▒▓▓████████████████████\x1b[0m', + ); + console.log('* config.module = '); + console.log(config.module); + console.log( + '\x1b[34m▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄\x1b[0m', + ); + + console.log( + '\x1b[34m████████████████████▓▓▒▒░ contract.factory.ts:29 ░▒▒▓▓████████████████████\x1b[0m', + ); + console.log('* chain = '); + console.log(chain); + console.log( + '\x1b[34m▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄\x1b[0m', + ); + if (config.module === 'Xtokens') { - return new Xtokens(config, signer); + const address = chain?.contracts?.Xtokens; + + return new Xtokens(config, signer, address); } throw new Error(`Contract ${config.module} not found`); @@ -40,6 +61,6 @@ export function createContract( chain?: AnyChain, ): TransferContractInterface | BalanceContractInterface { return signer - ? createContractWithSigner(config, signer) + ? createContractWithSigner(config, chain, signer) : createContractWithoutSigner(config, chain as EvmParachain); } diff --git a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts index d7041359..ef26f4ae 100644 --- a/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts +++ b/packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts @@ -1,6 +1,7 @@ import { ContractConfig } from '@moonbeam-network/xcm-builder'; import { Contract, TransactionResponse } from 'ethers'; import { + Address, GetContractReturnType, PublicClient, WriteContractReturnType, @@ -16,7 +17,10 @@ import abi from './XtokensABI.json'; type XtokensContract = GetContractReturnType; export class Xtokens implements TransferContractInterface { - readonly address = '0x0000000000000000000000000000000000000804'; + readonly defaultMoonChainAddress = + '0x0000000000000000000000000000000000000804'; + + readonly address: Address; readonly #config: ContractConfig; @@ -24,11 +28,13 @@ export class Xtokens implements TransferContractInterface { readonly #signer: EvmSigner; - constructor(config: ContractConfig, signer: EvmSigner) { + constructor(config: ContractConfig, signer: EvmSigner, address?: Address) { this.#config = config; this.#signer = signer; + this.address = address || this.defaultMoonChainAddress; + this.#contract = isEthersSigner(signer) ? new Contract(this.address, abi, signer) : getContract({ diff --git a/packages/sdk/src/getTransferData/getSourceData.ts b/packages/sdk/src/getTransferData/getSourceData.ts index 2d69c517..23c74316 100644 --- a/packages/sdk/src/getTransferData/getSourceData.ts +++ b/packages/sdk/src/getTransferData/getSourceData.ts @@ -10,7 +10,11 @@ import { FeeAssetConfig, TransferConfig, } from '@moonbeam-network/xcm-config'; -import { AnyChain, AssetAmount } from '@moonbeam-network/xcm-types'; +import { + AnyChain, + AssetAmount, + EvmParachain, +} from '@moonbeam-network/xcm-types'; import { convertDecimals, toBigInt, @@ -42,6 +46,7 @@ export async function getSourceData({ destinationFee, polkadot, sourceAddress, + evmSigner, }: GetSourceDataParams): Promise { const { asset, @@ -145,6 +150,7 @@ export async function getSourceData({ decimals: zeroFeeAmount.decimals, destinationFeeBalanceAmount, destinationFeeConfig: config.destinationFee, + evmSigner, extrinsic, feeConfig: config.fee, polkadot, @@ -260,7 +266,13 @@ export async function getFee({ } } - return getContractFee(balance, contract, decimals, evmSigner); + return getContractFee({ + balance, + chain: chain as EvmParachain, + config: contract, + decimals, + evmSigner, + }); } if (extrinsic) { @@ -283,15 +295,23 @@ export async function getFee({ throw new Error('Either contract or extrinsic must be provided'); } -export async function getContractFee( - balance: bigint, - config: ContractConfig, - decimals: number, - evmSigner: EvmSigner, -): Promise { +export async function getContractFee({ + balance, + config, + decimals, + evmSigner, + chain, +}: { + balance: bigint; + config: ContractConfig; + decimals: number; + evmSigner: EvmSigner; + chain: EvmParachain; +}): Promise { const contract = createContract( config, evmSigner, + chain, ) as TransferContractInterface; const fee = await contract.getFee(balance); diff --git a/packages/sdk/src/getTransferData/getTransferData.ts b/packages/sdk/src/getTransferData/getTransferData.ts index cc8c6442..a94c4516 100644 --- a/packages/sdk/src/getTransferData/getTransferData.ts +++ b/packages/sdk/src/getTransferData/getTransferData.ts @@ -46,9 +46,19 @@ export async function getTransferData({ destination.fee, ); + console.log( + '\x1b[34m████████████████████▓▓▒▒░ getTransferData.ts:49 ░▒▒▓▓████████████████████\x1b[0m', + ); + console.log('* transferConfig = '); + console.log(transferConfig); + console.log( + '\x1b[34m▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄\x1b[0m', + ); + const source = await getSourceData({ destinationAddress, destinationFee, + evmSigner, polkadot: srcPolkadot, sourceAddress, transferConfig, @@ -123,7 +133,11 @@ export async function getTransferData({ } return ( - createContract(contract, evmSigner) as TransferContractInterface + createContract( + contract, + evmSigner, + chain, + ) as TransferContractInterface ) .transfer() .then((tx) => diff --git a/packages/types/src/chain/parachain/EvmParachain.ts b/packages/types/src/chain/parachain/EvmParachain.ts index fb23e9e8..f221b2b7 100644 --- a/packages/types/src/chain/parachain/EvmParachain.ts +++ b/packages/types/src/chain/parachain/EvmParachain.ts @@ -1,4 +1,4 @@ -import { defineChain } from 'viem'; +import { Address, defineChain } from 'viem'; import { Chain } from 'viem/chains'; import { ChainType } from '../Chain.interfaces'; import { Parachain, ParachainConstructorParams } from './Parachain'; @@ -9,6 +9,7 @@ export interface EvmParachainConstructorParams rpc: string; nativeCurrency: NativeCurrency; isEvmSigner?: boolean; + contracts?: Contracts; } type NativeCurrency = { @@ -17,6 +18,10 @@ type NativeCurrency = { symbol: string; }; +type Contracts = { + Xtokens?: Address; +}; + export class EvmParachain extends Parachain { readonly id: number; @@ -26,15 +31,19 @@ export class EvmParachain extends Parachain { readonly isEvmSigner: boolean; + readonly contracts?: Contracts; + constructor({ id, rpc, nativeCurrency, isEvmSigner = false, + contracts, ...others }: EvmParachainConstructorParams) { super({ type: ChainType.EvmParachain, ...others }); + this.contracts = contracts; this.id = id; this.rpc = rpc; this.nativeCurrency = nativeCurrency;