Tracking transfer event & updating nft owner (#436)

* Adding Tranfer eventHandler to subgraph.template.yaml

* Started adding transfer event to mapping

* updating getNftTokenWithID function

* Pulling from Origin v4main

* Removing console.log

* Updating token Utils

* Updating newOwner with User & writting new test

* Updating test

* Ensuring account is in lowercase

* Creating seperate test for transfering an NFT query

* Updating test name

* Removing console.logs

* Moviving transfer event tracking from factory to template

* Moving handleNftTransferred to nftUpdate mapping

* Adding logging

* Using correct token address

* Fix linting

* Checking the correct owner account has been set originally
This commit is contained in:
Jamie Hewitt 2022-06-24 10:06:44 +03:00 committed by GitHub
parent 28979c8c79
commit 603a25f26a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 120 additions and 4 deletions

View File

@ -1,6 +1,6 @@
/* eslint-disable no-unused-vars */ /* eslint-disable no-unused-vars */
var fs = require('fs') const fs = require('fs')
var addresses = require('@oceanprotocol/contracts/addresses/address.json') let addresses = require('@oceanprotocol/contracts/addresses/address.json')
async function replaceContractAddresses() { async function replaceContractAddresses() {
// load addresses file first // load addresses file first

View File

@ -12,10 +12,12 @@ import {
RemovedFromCreateERC20List, RemovedFromCreateERC20List,
RemovedFromMetadataList, RemovedFromMetadataList,
RemovedManager, RemovedManager,
CleanedPermissions CleanedPermissions,
Transfer
} from '../@types/templates/ERC721Template/ERC721Template' } from '../@types/templates/ERC721Template/ERC721Template'
import { NftUpdateType } from './utils/constants' import { NftUpdateType } from './utils/constants'
import { getNftToken } from './utils/tokenUtils' import { getNftToken, getNftTokenWithID } from './utils/tokenUtils'
import { getUser } from './utils/userUtils'
function getId(tx: string, nftAddress: string): string { function getId(tx: string, nftAddress: string): string {
return `${tx}-${nftAddress}` return `${tx}-${nftAddress}`
@ -247,3 +249,12 @@ export function handleCleanedPermissions(event: CleanedPermissions): void {
nft.managerRole = newList nft.managerRole = newList
nft.save() nft.save()
} }
export function handleNftTransferred(event: Transfer): void {
const id = event.address.toHex()
const nft = getNftTokenWithID(id)
const newOwner = getUser(event.params.to.toHexString())
nft.owner = newOwner.id
nft.save()
}

View File

@ -60,6 +60,20 @@ export function getNftToken(address: Address): Nft {
return newToken return newToken
} }
export function getNftTokenWithID(tokenId: string): Nft {
let nftToken = Nft.load(tokenId)
if (nftToken === null) {
nftToken = new Nft(tokenId)
// const contract = ERC721Template.bind(address)
nftToken.name = ''
nftToken.symbol = ''
nftToken.address = tokenId
nftToken.save()
addNft()
}
return nftToken
}
export function getUSDValue( export function getUSDValue(
address: string, address: string,
value: BigDecimal, value: BigDecimal,

View File

@ -197,6 +197,8 @@ templates:
handler: handleRemovedManager handler: handleRemovedManager
- event: CleanedPermissions(indexed address,uint256,uint256) - event: CleanedPermissions(indexed address,uint256,uint256)
handler: handleCleanedPermissions handler: handleCleanedPermissions
- event: Transfer(indexed address,indexed address,indexed uint256)
handler: handleNftTransferred
- name: Dispenser - name: Dispenser
kind: ethereum/contract kind: ethereum/contract

View File

@ -192,4 +192,93 @@ describe('Simple Publish & consume test', async () => {
} }
*/ */
}) })
it('should publish and transfer an NFT', async () => {
const nft = new Nft(web3)
const Factory = new NftFactory(addresses.ERC721Factory, web3)
const accounts = await web3.eth.getAccounts()
const publisherAccount = accounts[0]
const newOwnerAccount = accounts[1].toLowerCase()
const nftParams: NftCreateData = {
name: 'testNFT',
symbol: 'TST',
templateIndex: 1,
tokenURI: '',
transferable: true,
owner: publisherAccount
}
const erc20Params: Erc20CreateParams = {
templateIndex: 1,
cap: '100000',
feeAmount: '0',
paymentCollector: '0x0000000000000000000000000000000000000000',
feeToken: '0x0000000000000000000000000000000000000000',
minter: publisherAccount,
mpFeeAddress: '0x0000000000000000000000000000000000000000'
}
const result = await Factory.createNftWithErc20(
publisherAccount,
nftParams,
erc20Params
)
await sleep(2000)
const erc721Address = result.events.NFTCreated.returnValues[0]
const datatokenAddress = result.events.TokenCreated.returnValues[0]
const graphNftToken = erc721Address.toLowerCase()
const queryOriginalOwner = {
query: `query {
nft(id:"${graphNftToken}"){symbol,id,owner}}`
}
const initialResponse = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(queryOriginalOwner)
})
const initialResult = await initialResponse.json()
// Checking original owner account has been set correctly
assert(
initialResult.data.nft.owner.toLowerCase() ===
publisherAccount.toLowerCase()
)
// create the files encrypted string
let providerResponse = await ProviderInstance.encrypt(assetUrl, providerUrl)
ddo.services[0].files = await providerResponse
ddo.services[0].datatokenAddress = datatokenAddress
// update ddo and set the right did
ddo.nftAddress = erc721Address
const chain = await web3.eth.getChainId()
ddo.id =
'did:op:' +
SHA256(web3.utils.toChecksumAddress(erc721Address) + chain.toString(10))
providerResponse = await ProviderInstance.encrypt(ddo, providerUrl)
const encryptedResponse = await providerResponse
const metadataHash = getHash(JSON.stringify(ddo))
await nft.setMetadata(
erc721Address,
publisherAccount,
0,
providerUrl,
'',
'0x2',
encryptedResponse,
'0x' + metadataHash
)
await sleep(2000)
// Transfer the NFT
await nft.transferNft(graphNftToken, publisherAccount, newOwnerAccount)
await sleep(2000)
const query2 = {
query: `query {
nft(id:"${graphNftToken}"){symbol,id,owner, transferable}}`
}
const response = await fetch(subgraphUrl, {
method: 'POST',
body: JSON.stringify(query2)
})
const queryResult = await response.json()
assert(queryResult.data.nft.owner === newOwnerAccount)
})
}) })