Skip to content

Commit

Permalink
Merge 4822aad into 8a51543
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo committed Nov 30, 2021
2 parents 8a51543 + 4822aad commit 96160ad
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
1 change: 0 additions & 1 deletion src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export async function initComponents(): Promise<AppComponents> {
})
const contracts = createContractsComponent({
config,
logs,
fetcher,
collectionsSubgraph,
})
Expand Down
36 changes: 19 additions & 17 deletions src/ports/contracts/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ let whitelistedAddresses: string[] = []
let lastFetch: number = Date.now()

export function createContractsComponent(
components: Pick<
AppComponents,
'config' | 'fetcher' | 'collectionsSubgraph' | 'logs'
>
components: Pick<AppComponents, 'config' | 'fetcher' | 'collectionsSubgraph'>
): IContractsComponent {
const {
config,
collectionsSubgraph,
fetcher: { fetch },
logs,
} = components

const logger = logs.getLogger('contract-component')

// Methods
async function isValidContractAddress(address: string): Promise<boolean> {
const validations = await Promise.all([
Expand All @@ -47,11 +41,7 @@ export function createContractsComponent(
'CONTRACT_ADDRESSES_URL'
)
const chainId = await config.requireNumber('COLLECTIONS_CHAIN_ID')
const chainName = await getCollectionChainName(chainId)

logger.log(
`Fetching whitelisted contract addresses from ${contractAddressesURL} for chain ${chainName}`
)
const chainName = await getRemoteContractURLChainName(chainId)

const remoteResult = await fetch(contractAddressesURL, {
headers: { 'content-type': 'application/json' },
Expand All @@ -66,9 +56,9 @@ export function createContractsComponent(

const contractAddresses: ContractsResponse = await remoteResult.json()

whitelistedAddresses = Object.values(contractAddresses[chainName]).map(
(contractAddress) => contractAddress.toLowerCase()
)
whitelistedAddresses = Object.values(
contractAddresses[chainName.toLowerCase()]
).map((contractAddress) => contractAddress.toLowerCase())
lastFetch = Date.now()
}

Expand Down Expand Up @@ -117,13 +107,25 @@ export function createContractsComponent(
return component
}

async function getRemoteContractURLChainName(
chainId: ChainId
): Promise<string> {
let chainName = await getCollectionChainName(chainId)

// The collections json we're dealing with, has `matic` as a name for matic mainnet instead of `polygon` and names as lowercase,
// so we need to account for those here
if (chainName === ChainName.MATIC_MAINNET) {
chainName = 'matic' as ChainName
}
return chainName.toLowerCase()
}

async function getCollectionChainName(chainId: ChainId): Promise<ChainName> {
if (!ChainId.validate(chainId)) {
throw new Error(`Invalid chainId ${chainId}`)
}

const chainName = getChainName(chainId)!
return chainName.toLowerCase() as ChainName
return getChainName(chainId)!
}

function isOlderThan(timestamp: number, intervalInMs: number): boolean {
Expand Down
4 changes: 1 addition & 3 deletions src/ports/contracts/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ChainName } from '@dcl/schemas'

export interface IContractsComponent {
isValidContractAddress(address: string): Promise<boolean>
isCollectionAddress(address: string): Promise<boolean>
Expand All @@ -8,5 +6,5 @@ export interface IContractsComponent {
clearCache(): void
}

export type ContractsResponse = Record<ChainName, string>
export type ContractsResponse = Record<string, string> // Record<ChainName, string>
export type RemoteCollection = { id: string }
1 change: 0 additions & 1 deletion test/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export async function initComponents(): Promise<TestComponents> {
})
const contracts = createContractsComponent({
config,
logs,
fetcher,
collectionsSubgraph,
})
Expand Down
6 changes: 1 addition & 5 deletions test/tests/components/contracts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ test('contracts component', function ({ components, stubComponents }) {
address = '0xabc123123'
contracts = createContractsComponent({
config,
logs,
fetcher,
collectionsSubgraph,
})
Expand Down Expand Up @@ -100,7 +99,6 @@ test('contracts component', function ({ components, stubComponents }) {
address = '0xabc123123'
contracts = createContractsComponent({
config,
logs,
fetcher,
collectionsSubgraph,
})
Expand Down Expand Up @@ -155,7 +153,6 @@ test('contracts component', function ({ components, stubComponents }) {

contracts = createContractsComponent({
config,
logs,
fetcher,
collectionsSubgraph,
})
Expand Down Expand Up @@ -185,7 +182,7 @@ test('contracts component', function ({ components, stubComponents }) {

const url = 'https://contracts.decentraland.org/addresses.json'
const body = JSON.stringify({
[ChainName.MATIC_MAINNET.toLowerCase()]: {
matic: {
SomeContract: address,
},
})
Expand All @@ -199,7 +196,6 @@ test('contracts component', function ({ components, stubComponents }) {

contracts = createContractsComponent({
config,
logs,
fetcher,
collectionsSubgraph,
})
Expand Down

0 comments on commit 96160ad

Please sign in to comment.