mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-02-02 16:22:50 +00:00
Fix getting negative values from signed integers. (#88)
Co-authored-by: nikugogoi <95nikass@gmail.com>
This commit is contained in:
parent
fc30290685
commit
27b732fef7
@ -208,8 +208,17 @@ describe('Get value from storage', () => {
|
|||||||
describe('signed integer type', () => {
|
describe('signed integer type', () => {
|
||||||
let integers: Contract, storageLayout: StorageLayout, blockHash: string;
|
let integers: Contract, storageLayout: StorageLayout, blockHash: string;
|
||||||
const int1Value = 12;
|
const int1Value = 12;
|
||||||
const int2Value = 34;
|
const int2Value = -34;
|
||||||
const int3Value = 123;
|
const int3Value = 123;
|
||||||
|
const int4Value = -12;
|
||||||
|
const int5Value = -123;
|
||||||
|
const int6Value = -456;
|
||||||
|
const int7Value = -789;
|
||||||
|
const int9Value = -1234;
|
||||||
|
const int10Value = -4567;
|
||||||
|
const int11Value = -12345;
|
||||||
|
const int12Value = -67890;
|
||||||
|
const int13Value = -123456;
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
({ contract: integers, storageLayout } = contracts.TestIntegers);
|
({ contract: integers, storageLayout } = contracts.TestIntegers);
|
||||||
@ -217,7 +226,16 @@ describe('Get value from storage', () => {
|
|||||||
const transactions = await Promise.all([
|
const transactions = await Promise.all([
|
||||||
integers.setInt1(int1Value),
|
integers.setInt1(int1Value),
|
||||||
integers.setInt2(int2Value),
|
integers.setInt2(int2Value),
|
||||||
integers.setInt3(int3Value)
|
integers.setInt3(int3Value),
|
||||||
|
integers.setInt4(int4Value),
|
||||||
|
integers.setInt5(int5Value),
|
||||||
|
integers.setInt6(int6Value),
|
||||||
|
integers.setInt7(int7Value),
|
||||||
|
integers.setInt9(int9Value),
|
||||||
|
integers.setInt10(int10Value),
|
||||||
|
integers.setInt11(int11Value),
|
||||||
|
integers.setInt12(int12Value),
|
||||||
|
integers.setInt13(int13Value)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
await Promise.all(transactions.map(transaction => transaction.wait()));
|
await Promise.all(transactions.map(transaction => transaction.wait()));
|
||||||
@ -248,6 +266,71 @@ describe('Get value from storage', () => {
|
|||||||
assertProofData(blockHash, integers.address, JSON.parse(proofData));
|
assertProofData(blockHash, integers.address, JSON.parse(proofData));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('get value for integer type variables with negative values', async () => {
|
||||||
|
let { value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int4');
|
||||||
|
expect(value).to.equal(BigInt(int4Value));
|
||||||
|
|
||||||
|
if (isIpldGql) {
|
||||||
|
assertProofData(blockHash, integers.address, JSON.parse(proofData));
|
||||||
|
}
|
||||||
|
|
||||||
|
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int5'));
|
||||||
|
expect(value).to.equal(BigInt(int5Value));
|
||||||
|
|
||||||
|
if (isIpldGql) {
|
||||||
|
assertProofData(blockHash, integers.address, JSON.parse(proofData));
|
||||||
|
}
|
||||||
|
|
||||||
|
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int6'));
|
||||||
|
expect(value).to.equal(BigInt(int6Value));
|
||||||
|
|
||||||
|
if (isIpldGql) {
|
||||||
|
assertProofData(blockHash, integers.address, JSON.parse(proofData));
|
||||||
|
}
|
||||||
|
|
||||||
|
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int7'));
|
||||||
|
expect(value).to.equal(BigInt(int7Value));
|
||||||
|
|
||||||
|
if (isIpldGql) {
|
||||||
|
assertProofData(blockHash, integers.address, JSON.parse(proofData));
|
||||||
|
}
|
||||||
|
|
||||||
|
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int9'));
|
||||||
|
expect(value).to.equal(BigInt(int9Value));
|
||||||
|
|
||||||
|
if (isIpldGql) {
|
||||||
|
assertProofData(blockHash, integers.address, JSON.parse(proofData));
|
||||||
|
}
|
||||||
|
|
||||||
|
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int10'));
|
||||||
|
expect(value).to.equal(BigInt(int10Value));
|
||||||
|
|
||||||
|
if (isIpldGql) {
|
||||||
|
assertProofData(blockHash, integers.address, JSON.parse(proofData));
|
||||||
|
}
|
||||||
|
|
||||||
|
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int11'));
|
||||||
|
expect(value).to.equal(BigInt(int11Value));
|
||||||
|
|
||||||
|
if (isIpldGql) {
|
||||||
|
assertProofData(blockHash, integers.address, JSON.parse(proofData));
|
||||||
|
}
|
||||||
|
|
||||||
|
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int12'));
|
||||||
|
expect(value).to.equal(BigInt(int12Value));
|
||||||
|
|
||||||
|
if (isIpldGql) {
|
||||||
|
assertProofData(blockHash, integers.address, JSON.parse(proofData));
|
||||||
|
}
|
||||||
|
|
||||||
|
({ value, proof: { data: proofData } } = await getStorageValue(storageLayout, getStorageAt, blockHash, integers.address, 'int13'));
|
||||||
|
expect(value).to.equal(BigInt(int13Value));
|
||||||
|
|
||||||
|
if (isIpldGql) {
|
||||||
|
assertProofData(blockHash, integers.address, JSON.parse(proofData));
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('unsigned integer type', () => {
|
describe('unsigned integer type', () => {
|
||||||
|
@ -79,9 +79,17 @@ export const getValueByType = (storageValue: string, typeLabel: string): bigint
|
|||||||
return !BigNumber.from(storageValue).isZero();
|
return !BigNumber.from(storageValue).isZero();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [isIntegerOrEnum, isInteger, isUnsigned, integerSize] = typeLabel.match(/^enum|((u?)int([0-9]+))/) || [false];
|
||||||
|
|
||||||
// Parse value for uint/int type or enum type.
|
// Parse value for uint/int type or enum type.
|
||||||
if (typeLabel.match(/^enum|u?int[0-9]+/)) {
|
if (isIntegerOrEnum) {
|
||||||
return BigInt(storageValue);
|
let bigNumber = BigNumber.from(storageValue);
|
||||||
|
|
||||||
|
if (Boolean(isInteger) && !isUnsigned) {
|
||||||
|
bigNumber = bigNumber.fromTwos(Number(integerSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
return BigInt(bigNumber.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse value for string type.
|
// Parse value for string type.
|
||||||
|
@ -10,7 +10,16 @@ contract TestIntegers {
|
|||||||
int256 int3;
|
int256 int3;
|
||||||
|
|
||||||
// Integer type variable is stored in the next slot as there is not enough space for it in the previous slot.
|
// Integer type variable is stored in the next slot as there is not enough space for it in the previous slot.
|
||||||
int32 int4;
|
int24 int4;
|
||||||
|
|
||||||
|
int32 int5;
|
||||||
|
int64 int6;
|
||||||
|
int72 int7;
|
||||||
|
int96 int9;
|
||||||
|
int128 int10;
|
||||||
|
int200 int11;
|
||||||
|
int232 int12;
|
||||||
|
int256 int13;
|
||||||
|
|
||||||
// Set variable int1.
|
// Set variable int1.
|
||||||
function setInt1(int8 value) external {
|
function setInt1(int8 value) external {
|
||||||
@ -28,7 +37,47 @@ contract TestIntegers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set variable int4.
|
// Set variable int4.
|
||||||
function setInt4(int32 value) external {
|
function setInt4(int24 value) external {
|
||||||
int4 = value;
|
int4 = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set variable int5.
|
||||||
|
function setInt5(int32 value) external {
|
||||||
|
int5 = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set variable int6.
|
||||||
|
function setInt6(int64 value) external {
|
||||||
|
int6 = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set variable int7.
|
||||||
|
function setInt7(int72 value) external {
|
||||||
|
int7 = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set variable int9.
|
||||||
|
function setInt9(int96 value) external {
|
||||||
|
int9 = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set variable int10.
|
||||||
|
function setInt10(int128 value) external {
|
||||||
|
int10 = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set variable int11.
|
||||||
|
function setInt11(int200 value) external {
|
||||||
|
int11 = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set variable int12.
|
||||||
|
function setInt12(int232 value) external {
|
||||||
|
int12 = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set variable int13.
|
||||||
|
function setInt13(int256 value) external {
|
||||||
|
int13 = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user