Skip to content

Commit

Permalink
+exactAmount
Browse files Browse the repository at this point in the history
  • Loading branch information
Okavango committed Oct 16, 2022
1 parent b2c90b8 commit 752c033
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
4 changes: 2 additions & 2 deletions contracts/RouteProcessor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ contract RouteProcessor {
IERC20(token).transferFrom(msg.sender, to, amount);
}

// Transfers input tokens from msg.sender to BentoBox. Tokens should be approved
// Transfers input tokens from BentoBox to a pool.
// Expected to be launched for initial liquidity distribution from user to Bento, so we know exact amounts
function bentoDepositAmountFromSender(address token, bytes memory route, uint position)
private returns (uint amount, uint positionAfter) {
Expand All @@ -110,7 +110,7 @@ contract RouteProcessor {
positionAfter := add(position, 52)
}

IERC20(token).transferFrom(msg.sender, address(BentoBox), amount);
//IERC20(token).transferFrom(msg.sender, address(BentoBox), amount);
BentoBox.deposit(token, address(BentoBox), to, amount, 0);
}

Expand Down
8 changes: 5 additions & 3 deletions scripts/TinesToRouteProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ export function getRouteProcessorCode(
// 1. Transfer route.amountIn input tokens from msg.sender to all input pools according to proportion 'leg.absolutePortion'
const inputLegs = tokenOutputLegs.get(route.fromToken.tokenId as string) as RouteLeg[]
let inputAmountPrevious: BigNumber = BigNumber.from(0)
const inputAmount: Map<RouteLeg, BigNumber> = new Map()
inputLegs.forEach(l => {
const amount: BigNumber = l.swapPortion != 1 ?
getBigNumber(route.amountIn * l.absolutePortion) : route.amountInBN.sub(inputAmountPrevious)
res += codeTransferERC20(route.fromToken, l.poolAddress, amount)
inputAmountPrevious = inputAmountPrevious.add(amount)
inputAmount.set(l, amount)
})
assert(inputAmountPrevious.eq(route.amountInBN), "Wrong input distribution")

Expand All @@ -50,7 +52,7 @@ export function getRouteProcessorCode(
// swap without further fork - send swap's output to the RouteProcessor
outAddress = routeProcessorAddress
}
res += codeSwap(l, outAddress, reg)
res += codeSwap(l, outAddress, reg, inputAmount.get(l))
})

return res;
Expand Down Expand Up @@ -88,10 +90,10 @@ function codeSendERC20(token: RToken, poolAddress: string, share: number): strin
return code
}

function codeSwap(leg: RouteLeg, toAddress: string, reg: PoolRegistarator): string {
function codeSwap(leg: RouteLeg, toAddress: string, reg: PoolRegistarator, exactAmount?: BigNumber): string {
const provider = reg.getProvider(leg.poolAddress)
if (provider !== undefined) {
const code = provider.getSwapCodeForRouteProcessor(leg, toAddress)
const code = provider.getSwapCodeForRouteProcessor(leg, toAddress, exactAmount)
return code
} else {
throw new Error("unknown pool: " + leg.poolAddress)
Expand Down
4 changes: 2 additions & 2 deletions scripts/liquidityProviders/LiquidityProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RouteLeg, RPool } from "@sushiswap/tines"
import { ethers } from "ethers"
import { BigNumber, ethers } from "ethers"
import { Limited } from "../Limited"
import { Network, Token } from "../networks/Network"

Expand Down Expand Up @@ -39,5 +39,5 @@ export abstract class LiquidityProvider {
// abstract updateData(): void;
abstract getProviderName(): string;
abstract getPools(t0: Token, t1: Token): Promise<RPool[]>;
abstract getSwapCodeForRouteProcessor(leg: RouteLeg, toAddress: string): string;
abstract getSwapCodeForRouteProcessor(leg: RouteLeg, toAddress: string, exactAmount?: BigNumber): string;
}
53 changes: 34 additions & 19 deletions scripts/liquidityProviders/Trident.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,39 @@ export class TridentProvider extends LiquidityProvider {
return code
}

getSwapCodeForRouteProcessor(leg: RouteLeg, toAddress: string): string {
return 'Unimplemented'
// const {poolAddress, tokenFrom} = leg
// const pool = this.pools.get(poolAddress)
// if (pool === undefined) {
// throw new Error("Unknown pool " + poolAddress)
// } else {
// if (tokenFrom.address !== pool.token0.address && tokenFrom.address !== pool.token1.address) {
// throw new Error(`Unknown token ${tokenFrom.address} for the pool ${poolAddress}`)
// }
// // swapUniswapPool = 0x20(address pool, address tokenIn, bool direction, address to)
// const code = new HEXer()
// .uint8(20).address(poolAddress)
// .address(tokenFrom.address).bool(tokenFrom.address == pool.token0.address)
// .address(toAddress).toString()
// console.assert(code.length == 62*2, "Sushi.getSwapCodeForRouteProcessor unexpected code length")
// return code
// }
getSwapCodeForRouteProcessor(leg: RouteLeg, toAddress: string, exactAmount?: BigNumber): string {
if (leg.poolAddress === BentoBox[this.network.chainId]) {
const chainId = leg.tokenFrom.chainId
if (typeof chainId == 'string' && chainId.startsWith('Bento')) {
// From Bento
return this._getWithdrawalCode(leg, toAddress)
} else {
// To Bento
return this._getDepositCode(leg, toAddress, exactAmount)
}
} else {
return this._getswapCode(leg, toAddress)
}
}

_getDepositCode(leg: RouteLeg, toAddress: string, exactAmount?: BigNumber): string {
if (exactAmount === undefined) {
throw new Error('_getDepositCode undefined exactAmount')
} else {
const code = new HEXer()
.uint8(20).address(toAddress)
.uint(exactAmount).toString()
console.assert(code.length == 53*2, "BentoBridge deposit unexpected code length")
return code
}
}

_getWithdrawalCode(leg: RouteLeg, toAddress: string): string {
return 'Unimplemented _getWithdrawalCode'
}

_getswapCode(leg: RouteLeg, toAddress: string): string {
return 'Unimplemented _getWithdrawalCode'
}

async _getTokenPairPools(
Expand Down Expand Up @@ -162,7 +177,7 @@ export class TridentProvider extends LiquidityProvider {
const totals: {elastic: BigNumber, base: BigNumber} =
await this.limited.call(() => BentoContract.totals(t.address))
return new BridgeBento(
`Bento bridge for ${t.symbol}`,
BentoBox[this.network.chainId],
tokenOutputMap.get(t.address) as RToken,
t,
totals.elastic,
Expand Down

0 comments on commit 752c033

Please sign in to comment.