1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Merge pull request #1558 from oceanprotocol/issue-1556-add-nft-setData()-function

Issue-#1556: Add nft.setData() function
This commit is contained in:
Miquel A. Cabot 2022-07-25 12:21:31 +02:00 committed by GitHub
commit b83872bc7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 11 deletions

View File

@ -1168,10 +1168,12 @@ export class Datatoken {
this.config
)
const estGas = await estimateGas(address, dtContract.methods.setData, value)
const valueHex = this.web3.utils.asciiToHex(value)
const estGas = await estimateGas(address, dtContract.methods.setData, valueHex)
// Call setData function of the contract
const trxReceipt = await dtContract.methods.setData(value).send({
const trxReceipt = await dtContract.methods.setData(valueHex).send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3, this.config)

View File

@ -1159,6 +1159,50 @@ export class Nft {
return trxReceipt
}
/** setData
* This function allows to store data with a preset key (keccak256(ERC20Address)) into NFT 725 Store
* only ERC20Deployer can succeed
* @param nftAddress erc721 contract adress
* @param address user adress
* @param key Key of the data to be stored into 725Y standard
* @param value Data to be stored into 725Y standard
* @return {Promise<TransactionReceipt>} transactionId
*/
public async setData(
nftAddress: string,
address: string,
key: string,
value: string
): Promise<TransactionReceipt> {
if ((await this.getNftPermissions(nftAddress, address)).store !== true) {
throw new Error(`User is not ERC20 store updater`)
}
const nftContract = setContractDefaults(
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
const keyHash = this.web3.utils.keccak256(key)
const valueHex = this.web3.utils.asciiToHex(value)
const estGas = await estimateGas(
address,
nftContract.methods.setNewData,
keyHash,
valueHex
)
// Call setData function of the contract
const trxReceipt = await nftContract.methods.setNewData(keyHash, valueHex).send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3, this.config)
})
return trxReceipt
}
/** Get Owner
* @param {String} nftAddress erc721 contract adress
* @return {Promise<string>} string
@ -1222,8 +1266,9 @@ export class Nft {
new this.web3.eth.Contract(this.nftAbi, nftAddress),
this.config
)
const data = await nftContract.methods.getData(key).call()
return data
const keyHash = this.web3.utils.keccak256(key)
const data = await nftContract.methods.getData(keyHash).call()
return data ? this.web3.utils.hexToAscii(data) : null
}
/** Gets data at a given `key`

View File

@ -549,19 +549,18 @@ describe('Datatoken', () => {
})
it('#setData - should set a value into 725Y standard, if Caller has ERC20Deployer permission', async () => {
const data = web3.utils.asciiToHex('SomeData')
const data = 'SomeData'
assert((await nftDatatoken.isErc20Deployer(nftAddress, nftOwner)) === true)
await datatoken.setData(datatokenAddress, nftOwner, data)
const key = web3.utils.keccak256(datatokenAddress)
assert((await nftDatatoken.getData(nftAddress, key)) === data)
assert((await nftDatatoken.getData(nftAddress, datatokenAddress)) === data)
})
it('#setData - should FAIL to set a value into 725Y standard, if Caller has NOT ERC20Deployer permission', async () => {
const data = web3.utils.asciiToHex('NewData')
const OldData = web3.utils.asciiToHex('SomeData')
const data = 'NewData'
const OldData = 'SomeData'
assert((await nftDatatoken.isErc20Deployer(nftAddress, user1)) === false)
try {
@ -570,8 +569,7 @@ describe('Datatoken', () => {
} catch (e) {
assert(e.message === 'User is not ERC20 Deployer')
}
const key = web3.utils.keccak256(datatokenAddress)
assert((await nftDatatoken.getData(nftAddress, key)) === OldData)
assert((await nftDatatoken.getData(nftAddress, datatokenAddress)) === OldData)
})
it('#getDecimals - should return the number of decimals of the datatoken', async () => {

View File

@ -433,4 +433,31 @@ describe('NFT', () => {
assert(metadata[0] === metadataAndTokenURI.metaDataDecryptorUrl)
assert(metadata[1] === metadataAndTokenURI.metaDataDecryptorAddress)
})
it('#setData - should FAIL to set a value into 725Y standard, if Caller has NOT store updater permission', async () => {
const key = 'KEY'
const data = 'NewData'
assert((await nftDatatoken.getNftPermissions(nftAddress, user1)).store === false)
try {
await nftDatatoken.setData(nftAddress, user1, key, data)
assert(false)
} catch (e) {
assert(e.message === 'User is not ERC20 store updater')
}
assert((await nftDatatoken.getData(nftAddress, key)) === null)
})
it('#setData - should set a value into 725Y standard, if Caller has store updater permission', async () => {
const key = 'KEY'
const data = 'NewData'
// add store updater permission
await nftDatatoken.addStoreUpdater(nftAddress, user1, user1)
assert((await nftDatatoken.getNftPermissions(nftAddress, user1)).store === true)
await nftDatatoken.setData(nftAddress, user1, key, data)
assert((await nftDatatoken.getData(nftAddress, key)) === data)
})
})