Skip to content

Commit cbf377d

Browse files
authored
Use high resolution credits on new contract accounts. (#603)
Increasing resolution on new, non-rebasing contracts.
1 parent eba2f83 commit cbf377d

3 files changed

Lines changed: 64 additions & 7 deletions

File tree

contracts/contracts/token/OUSD.sol

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,22 @@ contract OUSD is Initializable, InitializableERC20Detailed, Governable {
382382
*/
383383
function _ensureRebasingMigration(address _account) internal {
384384
if (nonRebasingCreditsPerToken[_account] == 0) {
385-
// Set fixed credits per token for this account
386-
nonRebasingCreditsPerToken[_account] = rebasingCreditsPerToken;
387-
// Update non rebasing supply
388-
nonRebasingSupply = nonRebasingSupply.add(balanceOf(_account));
389-
// Update credit tallies
390-
rebasingCredits = rebasingCredits.sub(_creditBalances[_account]);
385+
if (_creditBalances[_account] == 0) {
386+
// Since there is no existing balance, we can directly set to
387+
// high resolution, and do not have to do any other bookkeeping
388+
nonRebasingCreditsPerToken[_account] = 1e27;
389+
} else {
390+
// Migrate an existing account:
391+
392+
// Set fixed credits per token for this account
393+
nonRebasingCreditsPerToken[_account] = rebasingCreditsPerToken;
394+
// Update non rebasing supply
395+
nonRebasingSupply = nonRebasingSupply.add(balanceOf(_account));
396+
// Update credit tallies
397+
rebasingCredits = rebasingCredits.sub(
398+
_creditBalances[_account]
399+
);
400+
}
391401
}
392402
}
393403

contracts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"echidna": "yarn run clean && echidna-test . --contract PropertiesOUSDTransferable --config contracts/crytic/TestOUSDTransferable.yaml",
2323
"compute-merkle-proofs-local": "HARDHAT_NETWORK=localhost node scripts/staking/airDrop.js reimbursements.csv scripts/staking/merkleProofedAccountsToBeCompensated.json && cp scripts/staking/merkleProofedAccountsToBeCompensated.json ../dapp/src/constants/merkleProofedAccountsToBeCompensated.json",
2424
"compute-merkle-proofs-mainnet": "HARDHAT_NETWORK=mainnet node scripts/staking/airDrop.js reimbursements.csv scripts/staking/merkleProofedAccountsToBeCompensated.json && cp scripts/staking/merkleProofedAccountsToBeCompensated.json ../dapp/src/constants/merkleProofedAccountsToBeCompensated.json",
25-
"slither": "yarn run clean && slither . --filter-paths \"crytic|mocks|@openzeppelin\" --exclude-low --exclude-informational --exclude conformance-to-solidity-naming-conventions,different-pragma-directives-are-used,external-function,assembly",
25+
"slither": "yarn run clean && slither . --filter-paths \"crytic|mocks|@openzeppelin\" --exclude-low --exclude-informational --exclude conformance-to-solidity-naming-conventions,different-pragma-directives-are-used,external-function,assembly,incorrect-equality",
2626
"clean": "rm -rf build crytic-export artifacts cache deployments/local*"
2727
},
2828
"author": "Origin Protocol Inc <support@originprotocol.com>",

contracts/test/token.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,4 +828,51 @@ describe("Token", function () {
828828
await ousd.totalSupply()
829829
);
830830
});
831+
832+
it("Should exact transfer to new contract accounts", async () => {
833+
let { ousd, vault, matt, usdc, mockNonRebasing } = await loadFixture(
834+
defaultFixture
835+
);
836+
837+
// Add yield to so we need higher resolution
838+
await usdc.connect(matt).mint(usdcUnits("9671.2345"));
839+
await usdc.connect(matt).transfer(vault.address, usdcUnits("9671.2345"));
840+
await vault.rebase();
841+
842+
// Helper to verify balance-exact transfers in
843+
const checkTransferIn = async (amount) => {
844+
const beforeReceiver = await ousd.balanceOf(mockNonRebasing.address);
845+
await ousd.connect(matt).transfer(mockNonRebasing.address, amount);
846+
const afterReceiver = await ousd.balanceOf(mockNonRebasing.address);
847+
expect(beforeReceiver.add(amount)).to.equal(afterReceiver);
848+
};
849+
850+
// Helper to verify balance-exact transfers out
851+
const checkTransferOut = async (amount) => {
852+
const beforeReceiver = await ousd.balanceOf(mockNonRebasing.address);
853+
await mockNonRebasing.transfer(matt.address, amount);
854+
const afterReceiver = await ousd.balanceOf(mockNonRebasing.address);
855+
expect(beforeReceiver.sub(amount)).to.equal(afterReceiver);
856+
};
857+
858+
// In
859+
await checkTransferIn(1);
860+
await checkTransferIn(2);
861+
await checkTransferIn(5);
862+
await checkTransferIn(9);
863+
await checkTransferIn(100);
864+
await checkTransferIn(2);
865+
await checkTransferIn(5);
866+
await checkTransferIn(9);
867+
868+
// Out
869+
await checkTransferOut(1);
870+
await checkTransferOut(2);
871+
await checkTransferOut(5);
872+
await checkTransferOut(9);
873+
await checkTransferOut(100);
874+
await checkTransferOut(2);
875+
await checkTransferOut(5);
876+
await checkTransferOut(9);
877+
});
831878
});

0 commit comments

Comments
 (0)