-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathisDeployed.test.js
More file actions
63 lines (53 loc) · 2.28 KB
/
Copy pathisDeployed.test.js
File metadata and controls
63 lines (53 loc) · 2.28 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
const { SafeAccountV0_3_0, SafeAccountV0_2_0 } = require('../../dist/index.cjs');
describe('SafeAccount.isDeployed', () => {
const fakeRpc = 'https://rpc.example.invalid';
const address = '0x1111111111111111111111111111111111111111';
let originalFetch;
let lastRequest;
beforeEach(() => {
originalFetch = global.fetch;
lastRequest = null;
});
afterEach(() => {
global.fetch = originalFetch;
});
function mockFetchReturning(code) {
global.fetch = async (url, options) => {
lastRequest = { url, body: JSON.parse(options.body) };
const body = { jsonrpc: '2.0', id: 1, result: code };
return {
ok: true,
status: 200,
statusText: 'OK',
text: async () => JSON.stringify(body),
json: async () => body,
};
};
}
test('returns true when bytecode is deployed (V0_3_0)', async () => {
mockFetchReturning('0x6080604052' + 'aa'.repeat(40));
const result = await SafeAccountV0_3_0.isDeployed(address, fakeRpc);
expect(result).toBe(true);
expect(lastRequest.body.method).toBe('eth_getCode');
expect(lastRequest.body.params).toEqual([address, 'latest']);
});
test('returns false when no bytecode is present (V0_3_0)', async () => {
mockFetchReturning('0x');
const result = await SafeAccountV0_3_0.isDeployed(address, fakeRpc);
expect(result).toBe(false);
});
test('inherits onto V0_2_0 with same behavior', async () => {
mockFetchReturning('0x');
expect(await SafeAccountV0_2_0.isDeployed(address, fakeRpc)).toBe(false);
mockFetchReturning('0xdeadbeef');
expect(await SafeAccountV0_2_0.isDeployed(address, fakeRpc)).toBe(true);
});
test('treats EIP-7702 delegation prefix as deployed', async () => {
// 0xef0100 + 20-byte delegatee. Not a Safe, but bytecode is present
// so isDeployed must return true. Callers that need to distinguish
// delegation from a real Safe must do that check separately.
mockFetchReturning('0xef0100' + '11'.repeat(20));
const result = await SafeAccountV0_3_0.isDeployed(address, fakeRpc);
expect(result).toBe(true);
});
});