Skip to content

Commit

Permalink
chainId and address added to network.xyo.crypto.contract.erc721.info
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Nov 1, 2023
1 parent dfa846e commit ab8b1fe
Showing 1 changed file with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { assertEx } from '@xylabs/assert'
import { exists } from '@xylabs/exists'
import { AbstractDiviner } from '@xyo-network/abstract-diviner'
import {
CryptoContractFunctionCall,
Expand All @@ -20,8 +22,11 @@ export type Erc721ContractInfoSchema = typeof Erc721ContractInfoSchema

export type Erc721ContractInfo = Payload<
{
name: string
symbol: string
address: string
chainId: string
name?: string
symbol?: string
totalSupply?: string
},
Erc721ContractInfoSchema
>
Expand All @@ -36,10 +41,25 @@ const generateCallHash = async (address: string, functionName: string, params: u
return await PayloadHasher.hashAsync(callPayload)
}

const findCallResult = async (address: string, functionName: string, params: unknown[], payloads: CryptoContractFunctionCallResult[]) => {
const matchingExistingField = <R = string, T extends Payload = Payload>(objs: T[], field: keyof T) => {
const expectedValue = objs.at(0)?.[field] as R
const didNotMatch = objs.reduce((prev, obj) => {
return prev || obj[field] !== expectedValue
}, false)
return didNotMatch ? undefined : expectedValue
}

type FindCallResult<TResult = string, TPayload = Payload> = [TResult, TPayload] | [undefined, TPayload] | [undefined, undefined]

const findCallResult = async <TResult = string>(
address: string,
functionName: string,
params: unknown[],
payloads: CryptoContractFunctionCallResult[],
): Promise<FindCallResult<TResult, CryptoContractFunctionCallResult>> => {
const callHash = await generateCallHash(address, functionName, params)
const foundPayload = payloads.find((payload) => payload.call === callHash)
return foundPayload?.result.value as string
return foundPayload ? [foundPayload?.result.value as TResult, foundPayload] : [undefined, undefined]
}

export class CryptoContractErc721Diviner<
Expand All @@ -58,10 +78,16 @@ export class CryptoContractErc721Diviner<
)
const result = await Promise.all(
addresses.map(async (address) => {
const [name, namePayload] = await findCallResult(address, 'name', [], callResults)
const [symbol, symbolPayload] = await findCallResult(address, 'symbol', [], callResults)
const callResultPayloads = [namePayload, symbolPayload].filter(exists)

const erc721Info: Erc721ContractInfo = {
name: await findCallResult(address, 'name', [], callResults),
address: assertEx(matchingExistingField(callResultPayloads, 'address'), 'Mismatched address'),
chainId: assertEx(matchingExistingField(callResultPayloads, 'chainId'), 'Mismatched chainId'),
name,
schema: Erc721ContractInfoSchema,
symbol: await findCallResult(address, 'symbol', [], callResults),
symbol,
}
return erc721Info
}),
Expand Down

0 comments on commit ab8b1fe

Please sign in to comment.