optimized contracts 7fc76caf
Steve · 2023-10-14 09:00 7 file(s) · +83 −33
contracts/CosmicCowboy.sol +15 −6
46 46
        latestTokenId = tokenId;
47 47
        _safeMint(to, tokenId);
48 48
        _setTokenURI(tokenId, uri);
49 -
        setHealth(tokenId, 10);
49 +
        setHealth(tokenId, 6);
50 50
        tokenLocation[tokenId] = Location.Bar;
51 51
    }
52 52
56 56
    }
57 57
58 58
    // Function to visit a location
59 -
    function visitArea(uint256 tokenId, Location _location) external {
60 -
        require(ownerOf(tokenId) == msg.sender, "Not owner");
61 -
        tokenLocation[tokenId] = _location;
62 -
        emit VisitArea(tokenId, locationToString(_location));
59 +
60 +
    function goToHome(uint256 tokenId) external {
61 +
        tokenLocation[tokenId] = Location.Home;
62 +
        emit VisitArea(tokenId, locationToString(Location.Home));
63 +
    }
64 +
65 +
    function goToBar(uint256 tokenId) external {
66 +
        tokenLocation[tokenId] = Location.Bar;
67 +
        emit VisitArea(tokenId, locationToString(Location.Bar));
68 +
    }
69 +
70 +
    function goToSupplyDepot(uint256 tokenId) external {
71 +
        tokenLocation[tokenId] = Location.SupplyDepot;
72 +
        emit VisitArea(tokenId, locationToString(Location.SupplyDepot));
63 73
    }
64 74
65 75
    // Function to get the current location of a token
67 77
        uint256 tokenId
68 78
    ) external view returns (string memory) {
69 79
        Location _location = tokenLocation[tokenId];
70 -
        require(_location != Location(0), "Token does not exist");
71 80
        return locationToString(_location);
72 81
    }
73 82
contracts/Operator.sol +25 −9
38 38
        return cosmicCowboys.latestTokenId();
39 39
    }
40 40
41 -
    function equipNPC(
42 -
        address to,
43 -
        uint256 currencyAmount,
44 -
        uint256 foodAmount,
45 -
        uint256 suppliesAmount
46 -
    ) public {
47 -
        goldenCorn.mint(to, currencyAmount);
48 -
        spaceSlop.mint(to, foodAmount);
49 -
        jupiterJunk.mint(to, suppliesAmount);
41 +
    function fundNPC(address to, uint256 amount) public {
42 +
        goldenCorn.mint(to, amount);
43 +
    }
44 +
45 +
    function feedNPC(address to, uint256 amount) public {
46 +
        spaceSlop.mint(to, amount);
47 +
    }
48 +
49 +
    function supplyNPC(address to, uint256 amount) public {
50 +
        jupiterJunk.mint(to, amount);
50 51
    }
51 52
52 53
    function getNPCStats(
55 56
        uint8 health = cosmicCowboys.getHealth(tokenId);
56 57
        string memory location = cosmicCowboys.getCurrentLocation(tokenId);
57 58
        return (health, location);
59 +
    }
60 +
61 +
    function goToHome(uint256 tokenId) public {
62 +
        //require(cosmicCowboys.ownerOf(tokenId) == tba, "Not owner"); // Check OwnershipTransferred
63 +
        cosmicCowboys.goToHome(tokenId);
64 +
        uint8 newHealth = cosmicCowboys.getHealth(tokenId) + 2;
65 +
        cosmicCowboys.setHealth(tokenId, newHealth);
66 +
    }
67 +
68 +
    function goToBar(uint256 tokenId) public {
69 +
        cosmicCowboys.goToBar(tokenId);
70 +
    }
71 +
72 +
    function goToSupplyDepot(uint256 tokenId) public {
73 +
        cosmicCowboys.goToSupplyDepot(tokenId);
58 74
    }
59 75
60 76
    function getOwner() external view returns (address) {
hardhat.config.js +10 −6
7 7
  networks: {
8 8
    goerli: {
9 9
      url: `${process.env.ALCHEMY_URL}`,
10 -
      accounts: [process.env.PRIVATE_KEY]
10 +
      accounts: [process.env.PRIVATE_KEY],
11 11
    },
12 12
    scrollSepolia: {
13 13
      url: "https://sepolia-rpc.scroll.io/" || "",
17 17
    'base-goerli': {
18 18
      url: `${process.env.ALCHEMY_URL_BASE}`,
19 19
      accounts: [process.env.PRIVATE_KEY],
20 -
      gasPrice: 2000000000,
20 +
      gasPrice: 1000000000,
21 +
    },
22 +
    sepolia: {
23 +
      url: `${process.env.SEPOLIA_URL}`,
24 +
      accounts: [process.env.PRIVATE_KEY],
21 25
    }
22 26
  },
23 -
  etherscan: {
27 +
  /* etherscan: {
24 28
    apiKey: {
25 29
      "base-goerli": "PLACEHOLDER_STRING"
26 30
    },
34 38
        }
35 39
      }
36 40
    ]
37 -
  },
38 -
  /* etherscan: {
41 +
  }, */
42 +
  etherscan: {
39 43
    apiKey: process.env.ETHERSCAN_API_KEY
40 -
  } */
44 +
  }
41 45
42 46
};
package-lock.json +5 −5
7 7
      "name": "hardhat-project",
8 8
      "dependencies": {
9 9
        "@openzeppelin/contracts": "^5.0.0",
10 -
        "@tokenbound/sdk": "^0.3.9",
10 +
        "@tokenbound/sdk": "^0.3.12",
11 11
        "dotenv": "^16.3.1",
12 12
        "ethers": "^6.7.1"
13 13
      },
1824 1824
      }
1825 1825
    },
1826 1826
    "node_modules/@tokenbound/sdk": {
1827 -
      "version": "0.3.9",
1828 -
      "resolved": "https://registry.npmjs.org/@tokenbound/sdk/-/sdk-0.3.9.tgz",
1829 -
      "integrity": "sha512-Mldj/pmJDBtExJ88uhAPyetrZ1P4JZbmU10A4ZITt0yBRZKeTCUu2jOzVNWJErPDL3/kZGUlKYMkRqMhmOUibA==",
1827 +
      "version": "0.3.12",
1828 +
      "resolved": "https://registry.npmjs.org/@tokenbound/sdk/-/sdk-0.3.12.tgz",
1829 +
      "integrity": "sha512-q0t12HlMu0d+RuoIFAMN9G6CQCrlcPmuQwW4f6e8TmMADIyKfZoUaA7phE1muY4bRTSFeVmx2AmsZom+kb4MVA==",
1830 1830
      "dependencies": {
1831 -
        "viem": "^1.14.0"
1831 +
        "viem": "^1.16.2"
1832 1832
      }
1833 1833
    },
1834 1834
    "node_modules/@tsconfig/node10": {
package.json +1 −1
8 8
  },
9 9
  "dependencies": {
10 10
    "@openzeppelin/contracts": "^5.0.0",
11 -
    "@tokenbound/sdk": "^0.3.9",
11 +
    "@tokenbound/sdk": "^0.3.12",
12 12
    "dotenv": "^16.3.1",
13 13
    "ethers": "^6.7.1"
14 14
  }
scripts/deployContracts.js +15 −4
58 58
  await foodContract.transferOwnership(operatorContractAddress);
59 59
  await supplyContract.transferOwnership(operatorContractAddress);
60 60
61 -
  for (let i = 0; i < 2; i++) {
61 +
  for (let i = 0; i < 19; i++) {
62 62
    // create NPC
63 63
    const npcTx = await operatorContract.createNPC(wallet.address, `ipfs://QmQbwCMwDETHHZ1g8YaSHqLBwCRgVHqFuRNRfiGyNqCcXj/${i}.json`)
64 64
    const npcTxReceipt = await npcTx.wait()
75 75
    console.log("TBA:", tba)
76 76
77 77
    // equip NPC via TBA
78 +
    //
79 +
    const fundNpcTx = await operatorContract.fundNPC(tba, 20)
80 +
    const fundNpxTxReceipt = await fundNpcTx.wait()
81 +
    console.log("NPC Funded")
82 +
83 +
    const feedNpcTx = await operatorContract.feedNPC(tba, 5)
84 +
    const feedNpcTxReceipt = await feedNpcTx.wait()
85 +
    console.log("NPC Fed")
78 86
79 -
    const equipTx = await operatorContract.equipNPC(tba, 20, 5, 5)
80 -
    const equipTxReceipt = await equipTx.wait()
81 -
    console.log("NPC Equipped")
87 +
    const supplyNpcTx = await operatorContract.supplyNPC(tba, 5)
88 +
    const supplyNpcTxReceipt = await supplyNpcTx.wait()
89 +
    console.log("NPC Supplied")
90 +
82 91
  }
92 +
93 +
83 94
84 95
85 96
}
scripts/deployContractsBase.js +12 −2
82 82
83 83
    // equip NPC via TBA
84 84
85 -
    const equipTx = await operatorContract.equipNPC(tba, 20, 5, 5)
85 +
    /* const fundTx = await operatorContract.fundNPC(tba, 20)
86 +
    const fundTxReceipt = await fundTx.wait()
87 +
    console.log("NPC Funded") */
88 +
    const giveFoodTx = await operatorContract.giveFood(tba, 1)
89 +
    const giveFoodTxReceipt = await giveFoodTx.wait()
90 +
    console.log("NPC Fed")
91 +
    /* const giveSupplyTx = await operatorContract.supplyNPC(tba, 5)
92 +
    const giveSupplyTxReceipt = await giveSupplyTx.wait()
93 +
    console.log("NPC Supplied") */
94 +
95 +
    /* const equipTx = await operatorContract.equipNPC(tba, 20, 5, 5)
86 96
    const equipTxReceipt = await equipTx.wait()
87 -
    console.log("NPC Equipped")
97 +
    console.log("NPC Equipped") */
88 98
  }
89 99
90 100