Skip to content

Commit

Permalink
Add Kujira (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
hippocampus-web3 authored Sep 27, 2023
1 parent 516da27 commit 817270b
Show file tree
Hide file tree
Showing 16 changed files with 358 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/xchain-kujira/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.log
.DS_Store
node_modules
npm-debug.log
.env
3 changes: 3 additions & 0 deletions packages/xchain-kujira/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# v.0.1.0 (2023-09-24)

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

Copyright (c) 2020 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.
22 changes: 22 additions & 0 deletions packages/xchain-kujira/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# `@xchainjs/xchain-kujira`

Kujira Module

## Installation

```
yarn add @xchainjs/xchain-kujira
```

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

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

## Cosmos Client Testing

```
yarn install
yarn test
```
66 changes: 66 additions & 0 deletions packages/xchain-kujira/__e2e__/kujira-client.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Network, TxParams } from '@xchainjs/xchain-client'
import { assetToString, baseAmount } from '@xchainjs/xchain-util'

import { Client as KujiraClient } from '../src/client'
import { AssetKUJI, KUJIChain } from '../src/const'

let xchainClient: KujiraClient

const AssetTokenKuji = {
chain: KUJIChain,
symbol: 'factory/kujira1ltvwg69sw3c5z99c6rr08hal7v0kdzfxz07yj5/demo',
ticker: '',
synth: false,
}

describe('Kujira client Integration Tests', () => {
beforeEach(() => {
xchainClient = new KujiraClient({
network: Network.Testnet,
phrase: process.env.MAINNETPHRASE,
})
})
it('should fetch balances kujira', 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 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('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)
})
})
42 changes: 42 additions & 0 deletions packages/xchain-kujira/__tests__/kujira-client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Network } from '@xchainjs/xchain-client'

import { Client as KujiraClient } from '../src/client'
import { DEFAULT_FEE } from '../src/const'

let xchainClient: KujiraClient
const phraseOne = 'atom green various power must another rent imitate gadget creek fat then'

describe('Kujira client Integration Tests', () => {
beforeEach(() => {
xchainClient = new KujiraClient({
network: Network.Testnet,
phrase: phraseOne,
})
})
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 address = await xchainClient.getAddress(0)
expect(address).toBe('kujira1k688m5uq5gqwt2sltvvu0679vnh3ehlslvf9e2')
})
it('get fees', async () => {
const fees = await xchainClient.getFees()
expect(fees.average).toBe(DEFAULT_FEE)
})
it('get explorer address url', async () => {
const url = await xchainClient.getExplorerAddressUrl('kujira1k688m5uq5gqwt2sltvvu0679vnh3ehlslvf9e2')
expect(url).toBe('https://finder.kujira.network/harpoon-4/address/kujira1k688m5uq5gqwt2sltvvu0679vnh3ehlslvf9e2')
})
it('get explorer tx url', async () => {
const url = await xchainClient.getExplorerTxUrl('F3131AE603FFDE602217330410DD3ADFB9E21C987DDAA5CCF54F99DB15A6714B')
expect(url).toBe(
'https://finder.kujira.network/harpoon-4/tx/F3131AE603FFDE602217330410DD3ADFB9E21C987DDAA5CCF54F99DB15A6714B',
)
})
})
8 changes: 8 additions & 0 deletions packages/xchain-kujira/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-kujira/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-kujira/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest.setTimeout(60000)
47 changes: 47 additions & 0 deletions packages/xchain-kujira/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@xchainjs/xchain-kujira",
"version": "0.1.0",
"description": "Custom Kujira client",
"keywords": [
"XChain",
"Kujira"
],
"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": "[email protected]:xchainjs/xchainjs-lib.git"
},
"scripts": {
"clean": "rimraf lib/**",
"build": "yarn clean && rollup -c",
"test": "jest --passWithNoTests",
"e2e": "jest --config jest.config.e2e.js",
"lint": "eslint \"{src,__tests__}/**/*.ts\" --fix --max-warnings 0",
"prepublishOnly": "yarn build"
},
"devDependencies": {
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-util": "^0.13.2",
"@xchainjs/xchain-cosmos-sdk": "^0.1.0"
},
"publishConfig": {
"access": "public"
},
"peerDependencies": {
"@xchainjs/xchain-client": "^0.14.2",
"@xchainjs/xchain-util": "^0.13.2",
"@xchainjs/xchain-cosmos-sdk": "^0.1.0"
}
}
35 changes: 35 additions & 0 deletions packages/xchain-kujira/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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: [
json({}),
external(),
resolve({ preferBuiltins: true, browser: true }),
typescript({
exclude: '__tests__/**',
}),
commonjs(),
],
external: ['readable-stream', 'axios', 'buffer', 'crypto', 'stream', 'string_decoder'],
}
52 changes: 52 additions & 0 deletions packages/xchain-kujira/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { AssetInfo } from '@xchainjs/xchain-client'
import { Client as CosmosSdkClient, CosmosSdkClientParams } from '@xchainjs/xchain-cosmos-sdk'
import { Address, Asset, eqAsset } from '@xchainjs/xchain-util'

import { AssetKUJI, KUJI_DECIMAL } from './const'
import { defaultClientConfig, getDefaultExplorers } from './utils'

export type KujiraClientParams = Partial<CosmosSdkClientParams>

export class Client extends CosmosSdkClient {
constructor(config: KujiraClientParams = defaultClientConfig) {
super({
...config,
...defaultClientConfig,
})
}

getAssetInfo(): AssetInfo {
const assetInfo: AssetInfo = {
asset: AssetKUJI,
decimal: KUJI_DECIMAL,
}
return assetInfo
}

getDenom(asset: Asset): string | null {
if (eqAsset(asset, AssetKUJI)) return this.baseDenom
return null
}

assetFromDenom(denom: string): Asset | null {
if (denom === this.baseDenom) return AssetKUJI
return {
chain: AssetKUJI.chain,
symbol: denom,
ticker: '',
synth: false,
}
}

getExplorerUrl(): string {
return getDefaultExplorers()[this.network]
}

getExplorerAddressUrl(address: Address): string {
return `${this.getExplorerUrl()}/address/${address}`
}

getExplorerTxUrl(txID: string): string {
return `${this.getExplorerUrl()}/tx/${txID}`
}
}
11 changes: 11 additions & 0 deletions packages/xchain-kujira/src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Asset, baseAmount } from '@xchainjs/xchain-util'

export const KUJI_DECIMAL = 6

export const DEFAULT_GAS_LIMIT = '200000'

export const DEFAULT_FEE = baseAmount(5000, KUJI_DECIMAL)

export const KUJIChain = 'KUJI' as const

export const AssetKUJI: Asset = { chain: KUJIChain, symbol: 'KUJI', ticker: 'KUJI', synth: false }
1 change: 1 addition & 0 deletions packages/xchain-kujira/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './client'
34 changes: 34 additions & 0 deletions packages/xchain-kujira/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Network, RootDerivationPaths } from '@xchainjs/xchain-client'
import { CosmosSdkClientParams } from '@xchainjs/xchain-cosmos-sdk'

import { AssetKUJI, DEFAULT_FEE } from './const'

export const getDefaultClientUrls = (): Record<Network, string> => {
return {
[Network.Testnet]: 'https://test-rpc-kujira.mintthemoon.xyz/',
[Network.Stagenet]: 'https://rpc.cosmos.directory/kujira/',
[Network.Mainnet]: 'https://rpc.cosmos.directory/kujira/',
}
}

export const getDefaultRootDerivationPaths = (): RootDerivationPaths => ({
[Network.Mainnet]: `44'/118'/0'/0/`,
[Network.Testnet]: `44'/118'/0'/0/`,
[Network.Stagenet]: `44'/118'/0'/0/`,
})

export const getDefaultExplorers = (): RootDerivationPaths => ({
[Network.Mainnet]: 'https://finder.kujira.network/kaiyo-1',
[Network.Testnet]: 'https://finder.kujira.network/harpoon-4',
[Network.Stagenet]: 'https://finder.kujira.network/kaiyo-1',
})

export const defaultClientConfig: CosmosSdkClientParams = {
chain: AssetKUJI.chain,
clientUrls: getDefaultClientUrls(),
rootDerivationPaths: getDefaultRootDerivationPaths(),
prefix: 'kujira',
defaultDecimals: 6,
defaultFee: DEFAULT_FEE,
baseDenom: 'ukuji',
}
4 changes: 4 additions & 0 deletions packages/xchain-kujira/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": [ "src/**/*" ]
}

0 comments on commit 817270b

Please sign in to comment.