Skip to content

Commit

Permalink
New package xchain-cosmos-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
hippocampus-web3 committed Sep 23, 2023
1 parent 0a1af5d commit 3f77ef2
Show file tree
Hide file tree
Showing 15 changed files with 2,223 additions and 1,326 deletions.
6 changes: 6 additions & 0 deletions packages/xchain-cosmos-sdk/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.log
.DS_Store
node_modules
lerna-debug.log
npm-debug.log
packages/**/lib
3 changes: 3 additions & 0 deletions packages/xchain-cosmos-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# v.0.1.0 (2023-09-19)

First release
21 changes: 21 additions & 0 deletions packages/xchain-cosmos-sdk/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 THORChain

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.
17 changes: 17 additions & 0 deletions packages/xchain-cosmos-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# `@xchainjs/xchain-cosmos-sdk`

## Modules

- `client` - Custom client for communicating with cosmosSDK chains by using [`cosmjs`](https://github.com/cosmos/cosmjs)

## Installation

```
yarn add @xchainjs/xchain-cosmos-sdk
```

Following peer dependencies have to be installed into your project. These are not included in `@xchainjs/xchain-cosmos-sdk`.

```
yarn add @xchainjs/xchain-client @xchainjs/xchain-crypto @xchainjs/xchain-util
```
128 changes: 128 additions & 0 deletions packages/xchain-cosmos-sdk/__e2e__/cosmos-sdk-client.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { AssetInfo, Network, TxParams } from '@xchainjs/xchain-client'
import { Asset, assetFromString, assetToString, baseAmount, eqAsset } from '@xchainjs/xchain-util'

const AssetKUJI = assetFromString('KUJI.KUJI') as Asset
const AssetTokenKuji = {
chain: 'KUJI',
symbol: 'factory/kujira1ltvwg69sw3c5z99c6rr08hal7v0kdzfxz07yj5/demo',
ticker: '',
synth: false,
}

import Client from '../src/client'

let xchainClient: Client

class CustomSdkClient extends Client {
getAssetInfo(): AssetInfo {
throw new Error('Method not implemented.')
}
getDenom(asset: Asset): string | null {
if (eqAsset(asset, AssetKUJI)) return 'ukuji'
return null
}
assetFromDenom(denom: string): Asset | null {
if (denom === this.baseDenom) return AssetKUJI
return {
chain: AssetKUJI.chain,
symbol: denom,
ticker: '',
synth: false,
}
}
getExplorerUrl(): string {
throw new Error('Method not implemented.')
}
getExplorerAddressUrl(_address: string): string {
throw new Error('Method not implemented.')
}
getExplorerTxUrl(_txID: string): string {
throw new Error('Method not implemented.')
}
}

describe('Cosmos SDK client Integration Tests', () => {
beforeEach(() => {
const settings = {
network: Network.Testnet,
phrase: process.env.MAINNETPHRASE,
chain: AssetKUJI.chain,
defaultDecimals: 6,
prefix: 'kujira',
baseDenom: 'ukuji',
defaultFee: baseAmount(5000, 6),
rootDerivationPaths: {
[Network.Mainnet]: `44'/118'/0'/0/`,
[Network.Testnet]: `44'/118'/0'/0/`,
[Network.Stagenet]: `44'/118'/0'/0/`,
},
clientUrls: {
[Network.Testnet]: 'https://test-rpc-kujira.mintthemoon.xyz/',
[Network.Stagenet]: 'wip',
[Network.Mainnet]: 'wip',
},
}
xchainClient = new CustomSdkClient(settings)
})
it('should fetch balances cosmos sdk', async () => {
const address = await xchainClient.getAddress()
const balances = await xchainClient.getBalance(address)

balances.forEach((bal) => {
console.log(`${address} ${assetToString(bal.asset)} = ${bal.amount.amount()}`)
})
expect(balances.length).toBeGreaterThan(0)
})
it('should validate invalid addreses', async () => {
const isValid = xchainClient.validateAddress('asdadasd')
expect(isValid).toBe(false)
})
it('should validate valid addreses', async () => {
const isValid = xchainClient.validateAddress('kujira1es76p8qspctcxhex79c32nng9fvhuxjn4z6u7k')
expect(isValid).toBe(true)
})
it('should generate addreses', async () => {
const address0 = await xchainClient.getAddress(0)
console.log('address0', address0)
})
it('should get transactions', async () => {
const txs = await xchainClient.getTransactions({
address: 'kujira1kltgthzruhvdm8u2rjtke69tppwys63rx3fk8a',
})
console.log('txs', txs)
console.log('To:', txs.txs[0].to[0].amount.amount().toString())
console.log('From:', txs.txs[0].from[0].amount.amount().toString())
})
it('should get transaction data', async () => {
const tx = await xchainClient.getTransactionData('F3131AE603FFDE602217330410DD3ADFB9E21C987DDAA5CCF54F99DB15A6714B')
console.log('tx', tx)
tx.from.forEach((row) => console.log('from:', row.from, row.amount.amount().toString()))
tx.to.forEach((row) => console.log('to:', row.to, row.amount.amount().toString()))
})
it('get fees', async () => {
const fees = await xchainClient.getFees()
console.log('fees', fees.average.amount().toString())
})

it('transfer', async () => {
const txDate: TxParams = {
asset: AssetKUJI,
amount: baseAmount('1000', 6),
recipient: 'kujira1es76p8qspctcxhex79c32nng9fvhuxjn4z6u7k',
memo: 'Rosa melano',
}
const txHash = await xchainClient.transfer(txDate)
console.log('txHash', txHash)
})

it('Try secondary token transfer', async () => {
const txDate: TxParams = {
asset: AssetTokenKuji,
amount: baseAmount('100000', 6),
recipient: 'kujira1es76p8qspctcxhex79c32nng9fvhuxjn4z6u7k',
memo: 'Rosa melano',
}
const txHash = await xchainClient.transfer(txDate)
console.log('txHash', txHash)
})
})
8 changes: 8 additions & 0 deletions packages/xchain-cosmos-sdk/jest.config.e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/node_modules', '<rootDir>/lib'],
testMatch: ['<rootDir>/__e2e__/**/*.[jt]s?(x)'],
maxConcurrency: 1,
setupFilesAfterEnv: ['./jest.setup.js'],
}
6 changes: 6 additions & 0 deletions packages/xchain-cosmos-sdk/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/node_modules', '<rootDir>/lib'],
setupFilesAfterEnv: ['./jest.setup.js'],
}
1 change: 1 addition & 0 deletions packages/xchain-cosmos-sdk/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest.setTimeout(60000)
53 changes: 53 additions & 0 deletions packages/xchain-cosmos-sdk/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "@xchainjs/xchain-cosmos-sdk",
"version": "0.1.0",
"description": "Genereic Cosmos SDK client for XChainJS",
"keywords": [
"XChain",
"CosmosSDK"
],
"author": "XChainJS",
"homepage": "https://github.com/xchainjs/xchainjs-lib",
"license": "MIT",
"main": "lib/index.js",
"module": "lib/index.esm.js",
"typings": "lib/index.d.ts",
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib"
],
"repository": {
"type": "git",
"url": "git@github.com:xchainjs/xchainjs-lib.git"
},
"scripts": {
"clean": "rimraf lib/**",
"build": "yarn clean && rollup -c",
"test": "jest",
"lint": "eslint \"{src,__tests__, __mocks__}/**/*.ts\" --fix --max-warnings 0",
"prepublishOnly": "yarn build"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/secp256k1": "^4.0.3",
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1"
},
"peerDependencies": {
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-crypto": "^0.3.0",
"@xchainjs/xchain-util": "^0.13.1"
},
"dependencies": {
"@cosmjs/stargate": "^0.31.1",
"bech32": "^1.1.3",
"bip32": "^2.0.6",
"secp256k1": "^5.0.0"
}
}
37 changes: 37 additions & 0 deletions packages/xchain-cosmos-sdk/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import resolve from '@rollup/plugin-node-resolve'
import external from 'rollup-plugin-peer-deps-external'
import typescript from 'rollup-plugin-typescript2'

import pkg from './package.json'

export default {
input: 'src/index.ts',
output: [
{
file: pkg.main,
format: 'cjs',
exports: 'named',
sourcemap: true,
},
{
file: pkg.module,
format: 'es',
exports: 'named',
sourcemap: true,
},
],
plugins: [
// ignore(["@ethersproject/providers", "@ethersproject/abstract-provider", "@ethersproject/strings"]),
external(),
resolve({ preferBuiltins: true, browser: true }),
typescript({
tsconfig: './tsconfig.json',
exclude: '__tests__/**',
}),
commonjs(),
json(),
],
external: ['buffer', 'http', 'https', 'url', 'stream', 'string_decoder'],
}
Loading

0 comments on commit 3f77ef2

Please sign in to comment.