-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbbtc-agent-plugin.js
209 lines (182 loc) · 6.74 KB
/
cbbtc-agent-plugin.js
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
const { ethers } = require('ethers');
const CBBTCAgentPlugin = require('../src/CBBTCAgentPlugin');
// Test Configuration
const TEST_CONFIG = {
PRIVATE_KEY: '0x1234567890123456789012345678901234567890123456789012345678901234',
RPC_URL: 'https://sepolia.base.org', // Use a testnet RPC
ADDRESSES: {
VALID_SENDER: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
VALID_RECIPIENT: '0x7A1D0b3E8Bbb8A46Cc2B6E9b6b2A2D50A7Cf5b5',
INVALID_ADDRESS: 'invalid-address'
},
AMOUNTS: {
VALID_TRANSFER: '1.5',
INVALID_NEGATIVE: '-1.0',
ZERO_AMOUNT: '0'
}
};
// Error Messages
const ERROR_MESSAGES = {
INVALID_ADDRESS: /Invalid Ethereum address/,
INVALID_AMOUNT: /Invalid amount/,
PRIVATE_KEY_REQUIRED: /PRIVATE_KEY must be set/,
RPC_URL_REQUIRED: /BASE_RPC_URL must be set/
};
/**
* Test Suite for CBBTCAgentPlugin
*/
describe('CBBTCAgentPlugin - Comprehensive Test Suite', () => {
let plugin;
let mockContract;
// Utility function to create a mock contract
const createMockContract = (balanceResponse = ethers.parseUnits('10', 18)) => ({
balanceOf: jest.fn().mockResolvedValue(balanceResponse),
transfer: jest.fn().mockImplementation((to, amount, options) => ({
wait: jest.fn().mockResolvedValue({
status: 1,
transactionHash: '0x123456789abcdef'
}),
hash: '0x123456789abcdef'
})),
approve: jest.fn().mockImplementation((spender, amount) => ({
wait: jest.fn().mockResolvedValue({
status: 1,
transactionHash: '0x987654321fedcba'
}),
hash: '0x987654321fedcba'
}))
});
// Setup before each test
beforeEach(() => {
// Initialize plugin with test configuration
plugin = new CBBTCAgentPlugin(TEST_CONFIG.PRIVATE_KEY, TEST_CONFIG.RPC_URL);
// Create and inject mock contract
mockContract = createMockContract();
plugin.cbBTCContract = mockContract;
});
// Constructor Validation Tests
describe('Constructor Validation', () => {
it('should throw an error if private key is not provided', () => {
expect(() => new CBBTCAgentPlugin(null, TEST_CONFIG.RPC_URL))
.toThrow(ERROR_MESSAGES.PRIVATE_KEY_REQUIRED);
});
it('should throw an error if RPC URL is not provided', () => {
expect(() => new CBBTCAgentPlugin(TEST_CONFIG.PRIVATE_KEY, null))
.toThrow(ERROR_MESSAGES.RPC_URL_REQUIRED);
});
});
// Balance Check Tests
describe('Balance Checking', () => {
it('should successfully retrieve balance for a valid address', async () => {
const balance = await plugin.checkBalance(TEST_CONFIG.ADDRESSES.VALID_SENDER);
// Verify contract method was called correctly
expect(mockContract.balanceOf).toHaveBeenCalledWith(TEST_CONFIG.ADDRESSES.VALID_SENDER);
// Check balance is correctly formatted
expect(balance).toBe('10.0');
});
it('should throw an error for an invalid address', async () => {
await expect(plugin.checkBalance(TEST_CONFIG.ADDRESSES.INVALID_ADDRESS))
.rejects.toThrow(ERROR_MESSAGES.INVALID_ADDRESS);
});
});
// Token Transfer Tests
describe('Token Transfers', () => {
it('should successfully transfer tokens', async () => {
const txHash = await plugin.transferCbBTC(
TEST_CONFIG.ADDRESSES.VALID_RECIPIENT,
TEST_CONFIG.AMOUNTS.VALID_TRANSFER
);
// Verify contract method was called with correct parameters
expect(mockContract.transfer).toHaveBeenCalledWith(
TEST_CONFIG.ADDRESSES.VALID_RECIPIENT,
ethers.parseUnits(TEST_CONFIG.AMOUNTS.VALID_TRANSFER, 18),
expect.objectContaining({
type: 2,
gasLimit: expect.any(BigInt)
})
);
// Verify transaction hash is returned
expect(txHash).toBe('0x123456789abcdef');
});
it('should throw an error for invalid recipient address', async () => {
await expect(
plugin.transferCbBTC(TEST_CONFIG.ADDRESSES.INVALID_ADDRESS, TEST_CONFIG.AMOUNTS.VALID_TRANSFER)
).rejects.toThrow(ERROR_MESSAGES.INVALID_ADDRESS);
});
it('should throw an error for invalid transfer amount', async () => {
await expect(
plugin.transferCbBTC(
TEST_CONFIG.ADDRESSES.VALID_RECIPIENT,
TEST_CONFIG.AMOUNTS.INVALID_NEGATIVE
)
).rejects.toThrow(ERROR_MESSAGES.INVALID_AMOUNT);
});
it('should throw an error for zero transfer amount', async () => {
await expect(
plugin.transferCbBTC(
TEST_CONFIG.ADDRESSES.VALID_RECIPIENT,
TEST_CONFIG.AMOUNTS.ZERO_AMOUNT
)
).rejects.toThrow(ERROR_MESSAGES.INVALID_AMOUNT);
});
});
// Token Approval Tests
describe('Token Approvals', () => {
it('should successfully approve token spending', async () => {
const txHash = await plugin.approveSpender(
TEST_CONFIG.ADDRESSES.VALID_RECIPIENT,
TEST_CONFIG.AMOUNTS.VALID_TRANSFER
);
// Verify contract method was called with correct parameters
expect(mockContract.approve).toHaveBeenCalledWith(
TEST_CONFIG.ADDRESSES.VALID_RECIPIENT,
ethers.parseUnits(TEST_CONFIG.AMOUNTS.VALID_TRANSFER, 18),
expect.objectContaining({
type: 2
})
);
// Verify transaction hash is returned
expect(txHash).toBe('0x987654321fedcba');
});
it('should throw an error for invalid spender address', async () => {
await expect(
plugin.approveSpender(
TEST_CONFIG.ADDRESSES.INVALID_ADDRESS,
TEST_CONFIG.AMOUNTS.VALID_TRANSFER
)
).rejects.toThrow(ERROR_MESSAGES.INVALID_ADDRESS);
});
});
// Error Handling Tests
describe('Error Handling', () => {
it('should handle network errors gracefully', async () => {
// Simulate a network error
mockContract.balanceOf.mockRejectedValue(new Error('Network connection failed'));
await expect(
plugin.checkBalance(TEST_CONFIG.ADDRESSES.VALID_SENDER)
).rejects.toThrow(/Error checking balance/);
});
it('should provide meaningful error messages', async () => {
try {
await plugin.transferCbBTC(
TEST_CONFIG.ADDRESSES.INVALID_ADDRESS,
TEST_CONFIG.AMOUNTS.VALID_TRANSFER
);
} catch (error) {
expect(error.message).toMatch(ERROR_MESSAGES.INVALID_ADDRESS);
}
});
});
// Performance and Limit Tests
describe('Performance Considerations', () => {
it('should handle large transfer amounts', async () => {
const largeAmount = '1000000.0'; // Large but realistic amount
const txHash = await plugin.transferCbBTC(
TEST_CONFIG.ADDRESSES.VALID_RECIPIENT,
largeAmount
);
expect(txHash).toBeTruthy();
expect(mockContract.transfer).toHaveBeenCalled();
});
});
});