Repository: 0xs34n/starknet.js
Pull request #484 (2023-01-10 13:29:43)

Fix/uint256


Patch:
diff --git a/src/utils/uint256.ts b/src/utils/uint256.ts
index 527c65d9..8de1dc89 100644
--- a/src/utils/uint256.ts
+++ b/src/utils/uint256.ts
@@ -27,7 +27,7 @@ export function bnToUint256(bignumber: BigNumberish): Uint256 {
   const bn = toBigInt(bignumber);
   if (!isUint256(bn)) throw new Error('Number is too large');
   return {
-    low: addHexPrefix((bn & 128n).toString(16)),
+    low: addHexPrefix((bn & UINT_128_MAX).toString(16)),
     high: addHexPrefix((bn >> 128n).toString(16)),
   };
 }


Test patch:
diff --git a/__tests__/utils/uint256.test.ts b/__tests__/utils/uint256.test.ts
index 5e8d74d9..1c72f9f4 100644
--- a/__tests__/utils/uint256.test.ts
+++ b/__tests__/utils/uint256.test.ts
@@ -2,7 +2,7 @@ import { UINT_128_MAX, UINT_256_MAX, bnToUint256, uint256ToBN } from '../../src/
 
 describe('cairo uint256', () => {
   test('should convert 0 from BN to uint256 struct', () => {
-    const uint256 = bnToUint256('0');
+    const uint256 = bnToUint256(0n);
     expect(uint256).toMatchInlineSnapshot(`
       Object {
         "high": "0x0",
@@ -10,10 +10,38 @@ describe('cairo uint256', () => {
       }
     `);
   });
-  test('should convert 0 from uint256 to BN', () => {
-    expect(uint256ToBN({ low: '0x0', high: '0x0' }).toString()).toMatchInlineSnapshot(`"0"`);
+
+  test('should convert 1000 from BN to uint256 struct', () => {
+    const uint256 = bnToUint256(1000n);
+    expect(uint256).toMatchInlineSnapshot(`
+      Object {
+        "high": "0x0",
+        "low": "0x3e8",
+      }
+    `);
   });
-  test('should convert BN over 2^128 to uint256 struct', () => {
+
+  test('should convert 10^9 from BN to uint256 struct', () => {
+    const uint256 = bnToUint256(1000000000n);
+    expect(uint256).toMatchInlineSnapshot(`
+      Object {
+        "high": "0x0",
+        "low": "0x3b9aca00",
+      }
+    `);
+  });
+
+  test('should convert 2^128 from BN to uint256 struct', () => {
+    const uint256 = bnToUint256(UINT_128_MAX);
+    expect(uint256).toMatchInlineSnapshot(`
+      Object {
+        "high": "0x0",
+        "low": "0xffffffffffffffffffffffffffffffff",
+      }
+    `);
+  });
+
+  test('should convert 2^128 + 1 from BN to uint256 struct', () => {
     const uint256 = bnToUint256(UINT_128_MAX + 1n);
     expect(uint256).toMatchInlineSnapshot(`
       Object {
@@ -22,6 +50,52 @@ describe('cairo uint256', () => {
       }
     `);
   });
+
+  test('should convert 2^256 - 1 from BN to uint256 struct', () => {
+    const uint256 = bnToUint256(UINT_256_MAX);
+    expect(uint256).toMatchInlineSnapshot(`
+      Object {
+        "high": "0xffffffffffffffffffffffffffffffff",
+        "low": "0xffffffffffffffffffffffffffffffff",
+      }
+    `);
+  });
+
+  test('should convert 0 from uint256 to BN', () => {
+    expect(uint256ToBN({ low: '0x0', high: '0x0' }).toString()).toMatchInlineSnapshot(`"0"`);
+  });
+
+  test('should convert 1000 from uint256 to BN', () => {
+    expect(uint256ToBN({ low: '0x3e8', high: '0x0' }).toString()).toMatchInlineSnapshot(`"1000"`);
+  });
+
+  test('should convert 10^9 from uint256 to BN', () => {
+    expect(uint256ToBN({ low: '0x3b9aca00', high: '0x0' }).toString()).toMatchInlineSnapshot(
+      `"1000000000"`
+    );
+  });
+
+  test('should convert 2^128 from uint256 to BN', () => {
+    expect(
+      uint256ToBN({ low: '0xffffffffffffffffffffffffffffffff', high: '0x0' }).toString()
+    ).toEqual(UINT_128_MAX.toString());
+  });
+
+  test('should convert 2^128 + 1 from uint256 to BN', () => {
+    expect(uint256ToBN({ low: '0x0', high: '0x1' }).toString()).toEqual(
+      (UINT_128_MAX + 1n).toString()
+    );
+  });
+
+  test('should convert 2^256 - 1 from uint256 to BN', () => {
+    expect(
+      uint256ToBN({
+        low: '0xffffffffffffffffffffffffffffffff',
+        high: '0xffffffffffffffffffffffffffffffff',
+      }).toString()
+    ).toEqual(UINT_256_MAX.toString());
+  });
+
   test('should throw if BN over uint256 range', () => {
     expect(() => bnToUint256(UINT_256_MAX + 1n)).toThrowErrorMatchingInlineSnapshot(
       `"Number is too large"`