Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: PRO-2373 migrate ethers to viem #24

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
77b5802
feat: PRO-2373 viem migration from ethers
kanthgithub Jul 22, 2024
adfb011
chore: PRO-2373 increment the package version
kanthgithub Jul 22, 2024
5a97b5c
chore: PRO-2373 update package lock file
kanthgithub Jul 22, 2024
0215152
fix: PRO-2373 fix IEntrypoint type import
kanthgithub Jul 22, 2024
03c34e5
fix: PRO-2373 helper function to generate ModularSDK for all example …
kanthgithub Jul 22, 2024
4986580
fix: viem dependency issues fixed and DeterministicDeployer refactori…
kanthgithub Jul 23, 2024
fb050af
fix: PRO-2373 ethers cleanup and bug fixes, refactoring
kanthgithub Jul 24, 2024
0356363
feat: PRO-2373 remove ethers package dependency
kanthgithub Jul 24, 2024
61cbc91
fix: PRO-2373 add changeLog aspects
kanthgithub Jul 24, 2024
7fb8d7f
feat: PRO-2373 refactor the example scripts by grouping
kanthgithub Jul 24, 2024
bbea215
chore: PRO-2373 remove ethers specific TODO comments
kanthgithub Jul 24, 2024
e251d7e
fix: PRO-2373 SessionKeyValidator SDK to take bundlerProviderURL
kanthgithub Jul 24, 2024
a4c271f
chore: PRO-2373 modular sdk examples refactored
kanthgithub Jul 25, 2024
17eb2ff
feat: PRO-2373 add XDCMainnet support
kanthgithub Jul 26, 2024
f43a827
fix: PRO-2373 build issue fixes
kanthgithub Aug 1, 2024
b4c2298
fix: resolve compilation issue in SessionKeyValidator
kanthgithub Aug 15, 2024
50544cc
fix: sessionKeySDK functions fixed and tested
kanthgithub Aug 16, 2024
2090e6d
changes for cjs and esm builds, deleting spec files
Aug 16, 2024
f60ce8c
fix: update repo package name and missing dependencies in main index
kanthgithub Aug 16, 2024
26b9677
fix: update viem to correct version
kanthgithub Aug 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Changelog

## [1.2.0] - 2024-07-25
### New
- Migrated from `ethers` to `viem`
- Added helper functions to use `viem`
- Added support for XDC Mainnet.

## [1.1.2] - 2024-07-10
### New
- Added `SessionKeyValidator` module for ERC20 SessionKeyValidator.
Expand Down
43 changes: 28 additions & 15 deletions examples/05-add-guardians.ts → examples/basics/add-guardians.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { ethers } from 'ethers';
import { EtherspotBundler, ModularSdk } from '../src';
import { printOp } from '../src/sdk/common/OperationUtils';
import { printOp } from '../../src/sdk/common/OperationUtils';
import * as dotenv from 'dotenv';
import { sleep } from '../src/sdk/common';
import { sleep } from '../../src/sdk/common';
import { encodeFunctionData, parseAbi } from 'viem';
import { generateModularSDKInstance } from '../helpers/sdk-helper';

dotenv.config();

// tsx examples/basics/add-guardians.ts
async function main() {
const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';

// initializating sdk...
const modularSdk = new ModularSdk(
{ privateKey: process.env.WALLET_PRIVATE_KEY },
{ chainId: Number(process.env.CHAIN_ID), bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey) },
);

console.log('address: ', modularSdk.state.EOAAddress);
const modularSdk = generateModularSDKInstance(
process.env.WALLET_PRIVATE_KEY,
Number(process.env.CHAIN_ID),
bundlerApiKey
);// Testnets dont need apiKey on bundlerProvider

// get address of EtherspotWallet
const address: string = await modularSdk.getCounterFactualAddress();
Expand All @@ -29,12 +29,25 @@ async function main() {

console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`);

const addGuardianInterface = new ethers.utils.Interface(['function addGuardian(address _newGuardian)']);

const addGuardianData1 = addGuardianInterface.encodeFunctionData('addGuardian', [guardianAddresses[0]]);
const addGuardianData2 = addGuardianInterface.encodeFunctionData('addGuardian', [guardianAddresses[1]]);
const addGuardianData3 = addGuardianInterface.encodeFunctionData('addGuardian', [guardianAddresses[2]]);
const addGuardianInterface = ['function addGuardian(address _newGuardian)'];

const addGuardianData1 =
encodeFunctionData({
functionName: 'addGuardian',
abi: parseAbi(addGuardianInterface),
args: [guardianAddresses[0]],
});
const addGuardianData2 =
encodeFunctionData({
functionName: 'addGuardian',
abi: parseAbi(addGuardianInterface),
args: [guardianAddresses[1]],
});
const addGuardianData3 = encodeFunctionData({
functionName: 'addGuardian',
abi: parseAbi(addGuardianInterface),
args: [guardianAddresses[2]],
});
// clear the transaction batch
await modularSdk.clearUserOpsFromBatch();

Expand Down
21 changes: 10 additions & 11 deletions examples/07-callGasLimit.ts → examples/basics/callGasLimit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ethers } from 'ethers';
import { EtherspotBundler, ModularSdk } from '../src';
import { printOp } from '../src/sdk/common/OperationUtils';
import { printOp } from '../../src/sdk/common/OperationUtils';
import * as dotenv from 'dotenv';
import { sleep } from '../src/sdk/common';
import { sleep } from '../../src/sdk/common';
import { parseEther } from 'viem';
import { generateModularSDKInstance } from '../helpers/sdk-helper';

dotenv.config();

Expand All @@ -12,12 +12,11 @@ const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxM

async function main() {
// initializating sdk...
const modularSdk = new ModularSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, {
chainId: Number(process.env.CHAIN_ID),
bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey)
})

console.log('address: ', modularSdk.state.EOAAddress)
const modularSdk = generateModularSDKInstance(
process.env.WALLET_PRIVATE_KEY,
Number(process.env.CHAIN_ID),
bundlerApiKey
);// Testnets dont need apiKey on bundlerProvider

// get address of EtherspotWallet...
const address: string = await modularSdk.getCounterFactualAddress();
Expand All @@ -27,7 +26,7 @@ async function main() {
await modularSdk.clearUserOpsFromBatch();

// add transactions to the batch
const transactionBatch = await modularSdk.addUserOpsToBatch({ to: recipient, value: ethers.utils.parseEther(value) });
const transactionBatch = await modularSdk.addUserOpsToBatch({ to: recipient, value: parseEther(value) });
console.log('transactions: ', transactionBatch);

// get balance of the account address
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { BigNumber, ethers, providers } from 'ethers';
import { EtherspotBundler, ModularSdk } from '../src';
import { printOp } from '../src/sdk/common/OperationUtils';
import { printOp } from '../../src/sdk/common/OperationUtils';
import * as dotenv from 'dotenv';
import { sleep } from '../src/sdk/common';
import { sleep } from '../../src/sdk/common';
import { getPublicClient } from '../../src/sdk/common/utils/viem-utils';
import { Hex, http, parseEther, PublicClient } from 'viem';
import { generateModularSDKInstance } from '../helpers/sdk-helper';
import { BigNumber } from '../../src/sdk/types/bignumber';

dotenv.config();

Expand All @@ -11,20 +13,28 @@ const value = '0.000001'; // transfer value
const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';

async function main() {
const provider = new providers.JsonRpcProvider(process.env.RPC_PROVIDER_URL);
// initializating sdk...
const modularSdk = new ModularSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, {
// initializating sdk for index 0...
const modularSdk = generateModularSDKInstance(
process.env.WALLET_PRIVATE_KEY,
Number(process.env.CHAIN_ID),
bundlerApiKey
);// Testnets dont need apiKey on bundlerProvider

const publicClient = getPublicClient({
chainId: Number(process.env.CHAIN_ID),
bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey)
})

console.log('address: ', modularSdk.state.EOAAddress)
transport: http(bundlerApiKey)
}) as PublicClient;

// get address of EtherspotWallet...
const address: string = await modularSdk.getCounterFactualAddress();
console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`);

if ((await provider.getCode(address)).length <= 2) {
// get code
const code = await publicClient.getCode({
address: address as Hex
});

if (code.length <= 2) {
console.log("Account must be created first");
return;
}
Expand All @@ -33,7 +43,7 @@ async function main() {
await modularSdk.clearUserOpsFromBatch();

// add transactions to the batch
const transactionBatch = await modularSdk.addUserOpsToBatch({to: recipient, value: ethers.utils.parseEther(value)});
const transactionBatch = await modularSdk.addUserOpsToBatch({to: recipient, value: parseEther(value)});
console.log('transactions: ', transactionBatch);

// get balance of the account address
Expand Down
11 changes: 8 additions & 3 deletions examples/01-get-address.ts → examples/basics/get-address.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { EtherspotBundler, ModularSdk } from '../src';
import * as dotenv from 'dotenv';
import { generateModularSDKInstance } from '../helpers/sdk-helper';

dotenv.config();

// npx ts-node examples/01-get-address.ts
// tsx examples/basics/get-address.ts
async function main() {
const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';
const customBundlerUrl = '';

// initializating sdk...
const modularSdk = new ModularSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, { chainId: Number(process.env.CHAIN_ID), bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey, customBundlerUrl) }) // Testnets dont need apiKey on bundlerProvider
const modularSdk = generateModularSDKInstance(
process.env.WALLET_PRIVATE_KEY,
Number(process.env.CHAIN_ID),
bundlerApiKey
);// Testnets dont need apiKey on bundlerProvider

// get EtherspotWallet address...
const address: string = await modularSdk.getCounterFactualAddress();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { EtherspotBundler, ModularSdk } from '../src';
import * as dotenv from 'dotenv';
import { generateModularSDKInstance } from '../helpers/sdk-helper';

dotenv.config();


// tsx examples/basics/get-counterfactual-address.ts
async function main() {
const etherspotBundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';
// initializating sdk...
const modularSdk = new ModularSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, {
chainId: Number(process.env.CHAIN_ID),
bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), etherspotBundlerApiKey)
})
// initializating sdk for index 0...
const modularSdk = generateModularSDKInstance(
process.env.WALLET_PRIVATE_KEY,
Number(process.env.CHAIN_ID),
etherspotBundlerApiKey
);// Testnets dont need apiKey on bundlerProvider

// get EtherspotWallet address...
const address: string = await modularSdk.getCounterFactualAddress();
Expand Down
25 changes: 25 additions & 0 deletions examples/basics/get-gas-fees.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as dotenv from 'dotenv';
import { generateModularSDKInstance } from '../helpers/sdk-helper';

dotenv.config();

// tsx examples/basics/get-gas-fees.ts
async function main() {
const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';
const customBundlerUrl = '';

// initializating sdk...
const modularSdk = generateModularSDKInstance(
process.env.WALLET_PRIVATE_KEY,
Number(process.env.CHAIN_ID),
bundlerApiKey
);// Testnets dont need apiKey on bundlerProvider

// get EtherspotWallet address...
const gasFees: any = await modularSdk.getGasFee();
console.log(`gasFees is: ${gasFees}`);
}

main()
.catch(console.error)
.finally(() => process.exit());
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { EtherspotBundler, ModularSdk } from '../src';
import * as dotenv from 'dotenv';
import { generateModularSDKInstance } from '../helpers/sdk-helper';

dotenv.config();

const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';

// tsx examples/basics/get-multiple-accounts.ts
async function main() {
// initializating sdk for index 0...
const modularSdk = new ModularSdk(
{ privateKey: process.env.WALLET_PRIVATE_KEY },
{ chainId: Number(process.env.CHAIN_ID), bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey) },
);
const modularSdk = generateModularSDKInstance(
process.env.WALLET_PRIVATE_KEY,
Number(process.env.CHAIN_ID),
bundlerApiKey
);// Testnets dont need apiKey on bundlerProvider


// get EtherspotWallet address for index 0...
const address: string = await modularSdk.getCounterFactualAddress();
console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address for index 0: ${address}`);

// initializating sdk for index 1...
const modularSdk1 = new ModularSdk(
{ privateKey: process.env.WALLET_PRIVATE_KEY },
{ chainId: Number(process.env.CHAIN_ID), index: 1, bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey) },
);
const modularSdk1 = generateModularSDKInstance(
process.env.WALLET_PRIVATE_KEY,
Number(process.env.CHAIN_ID),
bundlerApiKey,
1
);// Testnets dont need apiKey on bundlerProvider

// get EtherspotWallet address for index 1...
const address1: string = await modularSdk1.getCounterFactualAddress();
Expand Down
25 changes: 25 additions & 0 deletions examples/basics/get-nonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as dotenv from 'dotenv';
import { generateModularSDKInstance } from '../helpers/sdk-helper';

dotenv.config();

// tsx examples/basics/get-nonce.ts
async function main() {
const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';
const customBundlerUrl = '';

// initializating sdk...
const modularSdk = generateModularSDKInstance(
process.env.WALLET_PRIVATE_KEY,
Number(process.env.CHAIN_ID),
bundlerApiKey
);// Testnets dont need apiKey on bundlerProvider

// get EtherspotWallet nonce...
const nonce = await modularSdk.getNonce();
console.log(`nonce is: ${nonce}`);
}

main()
.catch(console.error)
.finally(() => process.exit());
48 changes: 33 additions & 15 deletions examples/03-transfer-erc20.ts → examples/basics/transfer-erc20.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { ethers } from 'ethers';
import { EtherspotBundler, ModularSdk } from '../src';
import { printOp } from '../src/sdk/common/OperationUtils';
import { ERC20_ABI } from '../src/sdk/helpers/abi/ERC20_ABI';
import { EtherspotBundler, ModularSdk } from '../../src';
import { printOp } from '../../src/sdk/common/OperationUtils';
import { ERC20_ABI } from '../../src/sdk/helpers/abi/ERC20_ABI';
import * as dotenv from 'dotenv';
import { sleep } from '../src/sdk/common';
import { sleep } from '../../src/sdk/common';
import { getPublicClient, getViemAccount } from '../../src/sdk/common/utils/viem-utils';
import { encodeFunctionData, Hex, http, parseAbi, parseUnits } from 'viem';
import { generateModularSDKInstance } from '../helpers/sdk-helper';
import { erc20Abi } from '../../src/sdk/common/abis';

dotenv.config();

Expand All @@ -13,26 +16,41 @@ const value = '0.1'; // transfer value
const tokenAddress = '0x326C977E6efc84E512bB9C30f76E30c160eD06FB';
const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';

// tsx examples/basics/transfer-erc20.ts
async function main() {
// initializating sdk...
const modularSdk = new ModularSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, { chainId: Number(process.env.CHAIN_ID), bundlerProvider: new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey) })

console.log('address: ', modularSdk.state.EOAAddress)
const bundlerProvider = new EtherspotBundler(Number(process.env.CHAIN_ID), bundlerApiKey);
const modularSdk = generateModularSDKInstance(
process.env.WALLET_PRIVATE_KEY,
Number(process.env.CHAIN_ID),
bundlerApiKey
);// Testnets dont need apiKey on bundlerProvider

// get address of EtherspotWallet...
const address: string = await modularSdk.getCounterFactualAddress();
console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`);

const provider = new ethers.providers.JsonRpcProvider(process.env.BUNDLER_URL)
// get erc20 Contract Interface
const erc20Instance = new ethers.Contract(tokenAddress, ERC20_ABI, provider);
const publicClient = getPublicClient({
chainId: Number(process.env.CHAIN_ID),
transport: http(bundlerProvider.url)
});

// get decimals from erc20 contract
const decimals = await erc20Instance.functions.decimals();


const decimals = await publicClient.readContract({
address: tokenAddress as Hex,
abi: parseAbi(erc20Abi),
functionName: 'decimals',
args: []
})

// get transferFrom encoded data
const transactionData = erc20Instance.interface.encodeFunctionData('transfer', [recipient, ethers.utils.parseUnits(value, decimals)])

const transactionData = encodeFunctionData({
functionName: 'transfer',
abi: parseAbi(ERC20_ABI),
args: [recipient, parseUnits(value, decimals as number)]
});

// clear the transaction batch
await modularSdk.clearUserOpsFromBatch();

Expand Down
Loading