Skip to content

Commit

Permalink
Implement viem Client (#131)
Browse files Browse the repository at this point in the history
* -wip- initial commit implementing viem

* -wip-

* -wip-

* change dependencies

* rename types and fix interfaces

* change signer property name and update viem

* fix example

* change Readme property

* accept ethersSigner

* allow ethers signer as input

* add changeset

* Update packages/sdk/src/sdk.interfaces.ts

Co-authored-by: elmar <[email protected]>

* Update packages/sdk/src/contract/contracts/Erc20/Erc20.ts

Co-authored-by: elmar <[email protected]>

* implement viem contract instance

* fix args

* fix type

* put ethers and viem as peer dependencies

---------

Co-authored-by: elmar <[email protected]>
  • Loading branch information
mmaurello and ekenigs authored Sep 11, 2023
1 parent 30f33c6 commit 63d81d6
Show file tree
Hide file tree
Showing 18 changed files with 373 additions and 94 deletions.
6 changes: 6 additions & 0 deletions .changeset/four-gifts-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@moonbeam-network/xcm-config': minor
'@moonbeam-network/xcm-sdk': minor
---

Allow viem Wallet Client as signer
10 changes: 5 additions & 5 deletions examples/sdk-simple/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 evmSigner = new ethers.Wallet(moonbeamPrivateKey, provider);

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

Expand Down Expand Up @@ -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: evmSigner.address,
destinationKeyOrChain: moonbeam,
keyOrAsset: dot,
polkadotSigner: pair,
Expand Down Expand Up @@ -93,8 +93,8 @@ export async function fromMoonbeam() {
.asset(dot)
.source(moonbeam)
.destination(polkadot)
.accounts(ethersSigner.address, pair.address, {
ethersSigner,
.accounts(evmSigner.address, pair.address, {
evmSigner,
});

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

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

await fromPolkadot();
Expand Down
157 changes: 152 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -55,6 +54,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",
Expand All @@ -64,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",
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/configs/moonbaseBeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ const dataViaAssetsMethod = await assets()
.source('INSERT_SOURCE_CHAIN')
.destination('INSERT_DESTINATION_CHAIN')
.accounts('INSERT_SOURCE_ADDRESS', 'INSERT_DESTINATION_ADDRESS', {
ethersSigner?: 'INSERT_ETHERS_SIGNER',
evmSigner?: 'INSERT_EVM_SIGNER',
polkadotSigner?: 'INSERT_POLKADOT_SIGNER',
});

// Or via the getTransferData function
const dataViaGetTransferDataMethod = await getTransferData({
destinationAddress: 'INSERT_DESTINATION_ADDRESS',
destinationKeyOrChain: 'INSERT_DESTINATION_CHAIN',
ethersSigner?: 'INSERT_ETHERS_SIGNER',
evmSigner?: 'INSERT_EVM_SIGNER',
keyOrAsset: 'INSERT_ASSET',
polkadotSigner?: 'INSERT_POLKADOT_SIGNER',
sourceAddress: 'INSERT_SOURCE_ADDRESS',
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@polkadot/api-augment": "^10.9.1",
"@polkadot/types": "^10.9.1",
"@polkadot/util": "^12.3.2",
"ethers": "^5.7.2"
"ethers": "^5.7.2",
"viem": "^1.10.3"
}
}
4 changes: 2 additions & 2 deletions packages/sdk/src/contract/contract.factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ContractConfig } from '@moonbeam-network/xcm-builder';
import { Signer } from 'ethers';
import { EvmSigner } from '../sdk.interfaces';
import {
BalanceContractInterface,
TransferContractInterface,
Expand All @@ -8,7 +8,7 @@ import { Erc20, Xtokens } from './contracts';

export function createContract(
config: ContractConfig,
signer: Signer,
signer: EvmSigner,
): TransferContractInterface | BalanceContractInterface {
if (config.module === 'Erc20') {
return new Erc20(config, signer);
Expand Down
16 changes: 15 additions & 1 deletion packages/sdk/src/contract/contract.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import type { TransactionResponse } from '@ethersproject/abstract-provider';
import { Contract, Signer as EthersSigner } from 'ethers';
import { PublicClient, WalletClient, WriteContractReturnType } from 'viem';

export interface BaseContractInterface {
readonly address: string;
}

export interface TransferContractInterface extends BaseContractInterface {
transfer(): Promise<TransactionResponse>;
transfer(): Promise<TransactionResponse | WriteContractReturnType>;
getFee(amount: bigint): Promise<bigint>;
}

export interface BalanceContractInterface extends BaseContractInterface {
getBalance(): Promise<bigint>;
getDecimals(): Promise<number>;
}

export type ContractClient = EthersClient | ViemClient;

export interface EthersClient {
contract: Contract;
signer: EthersSigner;
}

export interface ViemClient {
publicClient: PublicClient;
walletClient: WalletClient;
}
Loading

0 comments on commit 63d81d6

Please sign in to comment.