Skip to main content
6 of 6
added 202 characters in body
  • 8.4k
  • 11
  • 64
  • 154

What is best what to store a string's compressed version on a smart-contract?

Storing string is expensive on smart-contracts and charge us expensive fees even if we want to store them in Events. Instead I want to store each string's decoded version in a smart-contract in order to save some memory and gas-fee charge. Overall, when I return string's decoded version, it also should be able to decoded into the original string.

As an example I want to pass my string as compressing base64 input format and its code. But when I stored compressed base64 string on a smart-contract, its returned version is not same as the one I passed.

Example example.js:

var Base64String = require('./base64-string');

var string = "QmWmyoMoctfbAaiEs2G46gpeUmhqFRDW6KWo64y5r581Vm";
console.log("Size of sample is: " + string.length);
var compressed = Base64String.compress(string);
console.log("compressed:" + compressed)
console.log("Size of compressed sample is: " + compressed.length);
string = Base64String.decompress(compressed);
console.log("Sample is: " + string);

Results node example.js: //as you can see 46 length string is compressed into 19 length.

Size of sample is: 46
compressed: ł斦쪃⡲ퟛƨ蒳憸幒桪ᔐ훨ꖨ릯鼵嗐
Size of compressed sample is: 19
Sample is: QmWmyoMoctfbAaiEs2G46gpeUmhqFRDW6KWo64y5r581VmAA

Passed base64 compressed string: //I want to store it on my smart-contract.



From the smart-contract its returned version seems not same as the string I passed. // Contract.get()


Overall: "ł斦쪃⡲ퟛƨ蒳憸幒桪ᔐ훨ꖨ릯鼵嗐" != "ł斦쪃⡲<d7db>ƨ蒳憸幒桪ᔐ훨<a5a8>릯鼵嗐"

Please note that I have tried this on remix-solidity.


pragma solidity ^0.4.0;
contract Contract {
    string str;

    function set(string val) public {
        str = val;

    function get() public constant returns (string) {
        return str;

Please also not that we can provide string's IPFS-hash, but still I want to compress the IPFS-hash which will bring the string size from 46 to 19, this will save nearly around more than half memory.

So possible approach would be:

Large File → IPFS-hash (46 characters) → compress base64(IPFS-hash) (19 characters)

[Q] How should I pass compressed base64 string into a smart-contract? or basically what should I do to store my string compressed version on a smart-contract that when I returned it, it should match with the string I passed?

Thank you for your valuable time and help.

  • 8.4k
  • 11
  • 64
  • 154