Skip to content

Commit eeff1e4

Browse files
authored
feat: make revertReason consistent on eth_getTransactionReceipt (#2517)
* chore: edit formatter Signed-off-by: nikolay <n.atanasow94@gmail.com> * chore: remove leftovers Signed-off-by: nikolay <n.atanasow94@gmail.com> --------- Signed-off-by: nikolay <n.atanasow94@gmail.com>
1 parent 92bc375 commit eeff1e4

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

packages/relay/src/formatters.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ function hexToASCII(str: string): string {
5050
return ascii;
5151
}
5252

53+
function ASCIIToHex(ascii: string): string {
54+
const hex: string[] = [];
55+
for (let n = 0; n < ascii.length; n++) {
56+
hex.push(Number(ascii.charCodeAt(n)).toString(16));
57+
}
58+
return hex.join('');
59+
}
60+
5361
/**
5462
* Converts an EVM ErrorMessage to a readable form. For example this :
5563
* 0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d53657420746f2072657665727400000000000000000000000000000000000000
@@ -289,4 +297,5 @@ export {
289297
toHexString,
290298
isValidEthereumAddress,
291299
isHex,
300+
ASCIIToHex,
292301
};

packages/relay/src/lib/eth.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import {
3939
toHash32,
4040
weibarHexToTinyBarInt,
4141
trimPrecedingZeros,
42+
ASCIIToHex,
43+
isHex,
4244
} from '../formatters';
4345
import crypto from 'crypto';
4446
import HAPIService from './services/hapiService/hapiService';
@@ -2003,7 +2005,9 @@ export class EthImpl implements Eth {
20032005
};
20042006

20052007
if (receiptResponse.error_message) {
2006-
receipt.revertReason = receiptResponse.error_message;
2008+
receipt.revertReason = isHex(prepend0x(receiptResponse.error_message))
2009+
? receiptResponse.error_message
2010+
: prepend0x(ASCIIToHex(receiptResponse.error_message));
20072011
}
20082012

20092013
this.logger.trace(`${requestIdPrefix} receipt for ${hash} found in block ${receipt.blockNumber}`);

packages/relay/tests/lib/formatters.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
isValidEthereumAddress,
3838
trimPrecedingZeros,
3939
isHex,
40+
ASCIIToHex,
4041
} from '../../src/formatters';
4142
import constants from '../../src/lib/constants';
4243
import { BigNumber as BN } from 'bignumber.js';
@@ -465,4 +466,26 @@ describe('Formatters', () => {
465466
expect(isHex('0x58')).to.be.true;
466467
});
467468
});
469+
describe('ASCIIToHex Function', () => {
470+
const inputs = ['Lorem Ipsum', 'Foo', 'Bar'];
471+
const outputs = ['4c6f72656d20497073756d', '466f6f', '426172'];
472+
473+
it('should return "" for empty string', () => {
474+
expect(ASCIIToHex('')).to.equal('');
475+
});
476+
477+
it('should return valid hex', () => {
478+
expect(isHex(prepend0x(ASCIIToHex(inputs[0])))).to.be.true;
479+
});
480+
481+
it('should return expected hex formatted value', () => {
482+
expect(inputs[0]).to.equal(hexToASCII(ASCIIToHex(inputs[0])));
483+
});
484+
485+
it('should decode correctly regarding hardcoded mapping', () => {
486+
for (let i = 0; i < inputs.length; i++) {
487+
expect(ASCIIToHex(inputs[i])).to.eq(outputs[i]);
488+
}
489+
});
490+
});
468491
});

0 commit comments

Comments
 (0)