Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add range test for bigIntToBytes32 #23

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions subgraph/tests/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ import {bigIntToBytes32} from '../../src/utils/utils';
import {BigInt} from '@graphprotocol/graph-ts';
import {assert, describe, test} from 'matchstick-as/assembly/index';

const ZERO_BYTES32 =
'0x0000000000000000000000000000000000000000000000000000000000000000';
const ONE_BYTES32 =
'0x0000000000000000000000000000000000000000000000000000000000000001';
const HALF_UINT256_BYTES32 =
'0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
const MAX_UINT256_BYTES32 =
'0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';

const MAX_UINT256_NUMBER_STRING =
'115792089237316195423570985008687907853269984665640564039457584007913129639935';

describe('bigIntToBytes32', () => {
test('should convert a BigInt to a bytes32 string with 64 zero padding', () => {
const input = BigInt.fromI32(123);
const expectedOutput =
'0x000000000000000000000000000000000000000000000000000000000000007b';
test('should successfully convert a range of `bigInt`s', function () {
const MAX_UINT256 = BigInt.fromString(MAX_UINT256_NUMBER_STRING);

assert.stringEquals(bigIntToBytes32(input), expectedOutput);
assert.stringEquals(bigIntToBytes32(BigInt.fromString('0')), ZERO_BYTES32);
assert.stringEquals(bigIntToBytes32(BigInt.fromString('1')), ONE_BYTES32);
assert.stringEquals(
bigIntToBytes32(MAX_UINT256.div(BigInt.fromString('2'))),
HALF_UINT256_BYTES32
);
assert.stringEquals(bigIntToBytes32(MAX_UINT256), MAX_UINT256_BYTES32);
});
});