Skip to content

Commit

Permalink
Merge pull request #135 from AbstractSDK/adair/ajs-46-create-a-new-xi…
Browse files Browse the repository at this point in the history
…on-provider-in-abstractjs

Create a new provider-xion package.
  • Loading branch information
adairrr authored Oct 30, 2024
2 parents 985dc61 + b5e465e commit 1dfb7c3
Show file tree
Hide file tree
Showing 12 changed files with 1,491 additions and 127 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-lobsters-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@abstract-money/provider-xion": patch
---

Initialize xion provider
8 changes: 6 additions & 2 deletions .github/workflows/manual-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ on:
- "@abstract-money/core"
- "@abstract-money/cli"
- "@abstract-money/react"
- "@abstract-money/provider-graz"
- "@abstract-money/cosmwasm-utils"
- "@abstract-money/provider-cosmoskit"
- "@abstract-money/provider-graz"
- "@abstract-money/provider-xion"
# TODO: Infer from package name
path:
type: choice
Expand All @@ -20,8 +22,10 @@ on:
- "core"
- "cli"
- "react"
- "provider-graz"
- "cosmwasm-utils"
- "provider-cosmoskit"
- "provider-graz"
- "provider-xion"
env:
FORCE_COLOR: true

Expand Down
4 changes: 4 additions & 0 deletions packages/provider-xion/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated file. Do not edit directly.
clients/**
utils/**
legacy/**
1 change: 1 addition & 0 deletions packages/provider-xion/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @abstract-money/provider-xion
21 changes: 21 additions & 0 deletions packages/provider-xion/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024, Abstract Money

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.
58 changes: 58 additions & 0 deletions packages/provider-xion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@abstract-money/provider-xion",
"version": "0.0.1",
"description": "Provider for Xion",
"homepage": "https://github.com/AbstractSDK/abstract.js#readme",
"author": "adair <[email protected]>",
"contributors": [
"dalechyn <[email protected]>",
"adairrr <[email protected]>"
],
"license": "ISC",
"repository": {
"type": "git",
"url": "git+https://github.com/AbstractSDK/abstract.js.git",
"directory": "packages/provider-xion"
},
"bugs": {
"url": "https://github.com/AbstractSDK/abstract.js/issues"
},
"scripts": {
"build": "tsup",
"clean": "rimraf dist",
"typecheck": "tsc --noEmit --jsx react-jsx"
},
"peerDependenciesMeta": {
"typescript": {
"optional": true
}
},
"type": "module",
"devDependencies": {
"@abstract-money/react": "workspace:*",
"@abstract-money/core": "workspace:*",
"@cosmjs/cosmwasm-stargate": "0.32.3",
"@types/node": "^20.0.0",
"@burnt-labs/abstraxion": "=1.0.0-alpha.50",
"rimraf": "^3.0.0"
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
"files": [
"/dist"
],
"sideEffects": false,
"peerDependencies": {
"@abstract-money/react": "workspace:*",
"@abstract-money/core": "workspace:*",
"@burnt-labs/abstraxion": "^1.0.0-alpha.50",
"typescript": ">=5.0.4"
}
}
49 changes: 49 additions & 0 deletions packages/provider-xion/src/cosmwasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { chainNameToId } from '@abstract-money/core'
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import type { UseQueryOptions } from '@tanstack/react-query/src/types'

export function getCosmWasmClientQueryKey(chainName: string) {
return ['cosmWasmClient', chainName]
}

/**
* Get a polkachu URL for the given chain id. TODO: replace with something from the Abstract API.
* @deprecated
* @param chainName
*/
const chainNameToRpc = (chainName: string) => {
const hyphenatedChainName = chainName.replace('testnet', '-testnet')
return `https://${hyphenatedChainName}-rpc.polkachu.com/`
}

async function getCosmWasmClient(chainName: string) {
const endpoint = chainNameToRpc(chainName)

const client = await CosmWasmClient.connect(endpoint).catch((e) => {
console.error('Failed to connect to chain', chainName, e)
throw e
})

const chainId = chainNameToId(chainName)

const clientChainId = await client.getChainId()
console.debug('Retrieved chain ID', clientChainId, chainName)
if (chainId !== clientChainId) {
throw new Error(
`Expected client for ${chainId}, got ${clientChainId} instead`,
)
}
return client
}

/**
* Query options for the CosmWasmClient.
* @param chainName
*/
export const cosmWasmClientQueryOptions = (chainName: string) =>
({
queryKey: getCosmWasmClientQueryKey(chainName),
queryFn: async () => {
return await getCosmWasmClient(chainName)
},
}) satisfies UseQueryOptions<CosmWasmClient, Error>
28 changes: 28 additions & 0 deletions packages/provider-xion/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Provider } from '@abstract-money/react'
import {
useAbstraxionAccount,
useAbstraxionSigningClient,
} from '@burnt-labs/abstraxion'
import { useQuery } from '@tanstack/react-query'
import { cosmWasmClientQueryOptions } from './cosmwasm'

const XION_TESTNET_CHAIN_NAME = 'xiontestnet'

export const xionProvider: Provider = {
useSenderAddress() {
const { data } = useAbstraxionAccount()
return data?.bech32Address
},
useSigningCosmWasmClient() {
const { client } = useAbstraxionSigningClient()
return client
},
useCosmWasmClient(args) {
// TODO: retrieve the xion chain name from the abstraxion config when they expose the hook
const chainName = args?.chainName ?? XION_TESTNET_CHAIN_NAME

const { data: client } = useQuery(cosmWasmClientQueryOptions(chainName))

return client
},
}
35 changes: 35 additions & 0 deletions packages/provider-xion/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
"downlevelIteration": true,
"esModuleInterop": true,
"isolatedModules": true,
"lib": ["es2021", "dom"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noImplicitAny": true,
"noUncheckedIndexedAccess": true,
// TODO: change to true
"noUnusedLocals": false,
"noUnusedParameters": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"strictNullChecks": true,
"target": "es2021",
"types": ["node", "@types/jest"],
"paths": {
// TODO: Remove once issue is fixed
// https://github.com/microsoft/TypeScript/issues/48212
"@cosmjs/cosmwasm-stargate": [
"../react/node_modules/@cosmjs/cosmwasm-stargate/build"
]
},
},
"exclude": ["**/node_modules/**", "**/dist/**"],
"include": [
"src/**/*",
]
}
14 changes: 14 additions & 0 deletions packages/provider-xion/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from 'tsup'

import { getConfig } from '../../scripts/tsup'
import { peerDependencies } from './package.json'

export default defineConfig(
getConfig({
experimentalDts: false,
outDir: 'dist',
//dev: process.env.DEV === 'true',
entry: ['src/index.ts'],
external: [...Object.keys(peerDependencies)],
}),
)
16 changes: 16 additions & 0 deletions packages/provider-xion/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"pipeline": {
"build": {
"dependsOn": ["@abstract-money/react#build", "clean"],
"outputs": ["dist/**"]
},
"typecheck": {
"dependsOn": ["@abstract-money/react#build", "clean"]
},
"clean": {
"cache": false
}
}
}
Loading

0 comments on commit 1dfb7c3

Please sign in to comment.