Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Rihyx committed May 29, 2024
1 parent 9cd13a5 commit af92721
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 14 deletions.
3 changes: 3 additions & 0 deletions packages/config/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,9 @@ export const peaqEvmAlphanet = new EvmParachain({
metadataId: 0,
},
],
contracts: {
Xtokens: '0x0000000000000000000000000000000000000803',
},
ecosystem: Ecosystem.AlphanetRelay,
genesisHash:
'0x2dfcd5c560f6db1667cbc2bc3791dfd337f88f400af6de39b1b8638ee7af6ed4',
Expand Down
25 changes: 23 additions & 2 deletions packages/sdk/src/contract/contract.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -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);
}
10 changes: 8 additions & 2 deletions packages/sdk/src/contract/contracts/Xtokens/Xtokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ContractConfig } from '@moonbeam-network/xcm-builder';
import { Contract, TransactionResponse } from 'ethers';
import {
Address,
GetContractReturnType,
PublicClient,
WriteContractReturnType,
Expand All @@ -16,19 +17,24 @@ import abi from './XtokensABI.json';
type XtokensContract = GetContractReturnType<typeof abi, PublicClient>;

export class Xtokens implements TransferContractInterface {
readonly address = '0x0000000000000000000000000000000000000804';
readonly defaultMoonChainAddress =
'0x0000000000000000000000000000000000000804';

readonly address: Address;

readonly #config: ContractConfig;

readonly #contract: Contract | XtokensContract;

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({
Expand Down
36 changes: 28 additions & 8 deletions packages/sdk/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -42,6 +46,7 @@ export async function getSourceData({
destinationFee,
polkadot,
sourceAddress,
evmSigner,
}: GetSourceDataParams): Promise<SourceChainTransferData> {
const {
asset,
Expand Down Expand Up @@ -145,6 +150,7 @@ export async function getSourceData({
decimals: zeroFeeAmount.decimals,
destinationFeeBalanceAmount,
destinationFeeConfig: config.destinationFee,
evmSigner,
extrinsic,
feeConfig: config.fee,
polkadot,
Expand Down Expand Up @@ -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) {
Expand All @@ -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<bigint> {
export async function getContractFee({
balance,
config,
decimals,
evmSigner,
chain,
}: {
balance: bigint;
config: ContractConfig;
decimals: number;
evmSigner: EvmSigner;
chain: EvmParachain;
}): Promise<bigint> {
const contract = createContract(
config,
evmSigner,
chain,
) as TransferContractInterface;
const fee = await contract.getFee(balance);

Expand Down
16 changes: 15 additions & 1 deletion packages/sdk/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -123,7 +133,11 @@ export async function getTransferData({
}

return (
createContract(contract, evmSigner) as TransferContractInterface
createContract(
contract,
evmSigner,
chain,
) as TransferContractInterface
)
.transfer()
.then((tx) =>
Expand Down
11 changes: 10 additions & 1 deletion packages/types/src/chain/parachain/EvmParachain.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,6 +9,7 @@ export interface EvmParachainConstructorParams
rpc: string;
nativeCurrency: NativeCurrency;
isEvmSigner?: boolean;
contracts?: Contracts;
}

type NativeCurrency = {
Expand All @@ -17,6 +18,10 @@ type NativeCurrency = {
symbol: string;
};

type Contracts = {
Xtokens?: Address;
};

export class EvmParachain extends Parachain {
readonly id: number;

Expand All @@ -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;
Expand Down

0 comments on commit af92721

Please sign in to comment.