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
4 changes: 2 additions & 2 deletions examples/10-dollar-cost-averaging/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ inputs:
- chainId: uint32
- tokenIn: address
- tokenOut: address
- amount: string # "1.5" = 1.5 tokenIn
- slippageBps: uint16 # 100 = 1%
- amount: string # e.g., '1.5' = 1.5 tokenIn
- slippageBps: uint16 # e.g., 50 = 0.50%
- recipient: address
27 changes: 10 additions & 17 deletions examples/10-dollar-cost-averaging/src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,26 @@ import { BigInt, ERC20Token, log, SwapBuilder, TokenAmount } from '@mimicprotoco

import { inputs } from './types'

const BPS_DENOMINATOR = BigInt.fromI32(10_000)

export default function main(): void {
// Log input parameters
log.info('Starting DCA swap: amountFromToken={}, slippageBps={}, chainId={}, recipient={}', [
inputs.amount,
inputs.slippageBps.toString(),
inputs.chainId.toString(),
inputs.recipient.toString(),
])
log.info(
`Starting DCA swap: amountFromToken=${inputs.amount}, slippageBps=${inputs.slippageBps}, chainId=${inputs.chainId}, recipient=${inputs.recipient}`
)

// Create token instances
const tokenIn = ERC20Token.fromAddress(inputs.tokenIn, inputs.chainId)
const tokenOut = ERC20Token.fromAddress(inputs.tokenOut, inputs.chainId)

// Create amount from decimal string and estimatate amount out
// Create amount from decimal string and estimate amount out
const amountIn = TokenAmount.fromStringDecimal(tokenIn, inputs.amount)
const amountOut = amountIn.toTokenAmount(tokenOut)
const expectedOut = amountIn.toTokenAmount(tokenOut)

// Apply slippage to calculate the expected minimum amount out
const basisPoints = BigInt.fromU16(10000)
const slippageBps = basisPoints.minus(BigInt.fromU16(inputs.slippageBps))
const minAmountOut = amountOut.times(slippageBps).div(basisPoints)

log.info('Calculated minOut: {} (equivalent={}, slippageBps={})', [
minAmountOut.toString(),
amountOut.toString(),
inputs.slippageBps.toString(),
])
const slippageFactor = BPS_DENOMINATOR.minus(BigInt.fromI32(inputs.slippageBps as i32))
const minAmountOut = expectedOut.times(slippageFactor).div(BPS_DENOMINATOR)
log.info(`Calculated minOut: ${minAmountOut} (equivalent=${expectedOut}, slippageBps=${inputs.slippageBps})`)

// Create and execute swap
SwapBuilder.forChain(inputs.chainId)
Expand Down
10 changes: 5 additions & 5 deletions examples/11-automated-refunds/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ version: 1.0.0
name: Client refunds
description: Refunds clients based on past orders
inputs:
- chainId: uint32 # Example: 42161 for Arbitrum
- token: string # Token contract address
- amount: string # Amount in wei (as string)
- recipient: string # Recipient address
- maxFee: string # Max fee in wei (as string)
- chainId: uint32
- token: string
- amount: string # Amount in wei (as string)
- recipient: address
- maxFee: string # e.g., '0.01' = 0.01 of the given token
8 changes: 3 additions & 5 deletions examples/11-automated-refunds/src/task.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { Address, BigInt, ERC20Token, log, TokenAmount, Transfer } from '@mimicprotocol/lib-ts'
import { BigInt, ERC20Token, log, TokenAmount, Transfer } from '@mimicprotocol/lib-ts'

import { inputs } from './types'

export default function main(): void {
const token = ERC20Token.fromString(inputs.token, inputs.chainId)
const tokenAmount = TokenAmount.fromStringDecimal(token, inputs.amount)
const maxFee = BigInt.fromStringDecimal(inputs.maxFee, token.decimals)
const recipient = Address.fromString(inputs.recipient)

Transfer.create(token, tokenAmount.amount, recipient, maxFee).send()

log.info('Created transfer intent of {}', [tokenAmount])
Transfer.create(token, tokenAmount.amount, inputs.recipient, maxFee).send()
log.info(`Created transfer intent of ${tokenAmount}`)
}
4 changes: 2 additions & 2 deletions examples/2-simple-transfer-with-inputs/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ description: Automated task to execute parameterized transfers
inputs:
- chainId: uint32
- token: address
- amount: uint256
- amount: string # e.g., '15.3' = 15.3 of the given token
- recipient: address
- maxFee: uint256
- maxFee: string # e.g., '0.01' = 0.01 of the given token
6 changes: 4 additions & 2 deletions examples/2-simple-transfer-with-inputs/src/task.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ERC20Token, Transfer } from '@mimicprotocol/lib-ts'
import { BigInt, ERC20Token, Transfer } from '@mimicprotocol/lib-ts'

import { inputs } from './types'

export default function main(): void {
const token = ERC20Token.fromAddress(inputs.token, inputs.chainId)
Transfer.create(token, inputs.amount, inputs.recipient, inputs.maxFee).send()
const amount = BigInt.fromStringDecimal(inputs.amount, token.decimals)
const maxFee = BigInt.fromStringDecimal(inputs.maxFee, token.decimals)
Transfer.create(token, amount, inputs.recipient, maxFee).send()
}
21 changes: 14 additions & 7 deletions examples/2-simple-transfer-with-inputs/tests/task.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OpType } from '@mimicprotocol/sdk'
import { Context, runTask, Transfer } from '@mimicprotocol/test-ts'
import { fp, OpType } from '@mimicprotocol/sdk'
import { Context, ContractCallMock, runTask, Transfer } from '@mimicprotocol/test-ts'
import { expect } from 'chai'

describe('Task', () => {
Expand All @@ -14,13 +14,20 @@ describe('Task', () => {
const inputs = {
chainId: 10, // Optimism
token: '0x7f5c764cbc14f9669b88837ca1490cca17c31607', // USDC
amount: '1000000', // 1 USDC
amount: '1.5', // 1.5 USDC
recipient: '0xbce3248ede29116e4bd18416dcc2dfca668eeb84',
maxFee: '100000', // 0.1 USDC
maxFee: '0.1', // 0.1 USDC
}

const calls: ContractCallMock[] = [
{
request: { to: inputs.token, chainId: inputs.chainId, fnSelector: '0x313ce567' }, // decimals
response: { value: '6', abiType: 'uint8' },
},
]

it('produces the expected intents', async () => {
const result = await runTask(taskDir, context, { inputs })
const result = await runTask(taskDir, context, { inputs, calls })
expect(result.success).to.be.true
expect(result.timestamp).to.be.equal(context.timestamp)

Expand All @@ -33,11 +40,11 @@ describe('Task', () => {
expect(intents[0].chainId).to.be.equal(inputs.chainId)
expect(intents[0].maxFees).to.have.lengthOf(1)
expect(intents[0].maxFees[0].token).to.be.equal(inputs.token)
expect(intents[0].maxFees[0].amount).to.be.equal(inputs.maxFee)
expect(intents[0].maxFees[0].amount).to.be.equal(fp(inputs.maxFee, 6).toString())

expect(intents[0].transfers).to.have.lengthOf(1)
expect(intents[0].transfers[0].token).to.be.equal(inputs.token)
expect(intents[0].transfers[0].amount).to.be.equal(inputs.amount)
expect(intents[0].transfers[0].amount).to.be.equal(fp(inputs.amount, 6).toString())
expect(intents[0].transfers[0].recipient).to.be.equal(inputs.recipient)
})
})
6 changes: 3 additions & 3 deletions examples/3-transfer-balance-threshold/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ description: Automated task to execute parameterized transfers based on balance
inputs:
- chainId: uint32
- token: address
- amount: uint256
- amount: string # e.g., '10.2' = 10.2 of the given token
- recipient: address
- maxFee: uint256
- threshold: uint256
- maxFee: string # e.g., '0.01' = 0.01 of the given token
- threshold: string # e.g., '20.5' = 20.5 of the given token
abis:
- ERC20: ./abis/ERC20.json
12 changes: 8 additions & 4 deletions examples/3-transfer-balance-threshold/src/task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ERC20Token, Transfer } from '@mimicprotocol/lib-ts'
import { BigInt, ERC20Token, Transfer } from '@mimicprotocol/lib-ts'

import { ERC20 } from './types/ERC20'
import { inputs } from './types'
Expand All @@ -7,8 +7,12 @@ export default function main(): void {
const tokenContract = new ERC20(inputs.token, inputs.chainId)
const balance = tokenContract.balanceOf(inputs.recipient)

if (balance.lt(inputs.threshold)) {
const token = ERC20Token.fromAddress(inputs.token, inputs.chainId)
Transfer.create(token, inputs.amount, inputs.recipient, inputs.maxFee).send()
const token = ERC20Token.fromAddress(inputs.token, inputs.chainId)
const threshold = BigInt.fromStringDecimal(inputs.threshold, token.decimals)

if (balance.lt(threshold)) {
const amount = BigInt.fromStringDecimal(inputs.amount, token.decimals)
const maxFee = BigInt.fromStringDecimal(inputs.maxFee, token.decimals)
Transfer.create(token, amount, inputs.recipient, maxFee).send()
}
}
23 changes: 17 additions & 6 deletions examples/3-transfer-balance-threshold/tests/task.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpType } from '@mimicprotocol/sdk'
import { fp, OpType } from '@mimicprotocol/sdk'
import { Context, ContractCallMock, runTask, Transfer } from '@mimicprotocol/test-ts'
import { expect } from 'chai'

Expand All @@ -14,10 +14,10 @@ describe('Task', () => {
const inputs = {
chainId: 10, // Optimism
token: '0x7f5c764cbc14f9669b88837ca1490cca17c31607', // USDC
amount: '1000000', // 1 USDC
amount: '1', // 1 USDC
recipient: '0xbce3248ede29116e4bd18416dcc2dfca668eeb84',
maxFee: '100000', // 0.1 USDC
threshold: '10000000', // 10 USDC
maxFee: '0.1', // 0.1 USDC
threshold: '10.2', // 10.2 USDC
}

const buildCalls = (balance: string): ContractCallMock[] => [
Expand All @@ -38,6 +38,17 @@ describe('Task', () => {
abiType: 'uint256',
},
},
{
request: {
to: inputs.token,
chainId: inputs.chainId,
fnSelector: '0x313ce567', // `decimals`
},
response: {
value: '6',
abiType: 'uint8',
},
},
]

describe('when the balance is below the threshold', () => {
Expand All @@ -58,11 +69,11 @@ describe('Task', () => {
expect(intents[0].chainId).to.be.equal(inputs.chainId)
expect(intents[0].maxFees).to.have.lengthOf(1)
expect(intents[0].maxFees[0].token).to.be.equal(inputs.token)
expect(intents[0].maxFees[0].amount).to.be.equal(inputs.maxFee)
expect(intents[0].maxFees[0].amount).to.be.equal(fp(inputs.maxFee, 6).toString())

expect(intents[0].transfers).to.have.lengthOf(1)
expect(intents[0].transfers[0].token).to.be.equal(inputs.token)
expect(intents[0].transfers[0].amount).to.be.equal(inputs.amount)
expect(intents[0].transfers[0].amount).to.be.equal(fp(inputs.amount, 6).toString())
expect(intents[0].transfers[0].recipient).to.be.equal(inputs.recipient)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ description: Automated task to execute parameterized transfers based on balance
inputs:
- chainId: uint32
- token: address
- amount: uint256
- amount: string # e.g., '10.2' = 10.2 of the given token
- recipient: address
- maxFee: uint256
- thresholdUSD: uint32
- maxFee: string # e.g., '0.01' = 0.01 of the given token
- thresholdUsd: string # e.g., '30.5' = 30.5 USD
abis:
- ERC20: ./abis/ERC20.json
10 changes: 6 additions & 4 deletions examples/4-transfer-balance-threshold-with-oracles/src/task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ERC20Token, log, TokenAmount, Transfer, USD } from '@mimicprotocol/lib-ts'
import { BigInt, ERC20Token, log, TokenAmount, Transfer, USD } from '@mimicprotocol/lib-ts'

import { ERC20 } from './types/ERC20'
import { inputs } from './types'
Expand All @@ -9,10 +9,12 @@ export default function main(): void {

const token = ERC20Token.fromAddress(inputs.token, inputs.chainId)
const balanceInUsd = TokenAmount.fromBigInt(token, balance).toUsd()
const thresholdUsd = USD.fromI32(inputs.thresholdUSD)
log.info('Balance in USD: ' + balanceInUsd.toString())
const thresholdUsd = USD.fromStringDecimal(inputs.thresholdUsd)
log.info(`Balance in USD: ${balanceInUsd}`)

if (balanceInUsd.lt(thresholdUsd)) {
Transfer.create(token, inputs.amount, inputs.recipient, inputs.maxFee).send()
const amount = BigInt.fromStringDecimal(inputs.amount, token.decimals)
const maxFee = BigInt.fromStringDecimal(inputs.maxFee, token.decimals)
Transfer.create(token, amount, inputs.recipient, maxFee).send()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpType } from '@mimicprotocol/sdk'
import { fp, OpType } from '@mimicprotocol/sdk'
import { Context, ContractCallMock, GetPriceMock, runTask, Transfer } from '@mimicprotocol/test-ts'
import { expect } from 'chai'

Expand All @@ -14,10 +14,10 @@ describe('Task', () => {
const inputs = {
chainId: 10, // Optimism
token: '0x7f5c764cbc14f9669b88837ca1490cca17c31607', // USDC
amount: '1000000', // 1 USDC
amount: '1', // 1 USDC
recipient: '0xbce3248ede29116e4bd18416dcc2dfca668eeb84',
maxFee: '100000', // 0.1 USDC
thresholdUSD: 10, // 10 USD
maxFee: '0.1', // 0.1 USDC
thresholdUsd: '10.5', // 10.5 USD
}

const prices: GetPriceMock[] = [
Expand Down Expand Up @@ -79,11 +79,11 @@ describe('Task', () => {
expect(intents[0].chainId).to.be.equal(inputs.chainId)
expect(intents[0].maxFees.length).to.be.equal(1)
expect(intents[0].maxFees[0].token).to.be.equal(inputs.token)
expect(intents[0].maxFees[0].amount).to.be.equal(inputs.maxFee)
expect(intents[0].maxFees[0].amount).to.be.equal(fp(inputs.maxFee, 6).toString())

expect(intents[0].transfers).to.have.lengthOf(1)
expect(intents[0].transfers[0].token).to.be.equal(inputs.token)
expect(intents[0].transfers[0].amount).to.be.equal(inputs.amount)
expect(intents[0].transfers[0].amount).to.be.equal(fp(inputs.amount, 6).toString())
expect(intents[0].transfers[0].recipient).to.be.equal(inputs.recipient)

expect(result.logs).to.have.lengthOf(1)
Expand Down
4 changes: 2 additions & 2 deletions examples/5-invest-aave-idle-balance/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ description: Automated task to invest in AAVE idle balance above certain thresho
inputs:
- chainId: uint32
- aToken: address
- thresholdUSD: uint32
- smartAccount: address
- maxFee: string
- thresholdUsd: string # e.g., '30.5' = 30.5 USD
- maxFeeUsd: string # e.g., '0.05' = 0.05 USD
abis:
- ERC20: ./abis/ERC20.json
- AaveToken: ./abis/AaveToken.json
Expand Down
6 changes: 3 additions & 3 deletions examples/5-invest-aave-idle-balance/src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default function main(): void {
const underlyingTokenBalanceAmount = underlyingTokenContract.balanceOf(inputs.smartAccount)
const underlyingTokenBalance = TokenAmount.fromBigInt(underlyingToken, underlyingTokenBalanceAmount)
const underlyingTokenBalanceInUsd = underlyingTokenBalance.toUsd()
const thresholdUsd = USD.fromI32(inputs.thresholdUSD)
log.info('Underlying balance in USD: ' + underlyingTokenBalanceInUsd.toString())
const thresholdUsd = USD.fromStringDecimal(inputs.thresholdUsd)
log.info(`Underlying balance in USD: ${underlyingTokenBalanceInUsd}`)

if (underlyingTokenBalanceInUsd.lt(thresholdUsd)) {
log.info('Threshold not met')
Expand All @@ -33,6 +33,6 @@ export default function main(): void {
)

// Use mimic credits to pay for the transaction fee
const feeWithCredits = TokenAmount.fromStringDecimal(DenominationToken.USD(), inputs.maxFee)
const feeWithCredits = TokenAmount.fromStringDecimal(DenominationToken.USD(), inputs.maxFeeUsd)
calls.addUser(inputs.smartAccount).addMaxFee(feeWithCredits).build().send()
}
4 changes: 2 additions & 2 deletions examples/5-invest-aave-idle-balance/tests/task.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe('Task', () => {
chainId: 10, // Optimism
aToken: '0x625e7708f30ca75bfd92586e17077590c60eb4cd', // Aave Optimism USDC
smartAccount: '0x756f45e3fa69347a9a973a725e3c98bc4db0b5a1',
thresholdUSD: 10, // 10 USD
maxFee: '0.1', // 0.1 USD
thresholdUsd: '10.5', // 10.5 USD
maxFeeUsd: '0.1', // 0.1 USD
}

const underlyingToken = '0x7f5c764cbc14f9669b88837ca1490cca17c31607' // USDC
Expand Down
4 changes: 2 additions & 2 deletions examples/6-withdraw-from-aave-balance-threshold/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ description: Automated task to withdraw from AAVE based on balance threshold in
inputs:
- chainId: uint32
- aToken: address
- slippage: uint32
- recipient: address
- thresholdUSD: uint32
- slippageBps: uint16 # e.g., 50 = 0.50%
- thresholdUsd: string # e.g., '30.5' = 30.5 USD
abis:
- ERC20: ./abis/ERC20.json
- AaveToken: ./abis/AaveToken.json
Loading