Skip to content

Commit

Permalink
Very quick and dirty proof of concept of navigation, img loading with…
Browse files Browse the repository at this point in the history
… the evm:// protocol.
  • Loading branch information
nand2 committed Mar 8, 2023
1 parent b1c264f commit 6ea4467
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
52 changes: 49 additions & 3 deletions contracts/TerraformExplorer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.18;

import "hardhat/console.sol";
import "./libs/ToString.sol";

import {IScriptyBuilder, WrappedScriptRequest} from "./interfaces/IScriptyBuilder.sol";

Expand All @@ -23,7 +24,7 @@ contract TerraformExplorer {

function indexHTML() public view returns (string memory) {

WrappedScriptRequest[] memory requests = new WrappedScriptRequest[](2);
WrappedScriptRequest[] memory requests = new WrappedScriptRequest[](3);
requests[0].name = "scriptyBase";
requests[0].wrapType = 0; // <script>[script]</script>
requests[0].contractAddress = scriptyStorageAddress;
Expand All @@ -40,8 +41,25 @@ contract TerraformExplorer {
// requests[1].wrapType = 0; // <script>[script]</script>
// requests[1].scriptContent = "xxx";

requests[1].wrapType = 4; // [wrapPrefix][script][suffix]
requests[1].scriptContent = '<h1 style="text-align: center">Terraform explorer</h1>';
requests[1].wrapType = 4; // [wrapPrefix][script][wrapSuffix]
requests[1].wrapPrefix = "<style>";
requests[1].scriptContent = 'body {background-color: #171717; color: #f5f5f5}';
requests[1].wrapSuffix = "</style>";

requests[2].wrapType = 4; // [wrapPrefix][script][wrapSuffix]
requests[2].scriptContent = abi.encodePacked(
'<h1 style="text-align: center">Terraform explorer</h1>'
'<br />'
'<a href="evm://0x', ToString.addressToString(address(this)), '/viewHTML?tokenId:uint256=4197">'
'<img src="evm://0x4e1f41613c9084fdb9e34e11fae9412427480e56/tokenSVG?tokenId:uint256=4197" style="width:200px">'
'</a>'
'<a href="evm://0x', ToString.addressToString(address(this)), '/viewHTML?tokenId:uint256=4198">'
'<img src="evm://0x4e1f41613c9084fdb9e34e11fae9412427480e56/tokenSVG?tokenId:uint256=4198" style="width:200px">'
'</a>'
'<a href="evm://0x', ToString.addressToString(address(this)), '/viewHTML?tokenId:uint256=4199">'
'<img src="evm://0x4e1f41613c9084fdb9e34e11fae9412427480e56/tokenSVG?tokenId:uint256=4199" style="width:200px">'
'</a>'
);

// Random buffer for now
uint bufferSize = 291925;
Expand All @@ -52,5 +70,33 @@ contract TerraformExplorer {
return string(html);
}

function viewHTML(uint256 tokenId) public view returns (string memory) {

WrappedScriptRequest[] memory requests = new WrappedScriptRequest[](3);
requests[0].name = "scriptyBase";
requests[0].wrapType = 0; // <script>[script]</script>
requests[0].contractAddress = scriptyStorageAddress;


requests[1].wrapType = 4; // [wrapPrefix][script][wrapSuffix]
requests[1].wrapPrefix = "<style>";
requests[1].scriptContent = 'body {background-color: #171717; color: #f5f5f5}';
requests[1].wrapSuffix = "</style>";

requests[2].wrapType = 4; // [wrapPrefix][script][wrapSuffix]
requests[2].scriptContent = abi.encodePacked(
'<h1 style="text-align: center">Terraform ', ToString.toString(tokenId), '</h1>'
'<br />'
'<img src="evm://0x4e1f41613c9084fdb9e34e11fae9412427480e56/tokenSVG?tokenId:uint256=', ToString.toString(tokenId) ,'">'
);

// Random buffer for now
uint bufferSize = 291925;

bytes memory html = IScriptyBuilder(scriptyBuilderAddress)
.getHTMLWrapped(requests, bufferSize);

return string(html);
}

}
56 changes: 56 additions & 0 deletions contracts/libs/ToString.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// Uncomment this line to use console.log
import "hardhat/console.sol";

library ToString {

function toString(uint value) internal pure returns (string memory) {
// Inspired by OraclizeAPI's implementation - MIT license
// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol

if (value == 0) {
return "0";
}
uint temp = value;
uint digits;
while (temp != 0) {
digits++;
temp /= 10;
}
bytes memory buffer = new bytes(digits);
while (value != 0) {
digits -= 1;
buffer[digits] = bytes1(uint8(48 + uint(value % 10)));
value /= 10;
}
return string(buffer);
}

function toString(int value) internal pure returns (string memory) {
return string(abi.encodePacked(value < 0 ? "-" : "", toString(uint(value < 0 ? - value : value))));
}

function addressToString(address x) internal pure returns (string memory) {
// Fetched from https://ethereum.stackexchange.com/questions/8346/convert-address-to-string/8447#8447

bytes memory s = new bytes(40);
for (uint i = 0; i < 20; i++) {
bytes1 b = bytes1(uint8(uint(uint160(x)) / (2**(8*(19 - i)))));
bytes1 hi = bytes1(uint8(b) / 16);
bytes1 lo = bytes1(uint8(b) - 16 * uint8(hi));
s[2*i] = char(hi);
s[2*i+1] = char(lo);
}
return string(s);
}

function char(bytes1 b) internal pure returns (bytes1 c) {
// Fetched from https://ethereum.stackexchange.com/questions/8346/convert-address-to-string/8447#8447

if (uint8(b) < 10) return bytes1(uint8(b) + 0x30);
else return bytes1(uint8(b) + 0x57);
}

}
1 change: 1 addition & 0 deletions test/TerraformExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
const { expect } = require("chai");


// We define a fixture to reuse the same setup in every test.
async function deployFixture() {

Expand Down

0 comments on commit 6ea4467

Please sign in to comment.