Moralis-Resolve API || IPFS API ||Utils API
Resolve API
The Resolve API is a tool provided by Moralis that helps developers retrieve information about blockchain addresses and transactions. When you use the Resolve API, it searches different data sources to find information like the owner of an address, the number of tokens in an address, and when a transaction took place. This information is useful for building blockchain applications. For example, it can help you track the ownership of assets or monitor the activity of specific addresses. The Resolve API also supports resolving domain names to blockchain addresses, which can help create decentralized applications that use human-readable names instead of long addresses.
Resolving ENS domain
The resolveENSDomain() function takes a single parameter, the ENS domain name you want to resolve, and returns a Promise that resolves to the corresponding blockchain address. For example, if you call resolveENSDomain('myname.eth'), it will return a Promise that resolves to the Ethereum address associated with the "myname.eth" domain name.const response = await Moralis.EvmApi.resolve.resolveENSDomain({ domain, });
So basically it will return the address associated with that domain name "aaronRebelo.eth".
Now what if you have the address, and wish to find the domain name to display on your frontend application? no problem Moralis has got you covered.
const response = await Moralis.EvmApi.resolve.resolveAddress({ address, });
IPFS API
The IPFS API enables you to easily upload and get your data pinned automatically to IPFS. With IPFS API, your data will be stored in a decentralized manner, which makes it resistant to tampering and censorship.
If I want to add an image to IPFS, I'll use the below syntax
const fs = require('fs');
const Moralis = require('moralis/node');
const imageBuffer = fs.readFileSync('my-image.png');
const ipfsResponse = await Moralis.EvmApi.ipfs.add(imageBuffer);
const ipfsHash = ipfsResponse.cid.toString();
console.log(`Image uploaded to IPFS with hash: ${ipfsHash}`);
now if I want to add the metadata along with the image I'll first upload the image, get the hash of the uploaded image, and after that, I'll add it to the metadata. following this I'll upload the metadata to the the IPFS and get the new hash value, I'll later on add this hash to the contract.
Utils API
Now even though we've played around with all of this, I'd still like to know how to call the read/write functions on a given smart contract.
runContractFunction()
method is a powerful tool that allows you to interact with smart contracts in a simple and intuitive way, regardless of your level of experience with Ethereum development.
Reading from the contract
const response = await Moralis.EvmApi.utils.runContractFunction({
address,
functionName, //function name you want to call
abi, //whole ABI of the contract
chain,
params: {
"InputName":value
}
});
. These types of functions are responsible for interacting with the “read” functions of smart contracts. Essentially, these are read-only functions that do not involve the process of sending transactions. Thus, you can call them without executing a blockchain transaction. Moreover, since these functions do not involve on-chain transactions, they don’t modify the state of the blockchain (contract’s storage). Hence, we use “call” when we want to read contract data. So, a Web3 JS call contract function can be used to trigger smart contracts’ “read” functions to fetch specific storage variables of the deployed smart contract.