forked from SetProtocol/set-protocol-v2
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(BytesArrayUtils): Add BytesArrayUtils library (SetProtocol#248)
* Add BytesLibUtils library * Rename to BytesArrayUtils; Add mock contract * Add unit tests * Use BytesArrayUtils in UniswapV3ExchangeAdapterV2 * Add missing import * Add BytesLib path to javadocs
- Loading branch information
Sachin
authored
Apr 14, 2022
1 parent
2e5bad8
commit 7b35392
Showing
7 changed files
with
197 additions
and
90 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,49 @@ | ||
/* | ||
Copyright 2022 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 BytesArrayUtils | ||
* @author Set Protocol | ||
* | ||
* Utility library to type cast bytes arrays. Extends BytesLib (external/contracts/uniswap/v3/lib/BytesLib.sol) | ||
* library functionality. | ||
*/ | ||
library BytesArrayUtils { | ||
|
||
/** | ||
* Type cast byte to boolean. | ||
* @param _bytes Bytes array | ||
* @param _start Starting index | ||
* @return bool Boolean value | ||
*/ | ||
function toBool(bytes memory _bytes, uint256 _start) internal pure returns (bool) { | ||
require(_start + 1 >= _start, "toBool_overflow"); | ||
require(_bytes.length >= _start + 1, "toBool_outOfBounds"); | ||
uint8 tempUint; | ||
|
||
assembly { | ||
tempUint := mload(add(add(_bytes, 0x1), _start)) | ||
} | ||
|
||
require(tempUint <= 1, "Invalid bool data"); // Should be either 0 or 1 | ||
|
||
return (tempUint == 0) ? false : true; | ||
} | ||
} |
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,31 @@ | ||
/* | ||
Copyright 2022 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 { BytesArrayUtils } from "../lib/BytesArrayUtils.sol"; | ||
|
||
|
||
contract BytesArrayUtilsMock { | ||
using BytesArrayUtils for bytes; | ||
|
||
function testToBool(bytes memory _bytes, uint256 _start) external pure returns (bool) { | ||
return _bytes.toBool(_start); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import "module-alias/register"; | ||
import { BigNumber } from "ethers"; | ||
import { solidityPack } from "ethers/lib/utils"; | ||
|
||
import { Address, Bytes } from "@utils/types"; | ||
import { Account } from "@utils/test/types"; | ||
import { MAX_UINT_256 } from "@utils/constants"; | ||
import { BytesArrayUtilsMock } from "@utils/contracts"; | ||
import DeployHelper from "@utils/deploys"; | ||
import { | ||
addSnapshotBeforeRestoreAfterEach, | ||
getAccounts, | ||
getWaffleExpect, | ||
getRandomAddress | ||
} from "@utils/test/index"; | ||
|
||
const expect = getWaffleExpect(); | ||
|
||
describe("BytesArrayUtils", () => { | ||
let owner: Account; | ||
let deployer: DeployHelper; | ||
|
||
let bytesArrayUtils: BytesArrayUtilsMock; | ||
|
||
|
||
before(async () => { | ||
[ | ||
owner, | ||
] = await getAccounts(); | ||
|
||
deployer = new DeployHelper(owner.wallet); | ||
bytesArrayUtils = await deployer.mocks.deployBytesArrayUtilsMock(); | ||
}); | ||
|
||
addSnapshotBeforeRestoreAfterEach(); | ||
|
||
describe("#toBool", async () => { | ||
let bool: boolean; | ||
let randomAddress: Address; | ||
|
||
let subjectBytes: Bytes; | ||
let subjectStart: BigNumber; | ||
|
||
before(async () => { | ||
randomAddress = await getRandomAddress(); | ||
}); | ||
|
||
beforeEach(async() => { | ||
bool = true; | ||
|
||
subjectBytes = solidityPack( | ||
["address", "bool"], | ||
[randomAddress, bool] | ||
); | ||
subjectStart = BigNumber.from(20); // Address is 20 bytes long | ||
}); | ||
|
||
async function subject(): Promise<boolean> { | ||
return await bytesArrayUtils.testToBool(subjectBytes, subjectStart); | ||
} | ||
|
||
it("should return correct bool", async () => { | ||
const actualBool = await subject(); | ||
|
||
expect(actualBool).to.eq(bool); | ||
}); | ||
|
||
describe("when bool is false", async () => { | ||
beforeEach(async() => { | ||
bool = false; | ||
|
||
subjectBytes = solidityPack( | ||
["address", "bool"], | ||
[randomAddress, bool] | ||
); | ||
}); | ||
|
||
it("should return correct bool", async () => { | ||
const actualBool = await subject(); | ||
|
||
expect(actualBool).to.eq(bool); | ||
}); | ||
}); | ||
|
||
describe("when start is max uint 256", async () => { | ||
beforeEach(() => { | ||
subjectStart = MAX_UINT_256; | ||
}); | ||
|
||
it("should revert", async () => { | ||
await expect(subject()).to.be.revertedWith("toBool_overflow"); | ||
}); | ||
}); | ||
|
||
|
||
describe("when start is out of bounds", async () => { | ||
beforeEach(() => { | ||
subjectStart = BigNumber.from(subjectBytes.length); | ||
}); | ||
|
||
it("should revert", async () => { | ||
await expect(subject()).to.be.revertedWith("toBool_outOfBounds"); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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