Skip to content

Commit

Permalink
Fix expired offer acceptance bug (#68)
Browse files Browse the repository at this point in the history
- Fix start index optimization for token offers
- Fix calculation of withdrawable amount
- Add some utility tasks
close #67
  • Loading branch information
sirnicolaz authored May 17, 2023
1 parent 386d662 commit 9e3c107
Show file tree
Hide file tree
Showing 18 changed files with 1,586 additions and 286 deletions.
491 changes: 491 additions & 0 deletions .openzeppelin/unknown-9000.json

Large diffs are not rendered by default.

690 changes: 672 additions & 18 deletions .openzeppelin/unknown-9001.json

Large diffs are not rendered by default.

40 changes: 22 additions & 18 deletions contracts/InternalMarket/InternalMarketBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,24 @@ contract InternalMarketBase {

function _beforeWithdraw(address from, uint256 amount) internal virtual {
Offers storage offers = _offers[from];
uint256 offered = 0;

for (uint128 i = offers.start; i < offers.end && amount > 0; i++) {
for (uint128 i = offers.start; i < offers.end; i++) {
Offer storage offer = offers.offer[i];

if (block.timestamp > offer.expiredAt) {
// FIXME it was > not >=
if (amount >= offer.amount) {
amount -= offer.amount;
_vaultContributors[from] -= offer.amount;
delete offers.offer[offers.start++];
} else {
offer.amount -= amount;
_vaultContributors[from] -= amount;
amount = 0;
}
if (block.timestamp >= offer.expiredAt) {
// clean up old offers
delete offers.offer[offers.start++];
} else {
offered += offer.amount;
}
}

require(amount == 0, "InternalMarket: amount exceeds balance");
require(
_vaultContributors[from] >= offered + amount,
"InternalMarket: amount exceeds balance"
);
_vaultContributors[from] -= amount;
}

function _beforeMatchOffer(
Expand All @@ -142,7 +141,11 @@ contract InternalMarketBase {

for (uint128 i = offers.start; i < offers.end && amount > 0; i++) {
Offer storage offer = offers.offer[i];
if (block.timestamp < offer.expiredAt) {

if (block.timestamp >= offer.expiredAt) {
// clean up old offers
delete offers.offer[offers.start++];
} else {
// If offer is active check if the amount is bigger than the
// current offer.
if (amount >= offer.amount) {
Expand All @@ -151,6 +154,7 @@ contract InternalMarketBase {

// Remove the offer
emit OfferMatched(i, from, to, offer.amount);

delete offers.offer[offers.start++];
// If the amount is smaller than the offer amount, then
} else {
Expand Down Expand Up @@ -257,16 +261,16 @@ contract InternalMarketBase {
Offers storage offers = _offers[account];

uint256 vault = _vaultContributors[account];
uint256 unlocked = 0;
uint256 offered = 0;

for (uint128 i = offers.start; i < offers.end; i++) {
Offer storage offer = offers.offer[i];

if (block.timestamp > offer.expiredAt) {
unlocked += offer.amount;
if (block.timestamp < offer.expiredAt) {
offered += offer.amount;
}
}
return (vault - unlocked, unlocked);
return (offered, vault - offered);
}

// Tokens owned by a contributor that are offered to other contributors
Expand Down
2 changes: 1 addition & 1 deletion deployments/9000.GovernanceToken.arguments.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["0x4e0cf41aB19Ae1791e1B5e2D722f860Db6585CA9","NEOKingdom DAO Governance","NEOKGOV"]
["0x8D50cda929a40E4948Dd45668eF97a4AF8EC7D33","NEOKingdom DAO Governance","NEOKGOV"]
2 changes: 1 addition & 1 deletion deployments/9000.InternalMarket.arguments.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["0x4e0cf41aB19Ae1791e1B5e2D722f860Db6585CA9","0x6Cf70E87B2650823bca4FdbE0Fc02167989e7B27"]
["0x8D50cda929a40E4948Dd45668eF97a4AF8EC7D33","0x35379a04024C6e803a44c31fb0fB02f13Da98B39"]
2 changes: 1 addition & 1 deletion deployments/9000.RedemptionController.arguments.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["0x4e0cf41aB19Ae1791e1B5e2D722f860Db6585CA9"]
["0x8D50cda929a40E4948Dd45668eF97a4AF8EC7D33"]
2 changes: 1 addition & 1 deletion deployments/9000.ResolutionManager.arguments.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["0x4e0cf41aB19Ae1791e1B5e2D722f860Db6585CA9","0xFCaAFD3e43753D170C3E9baf1cD5A501c4AB2f3e","0x6Cf70E87B2650823bca4FdbE0Fc02167989e7B27","0x805762236A93aEe6fec5B5979aaEF0Af07208a45"]
["0x8D50cda929a40E4948Dd45668eF97a4AF8EC7D33","0x3bC64a0C8049c14C71bD03fd4149E864a2E539a5","0x35379a04024C6e803a44c31fb0fB02f13Da98B39","0xBff36aAf299e04ADF587a1B71f9109510cDfC1Dc"]
2 changes: 1 addition & 1 deletion deployments/9000.ShareholderRegistry.arguments.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["0x4e0cf41aB19Ae1791e1B5e2D722f860Db6585CA9","NeokingdomDAO Share","NEOKSHARE"]
["0x8D50cda929a40E4948Dd45668eF97a4AF8EC7D33","NeokingdomDAO Share","NEOKSHARE"]
2 changes: 1 addition & 1 deletion deployments/9000.Voting.arguments.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["0x4e0cf41aB19Ae1791e1B5e2D722f860Db6585CA9"]
["0x8D50cda929a40E4948Dd45668eF97a4AF8EC7D33"]
66 changes: 33 additions & 33 deletions deployments/9000.network.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
{
"DAORoles": {
"address": "0x4e0cf41aB19Ae1791e1B5e2D722f860Db6585CA9",
"blockNumber": 14023613,
"blockHash": "0x618e2325c4c7ca5b0bc2fd8ee77d63bceaf7b124c95abb99b87664dcf704656d"
"address": "0x8D50cda929a40E4948Dd45668eF97a4AF8EC7D33",
"blockNumber": 14292733,
"blockHash": "0x7c4895b40cfa11592962dd5e9d7857eaf9641eefb24d51d5ede3c27e56d9f83e"
},
"TokenMock": {
"address": "0x7D8A8A33cfcd201a905efE4c4804e25397bbdF25",
"blockNumber": 14023615,
"blockHash": "0x04fc147910b2dad9079319d029bbe1b6495ddf10fd01e930568d880e6f5f84ab"
"address": "0x85BF065bAd3C2159Ec01678a0E595D4f1E586250",
"blockNumber": 14292734,
"blockHash": "0x4b818a9afa34a85021aceca69a5557b936944c9a39a9895dfc28f5dcb7caa70b"
},
"PriceOracle": {
"address": "0xf416Ac72BE6DA048b1c5D88A4F30db64459c54ed",
"blockNumber": 14023617,
"blockHash": "0x7314dd81bd9338f1d1651ca0363ad4dd8338a13f5899e63c14799e4962a6ee0f"
"address": "0x77e69A27D7E2D747C36E3c5E6ec0BA497A9E6bA4",
"blockNumber": 14292737,
"blockHash": "0x7a3d0e915792be657dacd60e6f34fc2550962491dbd728ec709911f7c643cab7"
},
"Voting": {
"address": "0x805762236A93aEe6fec5B5979aaEF0Af07208a45",
"blockNumber": 14023620,
"blockHash": "0xa35c29c872af9714744182b5bc68d33fdde09f2e06b4abdb8c096d9dedf292f7"
"address": "0xBff36aAf299e04ADF587a1B71f9109510cDfC1Dc",
"blockNumber": 14292738,
"blockHash": "0xaae7f1e6bfb06722b598ff2db15424612887e9d9f2d78cb99c0db4b43f6c9cee"
},
"GovernanceToken": {
"address": "0x6Cf70E87B2650823bca4FdbE0Fc02167989e7B27",
"blockNumber": 14023623,
"blockHash": "0xb7b08916d695eb740aaa05a12d75e72a0bcc46b7e97945f1c76d974ea44c1184"
"address": "0x35379a04024C6e803a44c31fb0fB02f13Da98B39",
"blockNumber": 14292740,
"blockHash": "0xb1b83bf55e0df9728f75f1799ebc77e0fc11fe3ae5fb970f4f18121b2aadd858"
},
"NeokingdomToken": {
"address": "0xb7eEE65644b3f6195C4446b8Ba4F1990BC93FB77",
"blockNumber": 14023626,
"blockHash": "0x90781f6dcebc8a3ee8a60c50beb95de758f57c5aec91a9b304737ec99b22584f"
"address": "0xE29Fb8D3C70B11cAaD48bcE19Cb35CA83C3C1Fd0",
"blockNumber": 14292741,
"blockHash": "0x333f9e91c8ed124effdfa6603b1d223242017ebbe3306df981b4248cd21880ca"
},
"RedemptionController": {
"address": "0x031cD3BAD9130d368Dd5C850f0c201CF95Ce07D5",
"blockNumber": 14023627,
"blockHash": "0x9f9b6e46dd07298669e93bf04e4a6b015804afc5044c967be3973dccb3aafe15"
"address": "0x0F3643F205F0CdFA674E6C83CDC6eFdBc2A0C744",
"blockNumber": 14292745,
"blockHash": "0xc5e872e71e457d0ce497ad4acd68c066e2300ca25f7cf583a426228799f41de2"
},
"InternalMarket": {
"address": "0xdc8f9BAc31ed959FCc0AdCFB4b59a1d4B87F36Aa",
"blockNumber": 14023630,
"blockHash": "0x3ff6b135b18b389039e93196ccd56bcfc28f756a08575f4aa2c6c338f3f949d6"
"address": "0x0d5Fe1eaE52C99Fa4b2330D4D1e46f7E145223e6",
"blockNumber": 14292747,
"blockHash": "0x1452c70ea60a095b9e8fa98f0636f4bef7a7f7d76800694c3a80276d08a6df00"
},
"ShareholderRegistry": {
"address": "0xFCaAFD3e43753D170C3E9baf1cD5A501c4AB2f3e",
"blockNumber": 14023633,
"blockHash": "0xb5cef43348c1c37d6b0eadddcaa4641c24886373ff8322e58b86273665d7faba"
"address": "0x3bC64a0C8049c14C71bD03fd4149E864a2E539a5",
"blockNumber": 14292749,
"blockHash": "0xe3028f3dc5f2fc16218f009a6096b206d03ee40053073f6452993cebdcab3c61"
},
"ResolutionManager": {
"address": "0x4296d7b37F7d23A8910BF849E1F6ff9a80F68242",
"blockNumber": 14023638,
"blockHash": "0x6903d980644bf1b65d851fd14546e1fbc1f6652bd59b6df3ebf8b8192117e35f"
"address": "0x717c51FB4CE06b41dD1160C0d899aaC294c997e6",
"blockNumber": 14292750,
"blockHash": "0x40f9ed15c5348e6f1bd0529cac9b24eef92c6650c3a76ee8c5198733d2882557"
},
"ProxyAdmin": {
"address": "0x7f1E2778dA4B6c9ce90cEb27835c29e5dF29b062",
"blockNumber": 14024376,
"blockHash": "0x63d209ead3e2b72ce4d6b7ae31fb6484a29d65111cde2420aef2c600372425e3"
"address": "0xB0bF2ca48Bd4A143C96E706E02C4a23bf7790Ce8",
"blockNumber": 14292751,
"blockHash": "0xee1fd488312de200bf74688c3e285211600eb866d63f5f8477936e8d5efc83fe"
}
}
6 changes: 6 additions & 0 deletions dev-wallets.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@
"address": "0xbded7c8a2efa4cf2ee6c953e6447a246f3ac4e12",
"status": "board",
"tokens": "22000000000000000000000"
},
{
"name": "nicola",
"address": "0x2e1aF63Cd595A6D715E5e2D92151801F0d406a6b",
"status": "contributor",
"tokens": "22000000000000000000000"
}
]
11 changes: 11 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ const config: HardhatUserConfig = {
browserURL: "https://escan.live",
},
},
{
network: "tevmos",
chainId: 9000,
urls: {
apiURL: "https://testnet.escan.live/api",
browserURL: "https://testnet.escan.live",
},
},
],
},
gasReporter: {
Expand All @@ -122,6 +130,9 @@ const config: HardhatUserConfig = {
typechain: {
outDir: "./typechain",
},
mocha: {
bail: true,
},
};

export default config;
15 changes: 15 additions & 0 deletions tasks/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { task } from "hardhat/config";

import { NeokingdomDAOHardhat } from "../lib";

task("admin:transfer", "Transfer ProxyAdmin ownership")
.addPositionalParam("address", "Requested address")
.setAction(async ({ address }: { address: string }, hre) => {
const neokingdom = await NeokingdomDAOHardhat.initialize(hre);
const contracts = await neokingdom.loadContracts();

const tx = await contracts.proxyAdmin.transferOwnership(address);
await tx.wait(1);

console.log("Done");
});
3 changes: 2 additions & 1 deletion tasks/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import "./admin";
import "./delegate";
import "./deploy";
import "./oracle";
import "./resolution";
import "./role";
import "./status";
import "./tokens";
import "./types";
import "./upgrade";
38 changes: 0 additions & 38 deletions tasks/role.ts

This file was deleted.

28 changes: 28 additions & 0 deletions tasks/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { formatEther } from "ethers/lib/utils";
import { task } from "hardhat/config";

import { NeokingdomDAOHardhat } from "../lib";

task("tokens:offered", "Offered balance")
.addPositionalParam("address", "Requested address")
.setAction(async ({ address }: { address: string }, hre) => {
const neokingdom = await NeokingdomDAOHardhat.initialize(hre);
const contracts = await neokingdom.loadContracts();

const balance = await contracts.internalMarket.offeredBalanceOf(address);

console.log(`Balance of ${address}: ${formatEther(balance)}`);
});

task("tokens:withdrawable", "Withdrawable balance")
.addPositionalParam("address", "Requested address")
.setAction(async ({ address }: { address: string }, hre) => {
const neokingdom = await NeokingdomDAOHardhat.initialize(hre);
const contracts = await neokingdom.loadContracts();

const balance = await contracts.internalMarket.withdrawableBalanceOf(
address
);

console.log(`Balance of ${address}: ${formatEther(balance)}`);
});
Loading

0 comments on commit 9e3c107

Please sign in to comment.