Skip to content

Commit

Permalink
feat: check if an address is a manager using thegraph (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo committed Oct 22, 2021
1 parent 6310f25 commit 5f61902
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 9 deletions.
23 changes: 22 additions & 1 deletion src/ethereum/api/thirdParty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ import {
} from './BaseGraphAPI'

const getThirdPartiesQuery = (manager: string = '') => gql`
query getThirdParties(${PAGINATION_VARIABLES}, ) {
query getThirdParties(${PAGINATION_VARIABLES}) {
thirdParties(${PAGINATION_ARGUMENTS}, where: { managers_contains: ["${manager}"], isApproved: true }) {
...thirdPartyFragment
}
}
${thirdPartyFragment()}
`

const getThirdPartyQuery = (urn: string, manager: string) => gql`
query getThirdPartyQuery {
thirdParties(first: 1, where: { id: "${urn}", managers_contains: ["${manager}"], isApproved: true }) {
...thirdPartyFragment
}
}
${thirdPartyFragment()}
`

export const THIRD_PARTY_URL = env.get('THIRD_PARTY_GRAPH_URL', '')

export class ThirdPartyAPI extends BaseGraphAPI {
Expand All @@ -26,6 +35,18 @@ export class ThirdPartyAPI extends BaseGraphAPI {
query: getThirdPartiesQuery(manager),
})
}

isManager = async (urn: string, manager: string): Promise<boolean> => {
const {
data: { thirdParties = [] },
} = await this.query<{
thirdParties: ThirdPartyFragment[]
}>({
query: getThirdPartyQuery(urn, manager),
variables: { urn, manager: manager.toLowerCase() },
})
return thirdParties.length > 0
}
}

export const thirdPartyAPI = new ThirdPartyAPI(THIRD_PARTY_URL)
58 changes: 58 additions & 0 deletions src/ethereum/api/tpw.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { env } from 'decentraland-commons'
import { thirdPartyAPI } from './thirdParty'
import { isManager } from './tpw'

jest.mock('decentraland-commons')
jest.mock('./thirdParty')

describe('isManager', () => {
describe('when an address belongs to the TPW_MANAGER_ADDRESSES env var', () => {
let manager: string
beforeEach(() => {
manager = '0x123123'
;(env.get as jest.Mock).mockReturnValueOnce(
`0x555,${manager},0x444,0x333`
)
})

it('should return true', async () => {
expect(await isManager('', manager)).toBe(true)
expect(env.get).toHaveBeenCalledWith('TPW_MANAGER_ADDRESSES', '')
})
})

describe('when an address does not belong to the TPW_MANAGER_ADDRESSES env var', () => {
let urn: string
let manager: string

beforeEach(() => {
urn = 'some:valid:urn'
manager = '0x123123'
;(env.get as jest.Mock).mockReturnValueOnce(`0x555,0x444,0x333`)
})

describe('when thegraph has a urn with the address as manager', () => {
beforeEach(() => {
;(thirdPartyAPI.isManager as jest.Mock).mockReturnValueOnce(true)
})

it('should return true', async () => {
expect(await isManager(urn, manager)).toBe(true)
expect(thirdPartyAPI.isManager).toHaveBeenCalledWith(urn, manager)
expect(env.get).toHaveBeenCalledWith('TPW_MANAGER_ADDRESSES', '')
})
})

describe('when thegraph does not has a urn with the address as manager', () => {
beforeEach(() => {
;(thirdPartyAPI.isManager as jest.Mock).mockReturnValueOnce(false)
})

it('should return true', async () => {
expect(await isManager(urn, manager)).toBe(false)
expect(thirdPartyAPI.isManager).toHaveBeenCalledWith(urn, manager)
expect(env.get).toHaveBeenCalledWith('TPW_MANAGER_ADDRESSES', '')
})
})
})
})
28 changes: 20 additions & 8 deletions src/ethereum/api/tpw.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { env } from 'decentraland-commons'
const MANAGERS = env
.get('TPW_MANAGER_ADDRESSES', '')
.split(/[ ,]+/)
.map((address) => address.toLowerCase())
import { thirdPartyAPI } from './thirdParty'

/**
* Checks if an address manages a third party wearable collection.
*
* @param urn - The URN of the TWP collection where to get the information about the collection.
* @param address - The address to check if it manages the collection.
*/
export function isManager(_: string, address: string): Promise<boolean> {
if (MANAGERS.includes(address.toLowerCase())) {
return Promise.resolve(true)
export async function isManager(
urn: string,
address: string
): Promise<boolean> {
if (isEnvManager(address)) {
return true
}
return Promise.resolve(false)

return await thirdPartyAPI.isManager(urn, address)
}

function isEnvManager(address: string) {
return (
!env.isProduction() &&
env
.get('TPW_MANAGER_ADDRESSES', '')
.toLowerCase()
.search(address.toLowerCase()) > -1
)
}

0 comments on commit 5f61902

Please sign in to comment.