Skip to content

Commit

Permalink
Fix computing ETH value of WETH token (#24)
Browse files Browse the repository at this point in the history
* Fix computing ETH value of WETH token

* Adapt tests
  • Loading branch information
fedgiac committed Jul 25, 2022
1 parent 72623a0 commit 57fd57f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
9 changes: 6 additions & 3 deletions src/tasks/ts/value.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumber } from "ethers";

import { OrderKind } from "../../ts";
import { BUY_ETH_ADDRESS, OrderKind } from "../../ts";
import { Api } from "../../ts/api";
import { SupportedNetwork } from "../ts/deployment";
import {
Expand Down Expand Up @@ -74,12 +74,15 @@ export async function ethValue(
network: SupportedNetwork,
api: Api,
): Promise<BigNumber> {
if (isNativeToken(token)) {
if (
isNativeToken(token) ||
token.address == WRAPPED_NATIVE_TOKEN_ADDRESS[network] // todo: remove when services support selling weth for eth
) {
return amount;
}
return await api.estimateTradeAmount({
sellToken: token.address,
buyToken: WRAPPED_NATIVE_TOKEN_ADDRESS[network], // todo: replace WETH address with BUY_ETH_ADDRESS when services support ETH estimates
buyToken: BUY_ETH_ADDRESS,
amount,
kind: OrderKind.SELL,
});
Expand Down
13 changes: 3 additions & 10 deletions test/tasks/dump.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
GetDumpInstructionInput,
getDumpInstructions,
} from "../../src/tasks/dump";
import { SupportedNetwork } from "../../src/tasks/ts/deployment";
import { ProviderGasEstimator } from "../../src/tasks/ts/gas";
import { Erc20Token, isNativeToken } from "../../src/tasks/ts/tokens";
import { BUY_ETH_ADDRESS, OrderKind } from "../../src/ts";
Expand Down Expand Up @@ -101,7 +100,7 @@ export function mockQuerySellingTokenForEth({
.expects("estimateTradeAmount")
.withArgs({
sellToken: token,
buyToken: wrappedNativeToken,
buyToken: BUY_ETH_ADDRESS,
kind: OrderKind.SELL,
amount,
})
Expand All @@ -121,14 +120,8 @@ async function handledRejection(error?: unknown) {
return { rejection };
}

// The getDumpInstructions function depends on the network only to retrieve
// the right weth address for the network, and even then this is only needed
// because of an issue in the services where BUY_ETH_ADDRESS cannot be used
// to get a price quote.
// TODO: remove when BUY_ETH_ADDRESS is supported and implemented in price
// quotes.
const network = undefined as unknown as SupportedNetwork;
const wrappedNativeToken = undefined as unknown as string;
// Any network works for testing.
const network = "mainnet";

describe("getDumpInstructions", () => {
let consoleLogOutput: unknown = undefined;
Expand Down

0 comments on commit 57fd57f

Please sign in to comment.