Skip to content

Commit

Permalink
Merge pull request #4 from etherspot/add-examples-for-sdk
Browse files Browse the repository at this point in the history
First Version of the sdk
  • Loading branch information
vignesha22 authored Jun 15, 2023
2 parents 12bf8d9 + c2588ec commit 9607d7f
Show file tree
Hide file tree
Showing 416 changed files with 55,047 additions and 14,559 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ demo
test.js

# .env file
.env
.env

# config
examples/config.json

# contracts exposed
src/contracts/factories/contracts-exposed/
33 changes: 33 additions & 0 deletions DEPLOYMENT_COSTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Deployment Costs

## Average Gas Costs (per transaction)

| Transaction | Gas Cost (Avg) |
|--------------|----------------|
| Wallet deployment | 180000 gwei |
| Native token transfer | 102344 gwei |
| ERC20 token transfer | 127683 gwei |
| Token swap (Uniswap v3) | 308041 gwei (swap + approval: 185648 + 122393) |

## Costs By Chain (per transaction)

| Ethereum | Standard (53 gwei) | Fast (57 gwei) |
|----------|--------------------|----------------|
| Wallet deployment | $19.32 | $20.25 |
| Native token transfer | $10.07 | $10.83 |
| ERC20 token transfer | $12.61 | $13.55 |
| Token swap (Uniswap v3) | $30.27 | $32.67 |

| Polygon | Low (425.6 gwei) | High (426.6 gwei) |
|----------|--------------------|----------------|
| Wallet deployment | $0.09 | $0.09 |
| Native token transfer | $0.05 | $0.05 |
| ERC20 token transfer | $0.06 | $0.06 |
| Token swap (Uniswap v3) | $0.14 | $0.14 |

| Binance Smart Chain | Low (3 gwei) | High (3 gwei) |
|----------|--------------------|----------------|
| Wallet deployment | $0.19 | $0.19 |
| Native token transfer | $0.11 | $0.11 |
| ERC20 token transfer | $0.13 | $0.13 |
| Token swap (Uniswap v3) | $0.31 | $0.31 |
3 changes: 3 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
WALLET_PRIVATE_KEY=

RPC_PROVIDER_URL=
18 changes: 18 additions & 0 deletions examples/01-get-address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PrimeSdk } from '../src';
import * as dotenv from 'dotenv';

dotenv.config();


async function main() {
// initializating sdk...
const primeSdk = new PrimeSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, { chainId: Number(process.env.CHAIN_ID) })

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

main()
.catch(console.error)
.finally(() => process.exit());
46 changes: 46 additions & 0 deletions examples/02-transfer-funds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ethers } from 'ethers';
import { PrimeSdk } from '../src';
import { printOp } from '../src/sdk/common/OperationUtils';

const recipient: string = '0x80a1874E1046B1cc5deFdf4D3153838B72fF94Ac'; // recipient wallet address
const value: string = '0.01'; // transfer value

async function main() {
// initializating sdk...
const primeSdk = new PrimeSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, { chainId: Number(process.env.CHAIN_ID) })

console.log('address: ', primeSdk.state.walletAddress)

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

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

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

// get balance of the account address
const balance = await primeSdk.getNativeBalance();

console.log('balances: ', balance);

// sign transactions added to the batch
const op = await primeSdk.sign();
console.log(`Signed UserOp: ${await printOp(op)}`);

// sending to the bundler...
const uoHash = await primeSdk.send(op);
console.log(`UserOpHash: ${uoHash}`);

// get transaction hash...
console.log('Waiting for transaction...');
const txHash = await primeSdk.getUserOpReceipt(uoHash);
console.log('\x1b[33m%s\x1b[0m', `Transaction hash: ${txHash}`);
}

main()
.catch(console.error)
.finally(() => process.exit());
57 changes: 57 additions & 0 deletions examples/03-transfer-erc20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { ethers } from 'ethers';
import { PrimeSdk } from '../src';
import { printOp } from '../src/sdk/common/OperationUtils';
import { ERC20_ABI } from '../src/sdk/helpers/abi/ERC20_ABI';
import * as dotenv from 'dotenv';

dotenv.config();

// add/change these values
const recipient: string = '0xD129dB5e418e389c3F7D3ae0B8771B3f76799A52'; // recipient wallet address
const value: string = '0.1'; // transfer value
const tokenAddress: string = '0x326C977E6efc84E512bB9C30f76E30c160eD06FB';

async function main() {
// initializating sdk...
const primeSdk = new PrimeSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, { chainId: Number(process.env.CHAIN_ID) })

console.log('address: ', primeSdk.state.walletAddress)

// get address of EtherspotWallet...
const address: string = await primeSdk.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);

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

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

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

// add transactions to the batch
let userOpsBatch = await primeSdk.addUserOpsToBatch({to: tokenAddress, data: transactionData});
console.log('transactions: ', userOpsBatch);

// sign transactions added to the batch
const op = await primeSdk.sign();
console.log(`Signed UserOp: ${await printOp(op)}`);

// sending to the bundler...
const uoHash = await primeSdk.send(op);
console.log(`UserOpHash: ${uoHash}`);

// get transaction hash...
console.log('Waiting for transaction...');
const txHash = await primeSdk.getUserOpReceipt(uoHash);
console.log('\x1b[33m%s\x1b[0m', `Transaction hash: ${txHash}`);
}

main()
.catch(console.error)
.finally(() => process.exit());
52 changes: 52 additions & 0 deletions examples/04-transfer-nft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ethers } from 'ethers';
import { PrimeSdk } from '../src';
import { printOp } from '../src/sdk/common/OperationUtils';
import * as dotenv from 'dotenv';

dotenv.config();

// add/change these values
const recipient: string = '0xD129dB5e418e389c3F7D3ae0B8771B3f76799A52'; // recipient wallet address
const tokenAddress: string = '0xe55C5793a52AF819fBf3e87a23B36708E6FDd2Cc';
const tokenId = 4;

async function main() {
// initializating sdk...
const primeSdk = new PrimeSdk({ privateKey: process.env.WALLET_PRIVATE_KEY }, { chainId: Number(process.env.CHAIN_ID) })

console.log('address: ', primeSdk.state.walletAddress)

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

const erc721Interface = new ethers.utils.Interface([
'function safeTransferFrom(address _from, address _to, uint256 _tokenId)'
])

const erc721Data = erc721Interface.encodeFunctionData('safeTransferFrom', [address, recipient, tokenId]);

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

// add transactions to the batch
let userOpsBatch = await primeSdk.addUserOpsToBatch({to: tokenAddress, data: erc721Data});
console.log('transactions: ', userOpsBatch);

// sign transactions added to the batch
const op = await primeSdk.sign();
console.log(`Signed UserOp: ${await printOp(op)}`);

// sending to the bundler...
const uoHash = await primeSdk.send(op);
console.log(`UserOpHash: ${uoHash}`);

// get transaction hash...
console.log('Waiting for transaction...');
const txHash = await primeSdk.getUserOpReceipt(uoHash);
console.log('\x1b[33m%s\x1b[0m', `Transaction hash: ${txHash}`);
}

main()
.catch(console.error)
.finally(() => process.exit());
21 changes: 0 additions & 21 deletions examples/1-get-address.ts

This file was deleted.

32 changes: 0 additions & 32 deletions examples/2-transfer-funds.ts

This file was deleted.

45 changes: 45 additions & 0 deletions examples/scripts/commands/NFTTransfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ethers } from "ethers";
// @ts-ignore
import config from "../../config.json";
import { PrimeSdk } from "../../../src";
import { printOp } from "../../../src/sdk/common/OperationUtils";

export default async function main(
tknid: number,
t: string,
tkn: string,
) {
const primeSdk = new PrimeSdk({ privateKey: config.signingKey }, { chainId: config.chainId, rpcProviderUrl: config.rpcProviderUrl })

const address = await primeSdk.getCounterFactualAddress();

const tokenId = tknid;
const tokenAddress = ethers.utils.getAddress(tkn);
const to = ethers.utils.getAddress(t);
console.log(`Transferring NFT ${tknid} ...`);

const erc721Interface = new ethers.utils.Interface([
'function safeTransferFrom(address _from, address _to, uint256 _tokenId)'
])

const erc721Data = erc721Interface.encodeFunctionData('safeTransferFrom', [address, to, tokenId]);

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


await primeSdk.addUserOpsToBatch({to: tokenAddress, data: erc721Data});
console.log(`Added transaction to batch`);

const op = await primeSdk.sign();
console.log(`Signed UserOp: ${await printOp(op)}`);

// sending to the bundler...
const uoHash = await primeSdk.send(op);
console.log(`UserOpHash: ${uoHash}`);

// get transaction hash...
console.log('Waiting for transaction...');
const txHash = await primeSdk.getUserOpReceipt(uoHash);
console.log('\x1b[33m%s\x1b[0m', `Transaction hash: ${txHash}`);
}
10 changes: 10 additions & 0 deletions examples/scripts/commands/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @ts-ignore
import config from "../../config.json";
import { PrimeSdk } from "../../../src";

export default async function main() {
const primeSdk = new PrimeSdk({ privateKey: config.signingKey }, { chainId: 80001 })
const address = await primeSdk.getCounterFactualAddress();

console.log(`Etherspot address: ${address}`);
}
Loading

0 comments on commit 9607d7f

Please sign in to comment.