-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepositAndTransfer.ts
185 lines (159 loc) · 5.37 KB
/
depositAndTransfer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import {
createPublicClient,
createWalletClient,
http,
parseEther,
Address,
PublicClient,
WalletClient,
Account,
encodeFunctionData,
} from 'viem';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { baseSepolia } from 'viem/chains';
import dotenv from 'dotenv';
import { MockTokenAbi } from './abis/MockTokenAbi';
import { PTokenAbi } from '../abis/PTokenAbi';
import { resolve } from 'path';
import { validateEnvKeys } from './utils/validateEnvKeys';
// Load environment variables
dotenv.config({ path: resolve(process.cwd(), '.env.local') });
validateEnvKeys(['FUNDING_PRIVATE_KEY', 'USDC_ADDRESS', 'PUSDC_ADDRESS']);
// Configuration
const config = {
fundingKey: process.env.FUNDING_PRIVATE_KEY!,
usdcAddress: process.env.USDC_ADDRESS as Address,
pUsdcAddress: process.env.PUSDC_ADDRESS as Address,
};
class PUSDCFundAndReturn {
private publicClient: PublicClient;
private walletClient: WalletClient;
private account: Account;
private fundingAccount: Account;
constructor() {
// Create random wallet to receive funds
const randomPrivateKey = generatePrivateKey();
this.account = privateKeyToAccount(randomPrivateKey);
// Create funding account from env
this.fundingAccount = privateKeyToAccount(
config.fundingKey as `0x${string}`
);
// @ts-ignore
this.publicClient = createPublicClient({
chain: baseSepolia,
transport: http(),
});
this.walletClient = createWalletClient({
chain: baseSepolia,
transport: http(),
});
console.log(`Generated receiver wallet address: ${this.account.address}`);
console.log(`Funding wallet address: ${this.fundingAccount.address}`);
}
async sendInitialFunding() {
console.log('Sending initial ETH funding...');
const fundingHash = await this.walletClient.sendTransaction({
account: this.fundingAccount,
to: this.account.address,
value: parseEther('0.0001'),
chain: baseSepolia,
});
await this.publicClient.waitForTransactionReceipt({ hash: fundingHash });
console.log('Initial ETH funding sent successfully');
}
async mintDepositAndReturnHalf() {
try {
// Send initial ETH funding
await this.sendInitialFunding();
console.log('Minting USDC...');
// Mint USDC
const mintHash = await this.walletClient.sendTransaction({
account: this.account,
to: config.usdcAddress,
value: 0n,
chain: baseSepolia,
data: encodeFunctionData({
abi: MockTokenAbi,
functionName: 'mint',
}),
});
await this.publicClient.waitForTransactionReceipt({ hash: mintHash });
// Get USDC balance
const usdcBalance = await this.publicClient.readContract({
address: config.usdcAddress,
abi: MockTokenAbi,
functionName: 'balanceOf',
args: [this.account.address],
});
console.log(`Minted ${usdcBalance} USDC`);
// Approve pUSDC contract to spend USDC
console.log('Approving USDC spend...');
const approveHash = await this.walletClient.sendTransaction({
account: this.account,
to: config.usdcAddress,
value: 0n,
chain: baseSepolia,
data: encodeFunctionData({
abi: MockTokenAbi,
functionName: 'approve',
args: [config.pUsdcAddress, usdcBalance],
}),
});
await this.publicClient.waitForTransactionReceipt({ hash: approveHash });
// Deposit USDC to get pUSDC
console.log('Depositing USDC to get pUSDC...');
const depositHash = await this.walletClient.sendTransaction({
account: this.account,
to: config.pUsdcAddress,
value: 0n,
chain: baseSepolia,
data: encodeFunctionData({
abi: PTokenAbi,
functionName: 'deposit',
args: [usdcBalance, this.account.address],
}),
});
await this.publicClient.waitForTransactionReceipt({ hash: depositHash });
// Get pUSDC balance
const pUsdcBalance = await this.publicClient.readContract({
address: config.pUsdcAddress,
abi: PTokenAbi,
functionName: 'balanceOf',
args: [this.account.address],
});
// Calculate half of pUSDC to return
const returnAmount = pUsdcBalance / 2n;
if (returnAmount > 0n) {
console.log(`Returning ${returnAmount} pUSDC to funder`);
// Transfer half of pUSDC back to funder
const transferHash = await this.walletClient.sendTransaction({
account: this.account,
to: config.pUsdcAddress,
value: 0n,
chain: baseSepolia,
data: encodeFunctionData({
abi: PTokenAbi,
functionName: 'transfer',
args: [this.fundingAccount.address, returnAmount],
}),
});
await this.publicClient.waitForTransactionReceipt({
hash: transferHash,
});
console.log(`Successfully returned ${returnAmount} pUSDC to funder`);
}
console.log('Process completed successfully');
} catch (error) {
console.error('Transaction failed:', error);
throw error;
}
}
}
const fundAndReturn = async () => {
const processor = new PUSDCFundAndReturn();
await processor.mintDepositAndReturnHalf();
};
fundAndReturn().catch(error => {
console.error('Failed to complete fund and return process:', error);
process.exit(1);
});