2024-01-22 21:48:51 +00:00
|
|
|
// SPDX-License-Identifier: UNLICENSED
|
|
|
|
pragma solidity ^0.8.23;
|
|
|
|
|
|
|
|
import {Script, console2} from "forge-std/Script.sol";
|
|
|
|
import { DappIndexer} from "src/DappIndexer.sol";
|
|
|
|
|
|
|
|
contract DappIndexerScript is Script {
|
|
|
|
DappIndexer public dappIdxr;
|
|
|
|
string[] public dappsURI;
|
2024-02-24 20:49:07 +00:00
|
|
|
address public owner;
|
2024-01-22 21:48:51 +00:00
|
|
|
|
|
|
|
function setUp() public {
|
|
|
|
dappsURI = vm.envString("dapps", ' ');
|
2024-02-24 20:49:07 +00:00
|
|
|
owner = vm.envAddress("ACC0");
|
2024-01-22 21:48:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run is the entry point
|
|
|
|
function run() public {
|
2024-02-24 20:49:07 +00:00
|
|
|
uint256 deployerPrivateKey = vm.envUint("PK0");
|
2024-01-22 21:48:51 +00:00
|
|
|
// startBroadcast and stopBraodcast will let us execute transactions anything between them
|
2024-02-24 20:49:07 +00:00
|
|
|
vm.startBroadcast(deployerPrivateKey);
|
2024-01-22 21:48:51 +00:00
|
|
|
// here we just need to deploy a new contractc
|
2024-02-24 20:49:07 +00:00
|
|
|
dappIdxr = new DappIndexer(owner, owner);
|
|
|
|
console2.log("The Dapps Indexer contract address is: ", address(dappIdxr));
|
2024-01-22 21:48:51 +00:00
|
|
|
|
|
|
|
bytes32 CID1;
|
|
|
|
bytes32 CID2;
|
|
|
|
|
|
|
|
(CID1, CID2) = stringToBytes32Pair(dappsURI[0]);
|
|
|
|
|
|
|
|
// Add Dapp
|
|
|
|
console2.log("-----Adding Dapp-----");
|
2024-02-24 20:49:07 +00:00
|
|
|
console2.log("Dapp Name is:");
|
|
|
|
console2.logBytes32(bytes32("Dapp01"));
|
2024-01-22 21:48:51 +00:00
|
|
|
console2.log("Bytes32 first:");
|
|
|
|
console2.logBytes32(CID1);
|
|
|
|
console2.log("Bytes32 second:");
|
|
|
|
console2.logBytes32(CID2);
|
|
|
|
console2.log(Bytes32PairToString(CID1, CID2));
|
|
|
|
dappIdxr.addDapp(bytes32("Dapp01"), DappIndexer.PackedCID(CID1, CID2));
|
|
|
|
|
|
|
|
// retrive Dapp
|
|
|
|
console2.log("-----retriving Dapp-----");
|
2024-02-24 20:49:07 +00:00
|
|
|
DappIndexer.PackedCID memory dapp1 = dappIdxr.getDapp(bytes32("Dapp01"));
|
2024-01-22 21:48:51 +00:00
|
|
|
console2.log("Bytes32 first:");
|
|
|
|
console2.logBytes32(dapp1.CID1);
|
|
|
|
console2.log("Bytes32 second:");
|
|
|
|
console2.logBytes32(dapp1.CID2);
|
|
|
|
console2.log(Bytes32PairToString(dapp1.CID1, dapp1.CID2));
|
|
|
|
|
|
|
|
vm.stopBroadcast();
|
|
|
|
}
|
|
|
|
|
|
|
|
function Bytes32PairToString(bytes32 part1, bytes32 part2) public pure returns (string memory) {
|
|
|
|
// Concatenate the two bytes32 variables
|
|
|
|
bytes memory concatenatedBytes = abi.encodePacked(part1, part2);
|
|
|
|
|
2024-01-22 23:28:25 +00:00
|
|
|
// Truncate the concatenated bytes to 59 bytes
|
|
|
|
bytes memory truncatedBytes = new bytes(59);
|
|
|
|
for (uint i = 0; i < 59; i++) {
|
|
|
|
truncatedBytes[i] = concatenatedBytes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the truncated bytes to a string
|
|
|
|
string memory str = string(truncatedBytes);
|
2024-01-22 21:48:51 +00:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
function stringToBytes32Pair(string memory source) public pure returns (bytes32 part1, bytes32 part2) {
|
|
|
|
bytes memory sourceBytes = bytes(source);
|
2024-02-24 20:49:07 +00:00
|
|
|
require(sourceBytes.length == 59, "URI string must equal to 59 bytes");
|
2024-01-22 21:48:51 +00:00
|
|
|
|
|
|
|
assembly {
|
|
|
|
// Load the first 32 bytes of the string data
|
|
|
|
part1 := mload(add(sourceBytes, 32))
|
|
|
|
// Load the second 32 bytes of the string data
|
|
|
|
part2 := mload(add(sourceBytes, 64))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|