Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #72 from hermeznetwork/feature/fixBucketEth
Browse files Browse the repository at this point in the history
fix bucket eth case
  • Loading branch information
invocamanman authored Mar 23, 2021
2 parents f3f4c08 + 89b51b3 commit d439b23
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
19 changes: 11 additions & 8 deletions contracts/hermez/lib/InstantWithdrawManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,18 @@ contract InstantWithdrawManager is HermezHelpers {
uint256(tokenExchange[tokenAddress])) / _EXCHANGE_MULTIPLIER;

uint8 decimals;

// if decimals() is not implemented 0 decimals are assumed
(bool success, bytes memory data) = tokenAddress.staticcall(
abi.encodeWithSelector(_ERC20_DECIMALS)
);
if (success) {
decimals = abi.decode(data, (uint8));
// in case of ether, set 18 decimals
if (tokenAddress == address(0)) {
decimals = 18;
} else {
// if decimals() is not implemented 0 decimals are assumed
(bool success, bytes memory data) = tokenAddress.staticcall(
abi.encodeWithSelector(_ERC20_DECIMALS)
);
if (success) {
decimals = abi.decode(data, (uint8));
}
}

require(
decimals < 77,
"InstantWithdrawManager::_token2USD: TOKEN_DECIMALS_OVERFLOW"
Expand Down
37 changes: 37 additions & 0 deletions test/hermez/HermezWithdraw.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,44 @@ describe("Hermez instant withdraw manager", function () {
});

describe("Instant withdraw functionality", function () {
it("test token/eth to usd", async function () {
const tokenAddress = hardhatTokenERC20Mock.address;
const ethereumAddress = "0x0000000000000000000000000000000000000000"
const tokenPriceERC20 = 10 * _EXCHANGE_MULTIPLIER; //USD
const ethereumPrice = 1800 * _EXCHANGE_MULTIPLIER; //USD

const addressArray = [tokenAddress, ethereumAddress];
const valueArray = [tokenPriceERC20, ethereumPrice];

await expect(hardhatHermez
.connect(governance)
.updateTokenExchange(addressArray, valueArray))
.to.emit(hardhatHermez, "UpdateTokenExchange")
.withArgs(addressArray, valueArray);

expect(
await hardhatHermez.tokenExchange(tokenAddress)
).to.equal(valueArray[0]);

expect(
await hardhatHermez.tokenExchange(ethereumAddress)
).to.equal(valueArray[1]);

const tokenAmount = 2;
const tokenAmountDecimals = ethers.utils.parseEther(
tokenAmount.toString()
); // 18 decimals

const resultUSDToken = tokenPriceERC20 / _EXCHANGE_MULTIPLIER * tokenAmount;
const resultUSDEthereum = ethereumPrice / _EXCHANGE_MULTIPLIER * tokenAmount;
expect(
await hardhatHermez.token2USDTest(tokenAddress, tokenAmountDecimals)
).to.equal(resultUSDToken);
expect(
await hardhatHermez.token2USDTest(ethereumAddress, tokenAmountDecimals)
).to.equal(resultUSDEthereum);
});

it("test Helpers pack/unpack function matches SC", async function () {
const numBuckets = 5;
const buckets = [];
Expand Down

0 comments on commit d439b23

Please sign in to comment.