Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add function to fetch wallet world stats to the worlds api #2925

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/lib/api/ens.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ENSApi } from './ens'

describe('when fetching the ens list for an owner', () => {
describe('when fetching the external names for an owner', () => {
let owner: string

beforeEach(() => {
Expand All @@ -22,7 +22,7 @@ describe('when fetching the ens list for an owner', () => {
})

it('should throw an error containing the error of the fetch request', async () => {
await expect(ensApi.fetchENSList(owner)).rejects.toThrowError('Failed to fetch ENS list - Some Error')
await expect(ensApi.fetchExternalNames(owner)).rejects.toThrowError('Failed to fetch ENS list - Some Error')
})
})

Expand All @@ -32,7 +32,7 @@ describe('when fetching the ens list for an owner', () => {
})

it('should throw an error containing the status code of the failed response', async () => {
await expect(ensApi.fetchENSList(owner)).rejects.toThrowError('Failed to fetch ENS list - 500')
await expect(ensApi.fetchExternalNames(owner)).rejects.toThrowError('Failed to fetch ENS list - 500')
})
})

Expand All @@ -44,7 +44,7 @@ describe('when fetching the ens list for an owner', () => {
})

it('should throw an error containing the stringified errors property value', async () => {
await expect(ensApi.fetchENSList(owner)).rejects.toThrowError('Failed to fetch ENS list - [{"message":"Some Error"}]')
await expect(ensApi.fetchExternalNames(owner)).rejects.toThrowError('Failed to fetch ENS list - [{"message":"Some Error"}]')
})
})

Expand All @@ -57,12 +57,12 @@ describe('when fetching the ens list for an owner', () => {
.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve({ data: { domains: [{ name: 'some-name.eth' }] } }) } as any)
})

it('should return an array of ens names', async () => {
await expect(ensApi.fetchENSList(owner)).resolves.toEqual(['some-name.eth'])
it('should return an array of external names', async () => {
await expect(ensApi.fetchExternalNames(owner)).resolves.toEqual(['some-name.eth'])
})

it('should add the provided owner to the fetch request', async () => {
await ensApi.fetchENSList(owner)
await ensApi.fetchExternalNames(owner)

expect(mockFetch).toHaveBeenCalledWith(subgraphUrl, expect.objectContaining({ body: expect.stringContaining(owner) }))
})
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ENSApi {
/**
* Will fetch all ens domains owned by the given address.
*/
fetchENSList = async (owner: string): Promise<string[]> => {
fetchExternalNames = async (owner: string): Promise<string[]> => {
const lowercasedOwner = owner.toLowerCase()

let response: Response
Expand Down
47 changes: 47 additions & 0 deletions src/lib/api/worlds.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { WalletStats, content } from './worlds'

beforeEach(() => {
global.fetch = jest.fn()
})

describe('when fetching the world stats for a wallet', () => {
let wallet: string

beforeEach(() => {
wallet = '0x123'
})

describe('when the fetch result is not ok', () => {
beforeEach(() => {
global.fetch = () => Promise.resolve({ ok: false } as Response)
})

it('should return null', async () => {
expect(await content.fetchWalletStats(wallet)).toBeNull()
})
})

describe('when the fetch result is ok', () => {
let walletStats: WalletStats

beforeEach(() => {
walletStats = {
wallet,
dclNames: [],
ensNames: [],
maxAllowedSpace: '',
usedSpace: ''
}

global.fetch = () =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(walletStats)
} as Response)
})

it('should return null', async () => {
expect(await content.fetchWalletStats(wallet)).toBe(walletStats)
})
})
})
32 changes: 28 additions & 4 deletions src/lib/api/worlds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,36 @@ export type WorldInfo = {
}
}

export type WalletStats = {
wallet: string
dclNames: {
name: string
size: string
}[]
ensNames: {
name: string
size: string
}[]
usedSpace: string
maxAllowedSpace: string
}

export class WorldsAPI extends BaseAPI {
public async fetchWorld(name: string) {
const req = await fetch(`${this.url}/world/${name}/about`)
if (req.ok) {
const res = await req.json()
return res as WorldInfo
const result = await fetch(`${this.url}/world/${name}/about`)
if (result.ok) {
const json = await result.json()
return json as WorldInfo
} else {
return null
}
}

public async fetchWalletStats(address: string) {
const result = await fetch(`${this.url}/wallet/${address}/stats`)
if (result.ok) {
const json = await result.json()
return json as WalletStats
} else {
return null
}
Expand Down
6 changes: 3 additions & 3 deletions src/modules/ens/sagas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe('when handling the fetching of external ens names for an owner', () =>
await expectSaga(ensSaga, builderClient, ensApi)
.provide([
[select(getAddress), storedAddress],
[call([ensApi, ensApi.fetchENSList], storedAddress!), []]
[call([ensApi, ensApi.fetchExternalNames], storedAddress!), []]
])
.put(fetchExternalNamesSuccess(storedAddress!, []))
.dispatch(fetchExternalNamesRequest())
Expand Down Expand Up @@ -226,7 +226,7 @@ describe('when handling the fetching of external ens names for an owner', () =>

it('should dispatch an error action with the owner and the error', async () => {
await expectSaga(ensSaga, builderClient, ensApi)
.provide([[call([ensApi, ensApi.fetchENSList], owner), throwError(error)]])
.provide([[call([ensApi, ensApi.fetchExternalNames], owner), throwError(error)]])
.put(fetchExternalNamesFailure(ensError, owner))
.dispatch(fetchExternalNamesRequest(owner))
.silentRun()
Expand All @@ -242,7 +242,7 @@ describe('when handling the fetching of external ens names for an owner', () =>

it('should dispatch a success action with the owner and the names', async () => {
await expectSaga(ensSaga, builderClient, ensApi)
.provide([[call([ensApi, ensApi.fetchENSList], owner), names]])
.provide([[call([ensApi, ensApi.fetchExternalNames], owner), names]])
.put(fetchExternalNamesSuccess(owner, names))
.dispatch(fetchExternalNamesRequest(owner))
.silentRun()
Expand Down
2 changes: 1 addition & 1 deletion src/modules/ens/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ export function* ensSaga(builderClient: BuilderClient, ensApi: ENSApi) {
throw new Error('No owner address provided')
}

const names: string[] = yield call([ensApi, ensApi.fetchENSList], owner)
const names: string[] = yield call([ensApi, ensApi.fetchExternalNames], owner)
yield put(fetchExternalNamesSuccess(owner, names))
} catch (error) {
const ensError: ENSError = { message: error.message }
Expand Down
Loading