-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(utils): Port StringArrayUtils from set-v2-strategies [SIM-124] (#227
- Loading branch information
Showing
5 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2021 Set Labs Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
SPDX-License-Identifier: Apache License, Version 2.0 | ||
*/ | ||
|
||
pragma solidity 0.6.10; | ||
|
||
/** | ||
* @title StringArrayUtils | ||
* @author Set Protocol | ||
* | ||
* Utility functions to handle String Arrays | ||
*/ | ||
library StringArrayUtils { | ||
|
||
/** | ||
* Finds the index of the first occurrence of the given element. | ||
* @param A The input string to search | ||
* @param a The value to find | ||
* @return Returns (index and isIn) for the first occurrence starting from index 0 | ||
*/ | ||
function indexOf(string[] memory A, string memory a) internal pure returns (uint256, bool) { | ||
uint256 length = A.length; | ||
for (uint256 i = 0; i < length; i++) { | ||
if (keccak256(bytes(A[i])) == keccak256(bytes(a))) { | ||
return (i, true); | ||
} | ||
} | ||
return (uint256(-1), false); | ||
} | ||
|
||
/** | ||
* @param A The input array to search | ||
* @param a The string to remove | ||
*/ | ||
function removeStorage(string[] storage A, string memory a) | ||
internal | ||
{ | ||
(uint256 index, bool isIn) = indexOf(A, a); | ||
if (!isIn) { | ||
revert("String not in array."); | ||
} else { | ||
uint256 lastIndex = A.length - 1; // If the array would be empty, the previous line would throw, so no underflow here | ||
if (index != lastIndex) { A[index] = A[lastIndex]; } | ||
A.pop(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright 2021 Set Labs Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
SPDX-License-Identifier: Apache License, Version 2.0 | ||
*/ | ||
|
||
pragma solidity 0.6.10; | ||
pragma experimental "ABIEncoderV2"; | ||
|
||
import { StringArrayUtils } from "../lib/StringArrayUtils.sol"; | ||
|
||
|
||
contract StringArrayUtilsMock { | ||
using StringArrayUtils for string[]; | ||
|
||
string[] public storageArray; | ||
|
||
function testIndexOf(string[] memory A, string memory a) external pure returns (uint256, bool) { | ||
return A.indexOf(a); | ||
} | ||
|
||
function testRemoveStorage(string memory a) external { | ||
storageArray.removeStorage(a); | ||
} | ||
|
||
function setStorageArray(string[] memory A) external { | ||
storageArray = A; | ||
} | ||
|
||
function getStorageArray() external view returns(string[] memory) { | ||
return storageArray; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import "module-alias/register"; | ||
import { BigNumber } from "ethers"; | ||
|
||
import { Address } from "@utils/types"; | ||
import { MAX_UINT_256 } from "@utils/constants"; | ||
import { StringArrayUtilsMock } from "@utils/contracts/index"; | ||
import DeployHelper from "@utils/deploys"; | ||
import { | ||
addSnapshotBeforeRestoreAfterEach, | ||
getAccounts, | ||
getWaffleExpect, | ||
} from "@utils/test/index"; | ||
|
||
const expect = getWaffleExpect(); | ||
|
||
describe("StringArrayUtils", () => { | ||
let stringOne: string; | ||
let stringTwo: string; | ||
let stringThree: string; | ||
let unincludedString: string; | ||
let deployer: DeployHelper; | ||
|
||
let stringArrayUtils: StringArrayUtilsMock; | ||
|
||
let baseArray: Address[]; | ||
|
||
before(async () => { | ||
|
||
stringOne = "eth"; | ||
stringTwo = "to"; | ||
stringThree = "$10k"; | ||
|
||
unincludedString = "$0"; | ||
|
||
const [ owner ] = await getAccounts(); | ||
|
||
deployer = new DeployHelper(owner.wallet); | ||
stringArrayUtils = await deployer.mocks.deployStringArrayUtilsMock(); | ||
|
||
baseArray = [ stringOne, stringTwo, stringThree ]; | ||
}); | ||
|
||
addSnapshotBeforeRestoreAfterEach(); | ||
|
||
describe("#indexOf", async () => { | ||
let subjectArray: string[]; | ||
let subjectString: string; | ||
|
||
beforeEach(async () => { | ||
subjectArray = baseArray; | ||
subjectString = stringTwo; | ||
}); | ||
|
||
async function subject(): Promise<any> { | ||
return stringArrayUtils.testIndexOf(subjectArray, subjectString); | ||
} | ||
|
||
it("should return the correct index and true", async () => { | ||
const [index, isIn] = await subject(); | ||
|
||
expect(index).to.eq(BigNumber.from(1)); | ||
expect(isIn).to.be.true; | ||
}); | ||
|
||
describe("when passed address is not in array", async () => { | ||
beforeEach(async () => { | ||
subjectString = unincludedString; | ||
}); | ||
|
||
it("should return false and max number index", async () => { | ||
const [index, isIn] = await subject(); | ||
|
||
expect(index).to.eq(MAX_UINT_256); | ||
expect(isIn).to.be.false; | ||
}); | ||
}); | ||
}); | ||
|
||
describe("#removeStorage", async () => { | ||
let subjectString: string; | ||
|
||
beforeEach(async () => { | ||
await stringArrayUtils.setStorageArray(baseArray); | ||
subjectString = stringTwo; | ||
}); | ||
|
||
async function subject(): Promise<any> { | ||
return stringArrayUtils.testRemoveStorage(subjectString); | ||
} | ||
|
||
it("should make the correct updates to the storage array", async () => { | ||
await subject(); | ||
|
||
const actualArray = await stringArrayUtils.getStorageArray(); | ||
expect(JSON.stringify(actualArray)).to.eq(JSON.stringify([ stringOne, stringThree ])); | ||
}); | ||
|
||
describe("when item being removed is last in array", async () => { | ||
beforeEach(async () => { | ||
subjectString = stringThree; | ||
}); | ||
|
||
it("should just pop off last item", async () => { | ||
await subject(); | ||
|
||
const actualArray = await stringArrayUtils.getStorageArray(); | ||
expect(JSON.stringify(actualArray)).to.eq(JSON.stringify([ stringOne, stringTwo ])); | ||
}); | ||
}); | ||
|
||
describe("when passed address is not in array", async () => { | ||
beforeEach(async () => { | ||
subjectString = unincludedString; | ||
}); | ||
|
||
it("should revert", async () => { | ||
await expect(subject()).to.be.revertedWith("String not in array."); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters