Skip to content

Commit

Permalink
fix: moved eth address logic to zapper service, fixed some oracle logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jstashh committed Jul 4, 2021
1 parent 090c278 commit b91d424
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Integer, SdkError, Usdc } from "./types";

export const ZeroAddress = "0x0000000000000000000000000000000000000000";
export const EthAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
export const WethAddress = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";

// handle a non-200 `fetch` response.
export async function handleHttpError(response: Response): Promise<Response> {
Expand Down
28 changes: 9 additions & 19 deletions src/interfaces/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import BigNumber from "bignumber.js";

import { ChainId } from "../chain";
import { ServiceInterface } from "../common";
import { EthAddress, ZeroAddress } from "../helpers";
import { EthAddress } from "../helpers";
import {
Address,
Balance,
Expand Down Expand Up @@ -185,27 +185,23 @@ export class VaultInterface<T extends ChainId> extends ServiceInterface<T> {
const gasPrice = await this.yearn.services.zapper.gas();
const gasPriceFastGwei = new BigNumber(gasPrice.fast).times(new BigNumber(10 ** 9));

let sellToken = token;
if (EthAddress === token) {
// If Ether is being sent, the sellTokenAddress should be the zero address
sellToken = ZeroAddress;
}

const zapInParams = await this.yearn.services.zapper.zapIn(
account,
sellToken,
token,
amount,
vault,
gasPriceFastGwei.toString(),
options.slippage
);

const valueBigNumber = new BigNumber(zapInParams.value);

const transaction: TransactionRequest = {
to: zapInParams.to,
from: zapInParams.from,
gasPrice: zapInParams.gasPrice,
data: zapInParams.data as string,
value: zapInParams.value as string
data: zapInParams.data,
value: valueBigNumber.toFixed(0)
};

return signer.sendTransaction(transaction);
Expand Down Expand Up @@ -243,15 +239,9 @@ export class VaultInterface<T extends ChainId> extends ServiceInterface<T> {
const gasPrice = await this.yearn.services.zapper.gas();
const gasPriceFastGwei = new BigNumber(gasPrice.fast).times(new BigNumber(10 ** 9));

let toToken = token;
if (EthAddress === token) {
// If Ether is being received, the toTokenAddress should be the zero address
toToken = ZeroAddress;
}

const zapOutParams = await this.yearn.services.zapper.zapOut(
account,
toToken,
token,
amount,
vault,
gasPriceFastGwei.toString(),
Expand All @@ -262,8 +252,8 @@ export class VaultInterface<T extends ChainId> extends ServiceInterface<T> {
to: zapOutParams.to,
from: zapOutParams.from,
gasPrice: zapOutParams.gasPrice,
data: zapOutParams.data as string,
value: zapOutParams.value as string
data: zapOutParams.data,
value: zapOutParams.value
};

return signer.sendTransaction(transaction);
Expand Down
14 changes: 7 additions & 7 deletions src/services/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import BigNumber from "bignumber.js";
import { ChainId } from "../chain";
import { Service } from "../common";
import { Context } from "../context";
import { EthAddress, WethAddress } from "../helpers";
import { Address, Integer, SdkError } from "../types";
import { TransactionOutcome } from "../types/custom/simulation";
import { OracleService } from "./oracle";
Expand Down Expand Up @@ -219,23 +220,22 @@ async function zapIn(
to: zapInParams.to,
gas: gasLimit,
simulation_type: "quick",
gas_price: "0",
value: value,
save: true
};

const simulationResponse: SimulationResponse = await makeRequest(`${baseUrl}/simulate`, body);
const assetTokensReceived = simulationResponse.transaction.transaction_info.call_trace.output;
const assetTokensReceived = new BigNumber(simulationResponse.transaction.transaction_info.call_trace.output);
const pricePerShare = await vaultContract.pricePerShare();
const targetUnderlyingTokensReceived = new BigNumber(assetTokensReceived)
const targetUnderlyingTokensReceived = assetTokensReceived
.times(new BigNumber(pricePerShare.toString()))
.div(new BigNumber(10).pow(18))
.toFixed(0);

const oracle = new OracleService(chainId, ctx);

const zapInAmountUsdc = await oracle.getNormalizedValueUsdc(token, assetTokensReceived);
const boughtAssetAmountUsdc = await oracle.getNormalizedValueUsdc(vault, amount);
const zapInAmountUsdc = await oracle.getNormalizedValueUsdc(token === EthAddress ? WethAddress : token, amount);
const boughtAssetAmountUsdc = await oracle.getNormalizedValueUsdc(vault, assetTokensReceived.toFixed(0));

const conversionRate = new BigNumber(boughtAssetAmountUsdc).div(new BigNumber(zapInAmountUsdc)).toNumber();
const slippage = 1 - conversionRate;
Expand All @@ -244,7 +244,7 @@ async function zapIn(
sourceTokenAddress: token,
sourceTokenAmount: amount,
targetTokenAddress: zapInParams.buyTokenAddress,
targetTokenAmount: assetTokensReceived,
targetTokenAmount: assetTokensReceived.toFixed(0),
targetUnderlyingTokenAddress: underlyingTokenAddress,
targetUnderlyingTokenAmount: targetUnderlyingTokensReceived,
conversionRate: conversionRate,
Expand Down Expand Up @@ -327,8 +327,8 @@ async function zapOut(

const oracle = new OracleService(chainId, ctx);

const zapOutAmountUsdc = await oracle.getNormalizedValueUsdc(token, output);
const soldAssetAmountUsdc = await oracle.getNormalizedValueUsdc(vault, amount);
const zapOutAmountUsdc = await oracle.getNormalizedValueUsdc(token === EthAddress ? WethAddress : token, output);

const conversionRate = new BigNumber(zapOutAmountUsdc).div(new BigNumber(soldAssetAmountUsdc)).toNumber();
const slippage = 1 - conversionRate;
Expand Down
17 changes: 15 additions & 2 deletions src/services/zapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,24 @@ export class ZapperService extends Service {
gasPrice: Integer,
slippagePercentage: number
): Promise<ZapInOutput> {
let sellToken = token;
if (EthAddress === token) {
// If Ether is being sent, the sellTokenAddress should be the zero address
sellToken = ZeroAddress;
}

const url = "https://api.zapper.fi/v1/zap-in/yearn/transaction";
const params = new URLSearchParams({
ownerAddress: from,
sellTokenAddress: token,
sellTokenAddress: sellToken,
sellAmount: amount,
poolAddress: vault,
gasPrice: gasPrice,
slippagePercentage: slippagePercentage.toString(),
api_key: this.ctx.zapper,
skipGasEstimate: "true"
});

const response: ZapInOutput = await fetch(`${url}?${params}`)
.then(handleHttpError)
.then(res => res.json());
Expand All @@ -160,10 +167,16 @@ export class ZapperService extends Service {
gasPrice: Integer,
slippagePercentage: number
): Promise<ZapOutOutput> {
let toToken = token;
if (EthAddress === token) {
// If Ether is being received, the toTokenAddress should be the zero address
toToken = ZeroAddress;
}

const url = "https://api.zapper.fi/v1/zap-out/yearn/transaction";
const params = new URLSearchParams({
ownerAddress: from,
toTokenAddress: token,
toTokenAddress: toToken,
sellAmount: amount,
poolAddress: vault,
gasPrice: gasPrice,
Expand Down

0 comments on commit b91d424

Please sign in to comment.