Skip to content

Commit

Permalink
wrote unit test which reproduced the problem
Browse files Browse the repository at this point in the history
  • Loading branch information
EdNoepel committed Jul 18, 2023
1 parent 743d754 commit 977f4cb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/grant-fund.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Address, Bytes, ethereum, log } from '@graphprotocol/graph-ts'
// import { log } from "matchstick-as"

import {
DelegateRewardClaimed as DelegateRewardClaimedEvent,
Expand Down
7 changes: 3 additions & 4 deletions src/utils/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ export function addressArrayToBytesArray(addresses: Address[]): Bytes[] {
}

export function bigIntToBytes(bi: BigInt): Bytes {
// return Bytes.fromByteArray(Bytes.fromBigInt(bi))
return Bytes.fromByteArray(ByteArray.fromBigInt(bi))
const unsigned = BigInt.fromU64(bi.toU64())
return Bytes.fromByteArray(Bytes.fromBigInt(unsigned))
}

/***************************/
/*** To BigInt Functions ***/
/***************************/

export function bytesToBigInt(bytes: Bytes): BigInt {
// return BigInt.fromUnsignedBytes(bytes)
return BigInt.fromSignedBytes(bytes)
return BigInt.fromUnsignedBytes(bytes)
}

// converts a BigDecimal to a BigInt scaled to WAD precision
Expand Down
1 change: 0 additions & 1 deletion src/utils/grants/distribution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Address, BigInt, Bytes, dataSource, log } from "@graphprotocol/graph-ts"
// import { log } from "matchstick-as"

import { GrantFund } from "../../../generated/GrantFund/GrantFund"
import { DistributionPeriod } from "../../../generated/schema"
Expand Down
16 changes: 16 additions & 0 deletions tests/convert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BigInt } from '@graphprotocol/graph-ts'
import { assert, describe, test } from "matchstick-as/assembly/index";
import { ONE_BI } from "../src/utils/constants";
import { bigIntToBytes, bytesToBigInt } from "../src/utils/convert";

describe("Conversions", () => {
test("Reliably convert integer contract values to bytes", () => {
const one_bi = BigInt.fromU32(1)
const one_number = 1
assert.bigIntEquals(one_bi, BigInt.fromI32(one_number));

const one_bytes_from_bi = bigIntToBytes(one_bi);
const one_bytes_from_number = bigIntToBytes(BigInt.fromI32(one_number));
assert.bytesEquals(one_bytes_from_bi, one_bytes_from_number);
});
});

0 comments on commit 977f4cb

Please sign in to comment.