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

Commit

Permalink
feat: rns precache
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandro Yalet committed May 18, 2020
1 parent 03162b9 commit a5aaefe
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@rsksmart/rif-marketplace-storage": "^0.1.0-dev.0",
"@rsksmart/rns-reverse": "^1.0.2",
"@rsksmart/rns-rskregistrar": "^1.2.1",
"abi-decoder": "^2.3.0",
"async-sema": "^3.1.0",
"coingecko-api": "^1.0.10",
"colors": "^1.4.0",
Expand Down
2 changes: 2 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export interface Config {
// Sets if RNS service should be enabled
enabled?: boolean

fifsAddrRegistrar?: BlockchainServiceOptions

// Owner contract's options
owner?: BlockchainServiceOptions

Expand Down
4 changes: 4 additions & 0 deletions src/rns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import rnsReverseContractAbi from '@rsksmart/rns-reverse/NameResolverData.json'
import simplePlacementsContractAbi from '@rsksmart/rif-marketplace-nfts/ERC721SimplePlacementsABI.json'
import { errorHandler } from '../utils'

import rnsPrecache from './rns.precache'

const logger = loggingFactory('rns')

export class RnsService extends Service {
Expand All @@ -49,6 +51,8 @@ async function precache (eth?: Eth): Promise<void> {
const eventsDataQueue: EventData[] = []
const dataQueuePusher = (event: EventData): void => { eventsDataQueue.push(event) }

await rnsPrecache(eth, rnsContractAbi.abi as AbiItem[], config.get<string>(`rns.owner.contractAddress`), config.get<string>(`rns.fifsAddrRegistrar.contractAddress`))

await fetchEventsForService(eth, 'rns.owner', rnsContractAbi.abi as AbiItem[], dataQueuePusher)
await fetchEventsForService(eth, 'rns.reverse', rnsReverseContractAbi.abi as AbiItem[], dataQueuePusher)
await fetchEventsForService(eth, 'rns.placement', simplePlacementsContractAbi as AbiItem[], dataQueuePusher)
Expand Down
51 changes: 51 additions & 0 deletions src/rns/rns.precache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Utils from 'web3-utils'
import abiDecoder from 'abi-decoder'

import Domain from './models/domain.model'

abiDecoder.addABI([
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
},
{
"name": "_data",
"type": "bytes"
}
],
"name": "transferAndCall",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]);

export default async function processRskOwner(eth: Eth, contractAbi: Utils.AbiItem[], rskOwnerAddresses: string, fifsAddrAddress: string) {
const rskOwner = new eth.Contract(contractAbi, rskOwnerAddresses)
const rskOwnerEvents = await rskOwner.getPastEvents('Transfer', {
filter: { from: fifsAddrAddress },
fromBlock: 0
});
for (const rskOwnerEvent of rskOwnerEvents) {
const transaction = await eth.getTransaction(rskOwnerEvent.transactionHash)
const decodedData = abiDecoder.decodeMethod(transaction.input)
const name = Utils.hexToAscii("0x" + decodedData.params[2].value.slice(218, decodedData.params[2].value.length))
const tokenId = Utils.sha3(name)
const ownerAddress = rskOwnerEvent.returnValues.to.toLowerCase()
const req = new Domain({tokenId, name, ownerAddress})
await req.save()
};
}

0 comments on commit a5aaefe

Please sign in to comment.