Skip to content

Commit

Permalink
Get MAYANames by owner (#1195)
Browse files Browse the repository at this point in the history
* Get MAYANames by address

* Changeset version file
  • Loading branch information
0xp3gasus authored Jun 17, 2024
1 parent b45c0c8 commit cd327ab
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/brown-fireants-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@xchainjs/xchain-mayachain-query': patch
'@xchainjs/xchain-mayachain-amm': patch
---

New method `getMAYANamesByOwner` to retrieve the MAYANames owned by an address
5 changes: 5 additions & 0 deletions .changeset/thick-ants-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-mayachain-amm': patch
---

New method `getMAYANameDetails` to retrieve the details of a certain MAYAName.
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ export default {
}
return [404, 'Not found']
})
mock.onGet(/\/v2\/mayaname\/rlookup\/maya1gnehec7mf4uytuw3wj4uwpptvkyvzclgq2lj09/).reply(function () {
const resp = require(`./responses/owner.json`)
return [200, resp]
})
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"eld"
]
42 changes: 42 additions & 0 deletions packages/xchain-mayachain-amm/__tests__/mayachainAMM.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,46 @@ describe('Mayachain Client Test', () => {
expect(quoteSwap.errors[0]).toBe('affiliateAddress randomAffiliateAddress is not a valid MAYA address')
expect(quoteSwap.warning).toBe('')
})

it('Should get MAYAName details', async () => {
const details = await mayachainAmm.getMAYANameDetails('eld')
expect(details).toBeTruthy()
expect(details?.name).toBe('eld')
expect(details?.expire).toBe('66751601')
expect(details?.owner).toBe('maya1gnehec7mf4uytuw3wj4uwpptvkyvzclgq2lj09')
expect(details?.entries.length).toBe(6)
expect(details?.entries[0].address).toBe('bc1qdjqcm3fsadjn9zth9wk30gy5su6hkwkhfr0re9')
expect(details?.entries[0].chain).toBe('BTC')
expect(details?.entries[1].address).toBe('XiKv7A7vVQmYwNUp4TjCq8ZamkjB8zZtsL')
expect(details?.entries[1].chain).toBe('DASH')
expect(details?.entries[2].address).toBe('0x1509b1fe69be4d508a62ce8109635e1d1cf29a4f')
expect(details?.entries[2].chain).toBe('ETH')
expect(details?.entries[3].address).toBe('kujira1mg9jt63eeww5ptnkw963z2sw6jzezxw0trns50')
expect(details?.entries[3].chain).toBe('KUJI')
expect(details?.entries[4].address).toBe('maya1gnehec7mf4uytuw3wj4uwpptvkyvzclgq2lj09')
expect(details?.entries[4].chain).toBe('MAYA')
expect(details?.entries[5].address).toBe('thor1gnehec7mf4uytuw3wj4uwpptvkyvzclgqap7e4')
expect(details?.entries[5].chain).toBe('THOR')
})

it('Should get the MAYAnames owned by an address', async () => {
const mayaNames = await mayachainAmm.getMAYANamesByOwner('maya1gnehec7mf4uytuw3wj4uwpptvkyvzclgq2lj09')
expect(mayaNames.length).toBe(1)
expect(mayaNames[0]?.name).toBe('eld')
expect(mayaNames[0]?.expire).toBe('66751601')
expect(mayaNames[0]?.owner).toBe('maya1gnehec7mf4uytuw3wj4uwpptvkyvzclgq2lj09')
expect(mayaNames[0]?.entries.length).toBe(6)
expect(mayaNames[0]?.entries[0].address).toBe('bc1qdjqcm3fsadjn9zth9wk30gy5su6hkwkhfr0re9')
expect(mayaNames[0]?.entries[0].chain).toBe('BTC')
expect(mayaNames[0]?.entries[1].address).toBe('XiKv7A7vVQmYwNUp4TjCq8ZamkjB8zZtsL')
expect(mayaNames[0]?.entries[1].chain).toBe('DASH')
expect(mayaNames[0]?.entries[2].address).toBe('0x1509b1fe69be4d508a62ce8109635e1d1cf29a4f')
expect(mayaNames[0]?.entries[2].chain).toBe('ETH')
expect(mayaNames[0]?.entries[3].address).toBe('kujira1mg9jt63eeww5ptnkw963z2sw6jzezxw0trns50')
expect(mayaNames[0]?.entries[3].chain).toBe('KUJI')
expect(mayaNames[0]?.entries[4].address).toBe('maya1gnehec7mf4uytuw3wj4uwpptvkyvzclgq2lj09')
expect(mayaNames[0]?.entries[4].chain).toBe('MAYA')
expect(mayaNames[0]?.entries[5].address).toBe('thor1gnehec7mf4uytuw3wj4uwpptvkyvzclgqap7e4')
expect(mayaNames[0]?.entries[5].chain).toBe('THOR')
})
})
22 changes: 20 additions & 2 deletions packages/xchain-mayachain-amm/src/mayachain-amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { AssetETH, Client as EthClient, defaultEthParams } from '@xchainjs/xchai
import { MAX_APPROVAL, abi } from '@xchainjs/xchain-evm'
import { Client as KujiraClient, defaultKujiParams } from '@xchainjs/xchain-kujira'
import { Client as MayaClient, MAYAChain } from '@xchainjs/xchain-mayachain'
import { MayachainQuery, QuoteSwap, QuoteSwapParams } from '@xchainjs/xchain-mayachain-query'
import { MAYANameDetails, MayachainQuery, QuoteSwap, QuoteSwapParams } from '@xchainjs/xchain-mayachain-query'
import { Client as ThorClient } from '@xchainjs/xchain-thorchain'
import { Asset, CryptoAmount, baseAmount, eqAsset, getContractAddressFromAsset } from '@xchainjs/xchain-util'
import { Address, Asset, CryptoAmount, baseAmount, eqAsset, getContractAddressFromAsset } from '@xchainjs/xchain-util'
import { Wallet } from '@xchainjs/xchain-wallet'
import { ethers } from 'ethers'

Expand Down Expand Up @@ -235,6 +235,24 @@ export class MayachainAMM {
return errors
}

/**
* Get MAYAname details
* @param {string} MAYAName
* @returns {MAYANameDetails | undefined} MAYANames details or undefined it is does not exist
*/
public async getMAYANameDetails(MAYAName: string): Promise<MAYANameDetails | undefined> {
return this.mayachainQuery.getMAYANameDetails(MAYAName)
}

/**
* Get the MAYANames owned by an address
* @param {Address} owner - Thorchain address
* @returns {MAYANameDetails[]} List of MAYANames owned by the address
*/
public async getMAYANamesByOwner(owner: Address): Promise<MAYANameDetails[]> {
return this.mayachainQuery.getMAYANamesByOwner(owner)
}

/**
* Check if a name is a valid MAYAName
* @param {string} name MAYAName to check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,21 @@ describe('Estimate swap e2e tests', () => {
const estimate = await mayachainQuery.quoteSwap(swapParams)
printQuoteSwap(estimate)
})

it('Get MAYANames by owner', async () => {
const mayaNames = await mayachainQuery.getMAYANamesByOwner('maya13x0f2r0jltfplmxe40cc67hhca27np34ezmcjn')
mayaNames.forEach((mayaName) => {
console.log({
name: mayaName.name,
owner: mayaName.owner,
expire: mayaName.expire,
entries: mayaName.entries.map((entry) => {
return {
address: entry.address,
chain: entry.chain,
}
}),
})
})
})
})
8 changes: 8 additions & 0 deletions packages/xchain-mayachain-query/__mocks__/midgard-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@ export default {
const resp = require(`./responses/midgard/actions.json`)
return [200, resp]
})
mock.onGet(/\/v2\/mayaname\/lookup\/eld/).reply(function () {
const resp = require(`./responses/midgard/mayaname.json`)
return [200, resp]
})
mock.onGet(/\/v2\/mayaname\/rlookup\/maya13x0f2r0jltfplmxe40cc67hhca27np34ezmcjn/).reply(function () {
const resp = require(`./responses/midgard/owner.json`)
return [200, resp]
})
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"entries": [
{
"address": "bc1qvqxs558vgquwkchw64erd5hfpxfj5kmn7lyklz",
"chain": "BTC"
},
{
"address": "XgYRN3TLoYaodB5Y6AMZuYhjv8fKKG5tgh",
"chain": "DASH"
},
{
"address": "0x7316da75796f74e83b71c72ed6f138dd3b6b4957",
"chain": "ETH"
},
{
"address": "kujira1x8z69wuczjk42l22c3u6qzzd5vdeqdyhwkv84v",
"chain": "KUJI"
},
{
"address": "maya13x0f2r0jltfplmxe40cc67hhca27np34ezmcjn",
"chain": "MAYA"
},
{
"address": "thor13x0f2r0jltfplmxe40cc67hhca27np34e495yr",
"chain": "THOR"
}
],
"expire": "66754201",
"owner": "maya13x0f2r0jltfplmxe40cc67hhca27np34ezmcjn"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"eld"
]
42 changes: 42 additions & 0 deletions packages/xchain-mayachain-query/__tests__/mayachain-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,46 @@ describe('Mayachain-query tests', () => {
},
})
})

it('Should get MAYAName details', async () => {
const details = await mayachainQuery.getMAYANameDetails('eld')
expect(details).toBeTruthy()
expect(details?.name).toBe('eld')
expect(details?.expire).toBe('66754201')
expect(details?.owner).toBe('maya13x0f2r0jltfplmxe40cc67hhca27np34ezmcjn')
expect(details?.entries.length).toBe(6)
expect(details?.entries[0].address).toBe('bc1qvqxs558vgquwkchw64erd5hfpxfj5kmn7lyklz')
expect(details?.entries[0].chain).toBe('BTC')
expect(details?.entries[1].address).toBe('XgYRN3TLoYaodB5Y6AMZuYhjv8fKKG5tgh')
expect(details?.entries[1].chain).toBe('DASH')
expect(details?.entries[2].address).toBe('0x7316da75796f74e83b71c72ed6f138dd3b6b4957')
expect(details?.entries[2].chain).toBe('ETH')
expect(details?.entries[3].address).toBe('kujira1x8z69wuczjk42l22c3u6qzzd5vdeqdyhwkv84v')
expect(details?.entries[3].chain).toBe('KUJI')
expect(details?.entries[4].address).toBe('maya13x0f2r0jltfplmxe40cc67hhca27np34ezmcjn')
expect(details?.entries[4].chain).toBe('MAYA')
expect(details?.entries[5].address).toBe('thor13x0f2r0jltfplmxe40cc67hhca27np34e495yr')
expect(details?.entries[5].chain).toBe('THOR')
})

it('Should get the MAYAnames owned by an address', async () => {
const mayaNames = await mayachainQuery.getMAYANamesByOwner('maya13x0f2r0jltfplmxe40cc67hhca27np34ezmcjn')
expect(mayaNames.length).toBe(1)
expect(mayaNames[0]?.name).toBe('eld')
expect(mayaNames[0]?.expire).toBe('66754201')
expect(mayaNames[0]?.owner).toBe('maya13x0f2r0jltfplmxe40cc67hhca27np34ezmcjn')
expect(mayaNames[0]?.entries.length).toBe(6)
expect(mayaNames[0]?.entries[0].address).toBe('bc1qvqxs558vgquwkchw64erd5hfpxfj5kmn7lyklz')
expect(mayaNames[0]?.entries[0].chain).toBe('BTC')
expect(mayaNames[0]?.entries[1].address).toBe('XgYRN3TLoYaodB5Y6AMZuYhjv8fKKG5tgh')
expect(mayaNames[0]?.entries[1].chain).toBe('DASH')
expect(mayaNames[0]?.entries[2].address).toBe('0x7316da75796f74e83b71c72ed6f138dd3b6b4957')
expect(mayaNames[0]?.entries[2].chain).toBe('ETH')
expect(mayaNames[0]?.entries[3].address).toBe('kujira1x8z69wuczjk42l22c3u6qzzd5vdeqdyhwkv84v')
expect(mayaNames[0]?.entries[3].chain).toBe('KUJI')
expect(mayaNames[0]?.entries[4].address).toBe('maya13x0f2r0jltfplmxe40cc67hhca27np34ezmcjn')
expect(mayaNames[0]?.entries[4].chain).toBe('MAYA')
expect(mayaNames[0]?.entries[5].address).toBe('thor13x0f2r0jltfplmxe40cc67hhca27np34e495yr')
expect(mayaNames[0]?.entries[5].chain).toBe('THOR')
})
})
25 changes: 22 additions & 3 deletions packages/xchain-mayachain-query/src/mayachain-query.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Network } from '@xchainjs/xchain-client'
import { PoolDetail, Transaction } from '@xchainjs/xchain-mayamidgard'
import { MAYANameDetails } from '@xchainjs/xchain-mayamidgard-query'
import { QuoteSwapResponse } from '@xchainjs/xchain-mayanode'
import {
Address,
Asset,
CryptoAmount,
assetFromStringEx,
Expand All @@ -13,7 +13,7 @@ import {
} from '@xchainjs/xchain-util'

import { MayachainCache } from './mayachain-cache'
import { InboundDetail, QuoteSwap, QuoteSwapParams, SwapHistoryParams, SwapsHistory } from './types'
import { InboundDetail, MAYANameDetails, QuoteSwap, QuoteSwapParams, SwapHistoryParams, SwapsHistory } from './types'
import {
ArbAsset,
ArbChain,
Expand Down Expand Up @@ -193,7 +193,10 @@ export class MayachainQuery {
* @returns {MAYANameDetails | undefined} MAYANames details or undefined it is does not exist
*/
public async getMAYANameDetails(MAYAName: string): Promise<MAYANameDetails | undefined> {
return this.mayachainCache.midgardQuery.getMAYANameDetails(MAYAName)
const details = await this.mayachainCache.midgardQuery.getMAYANameDetails(MAYAName)
if (!details) return undefined

return { ...details, name: MAYAName }
}

/**
Expand Down Expand Up @@ -285,4 +288,20 @@ export class MayachainQuery {
}),
}
}

/**
* Get the MAYANames owned by an address
* @param {Address} owner - Thorchain address
* @returns {MAYANameDetails[]} List of MAYANames owned by the address
*/
public async getMAYANamesByOwner(owner: Address): Promise<MAYANameDetails[]> {
const mayaNames = await this.mayachainCache.midgardQuery.getMAYANameReverseLookup(owner)

if (!mayaNames || mayaNames.length === 0) return []

const tasks = mayaNames.map((mayaName) => this.getMAYANameDetails(mayaName))

const mayaNamesDetails = await Promise.all(tasks)
return mayaNamesDetails.filter((mayaNameDetails) => mayaNameDetails !== undefined) as MAYANameDetails[]
}
}
3 changes: 3 additions & 0 deletions packages/xchain-mayachain-query/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { TxHash } from '@xchainjs/xchain-client'
import { MAYANameDetails as BaseMAYANameDetails } from '@xchainjs/xchain-mayamidgard-query'
import { Address, Asset, Chain, CryptoAmount } from '@xchainjs/xchain-util'
import { BigNumber } from 'bignumber.js'

export type MAYANameDetails = BaseMAYANameDetails & { name: string }

/**
* Represents fees associated with a swap.
*/
Expand Down

0 comments on commit cd327ab

Please sign in to comment.