{"id":22609488,"url":"https://github.com/kelcheone/uni-wrapper-sdk","last_synced_at":"2025-04-11T06:29:57.220Z","repository":{"id":112228395,"uuid":"422967417","full_name":"kelcheone/Uni-wrapper-SDK","owner":"kelcheone","description":"an implementation of  a Uniswap SDK using polywrap toolchain","archived":false,"fork":false,"pushed_at":"2021-10-30T19:01:25.000Z","size":67,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T04:26:03.827Z","etag":null,"topics":["assemblyscript","ethereum","polywrap","sdk","toolchain","uniswap"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kelcheone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-30T19:01:20.000Z","updated_at":"2021-11-01T13:16:49.000Z","dependencies_parsed_at":"2023-05-11T16:45:18.737Z","dependency_job_id":null,"html_url":"https://github.com/kelcheone/Uni-wrapper-SDK","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelcheone%2FUni-wrapper-SDK","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelcheone%2FUni-wrapper-SDK/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelcheone%2FUni-wrapper-SDK/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kelcheone%2FUni-wrapper-SDK/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kelcheone","download_url":"https://codeload.github.com/kelcheone/Uni-wrapper-SDK/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248354952,"owners_count":21089912,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assemblyscript","ethereum","polywrap","sdk","toolchain","uniswap"],"created_at":"2024-12-08T15:12:34.987Z","updated_at":"2025-04-11T06:29:57.193Z","avatar_url":"https://github.com/kelcheone.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Your Own Polywrapper (Using Uniswap)\n\u003e NOTE: If at any point during this tutorial you feel stuck, you can see the completed project [here](https://github.com/polywrap/uni-workshop/tree/completed). Additionally please feel free to reach out to us on [Discord](http://discord.polywrap.io/) if you have any questions, or need some hands on help.\n\n## 0. Pre-Requisites\nHave installed:\n- Node.JS\n- NVM (Node Version Manager)\n- Yarn\n- Docker\n- Docker-Compose\n\nHave a basic understanding of:\n- GraphQL\n- AssemblyScript\n- Uniswap\n\n## 1. Installation\nFrom within this directory, run the following commands:\n* `nvm install` - install the version of node listed in the `.nvmrc` file.\n* `nvm use` - use the version of node listed in the `.nvmrc` file.\n* `yarn` - install all package.json dependencies.\n\n## 2. Write The Query Schema\nIn GraphQL \u0026 Polywrap, operations are seperated in Read \u0026 Write, or rather Query \u0026 Mutation. We'll start with first developing our Query module.\n\nFor each Polywrap module, there is a GraphQL schema \u0026 an Assemblyscript (or other wasm-compatible language) implementation. The GraphQL serves as the \"public interface\" for your webassembly code.\n\nNavigate to the file:  \n[`./src/query/schema.graphql`](./src/query/schema.graphql)\n\nAnd add the following code:  \n```graphql\n#import { Query, ChainId, TokenAmount } into Uni from \"w3://ens/v2.uniswap.web3api.eth\"\n\ntype Query {\n  fetchTokenTotalSupply(\n    chainId: Uni_ChainId!\n    address: String!\n    symbol: String\n    name: String\n  ): Uni_TokenAmount!\n}\n```\n\nIn this schema, you'll see that first we import some types from the Uniswap polywrapper. Next, we define the `fetchTokenTotalSupply` query function.\n\n## 3. Add the Query Implementation\n\nLet's write the \"implementation\" of this function in AssemblyScript, which will later be compiled down to WebAssembly :)\n\nIn the file: \n[`./src/query/index.ts`](./src/query/index.ts)\n\nAdd the following code:  \n```typescript\nimport {\n  Uni_Query,\n  Uni_TokenAmount,\n  Input_fetchTokenTotalSupply\n} from \"./w3\";\n\nexport function fetchTokenTotalSupply(input: Input_fetchTokenTotalSupply): Uni_TokenAmount {\n  const token = Uni_Query.fetchTokenData({\n    chainId: input.chainId,\n    address: input.address,\n    symbol: input.symbol,\n    name: input.name\n  });\n\n  const amount = Uni_Query.fetchTotalSupply({\n    token: token\n  });\n\n  return amount;\n}\n```\n\nIn the code above, you can see that we're querying the Uniswap polywrapper from within our own custom wrapper. This shows just how easy it is to integrate and compose polywrappers together.\n\nAdditionally, you'll notice that we're importing the same types that are used in our GraphQL schema from the `./w3` folder. This is our \"magic code-generation folder\", where Polywrap's CLI generates \"language bindings\" for your interface's types. This generated code does all the a lot of heavy lifting for you, so all you have to worry about is implementing the business logic that makes your wrapper unique and useful.\n\n## 4. Write The Mutation Schema\n\nNext, we'll create a Mutation (write) function. This function will make it a bit easier for a user to swap tokens using Uniswap.\n\nIn the file:  \n[`./src/mutation/schema.graphql`](./src/mutation/schema.graphql)\n\nAdd the following schema:  \n```graphql\n#import { Mutation, Query, ChainId, TokenAmount, TradeOptions } into Uni from \"w3://ens/v2.uniswap.web3api.eth\"\n\ntype Mutation {\n  simpleSwap(\n    chainId: Uni_ChainId!\n    tokenInAddress: String!\n    tokenOutAddress: String!\n    tokenInAmount: BigInt!\n    tradeOptions: Uni_TradeOptions!\n  ): SwapOutput!\n}\n\ntype SwapOutput {\n  txHash: String!\n}\n```\n\nAs you can see, we define another function named `simpleSwap`, as well as a new custom type `SwapOutput`.\n\n## 5. Add the Mutation Implementation\n\nAnd finally, the mutation's implementation:  \n[`./src/mutation/index.ts`](./src/mutation/index.ts)\n\n```typescript\nimport {\n  Uni_Query,\n  Uni_Mutation,\n  Input_simpleSwap,\n  SwapOutput,\n  Uni_TradeType\n} from \"./w3\";\n\nexport function simpleSwap(input: Input_simpleSwap): SwapOutput {\n  const tokenIn = Uni_Query.fetchTokenData({\n    chainId: input.chainId,\n    address: input.tokenInAddress,\n    symbol: null,\n    name: null\n  });\n\n  const tokenOut = Uni_Query.fetchTokenData({\n    chainId: input.chainId,\n    address: input.tokenOutAddress,\n    symbol: null,\n    name: null\n  });\n\n  const txResponse = Uni_Mutation.swap({\n    tokenIn: tokenIn,\n    tokenOut: tokenOut,\n    amount: input.tokenInAmount,\n    tradeType: Uni_TradeType.EXACT_INPUT,\n    tradeOptions: input.tradeOptions,\n    txOverrides: null\n  });\n\n  return {\n    txHash: txResponse.hash\n  }\n}\n```\n\n## 6. Build Your Polywrapper\n\nNow that our implementation is done, let's build this thing and see it running!\n\nFrom within this directory, run the following commands:  \n`yarn build`\n\nThis command may take a while the first time it's run. What it is doing (in the background) is asking the Polywrap CLI to build your polywrapper.\n\nIn order to make sure polywrappers can always be rebuilt on different machines, we use Docker. Docker allows us to create a \"fresh build image\" where your source files will be built into WebAssembly (Wasm) output files.\n\nDocker helps you and your team better collaborate, and in the future will provide \"source code verification\" so users can see what your polywrapper's code looks like, helping increase trust in what they're running inside their apps.\n\nAfter the build command finishes, you should see a `./build` folder. Take a peek inside! You'll find the fully-built GraphQL schema, Query \u0026 Mutation Wasm modules, and some web3api YAML manifest files. \n\n## 7. Test Your Polywrapper\n\nAlright enough chit-chat, let's test it!\n\nLuckily with Polywrap, testing is extremely easy to get up \u0026 running with. Since wrappers can be queried on-demand, we created a useful \"query recipe\" utility.\n\nChecking the query recipe at:  \n`./recipes/e2e.json`\n\nThis query recipe instructs the CLI to:  \n1. Connect to the polywrapper @ `/ens/testnet/uni-integration.eth`\n2. Execute the provided query\n\nLet's try it. Run the following commands from within this folder:  \n* `yarn test:env:up`\n* `yarn deploy`\n* `yarn test`\n\nYou should see the following output:\n```\n-----------------------------------\nquery {\n  fetchTokenTotalSupply(\n    chainId: $chainId\n    address: $address\n  )\n}\n\n{\n  \"chainId\": \"MAINNET\",\n  \"address\": \"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984\"\n}\n-----------------------------------\n-----------------------------------\n{\n  \"fetchTokenTotalSupply\": {\n    \"token\": {\n      \"chainId\": 0,\n      \"address\": \"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984\",\n      \"currency\": {\n        \"decimals\": 18,\n        \"symbol\": \"UNI\",\n        \"name\": \"Uniswap\"\n      }\n    },\n    \"amount\": \"1000000000000000000000000000\"\n  }\n}\n-----------------------------------\n```\n\nThe first section is the query, and the second section is the result returned from the polywrapper you've just developed!\n\nAfter you're done, be sure to run `yarn test:env:down` to teardown our testing docker environment.\n\n## 8. Extra Credit\nAs you can see above, we've only tested the Query method we built, but not the mutation method.\n\nWell, in order to test the mutation method, we'll have to create our own instance of the Web3ApiClient class pointed at the mainnet ganache fork running at 8546.\n\nAn example of how to do this can be found here:  \nhttps://github.com/polywrap/monorepo/blob/prealpha/packages/apis/uniswapv2/src/__tests__/e2e/swap_e2e.spec.ts\n\nBased on the project setup linked above, try to setup this project similarly with Jest testing. Use that setup to test the mutation method we added above.\n\n## Recap\nBuilding WebAssembly based SDKs has never been so easy. We've shown you how to define your own schemas, implementations, and import existing polywrappers into your own wrapper.\n\nPolywrap is Web3 composability on steroids for dApp developers, and we hope that this simple tutorial is starting to give you a better idea of why this is so.\n\nIf you'd like to learn more, checkout our landing page \u0026 documentation:\nhttps://polywrap.io\n\nIf you have any questions, don't hesitate to reach out:\nhttp://discord.polywrap.io\n\n## Resources\nTry The Uniswap \u003c\u003e Polywrap Demo App:  \nhttps://demo.uniswap.polywrap.io/\n\nCheckout the Uniswap Polywrapper's source-code:  \nhttps://github.com/polywrap/monorepo/tree/prealpha/packages/apis/uniswapv2\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkelcheone%2Funi-wrapper-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkelcheone%2Funi-wrapper-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkelcheone%2Funi-wrapper-sdk/lists"}