Skip to content

Commit

Permalink
Fix ethers contract identification error (#348)
Browse files Browse the repository at this point in the history
* fix how we validate if the contract is from Ethers and update example

* add changeset

* add comment
  • Loading branch information
mmaurello authored Sep 12, 2024
1 parent 5b9f548 commit 61e7143
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-meals-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moonbeam-network/xcm-sdk': patch
---

Fix Ethers contract identification
37 changes: 29 additions & 8 deletions examples/sdk-simple/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
/* eslint-disable import/no-extraneous-dependencies */
import { dot, moonbeam, polkadot } from '@moonbeam-network/xcm-config';
import { Sdk, TransferData } from '@moonbeam-network/xcm-sdk';
import { EvmSigner, Sdk, TransferData } from '@moonbeam-network/xcm-sdk';
import { Keyring } from '@polkadot/api';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { ethers } from 'ethers';
import { setTimeout } from 'node:timers/promises';

import { createWalletClient, http } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { moonbeam as moonbeamViem } from 'viem/chains';
// Moonbeam Signer ===========================================================

const moonbeamPrivateKey = '';
const provider = new ethers.WebSocketProvider(moonbeam.ws, {
const moonbeamPrivateKey = '0x...';
const provider = new ethers.WebSocketProvider(moonbeam.ws[0], {
chainId: moonbeam.id,
name: moonbeam.name,
});
const evmSigner = new ethers.Wallet(moonbeamPrivateKey, provider);

// Using ethers
// *****WARNING: IN THE UPCOMING VERSION OF THIS SDK, ethers SUPPORT WILL BE REMOVED, PLEASE SWITCH TO viem WHEN POSSIBLE
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const ethersSigner = new ethers.Wallet(moonbeamPrivateKey, provider);

// Using viem
const account = privateKeyToAccount(moonbeamPrivateKey);
const viemSigner = createWalletClient({
account,
chain: moonbeamViem,
transport: http('https://rpc.api.moonbeam.network'),
});

// ethers
// const evmSigner: EvmSigner = ethersSigner
// const address = ethersSigner.address
// viem
const evmSigner: EvmSigner = viemSigner;
const { address } = account;

// Polkadot Signer ===========================================================

Expand Down Expand Up @@ -65,7 +86,7 @@ export async function fromPolkadot() {
console.log('\nTransfer from Polkadot to Moonbeam\n');

const data = await Sdk().getTransferData({
destinationAddress: evmSigner.address,
destinationAddress: address,
destinationKeyOrChain: moonbeam,
keyOrAsset: dot,
polkadotSigner: pair,
Expand Down Expand Up @@ -93,7 +114,7 @@ export async function fromMoonbeam() {
.asset(dot)
.source(moonbeam)
.destination(polkadot)
.accounts(evmSigner.address, pair.address, {
.accounts(address, pair.address, {
evmSigner,
});

Expand All @@ -114,7 +135,7 @@ async function main() {
console.warn = () => null;
console.clear();

console.log(`\nMoonbeam address: ${evmSigner.address}.`);
console.log(`\nMoonbeam address: ${address}.`);
console.log(`Polkadot address: ${pair.address}.`);

await fromPolkadot();
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/contract/contract.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export function isWalletClient(signer: EvmSigner): signer is WalletClient {
export function isEthersContract(
contract: Contract | GetContractReturnType<Abi | readonly unknown[]>,
): contract is Contract {
return 'signer' in contract;
return !('abi' in contract);
}

0 comments on commit 61e7143

Please sign in to comment.