Skip to content

Commit

Permalink
Merge pull request #96 from ArtBlocks/generic-many-as-Set
Browse files Browse the repository at this point in the history
  • Loading branch information
ryley-o committed Sep 13, 2022
2 parents 175ce31 + d1c70e0 commit c76a63f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/minter-suite-mapping.ts
Expand Up @@ -707,7 +707,10 @@ export function handleAddManyValueGeneric<T, C>(
if (val) {
arr = val.toArray().map<BigInt>((v: JSONValue) => v.toBigInt());
}
arr.push(event.params._value);
// only add if it doesn't exist, so we act like a Set
if (!arr.includes(event.params._value)) {
arr.push(event.params._value);
}
newValue = arrayToJSONValue(arr.toString());
} else if (
event instanceof ConfigValueAddedToSetAddress ||
Expand All @@ -730,7 +733,10 @@ export function handleAddManyValueGeneric<T, C>(
// for Bytes, use method to determine if string or hexString
stringVal = bytesToJSONValue(event.params._value).toString();
}
arr.push(stringToJSONString(stringVal));
// only add if it doesn't exist, so we act like a Set
if (!arr.includes(stringVal)) {
arr.push(stringToJSONString(stringVal));
}
newValue = arrayToJSONValue(arr.toString());
}

Expand Down
66 changes: 65 additions & 1 deletion tests/subgraph/minter-suite/minter-suite-mapping.test.ts
Expand Up @@ -1711,14 +1711,78 @@ test("handleAddManyBigIntValue should add a value to an array at a designated ke
"extraMinterDetails",
'{"array":[100]}'
);
configValueSetEvent.parameters[2] = new ethereum.EventParam(
"_value",
ethereum.Value.fromSignedBigInt(BigInt.fromI32(200))
);
handleAddManyBigIntValue(configValueSetEvent);

assert.fieldEquals(
PROJECT_MINTER_CONFIGURATION_ENTITY_TYPE,
getProjectMinterConfigId(minterAddress.toHexString(), project.id),
"extraMinterDetails",
'{"array":[100,200]}'
);
});
test("handleAddManyBigIntValue should add a single value to an array at a designated key extraMinterDetails, when duplicate of same value is added", () => {
clearStore();
const minter = addNewMinterToStore("MinterHolderV0");
const minterAddress: Address = changetype<Address>(
Address.fromHexString(minter.id)
);
const minterType = minter.type;

const projectId = BigInt.fromI32(0);
const project = addNewProjectToStore(
TEST_CONTRACT_ADDRESS,
projectId,
"project 0",
randomAddressGenerator.generateRandomAddress(),
BigInt.fromI32(0),
CURRENT_BLOCK_TIMESTAMP.minus(BigInt.fromI32(10))
);

const projectMinterConfig = addNewProjectMinterConfigToStore(
project.id,
minterAddress
);

const configValueSetEvent: ConfigValueAddedToSetBigInt = changetype<
ConfigValueAddedToSetBigInt
>(newMockEvent());
configValueSetEvent.address = minterAddress;
configValueSetEvent.parameters = [
new ethereum.EventParam(
"_projectId",
ethereum.Value.fromUnsignedBigInt(projectId)
),
new ethereum.EventParam(
"_key",
ethereum.Value.fromBytes(Bytes.fromUTF8("array"))
),
new ethereum.EventParam(
"_value",
ethereum.Value.fromSignedBigInt(BigInt.fromI32(100))
)
];
configValueSetEvent.block.timestamp = CURRENT_BLOCK_TIMESTAMP;

handleAddManyBigIntValue(configValueSetEvent);

assert.fieldEquals(
PROJECT_MINTER_CONFIGURATION_ENTITY_TYPE,
getProjectMinterConfigId(minterAddress.toHexString(), project.id),
"extraMinterDetails",
'{"array":[100,100]}'
'{"array":[100]}'
);

handleAddManyBigIntValue(configValueSetEvent);

assert.fieldEquals(
PROJECT_MINTER_CONFIGURATION_ENTITY_TYPE,
getProjectMinterConfigId(minterAddress.toHexString(), project.id),
"extraMinterDetails",
'{"array":[100]}'
);
});
test("handleAddManyAddressValue should add a value to an array at a designated key extraMinterDetails", () => {
Expand Down

0 comments on commit c76a63f

Please sign in to comment.