Update struct when NFT is transferred

Is that possible?

My ERC721 is saving the "author" of the NFT, but I would like to change this to "holder" - This ofcause if quite easy, but I would like to have the NFT struct updated whenever the NFT changes wallet, in order to always make sure that the "holder" is the current and not the original minter

hey @Kion_Larsen
to do this you only have to update the struct at any transfer. If you can share the code I can help more

Hi

Well the struct looks like this:

struct Lip {
    string name;
    uint256 id;
    uint256 dna;
    uint16 level;
    uint8 rarity;
    address payable holder;
  }

And the mint function is like this:

function _createLip(string memory _name) internal {
    uint8 randRarity = uint8(_createRandomNum(100));
    uint256 randDna = _createRandomNum(10**16);
    Lip memory newLip = Lip(_name, COUNTER, randDna, 1, randRarity, payable(msg.sender));
    lips.push(newLip);
    _safeMint(msg.sender, COUNTER);
    emit NewLip(msg.sender, COUNTER, randDna);
    COUNTER++;
  }

I do not know exactly what you are looking for, but if you have an idea on how I can update the struct when transfer is completed OR maybe a solution on how I can update the struck manually, I'd be happy too :slight_smile:

Basically you should forget about the code, because this is just the inspiration, and update of the level can be done like this:

  function levelUp(uint256 _lipId) public payable {
    require(msg.value >= fee);
    require(ownerOf(_lipId) == msg.sender, "You are not owner");
    Lip storage lip = lips[_lipId];
    lip.level++;
  }

So if you know how to modify the last piece of code to change the holder in the struct, then that would be greatly appreciated

Sorry but I didn't understand very well your aim. Btw given your struct like this:

struct Lip {
    string name;
    uint256 id;
    uint256 dna;
    uint16 level;
    uint8 rarity;
    address payable holder;
  }

to update just one element, like the holder address you can do in this way:
Hp: lip is your object that need to be modified

lip.holder = 'insert here new address'

Hmmm

I don't quite follow what you mean... Could I try another?:

pragma solidity ^0.4.17;

contract Profile {
    struct User {
        uint256 fuel;
    }

    mapping(address => User) public users;
    string name;

    function createUser() public {
        users[msg.sender] = User({
            fuel: 5
        });
    }

    function getFuel() public view returns (uint256) {
        return users[msg.sender].fuel;
    }

    function setFuel(uint256 newFuel) public {
        users[msg.sender].fuel = newFuel;
    }
}

When I call(click) createUser, it updates fuel to have 5, how do I change the function setFuel to automatically add 10 to the current value, which is now 5?

Alright:

This code allows me to recharge the fuel level, and subtract 1 unit everytime the useFuel function i called:

Thank you for your help :slight_smile:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

contract Profile {
    struct User {
        uint256 fuel;
    }

    mapping(address => User) public users;
    string name;

    function createUser() public {
        users[msg.sender] = User({
            fuel: 1
        });
    }

    function getFuel() public view returns (uint256) {
        return users[msg.sender].fuel;
    }

    function setFuel() public payable {
        uint256 newFuel = 5;
        users[msg.sender].fuel = newFuel;
    }

    function useFuel() public {
        uint256 newuseFuel = users[msg.sender].fuel;
        users[msg.sender].fuel = (newuseFuel - 1);
    }


}