-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateDelegationScenarios.ts
99 lines (84 loc) · 2.85 KB
/
createDelegationScenarios.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
import { Address, encodeFunctionData } from 'viem';
import dotenv from 'dotenv';
import { resolve } from 'path';
import { BaseScenario } from './utils/baseScenario';
import { validateEnvKeys } from './utils/validateEnvKeys';
import { RiskEngineAbi } from '../abis/RiskEngineAbi';
// Load environment variables
dotenv.config({ path: resolve(process.cwd(), '.env.local') });
validateEnvKeys(['FUNDING_PRIVATE_KEY', 'RISK_ENGINE_ADDRESS']);
class DelegationScenario extends BaseScenario {
private funderAccount;
private testAccount1;
private testAccount2;
constructor() {
super(process.env.FUNDING_PRIVATE_KEY as `0x${string}`);
// Create random wallets
this.funderAccount = this.fundingAccount;
this.testAccount1 = this.createRandomAccount();
this.testAccount2 = this.createRandomAccount();
console.log('Funder address:', this.funderAccount.address);
console.log('Test Account 1:', this.testAccount1.address);
console.log('Test Account 2:', this.testAccount2.address);
}
private async updateDelegate(
account: any,
delegate: Address,
approved: boolean
) {
const hash = await this.sendTransaction({
account: account,
to: process.env.RISK_ENGINE_ADDRESS as Address,
data: encodeFunctionData({
abi: RiskEngineAbi,
functionName: 'updateDelegate',
args: [delegate, approved],
}),
});
await this.publicClient.waitForTransactionReceipt({ hash });
console.log(
`Updated delegate for ${account.address}: delegate=${delegate}, approved=${approved}`
);
}
async start() {
console.log('Starting delegation scenarios...');
try {
// Send initial ETH to test accounts
console.log('\nSending initial ETH to test accounts...');
await this.sendInitialEth(this.testAccount1.address);
await this.sendInitialEth(this.testAccount2.address);
// Scenario 1: Set delegate
console.log('\nScenario 1: Setting delegate for test account 1...');
await this.updateDelegate(
this.testAccount1,
this.funderAccount.address,
true
);
// Scenario 2: Set and unset delegate
console.log(
'\nScenario 2: Setting and unsetting delegate for test account 2...'
);
await this.updateDelegate(
this.testAccount2,
this.funderAccount.address,
true
);
// Wait a bit before unsetting
await new Promise(resolve => setTimeout(resolve, 2000));
await this.updateDelegate(
this.testAccount2,
this.funderAccount.address,
false
);
} catch (error) {
console.error('Error in delegation scenarios:', error);
process.exit(1);
}
}
}
// Execute the scenarios
const scenario = new DelegationScenario();
scenario.start().catch(error => {
console.error('Failed to execute delegation scenarios:', error);
process.exit(1);
});