-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy-script.js
More file actions
249 lines (208 loc) · 7.53 KB
/
deploy-script.js
File metadata and controls
249 lines (208 loc) · 7.53 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Script to deploy contracts to Sepolia testnet
const { ethers } = require('ethers');
const fs = require('fs');
// Configuration
const SEPOLIA_RPC = 'https://sepolia.infura.io/v3/YOUR_INFURA_KEY';
const PRIVATE_KEY = 'YOUR_PRIVATE_KEY'; // Never commit this!
async function deployContracts() {
console.log('🚀 Starting contract deployment to Sepolia...');
// Connect to Sepolia
const provider = new ethers.JsonRpcProvider(SEPOLIA_RPC);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
console.log('📍 Deploying from address:', wallet.address);
// Check balance
const balance = await provider.getBalance(wallet.address);
console.log('💰 Balance:', ethers.formatEther(balance), 'ETH');
if (parseFloat(ethers.formatEther(balance)) < 0.01) {
console.error('❌ Insufficient balance! Need at least 0.01 ETH for deployment');
return;
}
try {
// Load contract ABIs and bytecode
const testTokenABI = JSON.parse(fs.readFileSync('./frontend/src/abi/TestToken.json', 'utf8'));
const paymentSplitterABI = JSON.parse(fs.readFileSync('./frontend/src/abi/PaymentSplitter.json', 'utf8'));
// Deploy TestToken
console.log('📄 Deploying TestToken...');
const TestTokenFactory = new ethers.ContractFactory(
testTokenABI.abi,
testTokenABI.bytecode,
wallet
);
const testToken = await TestTokenFactory.deploy();
await testToken.waitForDeployment();
const testTokenAddress = await testToken.getAddress();
console.log('✅ TestToken deployed at:', testTokenAddress);
// Deploy PaymentSplitter
console.log('📄 Deploying PaymentSplitter...');
const PaymentSplitterFactory = new ethers.ContractFactory(
paymentSplitterABI.abi,
paymentSplitterABI.bytecode,
wallet
);
const paymentSplitter = await PaymentSplitterFactory.deploy(testTokenAddress);
await paymentSplitter.waitForDeployment();
const paymentSplitterAddress = await paymentSplitter.getAddress();
console.log('✅ PaymentSplitter deployed at:', paymentSplitterAddress);
// Update contract addresses in the frontend
const contractsContent = `// Sepolia Testnet Contract Addresses - Auto-generated
export const CONTRACT_ADDRESSES = {
testToken: '${testTokenAddress}',
paymentSplitter: '${paymentSplitterAddress}',
};
// Network validation
export const SEPOLIA_CHAIN_ID = 11155111;
export function validateNetwork(chainId: number): boolean {
return chainId === SEPOLIA_CHAIN_ID;
}
export async function validateContracts(signer: JsonRpcSigner): Promise<{
testToken: boolean;
paymentSplitter: boolean;
}> {
const provider = signer.provider;
try {
const testTokenCode = await provider.getCode(CONTRACT_ADDRESSES.testToken);
const paymentSplitterCode = await provider.getCode(CONTRACT_ADDRESSES.paymentSplitter);
return {
testToken: testTokenCode !== '0x',
paymentSplitter: paymentSplitterCode !== '0x',
};
} catch (error) {
console.error('Error validating contracts:', error);
return {
testToken: false,
paymentSplitter: false,
};
}
}`;
// Write the rest of the contracts.ts file content
const restOfFile = `
import { Contract, formatUnits, parseUnits } from 'ethers';
import type { JsonRpcSigner } from 'ethers';
import TestTokenABI from '../abi/TestToken.json';
import PaymentSplitterABI from '../abi/PaymentSplitter.json';
// Fallback addresses for testing (if main contracts are not deployed)
export const FALLBACK_ADDRESSES = {
// These are example addresses - replace with actual deployed contracts
testToken: '0x0000000000000000000000000000000000000000',
paymentSplitter: '0x0000000000000000000000000000000000000000',
};
export interface ContractInstances {
testToken: Contract;
paymentSplitter: Contract;
}
export function getContracts(signer: JsonRpcSigner): ContractInstances {
const testToken = new Contract(
CONTRACT_ADDRESSES.testToken,
TestTokenABI.abi,
signer
);
const paymentSplitter = new Contract(
CONTRACT_ADDRESSES.paymentSplitter,
PaymentSplitterABI.abi,
signer
);
return { testToken, paymentSplitter };
}
export async function getTokenBalance(
tokenContract: Contract,
address: string
): Promise<string> {
try {
// First check if the contract exists
const provider = tokenContract.runner?.provider;
if (provider) {
const code = await provider.getCode(await tokenContract.getAddress());
if (code === '0x') {
console.warn('Contract not deployed at address:', await tokenContract.getAddress());
return '0';
}
}
const balance = await tokenContract.balanceOf(address);
return formatUnits(balance, 18);
} catch (error: any) {
console.error('Error fetching balance:', error);
// Provide specific error messages
if (error.code === 'BAD_DATA') {
console.error('Contract not found or invalid ABI at address:', await tokenContract.getAddress());
}
return '0';
}
}
export async function approveTokens(
tokenContract: Contract,
spenderAddress: string,
amount: string
): Promise<any> {
try {
const amountWei = parseUnits(amount, 18);
const tx = await tokenContract.approve(spenderAddress, amountWei);
await tx.wait();
return tx;
} catch (error: any) {
console.error('Error approving tokens:', error);
throw new Error(error.message || 'Failed to approve tokens');
}
}
export async function payAndExtend(
paymentSplitterContract: Contract,
tokenContract: Contract,
listenerAddress: string,
extensionMinutes: number,
amount: string
): Promise<any> {
try {
const amountWei = parseUnits(amount, 18);
const extensionSeconds = extensionMinutes * 60;
const tx = await paymentSplitterContract.payAndSplit(
tokenContract,
listenerAddress,
amountWei,
extensionSeconds
);
await tx.wait();
return tx;
} catch (error: any) {
console.error('Error processing payment:', error);
throw new Error(error.message || 'Failed to process payment');
}
}
export async function getTokenRatePerMinute(
paymentSplitterContract: Contract
): Promise<string> {
// Since the contract doesn't have TOKEN_RATE_PER_MINUTE, we'll return a fixed rate
return '10';
}`;
fs.writeFileSync('./frontend/src/web3/contracts.ts', contractsContent + restOfFile);
console.log('📝 Updated frontend/src/web3/contracts.ts with new addresses');
// Create deployment summary
const summary = {
network: 'Sepolia Testnet',
chainId: 11155111,
deployedAt: new Date().toISOString(),
contracts: {
testToken: {
address: testTokenAddress,
txHash: testToken.deploymentTransaction()?.hash
},
paymentSplitter: {
address: paymentSplitterAddress,
txHash: paymentSplitter.deploymentTransaction()?.hash
}
},
deployer: wallet.address
};
fs.writeFileSync('./deployment-summary.json', JSON.stringify(summary, null, 2));
console.log('🎉 Deployment completed successfully!');
console.log('📋 Summary saved to deployment-summary.json');
console.log('🔗 View on Etherscan:');
console.log(\` TestToken: https://sepolia.etherscan.io/address/\${testTokenAddress}\`);
console.log(\` PaymentSplitter: https://sepolia.etherscan.io/address/\${paymentSplitterAddress}\`);
} catch (error) {
console.error('❌ Deployment failed:', error);
}
}
// Run deployment
if (require.main === module) {
deployContracts().catch(console.error);
}
module.exports = { deployContracts };