mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-24 03:59:06 +00:00
Add tests for nested struct. (#59)
Co-authored-by: nikugogoi <95nikass@gmail.com>
This commit is contained in:
parent
eb20708faf
commit
3815853f7b
@ -59,6 +59,10 @@ $ yarn test
|
|||||||
* [x] Value Types
|
* [x] Value Types
|
||||||
* [x] Get value of a single member in struct
|
* [x] Get value of a single member in struct
|
||||||
* [ ] Reference Types
|
* [ ] Reference Types
|
||||||
|
* [x] Struct type members (nested)
|
||||||
|
* [ ] Array type members
|
||||||
|
* [ ] Bytes and string type members
|
||||||
|
* [ ] Mapping type members
|
||||||
* [ ] Mapping Types
|
* [ ] Mapping Types
|
||||||
* [x] Value Type keys
|
* [x] Value Type keys
|
||||||
* [ ] Fixed-size byte array keys
|
* [ ] Fixed-size byte array keys
|
||||||
|
@ -596,13 +596,11 @@ describe('Get value from storage', () => {
|
|||||||
|
|
||||||
describe('structs with reference type members', () => {
|
describe('structs with reference type members', () => {
|
||||||
let testReferenceStructs: Contract, storageLayout: StorageLayout;
|
let testReferenceStructs: Contract, storageLayout: StorageLayout;
|
||||||
let fixedArrayStruct: {[key: string]: any};
|
|
||||||
|
|
||||||
const stringStruct = {
|
let fixedArrayStruct: {[key: string]: any},
|
||||||
string1: 'string1',
|
bytesStruct: {[key: string]: any},
|
||||||
uint1: BigInt(123),
|
stringStruct: {[key: string]: any},
|
||||||
string2: 'string2'
|
nestedStruct: {[key: string]: any};
|
||||||
};
|
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
const TestReferenceStructs = await ethers.getContractFactory('TestReferenceStructs');
|
const TestReferenceStructs = await ethers.getContractFactory('TestReferenceStructs');
|
||||||
@ -617,6 +615,26 @@ describe('Get value from storage', () => {
|
|||||||
uintArray: [1, 2, 3, 4].map(el => BigInt(el)),
|
uintArray: [1, 2, 3, 4].map(el => BigInt(el)),
|
||||||
addressArray: signers.slice(0, 3).map(signer => signer.address.toLowerCase())
|
addressArray: signers.slice(0, 3).map(signer => signer.address.toLowerCase())
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bytesStruct = {
|
||||||
|
byteArray: ethers.utils.hexlify(ethers.utils.randomBytes(40)),
|
||||||
|
address1: signers[1].address.toLowerCase(),
|
||||||
|
uint1: BigInt(1234)
|
||||||
|
};
|
||||||
|
|
||||||
|
stringStruct = {
|
||||||
|
string1: 'string1',
|
||||||
|
int1: BigInt(123),
|
||||||
|
uint1: BigInt(456),
|
||||||
|
string2: 'string2',
|
||||||
|
address1: signers[2].address.toLowerCase(),
|
||||||
|
bool1: false
|
||||||
|
};
|
||||||
|
|
||||||
|
nestedStruct = {
|
||||||
|
bytesStruct,
|
||||||
|
address1: signers[3].address.toLowerCase()
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get all members of a struct.
|
// Get all members of a struct.
|
||||||
@ -627,13 +645,27 @@ describe('Get value from storage', () => {
|
|||||||
expect(value).to.eql(fixedArrayStruct);
|
expect(value).to.eql(fixedArrayStruct);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('get value for struct with dynamically sized byte members', async () => {
|
||||||
|
await testReferenceStructs.setBytesStruct(bytesStruct);
|
||||||
|
const blockHash = await getBlockHash();
|
||||||
|
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'bytesStruct');
|
||||||
|
expect(value).to.eql(bytesStruct);
|
||||||
|
});
|
||||||
|
|
||||||
it('get value for struct with string type members', async () => {
|
it('get value for struct with string type members', async () => {
|
||||||
await testReferenceStructs.setStringStruct(stringStruct.string1, stringStruct.uint1, stringStruct.string2);
|
await testReferenceStructs.setStringStruct(stringStruct);
|
||||||
const blockHash = await getBlockHash();
|
const blockHash = await getBlockHash();
|
||||||
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'stringStruct');
|
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'stringStruct');
|
||||||
expect(value).to.eql(stringStruct);
|
expect(value).to.eql(stringStruct);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('get value for nested struct with struct type members', async () => {
|
||||||
|
await testReferenceStructs.setNestedStruct(nestedStruct);
|
||||||
|
const blockHash = await getBlockHash();
|
||||||
|
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'nestedStruct');
|
||||||
|
expect(value).to.eql(nestedStruct);
|
||||||
|
});
|
||||||
|
|
||||||
// Get value of a member in a struct
|
// Get value of a member in a struct
|
||||||
it('get value of fixed-size array member in a struct', async () => {
|
it('get value of fixed-size array member in a struct', async () => {
|
||||||
const member = 'uintArray';
|
const member = 'uintArray';
|
||||||
@ -643,9 +675,17 @@ describe('Get value from storage', () => {
|
|||||||
expect(value).to.eql(fixedArrayStruct[member]);
|
expect(value).to.eql(fixedArrayStruct[member]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('get value of bytes member in a struct', async () => {
|
||||||
|
const member = 'byteArray';
|
||||||
|
await testReferenceStructs.setBytesStruct(bytesStruct);
|
||||||
|
const blockHash = await getBlockHash();
|
||||||
|
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'bytesStruct', member);
|
||||||
|
expect(value).to.equal(bytesStruct[member]);
|
||||||
|
});
|
||||||
|
|
||||||
it('get value of string member in a struct', async () => {
|
it('get value of string member in a struct', async () => {
|
||||||
const member = 'string2';
|
const member = 'string2';
|
||||||
await testReferenceStructs.setStringStruct(stringStruct.string1, stringStruct.uint1, stringStruct.string2);
|
await testReferenceStructs.setStringStruct(stringStruct);
|
||||||
const blockHash = await getBlockHash();
|
const blockHash = await getBlockHash();
|
||||||
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'stringStruct', member);
|
const { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'stringStruct', member);
|
||||||
expect(value).to.eql(stringStruct[member]);
|
expect(value).to.eql(stringStruct[member]);
|
||||||
@ -686,6 +726,23 @@ describe('Get value from storage', () => {
|
|||||||
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'referenceMappingStruct', member, stringKey));
|
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'referenceMappingStruct', member, stringKey));
|
||||||
expect(value).to.equal(referenceMappingStruct[member].get(stringKey));
|
expect(value).to.equal(referenceMappingStruct[member].get(stringKey));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('get value of nested struct member', async () => {
|
||||||
|
await testReferenceStructs.setNestedStruct(nestedStruct);
|
||||||
|
const blockHash = await getBlockHash();
|
||||||
|
const member = 'bytesStruct';
|
||||||
|
let { value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'nestedStruct', member);
|
||||||
|
expect(value).to.eql(nestedStruct[member]);
|
||||||
|
|
||||||
|
// Get value inside the nested struct member.
|
||||||
|
let nestedMember = 'address1';
|
||||||
|
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'nestedStruct', member, nestedMember));
|
||||||
|
expect(value).to.eql(nestedStruct[member][nestedMember]);
|
||||||
|
|
||||||
|
nestedMember = 'byteArray';
|
||||||
|
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testReferenceStructs.address, 'nestedStruct', member, nestedMember));
|
||||||
|
expect(value).to.eql(nestedStruct[member][nestedMember]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('basic mapping type', () => {
|
describe('basic mapping type', () => {
|
||||||
@ -774,10 +831,13 @@ describe('Get value from storage', () => {
|
|||||||
|
|
||||||
// Tests for reference type values.
|
// Tests for reference type values.
|
||||||
it('get value for mapping with struct type values', async () => {
|
it('get value for mapping with struct type values', async () => {
|
||||||
const expectedValue = {
|
const [signer1] = await ethers.getSigners();
|
||||||
|
|
||||||
|
const expectedValue: {[key: string]: any} = {
|
||||||
uint1: BigInt(123),
|
uint1: BigInt(123),
|
||||||
int1: BigInt(456),
|
int1: BigInt(456),
|
||||||
bool1: true
|
bool1: true,
|
||||||
|
address1: signer1.address.toLowerCase()
|
||||||
};
|
};
|
||||||
|
|
||||||
const mapKey = 123;
|
const mapKey = 123;
|
||||||
@ -787,16 +847,23 @@ describe('Get value from storage', () => {
|
|||||||
expect(value).to.eql(expectedValue);
|
expect(value).to.eql(expectedValue);
|
||||||
|
|
||||||
// Get value of specified struct member in mapping.
|
// Get value of specified struct member in mapping.
|
||||||
const structMember = 'bool1';
|
let structMember = 'bool1';
|
||||||
|
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testMappingTypes.address, 'intStructMap', mapKey, structMember));
|
||||||
|
expect(value).to.equal(expectedValue[structMember]);
|
||||||
|
|
||||||
|
structMember = 'address1';
|
||||||
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testMappingTypes.address, 'intStructMap', mapKey, structMember));
|
({ value } = await getStorageValue(storageLayout, getStorageAt, blockHash, testMappingTypes.address, 'intStructMap', mapKey, structMember));
|
||||||
expect(value).to.equal(expectedValue[structMember]);
|
expect(value).to.equal(expectedValue[structMember]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('get value for mapping of fixed size bytes keys and struct type values', async () => {
|
it('get value for mapping of fixed size bytes keys and struct type values', async () => {
|
||||||
|
const [signer1] = await ethers.getSigners();
|
||||||
|
|
||||||
const expectedValue = {
|
const expectedValue = {
|
||||||
uint1: BigInt(123),
|
uint1: BigInt(123),
|
||||||
int1: BigInt(456),
|
int1: BigInt(456),
|
||||||
bool1: true
|
bool1: true,
|
||||||
|
address1: signer1.address.toLowerCase()
|
||||||
};
|
};
|
||||||
|
|
||||||
const mapKey = ethers.utils.hexlify(ethers.utils.randomBytes(32));
|
const mapKey = ethers.utils.hexlify(ethers.utils.randomBytes(32));
|
||||||
|
@ -35,6 +35,7 @@ contract TestBasicMapping {
|
|||||||
uint128 uint1;
|
uint128 uint1;
|
||||||
int56 int1;
|
int56 int1;
|
||||||
bool bool1;
|
bool bool1;
|
||||||
|
address address1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mapping with signed integer as keys and struct type values.
|
// Mapping with signed integer as keys and struct type values.
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
pragma solidity ^0.7.0;
|
pragma solidity ^0.7.6;
|
||||||
|
pragma abicoder v2;
|
||||||
|
|
||||||
contract TestReferenceStructs {
|
contract TestReferenceStructs {
|
||||||
struct FixedArrayStruct {
|
struct FixedArrayStruct {
|
||||||
@ -10,10 +11,21 @@ contract TestReferenceStructs {
|
|||||||
|
|
||||||
FixedArrayStruct fixedArrayStruct;
|
FixedArrayStruct fixedArrayStruct;
|
||||||
|
|
||||||
|
struct BytesStruct {
|
||||||
|
bytes byteArray;
|
||||||
|
address address1;
|
||||||
|
uint256 uint1;
|
||||||
|
}
|
||||||
|
|
||||||
|
BytesStruct bytesStruct;
|
||||||
|
|
||||||
struct StringStruct {
|
struct StringStruct {
|
||||||
string string1;
|
string string1;
|
||||||
uint8 uint1;
|
uint24 uint1;
|
||||||
string string2;
|
string string2;
|
||||||
|
address address1;
|
||||||
|
bool bool1;
|
||||||
|
int24 int1;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringStruct stringStruct;
|
StringStruct stringStruct;
|
||||||
@ -33,6 +45,13 @@ contract TestReferenceStructs {
|
|||||||
|
|
||||||
ReferenceMappingStruct referenceMappingStruct;
|
ReferenceMappingStruct referenceMappingStruct;
|
||||||
|
|
||||||
|
struct NestedStruct {
|
||||||
|
BytesStruct bytesStruct;
|
||||||
|
address address1;
|
||||||
|
}
|
||||||
|
|
||||||
|
NestedStruct nestedStruct;
|
||||||
|
|
||||||
// Set variable fixedArrayStruct.
|
// Set variable fixedArrayStruct.
|
||||||
function setFixedArrayStruct(int8 int1Value, uint16[4] calldata uintArrayValue, address[3] calldata addressArrayValue) external {
|
function setFixedArrayStruct(int8 int1Value, uint16[4] calldata uintArrayValue, address[3] calldata addressArrayValue) external {
|
||||||
fixedArrayStruct.int1 = int1Value;
|
fixedArrayStruct.int1 = int1Value;
|
||||||
@ -40,11 +59,14 @@ contract TestReferenceStructs {
|
|||||||
fixedArrayStruct.addressArray = addressArrayValue;
|
fixedArrayStruct.addressArray = addressArrayValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set variable bytesStruct.
|
||||||
|
function setBytesStruct(BytesStruct calldata value) external {
|
||||||
|
bytesStruct = value;
|
||||||
|
}
|
||||||
|
|
||||||
// Set variable stringStruct.
|
// Set variable stringStruct.
|
||||||
function setStringStruct(string calldata string1Value, uint8 uint1Value, string calldata string2Value) external {
|
function setStringStruct(StringStruct calldata value) external {
|
||||||
stringStruct.string1 = string1Value;
|
stringStruct = value;
|
||||||
stringStruct.uint1 = uint1Value;
|
|
||||||
stringStruct.string2 = string2Value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set variable valueMappingStruct.
|
// Set variable valueMappingStruct.
|
||||||
@ -59,4 +81,9 @@ contract TestReferenceStructs {
|
|||||||
referenceMappingStruct.bytesAddressMap[bytesAddressKey] = bytesAddressValue;
|
referenceMappingStruct.bytesAddressMap[bytesAddressKey] = bytesAddressValue;
|
||||||
referenceMappingStruct.stringUintMap[stringUintKey] = stringUintValue;
|
referenceMappingStruct.stringUintMap[stringUintKey] = stringUintValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set variable nestedStruct.
|
||||||
|
function setNestedStruct(NestedStruct calldata value) external {
|
||||||
|
nestedStruct = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user