-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add stringFloatToUnit string utils
- Loading branch information
gmtesla
committed
Sep 23, 2022
1 parent
d119cca
commit 7154d9a
Showing
3 changed files
with
89 additions
and
1 deletion.
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
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,14 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity ^0.8.9; | ||
|
||
import '../libraries/StringUtils.sol'; | ||
|
||
contract MockStringUtils { | ||
function stringFloatToUnit(bytes memory value) | ||
external | ||
pure | ||
returns (uint256) | ||
{ | ||
return StringUtils.stringFloatToUnit(value); | ||
} | ||
} |
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,26 @@ | ||
import { expect } from "chai"; | ||
import { utils } from "ethers"; | ||
import { ethers } from "hardhat"; | ||
import { CONTRACT_NAMES } from "../../constants"; | ||
import { MockStringUtils } from "../../typechain"; | ||
|
||
describe(CONTRACT_NAMES.StringUtils, () => { | ||
let stringUtils: MockStringUtils; | ||
before(async () => { | ||
const MockStringUtils = await ethers.getContractFactory("MockStringUtils"); | ||
stringUtils = <MockStringUtils>await MockStringUtils.deploy(); | ||
await stringUtils.deployed(); | ||
}) | ||
describe("stringFloatToUnit", () => { | ||
it("should return unit from bytes that has less than 18 precision", async () => { | ||
const value = 3.14159265359 | ||
const uint = await stringUtils.stringFloatToUnit(utils.formatBytes32String(value.toString())); | ||
expect(utils.formatUnits(uint, 18)).to.be.equal(value.toString()); | ||
}) | ||
it("should cut decimals if it has 18+ decimals", async () => { | ||
const value = 3.1415926535897932384626 // 22 decimals | ||
const uint = await stringUtils.stringFloatToUnit(utils.formatBytes32String(value.toString())); | ||
expect(utils.formatUnits(uint, 18)).to.be.equal('3.141592653589793'); | ||
}) | ||
}) | ||
}) |