Skip to content

Commit d1aed59

Browse files
Examples: Add relevant tokens query example (#21)
1 parent 22b701e commit d1aed59

8 files changed

Lines changed: 508 additions & 0 deletions

File tree

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
[
2+
{
3+
"constant": true,
4+
"inputs": [],
5+
"name": "name",
6+
"outputs": [
7+
{
8+
"name": "",
9+
"type": "string"
10+
}
11+
],
12+
"payable": false,
13+
"stateMutability": "view",
14+
"type": "function"
15+
},
16+
{
17+
"constant": false,
18+
"inputs": [
19+
{
20+
"name": "_spender",
21+
"type": "address"
22+
},
23+
{
24+
"name": "_value",
25+
"type": "uint256"
26+
}
27+
],
28+
"name": "approve",
29+
"outputs": [
30+
{
31+
"name": "",
32+
"type": "bool"
33+
}
34+
],
35+
"payable": false,
36+
"stateMutability": "nonpayable",
37+
"type": "function"
38+
},
39+
{
40+
"constant": true,
41+
"inputs": [],
42+
"name": "totalSupply",
43+
"outputs": [
44+
{
45+
"name": "",
46+
"type": "uint256"
47+
}
48+
],
49+
"payable": false,
50+
"stateMutability": "view",
51+
"type": "function"
52+
},
53+
{
54+
"constant": false,
55+
"inputs": [
56+
{
57+
"name": "_from",
58+
"type": "address"
59+
},
60+
{
61+
"name": "_to",
62+
"type": "address"
63+
},
64+
{
65+
"name": "_value",
66+
"type": "uint256"
67+
}
68+
],
69+
"name": "transferFrom",
70+
"outputs": [
71+
{
72+
"name": "",
73+
"type": "bool"
74+
}
75+
],
76+
"payable": false,
77+
"stateMutability": "nonpayable",
78+
"type": "function"
79+
},
80+
{
81+
"constant": true,
82+
"inputs": [],
83+
"name": "decimals",
84+
"outputs": [
85+
{
86+
"name": "",
87+
"type": "uint8"
88+
}
89+
],
90+
"payable": false,
91+
"stateMutability": "view",
92+
"type": "function"
93+
},
94+
{
95+
"constant": true,
96+
"inputs": [
97+
{
98+
"name": "_owner",
99+
"type": "address"
100+
}
101+
],
102+
"name": "balanceOf",
103+
"outputs": [
104+
{
105+
"name": "balance",
106+
"type": "uint256"
107+
}
108+
],
109+
"payable": false,
110+
"stateMutability": "view",
111+
"type": "function"
112+
},
113+
{
114+
"constant": true,
115+
"inputs": [],
116+
"name": "symbol",
117+
"outputs": [
118+
{
119+
"name": "",
120+
"type": "string"
121+
}
122+
],
123+
"payable": false,
124+
"stateMutability": "view",
125+
"type": "function"
126+
},
127+
{
128+
"constant": false,
129+
"inputs": [
130+
{
131+
"name": "_to",
132+
"type": "address"
133+
},
134+
{
135+
"name": "_value",
136+
"type": "uint256"
137+
}
138+
],
139+
"name": "transfer",
140+
"outputs": [
141+
{
142+
"name": "",
143+
"type": "bool"
144+
}
145+
],
146+
"payable": false,
147+
"stateMutability": "nonpayable",
148+
"type": "function"
149+
},
150+
{
151+
"constant": true,
152+
"inputs": [
153+
{
154+
"name": "_owner",
155+
"type": "address"
156+
},
157+
{
158+
"name": "_spender",
159+
"type": "address"
160+
}
161+
],
162+
"name": "allowance",
163+
"outputs": [
164+
{
165+
"name": "",
166+
"type": "uint256"
167+
}
168+
],
169+
"payable": false,
170+
"stateMutability": "view",
171+
"type": "function"
172+
},
173+
{
174+
"payable": true,
175+
"stateMutability": "payable",
176+
"type": "fallback"
177+
},
178+
{
179+
"anonymous": false,
180+
"inputs": [
181+
{
182+
"indexed": true,
183+
"name": "owner",
184+
"type": "address"
185+
},
186+
{
187+
"indexed": true,
188+
"name": "spender",
189+
"type": "address"
190+
},
191+
{
192+
"indexed": false,
193+
"name": "value",
194+
"type": "uint256"
195+
}
196+
],
197+
"name": "Approval",
198+
"type": "event"
199+
},
200+
{
201+
"anonymous": false,
202+
"inputs": [
203+
{
204+
"indexed": true,
205+
"name": "from",
206+
"type": "address"
207+
},
208+
{
209+
"indexed": true,
210+
"name": "to",
211+
"type": "address"
212+
},
213+
{
214+
"indexed": false,
215+
"name": "value",
216+
"type": "uint256"
217+
}
218+
],
219+
"name": "Transfer",
220+
"type": "event"
221+
}
222+
]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
eslintConfigPrettier
89+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: 1.0.0
2+
name: Relevant tokens query example
3+
description: Example of how to use the relevant tokens query
4+
inputs:
5+
- chainId: int32
6+
- feeAmountUsd: string # e.g., '1.5' = 1.5 USD
7+
- recipient: address
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@mimicprotocol/08-relevant-tokens-query",
3+
"version": "0.0.1",
4+
"license": "Unlicensed",
5+
"private": true,
6+
"type": "module",
7+
"scripts": {
8+
"build": "yarn codegen && yarn compile",
9+
"codegen": "mimic codegen",
10+
"compile": "mimic compile",
11+
"test": "mimic test",
12+
"lint": "eslint ."
13+
},
14+
"devDependencies": {
15+
"@mimicprotocol/cli": "latest",
16+
"@mimicprotocol/lib-ts": "latest",
17+
"@mimicprotocol/sdk": "latest",
18+
"@mimicprotocol/test-ts": "latest",
19+
"@types/chai": "^5.2.2",
20+
"@types/mocha": "^10.0.10",
21+
"@types/node": "^22.10.5",
22+
"assemblyscript": "0.27.36",
23+
"chai": "^4.3.7",
24+
"eslint": "^9.10.0",
25+
"json-as": "1.1.7",
26+
"mocha": "^10.2.0",
27+
"tsx": "^4.20.3",
28+
"typescript": "^5.8.3",
29+
"visitor-as": "0.11.4"
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { DenominationToken, environment, ListType, log, TokenAmount, TransferBuilder, USD } from '@mimicprotocol/lib-ts'
2+
3+
import { inputs } from './types'
4+
5+
export default function main(): void {
6+
const context = environment.getContext()
7+
const tokens = environment.getRelevantTokens(context.user, [inputs.chainId], USD.zero(), [], ListType.DenyList)
8+
const builder = TransferBuilder.forChain(inputs.chainId)
9+
10+
for (let i = 0; i < tokens.length; i++) {
11+
const token = tokens[i]
12+
builder.addTransferFromTokenAmount(token, inputs.recipient)
13+
log.info(`Adding transfer for ${token} on chain ${inputs.chainId}`)
14+
}
15+
16+
if (builder.transfers.length == 0) {
17+
log.info(`No tokens found on chain ${inputs.chainId}`)
18+
return
19+
}
20+
21+
builder.addMaxFee(TokenAmount.fromStringDecimal(DenominationToken.USD(), inputs.feeAmountUsd)).build().send()
22+
}

0 commit comments

Comments
 (0)