I want to store about 4MB of data on the IPFS.
I then want to create a smart contract that can access the data or a subset of the data on the IPFS.
I know that data already stored on the Ethereum blockchain casts no gas to access.
But what if my data is stored immutably on the IPFS and then accessed via a smart contract call; will it cost any gas?
If so, how much gas will it cost (a ratio like USD/MB-Retrieved would be nice).
-
Reading data costs no gas for an off-chain client. But when you want to do it in a smart contract, it will cost gas, too.– mafrasi2Mar 26, 2018 at 4:17
-
1Reading data anywhere does not cost gas. Smart contracts also allow public variable to be read without gas costs.– AyushyaMar 26, 2018 at 4:40
3 Answers
You cannot directly access IPFS data from a smartcontract. If you store file hash, it will cost you gas as this opcode list states https://docs.google.com/spreadsheets/d/1m89CVujrQe5LAFJ8-YAUCcNK950dUzMQPMJBxRtGCqs/edit#gid=0
But you can access IPFS source from a smart contract via oraclize provider, and yet it will charge you for each such transaction.
There is no cost associated to access data stored on the IPFS with a smart contract. However, you will have to pay if you want to store data on contract in the first place. Lets see how we are going to access our data:
- Store the data on IPFS. Get the hash/identifier of the file we stored.
- Store that hash/identifier in smart contract. This will cost gas.
- Get data from smart contract. This will not cost any gas.
- Use this hash in a call to IPFS api to get the data of file.
Note: You cannot directly access any file on IPFS.
-
First you say "I do not think that there would be any cost associated" and then you give instructions which cost gas. Hmm? Mar 26, 2018 at 7:50
-
Yes, for answering OP's question, there is no gas cost at reading step.– AyushyaMar 26, 2018 at 7:57
-
I have updated the answer to lay emphasis on "no cost to access data of smart contract"– AyushyaMar 26, 2018 at 8:16
IPFS is not a miracle worker. It is simply an external provider which is 'convenient' to use with Ethereum smart contracts.
This means you have to integrate the actual logic in an old-fashioned backend system. Ethereum blockchain (smart contract) can contain hashes for the IPFS files, so the contract will basically just have pointers to the files but no direct access to the files.
There is no USD/MB-Retrieved ratio as you can store whatever size files in IPFS and the hash will be always the same size. The only difference is the IPFS usage costs, but that has nothing to do with Ethereum per se.
The gas cost for using IPFS is the cost of storing the hash and whatever else your contract needs.