Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decimals on ratio outputs #176

Merged
merged 2 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions contracts/contracts/vault/Vault.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pragma solidity 0.5.11;

/*
The Vault contract stores assets. On a deposit, OUSD will be minted and sent to
the depositor. On a withdrawal, OUSD will be burned and assets will be sent to
Expand Down Expand Up @@ -594,6 +593,7 @@ contract Vault is Initializable, InitializableGovernable {
internal
returns (uint256[] memory outputs)
{
uint256 totalBalance = _checkBalance();
uint256 totalOutputValue = 0; // Running total of USD value of assets
uint256 combinedAssetValue = 0;
uint256 assetCount = getAssetCount();
Expand All @@ -602,20 +602,22 @@ contract Vault is Initializable, InitializableGovernable {
// Initialise arrays
// Price of each asset in USD in 1e18
uint256[] memory assetPrices = new uint256[](assetCount);
uint256[] memory assetDecimals = new uint256[](assetCount);
outputs = new uint256[](assetCount);

for (uint256 i = 0; i < allAssets.length; i++) {
uint256 assetDecimals = Helpers.getDecimals(allAssets[i]);
assetDecimals[i] = Helpers.getDecimals(allAssets[i]);
// Get all the USD prices of the asset in 1e18
assetPrices[i] = _priceUSDMax(
allAssets[i],
uint256(1).scaleBy(int8(assetDecimals))
uint256(1).scaleBy(int8(assetDecimals[i]))
);

// Get the proportional amount of this token for the redeem in 1e18
uint256 proportionalAmount = _checkBalance(allAssets[i])
.scaleBy(int8(18 - assetDecimals))
.scaleBy(int8(18 - assetDecimals[i]))
.mul(_amount)
.div(_checkBalance());
.div(totalBalance);

if (proportionalAmount > 0) {
// Non zero output means this asset is contributing to the
Expand All @@ -629,7 +631,7 @@ contract Vault is Initializable, InitializableGovernable {
);
// Save the output amount in the decimals of the asset
outputs[i] = proportionalAmount.scaleBy(
int8(assetDecimals - 18)
int8(assetDecimals[i] - 18)
);
}
}
Expand All @@ -642,14 +644,19 @@ contract Vault is Initializable, InitializableGovernable {
for (uint256 i = 0; i < outputs.length; i++) {
if (outputs[i] == 0) continue;

uint256 adjustment = 0;
if (outputValueDiff < 0) {
outputs[i] -= uint256(-outputValueDiff)
.divPrecisely(combinedAssetValue)
.div(redeemAssetCount);
adjustment = uint256(-outputValueDiff)
.div(combinedAssetValue)
.divPrecisely(redeemAssetCount)
.scaleBy(int8(assetDecimals[i] - 18));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any circumstance where combinedAssetValue would be greater than outputValueDiff?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I had this wrong though, should be updated now

outputs[i] -= adjustment;
} else if (outputValueDiff > 0) {
outputs[i] += uint256(outputValueDiff)
.divPrecisely(combinedAssetValue)
.div(redeemAssetCount);
adjustment = uint256(outputValueDiff)
.div(combinedAssetValue)
.divPrecisely(redeemAssetCount)
.scaleBy(int8(assetDecimals[i] - 18));
outputs[i] += adjustment;
}
}
}
Expand Down
31 changes: 23 additions & 8 deletions contracts/test/vault/redeem.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ describe("Vault Redeem", function () {
await vault.connect(anna).redeemAll();

// 100 USDC and 350 DAI in contract
// 100/450 * 250 USDC + existing 1000
// 350/450 * 250 DAI + existing 1000
await expect(anna).has.an.approxBalanceOf("1044.44", dai);
// (1000-100) + 100/450 * 250 USDC
// (1000-150) + 350/450 * 250 DAI
await expect(anna).has.an.approxBalanceOf("955.55", usdc);
await expect(anna).has.an.approxBalanceOf("1044.44", dai);
});

it("Should redeem entire OUSD balance, with a higher oracle price", async () => {
Expand All @@ -166,18 +166,33 @@ describe("Vault Redeem", function () {
await vault.connect(anna).mint(dai.address, daiUnits("150.0"));
await expect(anna).has.a.balanceOf("250.00", ousd);

await setOracleTokenPriceUsd("DAI", "1.20");
await setOracleTokenPriceUsd("USDC", "1.30");
await setOracleTokenPriceUsd("DAI", "1.20");
await vault.connect(governor).rebase();

// Anna's share of OUSD is 200/450
// Vault has 100 USDC and 350 DAI total
// 200/450 * 100 * 1.3 + 350 * 1.2
await expect(anna).has.an.approxBalanceOf("305.55", ousd);

// Withdraw all
await ousd.connect(anna).approve(vault.address, ousdUnits("250.0"));
await vault.connect(anna).redeemAll();

// 100 USDC and 350 DAI in contract
// 100/450 * 250 USDC + existing 1000
// 350/450 * 250 DAI + existing 1000
await expect(anna).has.an.approxBalanceOf("1", dai); // To be mathed out
await expect(anna).has.an.approxBalanceOf("1", usdc); // To be mathed out
// 100 * 1.30 = 130 USD value for USDC
// 350 * 1.20 = 420 USD value for DAI
// 100/450 * 305.5555555 = 67.9012345556 USDC
// 350/450 * 305.5555555 = 237.654320944 DAI
// Difference between output value and redeem value is:
// 305.5555555 - (67.9012345556 * 1.3 + 237.654320944 * 1.20) = -67.9012345551
// Remove 67.9012345551/2.5/2 from USDC
// Remove 67.9012345551/2.5/2 from DAI
// (1000-100) + 100/450 * 305.5555555 - 67.9012345551/2.5/2 USDC
// (1000-150) + 350/450 * 305.5555555 - 67.9012345551/2.5/2 DAI
await expect(anna).has.an.approxBalanceOf("954.32", usdc); // To be mathed out
await expect(anna).has.an.approxBalanceOf("1074.07", dai); // To be mathed out
});

it("Should redeem entire OUSD balance, with a lower oracle price");
});