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

[BatchExchangeViewer] allow to fetch balances #540

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 37 additions & 0 deletions contracts/BatchExchangeViewer.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t know if this is supported by current versions of web3 and ethabi and hence ethcontract. I’ll have to check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me know. If it's supported I'd love to refactor the orderbook fetching to also return list of structs instead of byte arrays (only in the Viewer contract though as we don't want experimental features in the core contract).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will go ahead and merge this now. As we are not depending on it yet it shouldn't cause any incompatibilities and we can change it later on if we need a different encoding for ethcontracts

Copy link
Contributor

Choose a reason for hiding this comment

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

Any news on the support check @nlordell ?

Copy link
Contributor

@nlordell nlordell Feb 24, 2020

Choose a reason for hiding this comment

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

Sorry, forgot to update here. No this is currently not supported by either web3 crate or ethcontract 😢

That being said, adding support is feasible. I can take a look at doing this later in the week.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this mean even the other methods cannot be deserialized or only the ones that would be returning a struct? I don't know if ABIEncoderV2 changes the encoding for "traditional" return types as well.


import "solidity-bytes-utils/contracts/BytesLib.sol";
import "openzeppelin-solidity/contracts/math/Math.sol";
import "./BatchExchange.sol";


Expand All @@ -16,6 +18,41 @@ contract BatchExchangeViewer {
batchExchange = exchange;
}

struct Balance {
address token; // The token for which this balance is reported
uint256 availableSellAmount; // Amount available to trade in the auction for which it was requested
uint256 pendingDeposit; // Amount that was deposited in the requested auction. Will be tradeable in the next auction
uint256 withdrawableBalance; // Amount that can be withdrawn in the requested auction
}

/** @dev Returns the user's token balances for the auction that is currently being solved
*/
function getBalances(address user) public view returns (Balance[] memory) {
uint32 currentBatch = batchExchange.getCurrentBatchId();
Balance[] memory result = new Balance[](batchExchange.numTokens());
for (uint16 index = 0; index < result.length; index++) {
address token = batchExchange.tokenIdToAddressMap(index);

(uint256 depositBalance, uint32 depositedInBatch) = batchExchange.getPendingDeposit(user, token);
if (depositedInBatch < currentBatch) {
depositBalance = 0;
}

(uint256 withdrawableBalance, uint32 withdrawRequestedInBatch) = batchExchange.getPendingWithdraw(user, token);
if (withdrawRequestedInBatch >= currentBatch) {
withdrawableBalance = 0;
}

result[index] = Balance({
token: token,
availableSellAmount: batchExchange.getBalance(user, token),
pendingDeposit: depositBalance,
withdrawableBalance: withdrawableBalance
});
}
return result;
}

/** @dev Queries the orderbook for the auction that is still accepting orders
* @param tokenFilter all returned order will have buy *and* sell token from this list (leave empty for "no filter")
* @return encoded bytes representing orders
Expand Down
58 changes: 58 additions & 0 deletions test/stablex/batch_exchange_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,64 @@ contract("BatchExchangeViewer", accounts => {
await batchExchange.addToken(token_2.address)
})

describe("getBalances", () => {
it("returns all listed tokens", async () => {
const viewer = await BatchExchangeViewer.new(batchExchange.address)
const result = await viewer.getBalances(accounts[0])
assert.equal(result.filter(balance => balance.availableSellAmount == 0).length, 3)
alfetopito marked this conversation as resolved.
Show resolved Hide resolved
})

it("returns pending deposits", async () => {
await token_1.givenAnyReturnBool(true)
await batchExchange.deposit(token_1.address, 100)

const viewer = await BatchExchangeViewer.new(batchExchange.address)
const result = await viewer.getBalances(accounts[0])
assert.equal(result[1].pendingDeposit, 100)
})

it("returns available balance once deposit is processed", async () => {
await token_1.givenAnyReturnBool(true)
await batchExchange.deposit(token_1.address, 100)
await closeAuction(batchExchange)

const viewer = await BatchExchangeViewer.new(batchExchange.address)
const result = await viewer.getBalances(accounts[0])
assert.equal(result[1].pendingDeposit, 0)
assert.equal(result[1].availableSellAmount, 100)
})

it("returns withdrawable balance once it is claimable", async () => {
await token_1.givenAnyReturnBool(true)
await batchExchange.deposit(token_1.address, 100)
await closeAuction(batchExchange)
await batchExchange.requestWithdraw(token_1.address, 100)

const viewer = await BatchExchangeViewer.new(batchExchange.address)
const before_valid = await viewer.getBalances(accounts[0])
assert.equal(before_valid[1].availableSellAmount, 100)
assert.equal(before_valid[1].withdrawableBalance, 0)

await closeAuction(batchExchange)

const after_valid = await viewer.getBalances(accounts[0])
assert.equal(after_valid[1].availableSellAmount, 0)
assert.equal(after_valid[1].withdrawableBalance, 100)
})

it("reports withdrawable balance higher than available balance", async () => {
// Due to https://github.com/gnosis/dex-contracts/issues/539
await token_1.givenAnyReturnBool(true)
await batchExchange.deposit(token_1.address, 100)
await batchExchange.requestWithdraw(token_1.address, 200)
await closeAuction(batchExchange)

const viewer = await BatchExchangeViewer.new(batchExchange.address)
const result = await viewer.getBalances(accounts[0])
assert.equal(result[1].withdrawableBalance, 200)
})
})

describe("getOpenOrderBook", () => {
it("can be queried without pagination", async () => {
const batchId = await batchExchange.getCurrentBatchId()
Expand Down