OpenZeppelin / openzeppelin-contracts

OpenZeppelin Contracts is a library for secure smart contract development.
https://openzeppelin.com/contracts
MIT License
24.98k stars 11.81k forks source link

A method to get an image from an ERC20 contract. #4130

Open njrapidinnovation opened 1 year ago

njrapidinnovation commented 1 year ago

🧐 Motivation There are multiple benefits of getting an image directly from a smart contract of an ERC20 token.

📝 Details

I took inspiration from generateNftSvgByTokenId method in On-chain-svg-nft-tickets

njrapidinnovation commented 1 year ago

I think we can use this as an ERC20 extension.

Amxx commented 1 year ago

Hello @njrapidinnovation

Is there a standard way for frontends/explorer/UI to fetch an ERC-20 logo/image? I'm not aware of such an ERC.

njrapidinnovation commented 1 year ago

Hi @Amxx , no there is no such ERC yet or a standard way, but we can fetch a url for an SVG image.

Below is the code to see such an example , this function will return a BTC SVG format image url paste that in browser to see the results.

CODE SNIPPET:-

  function getBtcLogo() public view returns (string memory) {
    return string(abi.encodePacked(
      'data:image/svg+xml;base64,',
      Base64.encode(bytes(abi.encodePacked(
        '<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="100%" height="100%" version="1.1" shape-rendering="geometricPrecision" text-rendering="geometricPrecision" image-rendering="optimizeQuality" fill-rule="evenodd" clip-rule="evenodd" viewBox="0 0 4091.27 4091.73" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xodm="http://www.corel.com/coreldraw/odm/2003">'
        '<g id="Layer_x0020_1">' '<metadata id="CorelCorpID_0Corel-Layer"/>'
        '<g id="_1421344023328">'
        '<path fill="#F7931A" fill-rule="nonzero" d="M4030.06 2540.77c-273.24,1096.01 -1383.32,1763.02 -2479.46,1489.71 -1095.68,-273.24 -1762.69,-1383.39 -1489.33,-2479.31 273.12,-1096.13 1383.2,-1763.19 2479,-1489.95 1096.06,273.24 1763.03,1383.51 1489.76,2479.57l0.02 -0.02z"/>'
        '<path fill="white" fill-rule="nonzero" d="M2947.77 1754.38c40.72,-272.26 -166.56,-418.61 -450,-516.24l91.95 -368.8 -224.5 -55.94 -89.51 359.09c-59.02,-14.72 -119.63,-28.59 -179.87,-42.34l90.16 -361.46 -224.36 -55.94 -92 368.68c-48.84,-11.12 -96.81,-22.11 -143.35,-33.69l0.26 -1.16 -309.59 -77.31 -59.72 239.78c0,0 166.56,38.18 163.05,40.53 90.91,22.69 107.35,82.87 104.62,130.57l-104.74 420.15c6.26,1.59 14.38,3.89 23.34,7.49 -7.49,-1.86 -15.46,-3.89 -23.73,-5.87l-146.81 588.57c-11.11,27.62 -39.31,69.07 -102.87,53.33 2.25,3.26 -163.17,-40.72 -163.17,-40.72l-111.46 256.98 292.15 72.83c54.35,13.63 107.61,27.89 160.06,41.3l-92.9 373.03 224.24 55.94 92 -369.07c61.26,16.63 120.71,31.97 178.91,46.43l-91.69 367.33 224.51 55.94 92.89 -372.33c382.82,72.45 670.67,43.24 791.83,-303.02 97.63,-278.78 -4.86,-439.58 -206.26,-544.44 146.69,-33.83 257.18,-130.31 286.64,-329.61l-0.07 -0.05zm-512.93 719.26c-69.38,278.78 -538.76,128.08 -690.94,90.29l123.28 -494.2c152.17,37.99 640.17,113.17 567.67,403.91zm69.43 -723.3c-63.29,253.58 -453.96,124.75 -580.69,93.16l111.77 -448.21c126.73,31.59 534.85,90.55 468.94,355.05l-0.02 0z"/>'
        '</g>'
        '</g>'
        '</svg>'
      ))
    )));
  }

RESULT:-

data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbDpzcGFjZT0icHJlc2VydmUiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIHZlcnNpb249IjEuMSIgc2hhcGUtcmVuZGVyaW5nPSJnZW9tZXRyaWNQcmVjaXNpb24iIHRleHQtcmVuZGVyaW5nPSJnZW9tZXRyaWNQcmVjaXNpb24iIGltYWdlLXJlbmRlcmluZz0ib3B0aW1pemVRdWFsaXR5IiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgdmlld0JveD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UuMDVsLTAuMDIgMHoiLz48L2c+PC9nPjwvc3ZnPg==

RESULT IN BROWSER:-

Screenshot 2023-03-22 at 8 51 30 PM
Amxx commented 1 year ago

If there is no standard interface to query, what is it that you expect us to implement?

njrapidinnovation commented 1 year ago

I want you guys to implement an ERC20 extension that can store SVG image data in an optimized way, allowing for updates and retrieval through a function.

abhi3700 commented 1 year ago

I think you should create an ERC in this repo & wait for the community to reply to your thread.

Amxx commented 1 year ago

I want you guys to implement an ERC20 extension that can store SVG image data in an optimized way, allowing for updates and retrieval through a function.

Each image is different, so we can't provide one image data. Since there is no standard query function, we can't provide a public interfaces. We already provide Base64.encode which is the one tool you need that is generic.

Also, the image data should NOT be in storage. it should be provided by a pure function. Putting that would just be wastefull. I also think updating the image would not be a good idea (for many reasons).