|
19 | 19 | */ |
20 | 20 |
|
21 | 21 | import { expect } from 'chai'; |
22 | | -import { JsonRpcError } from '../../../src/lib/errors/JsonRpcError'; |
| 22 | +import { JsonRpcError, predefined } from '../../../src'; |
| 23 | +import { AbiCoder, keccak256 } from 'ethers'; |
23 | 24 |
|
24 | 25 | describe('Errors', () => { |
25 | 26 | describe('JsonRpcError', () => { |
@@ -50,5 +51,84 @@ describe('Errors', () => { |
50 | 51 | // Check that request ID is prefixed |
51 | 52 | expect(err.message).to.eq('[Request ID: abcd-1234] test error: foo'); |
52 | 53 | }); |
| 54 | + |
| 55 | + describe('predefined.CONTRACT_REVERT', () => { |
| 56 | + const defaultErrorSignature = keccak256(Buffer.from('Error(string)')).slice(0, 10); // 0x08c379a0 |
| 57 | + const customErrorSignature = keccak256(Buffer.from('CustomError(string)')).slice(0, 10); // 0x8d6ea8be |
| 58 | + const decodedMessage = 'Some error message'; |
| 59 | + const encodedMessage = new AbiCoder().encode(['string'], [decodedMessage]).replace('0x', ''); |
| 60 | + const encodedCustomError = customErrorSignature + encodedMessage; |
| 61 | + const encodedDefaultError = defaultErrorSignature + encodedMessage; |
| 62 | + |
| 63 | + it('Returns decoded message when decoded message is provided as errorMessage and encoded default error is provided as data', () => { |
| 64 | + const error = predefined.CONTRACT_REVERT(decodedMessage, encodedDefaultError); |
| 65 | + expect(error.message).to.eq(`execution reverted: ${decodedMessage}`); |
| 66 | + }); |
| 67 | + |
| 68 | + it('Returns decoded message when decoded message is provided as errorMessage and encoded custom error is provided as data', () => { |
| 69 | + const error = predefined.CONTRACT_REVERT(decodedMessage, encodedCustomError); |
| 70 | + expect(error.message).to.eq(`execution reverted: ${decodedMessage}`); |
| 71 | + }); |
| 72 | + |
| 73 | + it('Returns decoded message when encoded default error is provided as errorMessage and data', () => { |
| 74 | + const error = predefined.CONTRACT_REVERT(encodedDefaultError, encodedDefaultError); |
| 75 | + expect(error.message).to.eq(`execution reverted: ${decodedMessage}`); |
| 76 | + }); |
| 77 | + |
| 78 | + it('Returns decoded message when encoded custom error is provided as errorMessage and data', () => { |
| 79 | + const error = predefined.CONTRACT_REVERT(encodedCustomError, encodedCustomError); |
| 80 | + expect(error.message).to.eq(`execution reverted: ${decodedMessage}`); |
| 81 | + }); |
| 82 | + |
| 83 | + it('Returns decoded message when decoded errorMessage is provided', () => { |
| 84 | + const error = predefined.CONTRACT_REVERT(decodedMessage); |
| 85 | + expect(error.message).to.eq(`execution reverted: ${decodedMessage}`); |
| 86 | + }); |
| 87 | + |
| 88 | + it('Returns decoded message when encoded default error is provided as errorMessage', () => { |
| 89 | + const error = predefined.CONTRACT_REVERT(encodedDefaultError); |
| 90 | + expect(error.message).to.eq(`execution reverted: ${decodedMessage}`); |
| 91 | + }); |
| 92 | + |
| 93 | + it('Returns decoded message when encoded custom error is provided as errorMessage', () => { |
| 94 | + const error = predefined.CONTRACT_REVERT(encodedCustomError); |
| 95 | + expect(error.message).to.eq(`execution reverted: ${decodedMessage}`); |
| 96 | + }); |
| 97 | + |
| 98 | + it('Returns decoded message when encoded default error is provided as data', () => { |
| 99 | + const error = predefined.CONTRACT_REVERT(undefined, encodedDefaultError); |
| 100 | + expect(error.message).to.eq(`execution reverted: ${decodedMessage}`); |
| 101 | + }); |
| 102 | + |
| 103 | + it('Returns decoded message when encoded custom error is provided as data', () => { |
| 104 | + const error = predefined.CONTRACT_REVERT(undefined, encodedCustomError); |
| 105 | + expect(error.message).to.eq(`execution reverted: ${decodedMessage}`); |
| 106 | + }); |
| 107 | + |
| 108 | + it('Returns decoded message when message is empty and encoded default error is provided as data', () => { |
| 109 | + const error = predefined.CONTRACT_REVERT('', encodedDefaultError); |
| 110 | + expect(error.message).to.eq(`execution reverted: ${decodedMessage}`); |
| 111 | + }); |
| 112 | + |
| 113 | + it('Returns decoded message when message is empty and encoded custom error is provided as data', () => { |
| 114 | + const error = predefined.CONTRACT_REVERT('', encodedCustomError); |
| 115 | + expect(error.message).to.eq(`execution reverted: ${decodedMessage}`); |
| 116 | + }); |
| 117 | + |
| 118 | + it('Returns default message when errorMessage is empty', () => { |
| 119 | + const error = predefined.CONTRACT_REVERT(''); |
| 120 | + expect(error.message).to.eq('execution reverted'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('Returns default message when data is empty', () => { |
| 124 | + const error = predefined.CONTRACT_REVERT(undefined, ''); |
| 125 | + expect(error.message).to.eq('execution reverted'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('Returns default message when neither errorMessage nor data is provided', () => { |
| 129 | + const error = predefined.CONTRACT_REVERT(); |
| 130 | + expect(error.message).to.eq('execution reverted'); |
| 131 | + }); |
| 132 | + }); |
53 | 133 | }); |
54 | 134 | }); |
0 commit comments