This repository was archived by the owner on Nov 16, 2023. It is now read-only.
forked from graphprotocol/example-subgraph
-
Notifications
You must be signed in to change notification settings - Fork 1
Add CosmWasm liquidity example #26
Open
Seb-figment
wants to merge
5
commits into
main
Choose a base branch
from
GRAPH-460-for-main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| build/ | ||
| generated/ | ||
|
|
||
| # Logs | ||
| logs | ||
| *.log | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| # Runtime data | ||
| pids | ||
| *.pid | ||
| *.seed | ||
| *.pid.lock | ||
|
|
||
| # Directory for instrumented libs generated by jscoverage/JSCover | ||
| lib-cov | ||
|
|
||
| # Coverage directory used by tools like istanbul | ||
| coverage | ||
|
|
||
| # nyc test coverage | ||
| .nyc_output | ||
|
|
||
| # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
| .grunt | ||
|
|
||
| # Bower dependency directory (https://bower.io/) | ||
| bower_components | ||
|
|
||
| # node-waf configuration | ||
| .lock-wscript | ||
|
|
||
| # Compiled binary addons (https://nodejs.org/api/addons.html) | ||
| build/Release | ||
|
|
||
| # Dependency directories | ||
| node_modules/ | ||
| jspm_packages/ | ||
|
|
||
| # TypeScript v1 declaration files | ||
| typings/ | ||
|
|
||
| # Optional npm cache directory | ||
| .npm | ||
|
|
||
| # Optional eslint cache | ||
| .eslintcache | ||
|
|
||
| # Optional REPL history | ||
| .node_repl_history | ||
|
|
||
| # Output of 'npm pack' | ||
| *.tgz | ||
|
|
||
| # Yarn Integrity file | ||
| .yarn-integrity | ||
|
|
||
| # dotenv environment variables file | ||
| .env | ||
|
|
||
| # next.js build output | ||
| .next |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2019 The Graph | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # CosmWasm Liquidity Example | ||
|
|
||
| This example subgraph stores `Contract` objects that represent CosmWasm contract. In order to do that, the handler function decode all `MsgExecuteContract` messages. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| version: '3' | ||
| services: | ||
| graph-node: | ||
| image: graphprotocol/graph-node:v0.22.0 | ||
| ports: | ||
| - '8000:8000' | ||
| - '8001:8001' | ||
| - '8020:8020' | ||
| - '8030:8030' | ||
| - '8040:8040' | ||
| depends_on: | ||
| - ipfs | ||
| - postgres | ||
| environment: | ||
| postgres_host: postgres | ||
| postgres_user: graph-node | ||
| postgres_pass: let-me-in | ||
| postgres_db: graph-node | ||
| ipfs: 'ipfs:5001' | ||
| # Change next line if you want to connect to a different JSON-RPC endpoint | ||
| ethereum: 'mainnet:http://host.docker.internal:8545' | ||
| GRAPH_LOG: info | ||
| ipfs: | ||
| image: ipfs/go-ipfs:v0.4.23 | ||
| ports: | ||
| - '5001:5001' | ||
| volumes: | ||
| - ./data/ipfs:/data/ipfs | ||
| postgres: | ||
| image: postgres | ||
| ports: | ||
| - '5432:5432' | ||
| command: ["postgres", "-cshared_preload_libraries=pg_stat_statements"] | ||
| environment: | ||
| POSTGRES_USER: graph-node | ||
| POSTGRES_PASSWORD: let-me-in | ||
| POSTGRES_DB: graph-node | ||
| volumes: | ||
| - ./data/postgres:/var/lib/postgresql/data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "name": "juno-cosmwasm-liquidity", | ||
| "version": "0.1.0", | ||
| "repository": "https://github.com/graphprotocol/example-subgraph", | ||
| "license": "MIT", | ||
| "scripts": { | ||
| "codegen": "graph codegen", | ||
| "build": "graph build", | ||
| "create-local": "graph create juno-cosmwasm-liquidity --node http://127.0.0.1:8020", | ||
| "deploy-local": "graph deploy juno-cosmwasm-liquidity -l v0.1.0 --ipfs http://127.0.0.1:5001 --node http://127.0.0.1:8020", | ||
| "remove-local": "graph remove juno-cosmwasm-liquidity --node http://127.0.0.1:8020", | ||
| "create": "graph create juno-cosmwasm-liquidity --node https://api.thegraph.com/deploy/", | ||
| "deploy": "graph deploy juno-cosmwasm-liquidity --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/" | ||
| }, | ||
| "devDependencies": { | ||
| "@graphprotocol/graph-cli": "https://github.com/figment-networks/graph-cli.git#cosmos-v1.1", | ||
| "@graphprotocol/graph-ts": "https://github.com/figment-networks/graph-ts.git#cosmos-v1.1" | ||
| }, | ||
| "dependencies": { | ||
| "@graphprotocol/juno-ts": "https://github.com/figment-networks/graph-juno-ts.git", | ||
| "assemblyscript-json": "^1.1.0", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove that now. |
||
| "babel-polyfill": "^6.26.0", | ||
| "babel-register": "^6.26.0" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| type Contract @entity { | ||
| id: ID! | ||
| liquidity: BigInt! | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { json, BigInt, cosmos, log, Bytes, typeConversion } from "@graphprotocol/graph-ts"; | ||
| import { cosmwasm } from "@graphprotocol/juno-ts"; | ||
| import { Contract } from "../generated/schema"; | ||
|
|
||
|
|
||
| export function handleMsgExecuteContract(data: cosmos.MessageData): void { | ||
| const message = cosmwasm.wasm.v1.decodeMsgExecuteContract(data.message.value); | ||
| const id = message.contract; | ||
| let contract = Contract.load(id); | ||
|
|
||
| if (contract == null) { | ||
| contract = new Contract(id); | ||
| contract.liquidity = BigInt.fromI32(0); | ||
| } | ||
|
|
||
| const result = json.try_fromBytes(Bytes.fromUint8Array(message.msg)); | ||
|
|
||
| if (result.isOk) { | ||
| const jsonMsg = result.value; | ||
| const add_liquidity = jsonMsg.toObject().getEntry("add_liquidity"); | ||
| if (add_liquidity != null) { | ||
| const amount = add_liquidity.value.toObject().getEntry("token1_amount"); | ||
| if (amount != null) { | ||
| if (validateBigInt(amount.value.toString())) { | ||
| contract.liquidity = contract.liquidity.plus(BigInt.fromString(amount.value.toString())); | ||
| } else { | ||
| log.warning("amount in not a valid number: {}", [amount.value.toString()]) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const remove_liquidity = jsonMsg.toObject().getEntry("remove_liquidity"); | ||
| if (remove_liquidity != null) { | ||
| const amount = remove_liquidity.value.toObject().getEntry("amount"); | ||
| if (amount != null) { | ||
| if (validateBigInt(amount.value.toString())) { | ||
| contract.liquidity = contract.liquidity.minus(BigInt.fromString(amount.value.toString())); | ||
| } else { | ||
| log.warning("amount in not a valid number: {}", [amount.value.toString()]) | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| log.warning("MsgExecuteContract.msg is not a valid json object: {}", [typeConversion.bytesToString(message.msg)]) | ||
| } | ||
|
|
||
| contract.save(); | ||
| } | ||
|
|
||
| function validateBigInt(str: string): boolean | ||
| { | ||
| for(let i = 0; i < str.length; i++) { | ||
| const num = parseInt(str[i]) | ||
| if (num == NaN) { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| return true; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| specVersion: 0.0.5 | ||
| description: Juno CosmWasm Liquidity Example | ||
| repository: https://github.com/graphprotocol/example-subgraph | ||
| schema: | ||
| file: ./schema.graphql | ||
| dataSources: | ||
| - kind: cosmos | ||
| name: juno-cosmwasm-liquidity | ||
| network: juno-1 | ||
| source: | ||
| startBlock: 0 | ||
| mapping: | ||
| apiVersion: 0.0.5 | ||
| language: wasm/assemblyscript | ||
| entities: | ||
| - Coin | ||
| messageHandlers: | ||
| - message: /cosmwasm.wasm.v1.MsgExecuteContract | ||
| handler: handleMsgExecuteContract | ||
| file: ./src/mapping.ts |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
decode => decodes