Skip to content

Commit

Permalink
Prevent anniversary pets from being transferred
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSamWitch committed Aug 25, 2024
1 parent 6c632f0 commit 12279f2
Show file tree
Hide file tree
Showing 9 changed files with 673 additions and 15 deletions.
628 changes: 628 additions & 0 deletions .openzeppelin/unknown-250-beta.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Territories [0xf31517db9f0987002f3a0fb4f787dfb9e892f184](https://ftmscan.com/add
CombatantsHelper [0xe8231ac805a88b3c72e9602c2ae14a5d3421bc7c](https://ftmscan.com/address/0xe8231ac805a88b3c72e9602c2ae14a5d3421bc7c)
DecoratorProvider [0xea8c4d188eb8d9704bc36931d89ba4f8e935cee2](https://ftmscan.com/address/0xea8c4d188eb8d9704bc36931d89ba4f8e935cee2)

PetNFTLibrary [0x0a6791e7a7f109bcf24435cb59c00662fc0dd17a](https://ftmscan.com/address/0x0a6791e7a7f109bcf24435cb59c00662fc0dd17a)
PetNFTLibrary [0x04a11e4afd667ca55b10d834a7367c4bed107750](https://ftmscan.com/address/0x04a11e4afd667ca55b10d834a7367c4bed107750)
PetNFT [0xa6489181b24e966402891225c65f8e2d136ddd2e](https://ftmscan.com/address/0xa6489181b24e966402891225c65f8e2d136ddd2e)
PassiveActions [0x3df5b6cad0d2de6b71f2d5084e0b933dbcd395f6](https://ftmscan.com/address/0x3df5b6cad0d2de6b71f2d5084e0b933dbcd395f6)

Expand Down
11 changes: 8 additions & 3 deletions contracts/PetNFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ contract PetNFT is UUPSUpgradeable, OwnableUpgradeable, ERC1155UpgradeableSingle
error NotPlayersOrAdminAndBeta();
error IllegalNameStart();
error SameName();
error CannotTransferThisPet(uint256 petId);

struct BasePetInput {
string description;
Expand Down Expand Up @@ -431,11 +432,15 @@ contract PetNFT is UUPSUpgradeable, OwnableUpgradeable, ERC1155UpgradeableSingle
}
}

function _updateOwner(uint256 _id, address _to) internal override {
bool isBurnt = _to == address(0);
if (isBurnt) {
function _updateOwner(uint256 _id, address _from, address _to) internal override {
if (_to == address(0)) {
// Burnt
delete pets[_id];
} else {
// Cannot transfer anniversary pets
if (_from != address(0) && basePetMetadatas[pets[_id].baseId].skin == PetSkin.ANNIV1) {
revert CannotTransferThisPet(_id);
}
pets[_id].owner = _to;
pets[_id].lastAssignmentTimestamp = uint40(block.timestamp);
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/PetNFTLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ library PetNFTLibrary {
if (_skin == PetSkin.CRYSTAL) {
return "Crystal";
}
if (_skin == PetSkin.ANNIVERSARY1) {
if (_skin == PetSkin.ANNIV1) {
return "Anniv1";
}
revert InvalidSkin(_skin);
Expand Down
2 changes: 1 addition & 1 deletion contracts/globals/pets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum PetSkin {
ONEKIN,
FROST,
CRYSTAL,
ANNIVERSARY1
ANNIV1
}

enum PetEnhancementType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ contract ERC1155UpgradeableSinglePerToken is
revert ERC1155InsufficientBalance();
}
if (from != to) {
_updateOwner(id, to);
_updateOwner(id, from, to);
}

emit TransferSingle(operator, from, to, id, amount);
Expand Down Expand Up @@ -260,7 +260,7 @@ contract ERC1155UpgradeableSinglePerToken is
revert ERC1155InsufficientBalance();
}
if (from != to) {
_updateOwner(id, to);
_updateOwner(id, from, to);
}
}

Expand Down Expand Up @@ -330,7 +330,7 @@ contract ERC1155UpgradeableSinglePerToken is
unchecked {
++_totalSupplyAll;
}
_updateOwner(id, to);
_updateOwner(id, address(0), to);

emit TransferSingle(operator, address(0), to, id, amount);

Expand Down Expand Up @@ -367,7 +367,7 @@ contract ERC1155UpgradeableSinglePerToken is
revert ERC1155MintingMoreThanOneSameNFT();
}

_updateOwner(ids[i], to);
_updateOwner(ids[i], address(0), to);
}

unchecked {
Expand Down Expand Up @@ -409,7 +409,7 @@ contract ERC1155UpgradeableSinglePerToken is
unchecked {
--_totalSupplyAll;
}
_updateOwner(id, address(0));
_updateOwner(id, from, address(0));

emit TransferSingle(operator, from, address(0), id, amount);

Expand Down Expand Up @@ -446,7 +446,7 @@ contract ERC1155UpgradeableSinglePerToken is
revert ERC115BurnAmountExceedsBalance();
}

_updateOwner(id, address(0));
_updateOwner(id, from, address(0));
}

unchecked {
Expand Down Expand Up @@ -589,7 +589,7 @@ contract ERC1155UpgradeableSinglePerToken is
return _owner[id];
}

function _updateOwner(uint256 id, address to) internal virtual {
function _updateOwner(uint256 id, address /*from*/, address to) internal virtual {
_owner[id] = to;
}
}
11 changes: 11 additions & 0 deletions data/abi/PetNFT.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
"name": "CallerIsNotOwner",
"type": "error"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "petId",
"type": "uint256"
}
],
"name": "CannotTransferThisPet",
"type": "error"
},
{
"inputs": [],
"name": "ERC1155InsufficientBalance",
Expand Down
2 changes: 1 addition & 1 deletion scripts/contractAddresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ if (!isBeta) {
oracle = "0x6f7911cbbd4b5a1d2bdaa817a76056e510d728e7";
samWitchVRF = "0x58E9fd2Fae18c861B9F564200510A88106C05756";
bazaar = "0x082480aAAF1ac5bb0Db2c241eF8b4230Da85E191";
petNFTLibrary = "0x0a6791e7a7f109bcf24435cb59c00662fc0dd17a";
petNFTLibrary = "0x04a11e4afd667ca55b10d834a7367c4bed107750";
petNFT = "0xa6489181b24e966402891225c65f8e2d136ddd2e";
passiveActions = "0x3df5b6cad0d2de6b71f2d5084e0b933dbcd395f6";
}
Expand Down
16 changes: 15 additions & 1 deletion test/Players/Pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {expect} from "chai";
import {ethers} from "hardhat";
import {playersFixture} from "./PlayersFixture";
import {setupBasicPetMeleeCombat, getXPFromLevel} from "./utils";
import {Skill} from "@paintswap/estfor-definitions/types";
import {PetSkin, Skill} from "@paintswap/estfor-definitions/types";
import {allBasePets} from "../../scripts/data/pets";
import {NO_DONATION_AMOUNT} from "../utils";

Expand Down Expand Up @@ -246,4 +246,18 @@ describe("Pets", function () {
await players.connect(alice).processActions(playerId);
expect(await players.xp(playerId, EstforTypes.Skill.MELEE)).to.eq(getXPFromLevel(5) + 36);
});

it("Cannot transfer an anniversary pet", async function () {
const {petNFT, owner, alice} = await loadFixture(playersFixture);

const basePet = allBasePets.find((pet) => pet.skin === PetSkin.ANNIV1) as EstforTypes.BasePetInput;
expect(basePet.skin).to.eq(PetSkin.ANNIV1);
await petNFT.addBasePets([basePet]);
await petNFT.mint(alice.address, basePet.baseId, 0);

const petId = 1;
await expect(petNFT.connect(alice).safeTransferFrom(alice.address, owner.address, petId, 1, "0x"))
.to.be.revertedWithCustomError(petNFT, "CannotTransferThisPet")
.withArgs(petId);
});
});

0 comments on commit 12279f2

Please sign in to comment.