Skip to content

Commit

Permalink
Adding support for thornames (#819)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: hippocampus-web3 <[email protected]>
  • Loading branch information
Thorian1te and hippocampus-web3 authored Sep 19, 2023
1 parent 122cae7 commit 0a1af5d
Show file tree
Hide file tree
Showing 19 changed files with 490 additions and 34 deletions.
6 changes: 6 additions & 0 deletions packages/xchain-midgard-query/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.1.2 (2023-09-18)

## Update

- Function getTHORNameReverseLookup

# v0.1.1 (2023-09-10)

## Update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,16 @@ describe('Midgard-query liquidity action end to end Tests', () => {
console.log('thorname', thorname)
printThorname(thorname)
})
it(`Should get thornames by alias address`, async () => {
const thornames = await midgardQuery.midgardCache.midgard.getTHORNameReverseLookup(
'bc1qy6hm644lr8ezl37wn0x59fzd5ps4k4hpufw0dp',
)
console.log('thornames', thornames)
})
it(`Try get thornames by alias address`, async () => {
const thornames = await midgardQuery.midgardCache.midgard.getTHORNameReverseLookup(
'thor138yxnksuhm37j9qqcugt3xzh8cykgvmmtgt63u',
)
console.log('thornames', thornames)
})
})
2 changes: 1 addition & 1 deletion packages/xchain-midgard-query/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-midgard-query",
"version": "0.1.1",
"version": "0.1.2",
"license": "MIT",
"description": "Module that is responsible for get data from Midgard API",
"keywords": [
Expand Down
28 changes: 27 additions & 1 deletion packages/xchain-midgard-query/src/utils/midgard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Network } from '@xchainjs/xchain-client'
import { Configuration, MidgardApi, PoolDetail, SaverDetails, THORNameDetails } from '@xchainjs/xchain-midgard'
import {
Configuration,
MidgardApi,
PoolDetail,
ReverseTHORNames,
SaverDetails,
THORNameDetails,
} from '@xchainjs/xchain-midgard'
import axios from 'axios'
import axiosRetry from 'axios-retry'

Expand Down Expand Up @@ -84,4 +91,23 @@ export class Midgard {
}
throw Error(`Midgard not responding`)
}

public async getTHORNameReverseLookup(address: string): Promise<ReverseTHORNames | undefined> {
for (const api of this.midgardApis) {
try {
const resp = await api.getTHORNamesByAddress(address)
if (resp.status == 404) {
return []
} else if (resp.status == 200) {
return resp.data
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
if (e.response.status == 404) {
return []
}
}
}
throw Error(`Midgard not responding`)
}
}
6 changes: 6 additions & 0 deletions packages/xchain-thorchain-amm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.7.1 (2023-09-18)

## Update

- Add functions getThornamesByAddress, registerThorname and updateThorname

# v0.7.0 (2023-09-10)

## Update
Expand Down
13 changes: 13 additions & 0 deletions packages/xchain-thorchain-amm/__e2e__/thorchain-thornames.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ThorchainQuery } from '@xchainjs/xchain-thorchain-query'

import { ThorchainAMM } from '../src/thorchain-amm'

const thorchainQueryMainnet = new ThorchainQuery()
const mainetThorchainAmm = new ThorchainAMM(thorchainQueryMainnet)

describe('Thorchain-amm thornames', () => {
it(`Should get all thornames by address`, async () => {
const thornames = await mainetThorchainAmm.getThornamesByAddress('0xc50531811f3d8161a2b53349974ae4c7c6d3bfba')
console.log('thornames', thornames)
})
})
73 changes: 72 additions & 1 deletion packages/xchain-thorchain-amm/__e2e__/wallet.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import cosmosclient from '@cosmos-client/core'
import { Network } from '@xchainjs/xchain-client'
import { ThorchainCache, ThorchainQuery, Thornode } from '@xchainjs/xchain-thorchain-query'
import {
AssetBTC,
AssetETH,
BCHChain,
BTCChain,
ETHChain,
ThorchainCache,
ThorchainQuery,
Thornode,
} from '@xchainjs/xchain-thorchain-query'
import { baseToAsset, formatAssetAmountCurrency, register9Rheader } from '@xchainjs/xchain-util'
import axios from 'axios'

Expand Down Expand Up @@ -42,4 +51,66 @@ describe('xchain-swap wallet Tests', () => {
console.error(e)
}
})
it(`Register thorname`, async () => {
try {
await mainnetWallet.registerThorname({
thorname: 'hippocampus',
chain: BTCChain,
preferredAsset: AssetBTC,
expirity: new Date(2024, 8, 11, 14, 30, 0, 0),
})
} catch (e) {
console.error(e)
}
})

it(`Update thorname`, async () => {
try {
await mainnetWallet.updateThorname({
thorname: 'hippo',
chain: BCHChain,
chainAddress: 'qz53fqdfjqwefhff9xf3dmq45g3l7jydyu6d990e76',
})
} catch (e) {
console.error(e)
}
})

it(`Update thorname with expirity`, async () => {
try {
await mainnetWallet.updateThorname({
thorname: 'hippo',
chain: BCHChain,
chainAddress: 'qz53fqdfjqwefhff9xf3dmq45g3l7jydyu6d990e76',
expirity: new Date(2024, 9, 11, 14, 30, 0, 0),
})
} catch (e) {
console.error(e)
}
})

it(`Update thorname prefered asset`, async () => {
try {
const hash = await mainnetWallet.updateThorname({
thorname: 'hippo',
chain: ETHChain,
preferredAsset: AssetETH,
})
console.log('hash', hash)
} catch (e) {
console.error(e)
}
})

it(`Try update thorname is not yours`, async () => {
try {
await mainnetWallet.updateThorname({
thorname: 'dx',
chain: ETHChain,
chainAddress: '0xc50531811f3d8161a2b53349974ae4c7c6d3bfba',
})
} catch (e) {
console.error(e)
}
})
})
6 changes: 3 additions & 3 deletions packages/xchain-thorchain-amm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-thorchain-amm",
"version": "0.7.0",
"version": "0.7.1",
"description": "module that exposes estimating & swappping cryptocurrency assets on thorchain",
"keywords": [
"THORChain",
Expand Down Expand Up @@ -55,7 +55,7 @@
"@xchainjs/xchain-midgard": "^0.5.1",
"@xchainjs/xchain-thorchain": "^0.28.6",
"@xchainjs/xchain-thornode": "^0.3.6",
"@xchainjs/xchain-thorchain-query": "^0.6.0",
"@xchainjs/xchain-thorchain-query": "^0.6.1",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"axios": "^1.3.6",
Expand Down Expand Up @@ -91,7 +91,7 @@
"@xchainjs/xchain-midgard": "^0.5.1",
"@xchainjs/xchain-thorchain": "^0.28.6",
"@xchainjs/xchain-thornode": "^0.3.6",
"@xchainjs/xchain-thorchain-query": "^0.6.0",
"@xchainjs/xchain-thorchain-query": "^0.6.1",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.2",
"axios": "^1.3.6",
Expand Down
9 changes: 9 additions & 0 deletions packages/xchain-thorchain-amm/src/thorchain-amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,13 @@ export class ThorchainAMM {
toAddress: `${withdrawLoan.inboundAddress}`,
})
}

/**
* Get all Thornames and its data associated owned by an address
* @param address - address
* @returns thornames data
*/
public async getThornamesByAddress(address: string) {
return this.thorchainQuery.thorchainCache.midgardQuery.midgardCache.midgard.getTHORNameReverseLookup(address)
}
}
18 changes: 18 additions & 0 deletions packages/xchain-thorchain-amm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,21 @@ export type LoanCloseParams = {
amount: CryptoAmount
toAddress: Address
}

export type RegisterThornameParams = {
thorname: string
owner?: string
chain?: string
chainAddress?: string
preferredAsset?: Asset
expirity?: Date
}

export type UpdateThornameParams = {
thorname: string
owner?: string
chain?: string
chainAddress?: string
preferredAsset?: Asset
expirity?: Date
}
88 changes: 86 additions & 2 deletions packages/xchain-thorchain-amm/src/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Client as AvaxClient, defaultAvaxParams } from '@xchainjs/xchain-avax'
import { Client as BnbClient } from '@xchainjs/xchain-binance'
import { Client as BtcClient } from '@xchainjs/xchain-bitcoin'
import { BTCChain, Client as BtcClient } from '@xchainjs/xchain-bitcoin'
import { Client as BchClient } from '@xchainjs/xchain-bitcoincash'
import { Client as BscClient, defaultBscParams } from '@xchainjs/xchain-bsc'
import { FeeOption, Network, XChainClient } from '@xchainjs/xchain-client'
Expand All @@ -11,15 +11,17 @@ import { Client as LtcClient } from '@xchainjs/xchain-litecoin'
import { Client as MayaClient } from '@xchainjs/xchain-mayachain'
import { Client as ThorClient, THORChain, ThorchainClient } from '@xchainjs/xchain-thorchain'
import { CryptoAmount, ThorchainQuery } from '@xchainjs/xchain-thorchain-query'
import { Address, Asset } from '@xchainjs/xchain-util'
import { Address, Asset, assetFromString } from '@xchainjs/xchain-util'

import {
AddLiquidity,
AllBalances,
ExecuteSwap,
LoanCloseParams,
LoanOpenParams,
RegisterThornameParams,
TxSubmitted,
UpdateThornameParams,
WithdrawLiquidity,
} from './types'
import { EvmHelper } from './utils/evm-helper'
Expand Down Expand Up @@ -414,6 +416,88 @@ export class Wallet {
}
}

/**
* Register a THORName with a default expirity of one year. By default chain and chainAddress is getting from wallet instance and is BTC.
* By default owner is getting from wallet
* @param thorname - Name to register
* @param chain - Chain to add alias
* @param chainAddress - Address to add to chain alias
* @param owner - Owner address (rune address)
* @param preferredAsset - referred asset
* @param expirity - expirity of the domain in MILLISECONDS
* @param isUpdate - true only if the domain is already register and you want to update its data
* @returns memo and value of deposit
*/
async registerThorname(params: RegisterThornameParams) {
const chainClient = this.clients[params.chain || BTCChain]
const thorClient = this.clients.THOR

if (!chainClient || !thorClient) {
throw Error('Can not find a wallet client')
}

const thornameEstimation = await this.thorchainQuery.estimateThorname({
...params,
chain: params.chain || BTCChain,
chainAddress: params.chainAddress || chainClient.getAddress(),
owner: params.owner || thorClient.getAddress(),
})

const castedThorClient = thorClient as unknown as ThorchainClient
const result = await castedThorClient.deposit({
asset: thornameEstimation.value.asset,
amount: thornameEstimation.value.baseAmount,
memo: thornameEstimation.memo,
})

return result
}

/**
* Register a THORName with a default expirity of one year. By default chain and chainAddress is getting from wallet instance and is BTC.
* By default owner is getting from wallet
* @param thorname - Name to register
* @param chain - Chain to add alias
* @param chainAddress - Address to add to chain alias
* @param owner - Owner address (rune address)
* @param preferredAsset - referred asset
* @param expirity - expirity of the domain in MILLISECONDS
* @param isUpdate - true only if the domain is already register and you want to update its data
* @returns memo and value of deposit
*/
async updateThorname(params: UpdateThornameParams) {
const chainClient = this.clients[params.chain || BTCChain]
const thorClient = this.clients.THOR

if (!chainClient || !thorClient) {
throw Error('Can not find a wallet client')
}

const thornameDetail = await this.thorchainQuery.getThornameDetails(params.thorname)

if (thornameDetail?.owner !== thorClient.getAddress()) {
throw Error('You cannot update a domain that is not yours')
}

const thornameEstimation = await this.thorchainQuery.estimateThorname({
...params,
chain: params.chain || BTCChain,
isUpdate: true,
preferredAsset: params.preferredAsset || assetFromString(thornameDetail.preferredAsset),
chainAddress: params.chainAddress || chainClient.getAddress(),
})

const castedThorClient = thorClient as unknown as ThorchainClient

const result = await castedThorClient.deposit({
asset: thornameEstimation.value.asset,
amount: thornameEstimation.value.baseAmount,
memo: thornameEstimation.memo,
})

return result
}

/** Function handles liquidity add for all non rune assets
*
* @param params - parameters for add liquidity
Expand Down
6 changes: 6 additions & 0 deletions packages/xchain-thorchain-query/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.6.1 (2023-09-18)

## Update

- New functions estimateThorname and getThornameDetails

# v0.6.0 (2023-09-10)

## Update
Expand Down
Loading

0 comments on commit 0a1af5d

Please sign in to comment.