Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions examples/09-subgraph-query/src/task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, BigInt, environment, ERC20Token, Swap } from '@mimicprotocol/lib-ts'
import { Address, BigInt, environment, ERC20Token, Swap, TokenAmount } from '@mimicprotocol/lib-ts'
import { JSON } from 'json-as/assembly'

import { ERC20 } from './types/ERC20'
Expand All @@ -18,11 +18,9 @@ class UniswapPoolsData {
}

const PRICE_PRECISION: u8 = 40
const BPS_DENOMINATOR = BigInt.fromI32(10_000)

export default function main(): void {
if (inputs.tokenIn == inputs.tokenOut) throw new Error('Token in and out must be different')
if (BigInt.fromI32(inputs.slippageBps as i32) > BPS_DENOMINATOR) throw new Error('Slippage must be between 0 and 100')

const me = environment.getContext().user
const amountIn = new ERC20(inputs.tokenIn, inputs.chainId).balanceOf(me).unwrap()
Expand All @@ -35,9 +33,9 @@ export default function main(): void {
.times(price)
.upscale(tokenOut.decimals)
.downscale(tokenIn.decimals + PRICE_PRECISION)
const slippageFactor = BPS_DENOMINATOR.minus(BigInt.fromI32(inputs.slippageBps as i32))
const minAmountOut = expectedOut.times(slippageFactor).div(BPS_DENOMINATOR)
Swap.create(inputs.chainId, tokenIn, amountIn, tokenOut, minAmountOut).send()
const expectedOutTokenAmount = TokenAmount.fromBigInt(tokenOut, expectedOut)
const minAmountOut = expectedOutTokenAmount.applySlippageBps(inputs.slippageBps as i32)
Swap.create(inputs.chainId, tokenIn, amountIn, tokenOut, minAmountOut.amount).send()
}

function getTokenPrice(chainId: i32, subgraphId: string, tokenIn: Address, tokenOut: Address): BigInt {
Expand Down
5 changes: 2 additions & 3 deletions examples/10-rebalancing-tokens/src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,14 @@ export default function main(): void {
const tokenInAmount = movedUSD.toTokenAmount(tokensMetadata[surplusTokenIndex]).unwrap()
const expectedTokenOutAmount = movedUSD.toTokenAmount(tokensMetadata[deficitTokenIndex]).unwrap()

const slippageFactor = BPS_DENOMINATOR.minus(BigInt.fromI32(inputs.slippageBps as i32))
const minAmountOut = expectedTokenOutAmount.amount.times(slippageFactor).div(BPS_DENOMINATOR)
const minAmountOut = expectedTokenOutAmount.applySlippageBps(inputs.slippageBps as i32)

Swap.create(
inputs.chainId,
tokensMetadata[surplusTokenIndex],
tokenInAmount.amount,
tokensMetadata[deficitTokenIndex],
minAmountOut
minAmountOut.amount
).send()

surpluses[surplusIndex].amountUSD = surpluses[surplusIndex].amountUSD.minus(movedUSD)
Expand Down
2 changes: 1 addition & 1 deletion examples/11-automated-refunds/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ description: Refunds clients based on past orders
inputs:
- chainId: uint32
- token: string
- amount: string # Amount in wei (as string)
- amount: string # e.g., '1.5' = 1.5 of the given token
- recipient: address
- maxFee: string # e.g., '0.01' = 0.01 of the given token
1 change: 0 additions & 1 deletion examples/14-fee-collection/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ name: Collect Task
description: Swaps all user tokens for USDC and sends the USDC to a recipient
inputs:
- chainId: int32
- tokensCsv: string
- slippageBps: uint16 # e.g., 50 = 0.50%
- recipient: address
6 changes: 1 addition & 5 deletions examples/14-fee-collection/src/task.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
Arbitrum,
Base,
BigInt,
ChainId,
environment,
ListType,
Expand All @@ -14,8 +13,6 @@ import {

import { inputs } from './types'

const BPS_DENOMINATOR = BigInt.fromI32(10_000)

export default function main(): void {
const chainId = inputs.chainId
const context = environment.getContext()
Expand All @@ -28,12 +25,11 @@ export default function main(): void {
}

const USDC = getUsdc(chainId)
const slippageFactor = BPS_DENOMINATOR.minus(BigInt.fromI32(inputs.slippageBps as i32))

for (let i = 0; i < amountsIn.length; i++) {
const amountIn = amountsIn[i]
const amountOut = amountIn.toTokenAmount(USDC).unwrap()
const minAmountOut = amountOut.times(slippageFactor).div(BPS_DENOMINATOR)
const minAmountOut = amountOut.applySlippageBps(inputs.slippageBps as i32)

// Note that the recipient will receive the USDC
SwapBuilder.forChain(chainId)
Expand Down