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
2,277 changes: 2,277 additions & 0 deletions examples/7-withdraw-from-aave-swap-and-transfer/abis/AavePool.json

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions examples/7-withdraw-from-aave-swap-and-transfer/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import eslintPluginTypeScript from "@typescript-eslint/eslint-plugin"
import eslintParserTypeScript from "@typescript-eslint/parser"
import eslintPluginImport from "eslint-plugin-import"
import eslintPluginSimpleImportSort from "eslint-plugin-simple-import-sort"
import eslintConfigPrettier from "eslint-config-prettier"
import eslintPluginPrettier from "eslint-plugin-prettier"

export default [
{
ignores: ["node_modules/**", "**/dist/**", "**/build/**", "**/.prettierrc.*", "./src/types/**"]
},
{
files: ["**/*.{ts,tsx}"],
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
parser: eslintParserTypeScript,
parserOptions: {
project: "./tsconfig.json"
}
},
plugins: {
"@typescript-eslint": eslintPluginTypeScript,
prettier: eslintPluginPrettier,
import: eslintPluginImport,
"simple-import-sort": eslintPluginSimpleImportSort
},
rules: {
...eslintPluginTypeScript.configs.recommended.rules,
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-unused-vars": ["error"],
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/no-explicit-any": "error",

"prettier/prettier": [
"error",
{
"semi": false,
"singleQuote": true,
"trailingComma": "es5",
"arrowParens": "always",
"bracketSpacing": true,
"printWidth": 120,
"tabWidth": 2,
"useTabs": false
}
],

"simple-import-sort/imports": [
"error",
{
groups: [
["^@?\\w"],
["^\\.\\.(?!/?$)", "^\\.\\./?$"],
["^\\./(?=.*/)(?!/?$)", "^\\.(?!/?$)", "^\\./?$"]
]
}
],
"simple-import-sort/exports": "error",

"comma-spacing": ["error", { before: false, after: true }],
"no-multiple-empty-lines": ["error", { max: 1, maxEOF: 1 }]
},
settings: {
"import/resolver": {
typescript: {
alwaysTryTypes: true,
project: "./tsconfig.json"
}
}
}
},
// configuration for test files
{
files: ["tests/**/*.{ts,tsx}", "**/*.spec.{ts,tsx}", "**/*.test.{ts,tsx}"],
languageOptions: {
ecmaVersion: "latest",
sourceType: "module",
parser: eslintParserTypeScript,
parserOptions: {
project: "./tests/tsconfig.json"
}
},
rules: {
"@typescript-eslint/no-unused-expressions": "off"
}
},
eslintConfigPrettier
]
8 changes: 8 additions & 0 deletions examples/7-withdraw-from-aave-swap-and-transfer/manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2.0.0
name: Claim-Swap-Transfer Loop Task
description: This task is for demo purposes. It withdraws from AAVE, swaps for an AAVE token and transfer to deposit in loop
inputs:
- usdFeeAmount: string
- smartAccount: address
abis:
- AavePool: ./abis/AavePool.json
30 changes: 30 additions & 0 deletions examples/7-withdraw-from-aave-swap-and-transfer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@mimicprotocol/examples-withdraw-from-aave-swap-and-transfer",
"version": "0.0.1",
"license": "Unlicensed",
"private": true,
"type": "module",
"scripts": {
"build": "yarn codegen && yarn compile",
"codegen": "mimic codegen",
"compile": "mimic compile",
"test": "mimic test",
"lint": "eslint ."
},
"devDependencies": {
"@mimicprotocol/cli": "^0.0.1-rc.11",
"@mimicprotocol/lib-ts": "^0.0.1-rc.11",
"@mimicprotocol/test-ts": "^0.0.1-rc.11",
"@types/chai": "^5.2.2",
"@types/mocha": "^10.0.10",
"@types/node": "^22.10.5",
"assemblyscript": "0.27.36",
"chai": "^4.3.7",
"eslint": "^9.10.0",
"json-as": "1.1.7",
"mocha": "^10.2.0",
"tsx": "^4.20.3",
"typescript": "^5.8.3",
"visitor-as": "0.11.4"
}
}
88 changes: 88 additions & 0 deletions examples/7-withdraw-from-aave-swap-and-transfer/src/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {
Address,
BigInt,
environment,
ListType,
log,
Optimism,
SwapBuilder,
Token,
TokenAmount,
TokenIn,
TokenOut,
TransferBuilder,
TransferData,
USD,
} from '@mimicprotocol/lib-ts'

import { AavePool } from './types/AavePool'
import { inputs } from './types'

// CLAIM USDC in Aave, SWAP USDC to aUSDC and TRANSFER aUSDC
// You will have to give allowance to the settler from the EOA that you are signing for all three tokens
export default function main(): void {
const chainId = Optimism.CHAIN_ID
const aUSDC = new Token(Address.fromString('0x625E7708f30cA75bfd92586e17077590C60eb4cD'), chainId)
const USDC = new Token(Address.fromString('0x7F5c764cBc14f9669B88837ca1490cCa17c31607'), chainId)
const USDT = new Token(Address.fromString('0x94b008aa00579c1307b0ef2c499ad98a8ce58e58'), chainId)
const aaveV3Pool = new AavePool(Address.fromString('0x794a61358d6845594f94dc1db02a252b5b4814ad'), chainId)

const context = environment.getContext()

const userTokens = environment.getRelevantTokens(
context.user,
[chainId],
USD.zero(),
[USDC, aUSDC],
ListType.AllowList
)

const smartAccountTokens = environment.getRelevantTokens(
inputs.smartAccount,
[chainId],
USD.zero(),
[aUSDC],
ListType.AllowList
)
const aUsdcSmartAccount = findTokenAmount(smartAccountTokens, aUSDC)
const usdcUser = findTokenAmount(userTokens, USDC)
const aUsdcUser = findTokenAmount(userTokens, aUSDC)

const feeUsdt = TokenAmount.fromStringDecimal(USDT, inputs.usdFeeAmount)

if (aUsdcSmartAccount && aUsdcSmartAccount.amount > BigInt.zero()) {
// Claim aUSDC to user EOA using USDC in smart account
aaveV3Pool
.withdraw(USDC.address, aUsdcSmartAccount.amount, context.user)
.addFee(feeUsdt)
.addUser(inputs.smartAccount)
.build()
.send()
}

if (usdcUser && usdcUser.amount > BigInt.zero()) {
// Swap USDC for aUSDC in user EOA
const minAmount = usdcUser.amount.times(BigInt.fromI32(97)).div(BigInt.fromI32(100))
SwapBuilder.forChain(chainId)
.addTokenIn(new TokenIn(USDC.address, usdcUser.amount))
.addTokenOut(new TokenOut(aUSDC.address, minAmount, context.user))
.build()
.send()
}

if (aUsdcUser && aUsdcUser.amount > BigInt.zero()) {
// Transfer aUSDC from user EOA to smart account
TransferBuilder.forChain(chainId)
.addTransfer(new TransferData(aUSDC.address, aUsdcUser.amount, inputs.smartAccount))
.addFee(feeUsdt)
.build()
.send()
}
}

function findTokenAmount(tokenAmounts: TokenAmount[], token: Token): TokenAmount | null {
for (let i = 0; i < tokenAmounts.length; i++) {
if (tokenAmounts[i].token.address == token.address) return tokenAmounts[i]
}
return null
}
143 changes: 143 additions & 0 deletions examples/7-withdraw-from-aave-swap-and-transfer/tests/Task.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { Call, RelevantTokens, runTask, Swap, Transfer } from '@mimicprotocol/test-ts'
import { expect } from 'chai'

describe('Task', () => {
const taskDir = './'

const chainId = 10 // Optimism

const tokens = {
aUSDC: '0x625e7708f30ca75bfd92586e17077590c60eb4cd',
USDC: '0x7f5c764cbc14f9669b88837ca1490cca17c31607',
USDT: '0x94b008aa00579c1307b0ef2c499ad98a8ce58e58',
}

const context = {
user: '0xae7168deb525862f4fee37d987a971b385b96952',
settlers: [{ address: '0xdcf1d9d12a0488dfb70a8696f44d6d3bc303963d', chainId: 10 }],
timestamp: Date.now(),
}

const inputs = {
chainId,
smartAccount: '0x863df6bfa4469f3ead0be8f9f2aae51c91a907b4',
usdFeeAmount: '1', // 1 USD in USDT
}

const prices = [
{ token: tokens.aUSDC, chainId, usdPrice: '1000000' },
{ token: tokens.USDC, chainId, usdPrice: '1000000' },
{ token: tokens.USDT, chainId, usdPrice: '1000000' },
]

const buildTokenBalances = ({
aUsdcSmartAccountBalance,
usdcUserBalance,
aUsdcUserBalance,
}: {
aUsdcSmartAccountBalance: string
usdcUserBalance: string
aUsdcUserBalance: string
}): RelevantTokens[] => [
{
owner: inputs.smartAccount,
chainIds: [10],
usdMinAmount: '0',
tokenFilter: 0,
tokens: [{ address: tokens.aUSDC, chainId }],
// aUSDC balance in smart account
output: [{ token: { address: tokens.aUSDC, chainId }, amount: aUsdcSmartAccountBalance }],
},

{
owner: context.user,
chainIds: [10],
usdMinAmount: '0',
tokenFilter: 0,
tokens: [
{ address: tokens.USDC, chainId },
{ address: tokens.aUSDC, chainId },
],
output: [
// USDC balance in user
{ token: { address: tokens.USDC, chainId }, amount: usdcUserBalance },
// aUSDC balance in user
{ token: { address: tokens.aUSDC, chainId }, amount: aUsdcUserBalance },
],
},
]

it('produces claim, swap, and transfer when all balances are present', async () => {
const balances = buildTokenBalances({
aUsdcSmartAccountBalance: '1000000',
usdcUserBalance: '1000000',
aUsdcUserBalance: '1000000',
})

const intents = await runTask(taskDir, context, { inputs, balances, prices })

const claimIntent = intents.find((i) => i.type === 'transfer')
const swapIntent = intents.find((i) => i.type === 'swap')
const transferIntent = intents.find((i) => i.type === 'call')

expect(claimIntent).to.exist
expect(swapIntent).to.exist
expect(transferIntent).to.exist
})

it('only produces claim when only aUSDC exists in smart account', async () => {
const balances = buildTokenBalances({
aUsdcSmartAccountBalance: '1000000',
usdcUserBalance: '0',
aUsdcUserBalance: '0',
})

const intents = (await runTask(taskDir, context, { inputs, balances, prices })) as Call[]
expect(intents).to.have.lengthOf(1)
expect(intents[0].type).to.equal('call')
expect(intents[0].user).to.equal(inputs.smartAccount)
})

it('only produces swap when only USDC exists in user', async () => {
const amount = '1000000'
const balances = buildTokenBalances({
aUsdcSmartAccountBalance: '0',
usdcUserBalance: amount,
aUsdcUserBalance: '0',
})

const intents = (await runTask(taskDir, context, { inputs, balances, prices })) as Swap[]
expect(intents).to.have.lengthOf(1)
expect(intents[0].type).to.equal('swap')
expect(intents[0].user).to.equal(context.user)
expect(intents[0].tokensIn[0].token).to.equal(tokens.USDC)
expect(intents[0].tokensIn[0].amount).to.equal(amount)
expect(intents[0].tokensOut[0].token).to.equal(tokens.aUSDC)
})

it('only produces transfer when only aUSDC exists in user', async () => {
const amount = '1000000'
const balances = buildTokenBalances({
aUsdcSmartAccountBalance: '0',
usdcUserBalance: '0',
aUsdcUserBalance: amount,
})
const intents = (await runTask(taskDir, context, { inputs, prices, balances })) as Transfer[]
expect(intents).to.have.lengthOf(1)
expect(intents[0].type).to.equal('transfer')
expect(intents[0].transfers[0].recipient).to.equal(inputs.smartAccount)
expect(intents[0].transfers[0].amount).to.equal(amount)
expect(intents[0].user).to.equal(context.user)
})

it('produces no intents when all balances are zero', async () => {
const balances = buildTokenBalances({
aUsdcSmartAccountBalance: '0',
usdcUserBalance: '0',
aUsdcUserBalance: '0',
})

const intents = await runTask(taskDir, context, { inputs, balances, prices })
expect(intents).to.be.an('array').that.is.empty
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"declaration": true,
"composite": true,
"outDir": "./dist",
"rootDir": "./",
"resolveJsonModule": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"lib": ["ES2020", "DOM"],
"types": ["mocha", "chai", "node"]
},
"include": ["./**/*.ts"],
"exclude": ["node_modules", "dist"]
}
6 changes: 6 additions & 0 deletions examples/7-withdraw-from-aave-swap-and-transfer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": ["./src/**/*.ts"],
"exclude": ["tests/**/*"],
"references": [{ "path": "./tests" }]
}
Loading