-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check if an address is a manager using thegraph (#324)
- Loading branch information
1 parent
6310f25
commit 5f61902
Showing
3 changed files
with
100 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', '') | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} |