Skip to content

Commit f6255d5

Browse files
authored
Examples: Add swap and split (#31)
1 parent c0e61e8 commit f6255d5

16 files changed

Lines changed: 560 additions & 1 deletion
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
build
3+
types
4+
scripts/.env
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"node-option": ["import=tsx/esm"]
3+
}

‎examples/16-swap-and-split/.npmrc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node-linker=hoisted
2+
legacy-peer-deps=true
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import eslintPluginTypeScript from "@typescript-eslint/eslint-plugin"
2+
import eslintParserTypeScript from "@typescript-eslint/parser"
3+
import eslintPluginImport from "eslint-plugin-import"
4+
import eslintPluginSimpleImportSort from "eslint-plugin-simple-import-sort"
5+
import eslintConfigPrettier from "eslint-config-prettier"
6+
import eslintPluginPrettier from "eslint-plugin-prettier"
7+
8+
export default [
9+
{
10+
ignores: ["node_modules/**", "**/dist/**", "**/build/**", "**/.prettierrc.*", "./src/types/**"]
11+
},
12+
{
13+
files: ["**/*.{ts,tsx}"],
14+
languageOptions: {
15+
ecmaVersion: "latest",
16+
sourceType: "module",
17+
parser: eslintParserTypeScript,
18+
parserOptions: {
19+
project: "./tsconfig.json"
20+
}
21+
},
22+
plugins: {
23+
"@typescript-eslint": eslintPluginTypeScript,
24+
prettier: eslintPluginPrettier,
25+
import: eslintPluginImport,
26+
"simple-import-sort": eslintPluginSimpleImportSort
27+
},
28+
rules: {
29+
...eslintPluginTypeScript.configs.recommended.rules,
30+
"@typescript-eslint/no-namespace": "off",
31+
"@typescript-eslint/no-unused-vars": ["error"],
32+
"@typescript-eslint/explicit-function-return-type": "error",
33+
"@typescript-eslint/no-explicit-any": "error",
34+
35+
"prettier/prettier": [
36+
"error",
37+
{
38+
"semi": false,
39+
"singleQuote": true,
40+
"trailingComma": "es5",
41+
"arrowParens": "always",
42+
"bracketSpacing": true,
43+
"printWidth": 120,
44+
"tabWidth": 2,
45+
"useTabs": false
46+
}
47+
],
48+
49+
"simple-import-sort/imports": [
50+
"error",
51+
{
52+
groups: [
53+
["^@?\\w"],
54+
["^\\.\\.(?!/?$)", "^\\.\\./?$"],
55+
["^\\./(?=.*/)(?!/?$)", "^\\.(?!/?$)", "^\\./?$"]
56+
]
57+
}
58+
],
59+
"simple-import-sort/exports": "error",
60+
61+
"comma-spacing": ["error", { before: false, after: true }],
62+
"no-multiple-empty-lines": ["error", { max: 1, maxEOF: 1 }]
63+
},
64+
settings: {
65+
"import/resolver": {
66+
typescript: {
67+
alwaysTryTypes: true,
68+
project: "./tsconfig.json"
69+
}
70+
}
71+
}
72+
},
73+
// configuration for test files
74+
{
75+
files: ["tests/**/*.{ts,tsx}", "**/*.spec.{ts,tsx}", "**/*.test.{ts,tsx}"],
76+
languageOptions: {
77+
ecmaVersion: "latest",
78+
sourceType: "module",
79+
parser: eslintParserTypeScript,
80+
parserOptions: {
81+
project: "./tests/tsconfig.json"
82+
}
83+
},
84+
rules: {
85+
"@typescript-eslint/no-unused-expressions": "off"
86+
}
87+
},
88+
{
89+
files: ["scripts/**/*.{ts,tsx}"],
90+
languageOptions: {
91+
ecmaVersion: "latest",
92+
sourceType: "module",
93+
parser: eslintParserTypeScript,
94+
parserOptions: {
95+
project: "./scripts/tsconfig.json"
96+
}
97+
},
98+
settings: {
99+
"import/resolver": {
100+
typescript: {
101+
alwaysTryTypes: true,
102+
project: "./scripts/tsconfig.json"
103+
}
104+
}
105+
}
106+
},
107+
eslintConfigPrettier
108+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: 1.0.0
2+
name: Swap And Split Function
3+
description: Swap tokens and transfer the output to multiple recipients
4+
inputs:
5+
- chainId: uint32
6+
- tokenIn: address
7+
- tokenOut: address
8+
- amount: string # e.g., '10.5' = 10.5 USDC
9+
- slippageBps: uint16 # e.g., 50 = 0.50%
10+
- user: address
11+
- allocations: string # e.g., '0xab...c:6075,0xde...f:3925' (60.75% to 0xab...c and 39.25% to 0xde...f)
12+
- maxFeeUsd: string # e.g., '0.01' = 0.01 USD
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@mimicprotocol/16-swap-and-split",
3+
"version": "0.0.1",
4+
"license": "Unlicensed",
5+
"private": true,
6+
"type": "module",
7+
"scripts": {
8+
"deploy": "mimic deploy",
9+
"build": "mimic build",
10+
"codegen": "mimic codegen",
11+
"compile": "mimic compile",
12+
"test": "mimic test",
13+
"lint": "eslint .",
14+
"trigger": "tsx scripts/create-trigger.ts",
15+
"prefill-url": "tsx scripts/get-trigger-prefill-url.ts",
16+
"try-function": "tsx scripts/try-function.ts"
17+
},
18+
"devDependencies": {
19+
"@mimicprotocol/cli": "~1.0.0",
20+
"@mimicprotocol/lib-ts": "~0.1.5",
21+
"@mimicprotocol/sdk": "~0.1.3",
22+
"@mimicprotocol/test-ts": "~0.1.0",
23+
"@types/chai": "^5.2.2",
24+
"@types/mocha": "^10.0.10",
25+
"@types/node": "^22.10.5",
26+
"assemblyscript": "0.27.36",
27+
"chai": "^4.3.7",
28+
"dotenv": "^17.2.3",
29+
"eslint": "^9.10.0",
30+
"json-as": "1.1.7",
31+
"mocha": "^10.2.0",
32+
"tsx": "^4.20.3",
33+
"typescript": "^5.8.3",
34+
"visitor-as": "0.11.4"
35+
}
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Chains, Client, createExecuteOnceTriggerConfig, EthersSigner } from '@mimicprotocol/sdk'
2+
import { config } from 'dotenv'
3+
4+
// Load environment variables from .env file
5+
config({ path: './scripts/.env' })
6+
7+
// TODO: Replace with your deployed function's CID
8+
const FUNCTION_CID = 'YOUR_FUNCTION_CID_HERE'
9+
10+
// TODO: Customize inputs to match your function's input structure
11+
// This template uses the example function's inputs: chainId, token, amount, recipient, maxFee
12+
const inputs = {
13+
chainId: Chains.Optimism,
14+
token: '0x0b2c639c533813f4aa9d7837caf62653d097ff85', // USDC on Optimism
15+
amount: '1',
16+
recipient: '0x...', // TODO: Replace with your recipient address
17+
maxFee: '0.1',
18+
}
19+
20+
async function main(): Promise<void> {
21+
const client = new Client({
22+
signer: EthersSigner.fromPrivateKey(process.env.PRIVATE_KEY!),
23+
})
24+
25+
// Get the manifest for the function
26+
const manifest = await client.functions.getManifest(FUNCTION_CID)
27+
28+
// TODO: Customize the version, description, and other parameters
29+
// - version: Function version (e.g., '1.0.0')
30+
// - description: Human-readable description for this trigger
31+
// - config: When to execute the function (cron schedule, delta, endDate)
32+
// - executionFeeLimit: Maximum fee for execution (use '0' for no limit)
33+
// - minValidations: Minimum number of validations required
34+
35+
await client.triggers.signAndCreate({
36+
functionCid: FUNCTION_CID,
37+
manifest: manifest,
38+
input: inputs,
39+
version: '1.0.0', // TODO: Update to match your function version
40+
description: `Default Transfer - ${inputs.chainId}`,
41+
config: createExecuteOnceTriggerConfig(),
42+
executionFeeLimit: '0',
43+
minValidations: 1,
44+
})
45+
46+
console.log(`Successfully created trigger`)
47+
}
48+
49+
main().catch((error) => {
50+
console.error('Error creating trigger:', error)
51+
process.exit(1)
52+
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Private key for signing triggers
2+
PRIVATE_KEY=your_private_key_here
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Chains, getTriggerPrefillUrl, TriggerType } from '@mimicprotocol/sdk'
2+
3+
// TODO: Replace with your deployed function's CID
4+
const FUNCTION_CID = 'YOUR_FUNCTION_CID_HERE'
5+
6+
// TODO: Customize inputs to match your function's input structure
7+
const inputs = {
8+
chainId: Chains.Optimism,
9+
token: '0x0b2c639c533813f4aa9d7837caf62653d097ff85', // USDC on Optimism
10+
amount: '1',
11+
recipient: '0x...',
12+
maxFee: '0.1',
13+
}
14+
15+
// TODO: Customize the trigger configuration
16+
const config = {
17+
type: TriggerType.Cron,
18+
schedule: '0 2 * * *', // every day at 2am
19+
delta: '2h',
20+
endDate: Date.now() + 7 * 24 * 60 * 60 * 1000, // one week from now
21+
}
22+
23+
async function main(): Promise<void> {
24+
// TODO: Replace with your parameters
25+
const prefillUrl = getTriggerPrefillUrl({
26+
functionCid: FUNCTION_CID,
27+
input: inputs,
28+
config,
29+
description: 'Example trigger description',
30+
version: '1.0.1',
31+
})
32+
33+
console.log(`Trigger prefill URL: ${prefillUrl}`)
34+
}
35+
36+
main().catch((error) => {
37+
console.error('Error getting trigger prefill URL:', error)
38+
process.exit(1)
39+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Chains, randomEvmAddress } from '@mimicprotocol/sdk'
2+
import { runFunction } from '@mimicprotocol/test-ts'
3+
4+
// TODO: Replace with your function's directory
5+
const FUNCTION_DIR = './build'
6+
const ORACLE_URL = 'https://api-protocol.mimic.fi/oracle'
7+
8+
const chainId = Chains.Optimism
9+
10+
const context = {
11+
user: randomEvmAddress(),
12+
settlers: [{ address: randomEvmAddress(), chainId }],
13+
timestamp: Date.now(),
14+
}
15+
16+
// TODO: Customize inputs to match your function's input structure
17+
const inputs = {
18+
chainId,
19+
token: '0x0b2c639c533813f4aa9d7837caf62653d097ff85', // USDC on Optimism
20+
amount: '1',
21+
recipient: context.user,
22+
maxFee: '0.1',
23+
}
24+
25+
async function main(): Promise<void> {
26+
const result = await runFunction(FUNCTION_DIR, context, { inputs }, ORACLE_URL)
27+
console.log(JSON.stringify(result, null, 2))
28+
}
29+
30+
main().catch((error) => {
31+
console.error('Error running function:', error)
32+
process.exit(1)
33+
})

0 commit comments

Comments
 (0)