Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
feat: adding types and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
julianmrodri committed Sep 14, 2020
1 parent ed21b98 commit 45d33fe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/blockchain/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import eventsEmitterFactory, { EventsEmitterOptions } from './events'
import { NewBlockEmitterOptions } from '../definitions'
import { BlockTracker } from './block-tracker'

export type RLPDecoded = Array<Array<number[]>>

function getBlockTracker (keyPrefix?: string): BlockTracker {
const store = getObject(keyPrefix)
return new BlockTracker(store)
Expand Down
63 changes: 37 additions & 26 deletions src/services/rns/rns.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Eth } from 'web3-eth'
import { EventData } from 'web3-eth-contract'
import Utils from 'web3-utils'
import { RnsBaseService, RnsServices } from '.'
import { getBlockDate } from '../../blockchain/utils'
import { getBlockDate, RLPDecoded } from '../../blockchain/utils'
import { Logger } from '../../definitions'
import DomainOffer from './models/domain-offer.model'
import Domain from './models/domain.model'
Expand Down Expand Up @@ -65,6 +65,32 @@ async function registerTransfer (
return transferDomain.id
}

// getDomainName: Decode domain name
function getDomainName (decodedData: DecodedData, tokenId: string, batchprocess: string): string | undefined {
if (decodedData) {
let name: string | undefined

if (decodedData.params[0].value === batchprocess.toLowerCase()) {
// Batch registration
const rlpDecoded: RLPDecoded = RLP.decode(decodedData.params[2].value) as unknown as RLPDecoded
const domainNames = rlpDecoded[1].map(
(domainData: number[]) =>
Utils.hexToAscii(
Utils.bytesToHex(
domainData.slice(88, domainData.length)
)
)
)
name = domainNames.find((domain: string) => Utils.numberToHex(Utils.sha3(domain) as string) === tokenId)
} else {
// Single registration
name = Utils.hexToAscii('0x' + decodedData.params[2].value.slice(218, decodedData.params[2].value.length))
}

return name
}
}

async function transferHandler (logger: Logger, eventData: EventData, eth: Eth, services: RnsServices): Promise<void> {
const tokenId = Utils.numberToHex(eventData.returnValues.tokenId)
const ownerAddress = eventData.returnValues.to.toLowerCase()
Expand All @@ -84,33 +110,18 @@ async function transferHandler (logger: Logger, eventData: EventData, eth: Eth,
const transaction = await eth.getTransaction(transactionHash)
const decodedData: DecodedData = abiDecoder.decodeMethod(transaction.input)

if (decodedData) {
// Decode domain name
let name: string | undefined

if (decodedData.params[0].value === batchprocess.toLowerCase()) {
// Batch registration
const rlpDecoded: any = RLP.decode(decodedData.params[2].value)
const domainNames = rlpDecoded[1].map(
(domainData: number[]) => Utils.hexToAscii((Utils.bytesToHex(domainData.slice(88, domainData.length))))
)
name = domainNames.find((domain: string) => Utils.numberToHex(Utils.sha3(domain) as string) === tokenId)
} else {
// Single registration
name = Utils.hexToAscii('0x' + decodedData.params[2].value.slice(218, decodedData.params[2].value.length))
const name: string | undefined = getDomainName(decodedData, tokenId, batchprocess)

if (name) {
try {
await Domain.upsert({ tokenId, name: `${name}.${tld}` })
} catch (e) {
await Domain.upsert({ tokenId })
logger.warn(`Domain name ${name}.${tld} for token ${tokenId} could not be stored.`)
}

if (name) {
try {
await Domain.upsert({ tokenId, name: `${name}.${tld}` })
} catch (e) {
await Domain.upsert({ tokenId })
logger.warn(`Domain name ${name}.${tld} for token ${tokenId} could not be stored.`)
}

if (domainsService.emit) {
domainsService.emit('patched', { tokenId })
}
if (domainsService.emit) {
domainsService.emit('patched', { tokenId })
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/services/rns/processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ describe('Domain events', () => {
})

it('should Decode Name for new Domain - Batch', async () => {
// Encoded multiple registrations for `domain.rsk` and `domainother.rsk`ß
// Encoded multiple registrations for `domain.rsk` and `domainother.rsk`
const txInput = '0x4000aea0000000000000000000000000c0b3b62dd0400e4baa721ddec9b8a384147b23ff' +
'0000000000000000000000000000000000000000000000003782dace9d90000000000000000000000000000000' +
'000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000' +
Expand Down

0 comments on commit 45d33fe

Please sign in to comment.