Skip to content

Commit

Permalink
first Trident route have been passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Okavango committed Oct 17, 2022
1 parent 7fade49 commit 2ab0a89
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 22 deletions.
7 changes: 4 additions & 3 deletions contracts/RouteProcessor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ contract RouteProcessor {
uint transferAmount;
(transferAmount, position) = transferERC20Amount(tokenIn, route, position + 1);
amountInAcc += transferAmount;

} else if (commandCode == 2) { // send ERC20 tokens from this router to an address
position = sendERC20Share(route, position + 1);

} else if (commandCode == 10) { // Sushi/Uniswap pool swap
(, position) = swapUniswapPool(route, position + 1);

} else if (commandCode == 20) {
uint transferAmount;
(transferAmount, position) = bentoDepositAmountFromSender(tokenIn, route, position + 1);
amountInAcc += transferAmount;
//uint transferAmount;
(, position) = bentoDepositAmountFromSender(tokenIn, route, position + 1);
//amountInAcc += transferAmount;
} else if (commandCode == 21) {
position = swapTrident(route, position + 1);
} else if (commandCode == 22) {
Expand Down
4 changes: 2 additions & 2 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ const config: HardhatUserConfig = {
hardhat: {
forking: {
enabled: true,
//url: `https://polygon-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_POLYGON_API_KEY}`,
url: `https://polygon-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_POLYGON_API_KEY}`,
//blockNumber: 15760474,
url: `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`,
//url: `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`,
},
live: false,
saveDeployments: true,
Expand Down
41 changes: 33 additions & 8 deletions scripts/HEXer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class HEXer {
return this
}

share16(share: number): HEXer {
return this.uint16(Math.round(share*65535))
}

uint32(data: number): HEXer {
if (data >= 256*256*256*256 || data < 0 || data !== Math.round(data)) {
throw new Error("Wrong uint32: " + data)
Expand All @@ -39,17 +43,26 @@ export class HEXer {
return this
}

uint256(data: BigNumber): HEXer {
const hex = data.toHexString().slice(2).padStart(64, '0')
if (data.lt(0) || hex.length > 64) {
throw new Error("Wrong uin256: " + data.toString())
uint256(data: BigNumber | number): HEXer {
if (typeof data == 'number') {
if (data > Number.MAX_SAFE_INTEGER || data < 0 || data !== Math.round(data)) {
throw new Error("Wrong uint256: " + data)
}
this.hex += data.toString(16).padStart(64, '0')

return this
} else {
const hex = data.toHexString().slice(2).padStart(64, '0')
if (data.lt(0) || hex.length > 64) {
throw new Error("Wrong uin256: " + data.toString())
}
this.hex += hex

return this
}
this.hex += hex

return this
}

uint(data: BigNumber): HEXer {
uint(data: BigNumber | number): HEXer {
return this.uint256(data)
}

Expand All @@ -73,4 +86,16 @@ export class HEXer {

return this
}

bytes(data: string): HEXer {
if (data.length % 2 != 0) {
throw new Error("Wrong hex data length: " + data.length)
}

if (data.startsWith('0x')) data = data.slice(2)
this.uint(data.length/2)
this.hex += data

return this
}
}
4 changes: 2 additions & 2 deletions scripts/Swapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export class Swapper {

async getRoute(tokenIn: Token, amountIn: BigNumber, tokenOut: Token): Promise<MultiRoute> {
const providers = [
new SushiProvider(this.poolRegistarator, this.chainDataProvider, this.network, this.limited),
new UniswapProvider(this.poolRegistarator, this.chainDataProvider, this.network, this.limited),
//new SushiProvider(this.poolRegistarator, this.chainDataProvider, this.network, this.limited),
//new UniswapProvider(this.poolRegistarator, this.chainDataProvider, this.network, this.limited),
new TridentProvider(this.poolRegistarator, this.chainDataProvider, this.network, this.limited),
]
const poolsPromises = providers.map(p => p.getPools(tokenIn, tokenOut))
Expand Down
5 changes: 3 additions & 2 deletions scripts/TinesToRouteProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class TinesToRouteProcessor {
// Sends tokens from the RouteProcessor to a pool
codeSendERC20(token: RToken, poolAddress: string, share: number): string {
const code = new HEXer().uint8(2).address(token.address)
.address(poolAddress).uint16(Math.round(share*65535)).toString()
.address(poolAddress).share16(share).toString()
console.assert(code.length == 43*2, "codeSendERC20 unexpected code length")
return code
}
Expand Down Expand Up @@ -110,7 +110,8 @@ export class TinesToRouteProcessor {
} else {
const legsOutput = res.get(tokenId) || new Map()
const provider = this.registrator.getProvider(l.poolAddress)
const startPoint = provider?.getLegStartPoint(l)
let startPoint = provider?.getLegStartPoint(l)
if (startPoint == 'RouteProcessor') startPoint = this.routeProcessorAddress
if (startPoint !== undefined) {
const legs = legsOutput.get(startPoint) || []
legs.push(l)
Expand Down
1 change: 1 addition & 0 deletions scripts/liquidityProviders/LiquidityProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export abstract class LiquidityProvider {
abstract getProviderName(): string;
abstract getPools(t0: Token, t1: Token): Promise<RPool[]>;
// the address where should be swap amount of liquidity before the swap
// returns 'RouteProcessor' if it is a RouteProcessor
getLegStartPoint(leg: RouteLeg): string {
return leg.poolAddress
}
Expand Down
41 changes: 37 additions & 4 deletions scripts/liquidityProviders/Trident.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class TridentProvider extends LiquidityProvider {
}

getSwapCodeForRouteProcessor(leg: RouteLeg, toAddress: string, exactAmount?: BigNumber): string {
if (leg.poolAddress === BentoBox[this.network.chainId]) {
if (leg.poolAddress.startsWith('Bento')) {
const chainId = leg.tokenFrom.chainId
if (typeof chainId == 'string' && chainId.startsWith('Bento')) {
// From Bento
Expand All @@ -102,11 +102,29 @@ export class TridentProvider extends LiquidityProvider {
}

_getWithdrawalCode(leg: RouteLeg, toAddress: string): string {
return 'Unimplemented _getWithdrawalCode'
const code = new HEXer()
.uint8(22)
.address(leg.tokenFrom.address)
.address(toAddress)
.share16(1) // TODO !!!!
.toString()
console.assert(code.length == 43*2, "BentoBridge withdraw unexpected code length")
return code
}

// TODO: Only ConstantProductPool
_getswapCode(leg: RouteLeg, toAddress: string): string {
return 'Unimplemented _getWithdrawalCode'
const coder = new ethers.utils.AbiCoder()
// TODO: add unwrap bento = true variant
// address tokenIn, address recipient, bool unwrapBento
const poolData = coder.encode(["address", "address", "bool"], [leg.tokenFrom.address, toAddress, false])
const code = new HEXer()
.uint8(21)
.address(leg.poolAddress)
.bytes(poolData)
.toString()

return code
}

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

getLegStartPoint(leg: RouteLeg): string {
if (leg.poolAddress.startsWith('Bento')) { // Bridge
const chainId = leg.tokenFrom.chainId
if (typeof chainId == 'string' && chainId.startsWith('Bento')) {
// From Bento
return 'RouteProcessor'
} else {
// To Bento
return BentoBox[this.network.chainId]
}
} else {
return leg.poolAddress
}
}
}
2 changes: 1 addition & 1 deletion test/RouteProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe("RouteProcessor", async function () {
it("Polygon WMATIC => FEI check", async function () {
const forking_url = (network.config as HardhatNetworkConfig)?.forking?.url;
if (forking_url !== undefined && forking_url.search('polygon') >= 0) {
await testRouteProcessor(POLYGON, 9000, POLYGON.tokens.SUSHI)
await testRouteProcessor(POLYGON, 9, POLYGON.tokens.SUSHI)
}
})
});

0 comments on commit 2ab0a89

Please sign in to comment.