Skip to content

Commit

Permalink
refactor: revenue buy back slippage config
Browse files Browse the repository at this point in the history
  • Loading branch information
doncesarts committed Aug 1, 2022
1 parent a79a9f3 commit 086309a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions tasks/emissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,22 @@ subtask("revenue-split-buy-back", "Buy back MTA from mUSD and mBTC gov fees")
const revenueSplitBuyBack = RevenueSplitBuyBack__factory.connect(revenueSplitBuyBackAddress, signer)
const musd = {
address: resolveAddress("mUSD", chain),
bAssetMinSlippage: 2, // 2%
bAssetMinSlippage: 1, // 1%
rewardMinSlippage: 1, // 1%
mAssetMinBalance: simpleToExactAmount(1000), // 1k USD
}

const mbtc = {
address: resolveAddress("mBTC", chain),
bAssetMinSlippage: 5, // 5%
rewardMinSlippage: 3, // 3%
bAssetMinSlippage: 3, // 3%
rewardMinSlippage: 2, // 2%
mAssetMinBalance: simpleToExactAmount(10, 14), // 10 wBTC
}
const mAssets = [musd, mbtc]
const request = {
mAssets,
revenueSplitBuyBack,
swapFees: [3000, 3000],
swapFees: [3000, 3000], // 0.3%, 0.3%, pools fee
blockNumber: "latest",
}
const tx = await splitBuyBackRewards(signer, request)
Expand Down
17 changes: 10 additions & 7 deletions tasks/utils/emissions-split-buy-back.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Signer } from "@ethersproject/abstract-signer"
import { ContractTransaction } from "ethers"
import { BN, simpleToExactAmount } from "@utils/math"
import { RevenueSplitBuyBack, ERC20__factory, IERC20Metadata } from "types/generated"
import { RevenueSplitBuyBack, IERC20Metadata__factory, Masset__factory } from "types/generated"
import { EncodedPaths, encodeUniswapPath, getWETHPath, quoteSwap } from "@utils/peripheral/uniswap"

export interface MAssetSwap {
Expand Down Expand Up @@ -66,15 +66,15 @@ export const calculateBuyBackRewardsQuote = async (signer: Signer, params: MainP
const treasuryFee: BN = await revenueSplitBuyBack.treasuryFee()
const rewardsToken = await revenueSplitBuyBack.REWARDS_TOKEN()

const rewardsTokenContract = (ERC20__factory.connect(rewardsToken, signer) as unknown as IERC20Metadata)
const rewardsTokenContract = IERC20Metadata__factory.connect(rewardsToken, signer)
const rTokenDecimals = await rewardsTokenContract.decimals()
const rTokenSymbol = await rewardsTokenContract.symbol()

for (let i = 0; i < mAssets.length; i = 1 + 1) {
for (let i = 0; i < mAssets.length; i += 1) {
const mAsset = mAssets[i]
const bAsset: string = await revenueSplitBuyBack.bassets(mAsset.address)
const mAssetContract = (ERC20__factory.connect(mAsset.address, signer)as unknown as IERC20Metadata)
const bAssetContract = (ERC20__factory.connect(bAsset, signer)as unknown as IERC20Metadata)
const mAssetContract = Masset__factory.connect(mAsset.address, signer)
const bAssetContract = IERC20Metadata__factory.connect(bAsset, signer)

const mAssetBalance: BN = await mAssetContract.balanceOf(revenueSplitBuyBack.address)
const mAssetSymbol: string = await mAssetContract.symbol()
Expand All @@ -99,6 +99,8 @@ export const calculateBuyBackRewardsQuote = async (signer: Signer, params: MainP
minBassetsAmounts.push(minBassetsAmount)
mAssetsToBuyBack.push(mAsset)

// Get the estimated redeem amount to price better the second swap, bAsset to reward.
const bAssetRedeemAmount = await mAssetContract.getRedeemOutput(bAsset, mAssetAmount)
// console for debugging purposes, do not delete

console.table({
Expand All @@ -110,6 +112,7 @@ export const calculateBuyBackRewardsQuote = async (signer: Signer, params: MainP
bAssetDecimals: bAssetDecimals.toString(),
mAssetAmount: mAssetAmount.toString(),
minBassetsAmount: minBassetsAmount.toString(),
bAssetRedeemAmount: bAssetRedeemAmount.toString(),
})

// 2 ============ minRewardsAmount ============//
Expand All @@ -122,7 +125,7 @@ export const calculateBuyBackRewardsQuote = async (signer: Signer, params: MainP
signer,
fromToken,
toToken,
minBassetsAmount,
bAssetRedeemAmount,
blockNumber,
undefined,
swapFees,
Expand All @@ -149,7 +152,7 @@ export const calculateBuyBackRewardsQuote = async (signer: Signer, params: MainP
})

// 3 ============ Uniswap path ============//
const uniswapPath = encodeUniswapPath(getWETHPath(bAsset, rewardsToken), [3000, 3000])
const uniswapPath = encodeUniswapPath(getWETHPath(bAsset, rewardsToken), swapFees)
uniswapPaths.push(uniswapPath)
console.log(`ts: swap ${bAssetSymbol} to ${rTokenSymbol}, encodeUniswapPath: ${uniswapPath.encoded.toString()}`)
}
Expand Down
4 changes: 2 additions & 2 deletions test-utils/peripheral/uniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const encodeUniswapPath = (tokenAddresses: string[], fees: number[]): Enc
encodedReversed = encodedAddress + encodedReversed

// 3 byte hex encoding of the fee
const encodedFee = fees[i].toString(16).padStart(2 * FEE_SIZE, "0")
const encodedFee = fee.toString(16).padStart(2 * FEE_SIZE, "0")
encoded += encodedFee
encodedReversed = encodedFee + encodedReversed
})
Expand Down Expand Up @@ -72,7 +72,7 @@ export const quoteSwap = async (
const encodedPath = encodeUniswapPath(uniswapPath, fees)
const quoter = IUniswapV3Quoter__factory.connect(uniswapQuoterV3Address, signer)
const outAmount = await quoteExactInput(quoter, encodedPath.encoded, inAmount, blockNumber)
const exchangeRate = outAmount.div(simpleToExactAmount(1, to.decimals)).mul(simpleToExactAmount(1, from.decimals)).div(inAmount)
const exchangeRate = inAmount.div(outAmount.div(simpleToExactAmount(1, to.decimals)))
// Exchange rate is not precise enough, better to relay on the output amount.
return { outAmount, exchangeRate }
}

0 comments on commit 086309a

Please sign in to comment.