Skip to main content

Set Up Your Local Environment

One of the most common questions we get asked is what development toolset to use to build on-chain integrations with the Media Protocol. There’s no right answer to this question but for this guide we’ll recommend a common one: Node.js , NPM and Hardhat.

At the end of this guide you’ll have a development environment set up that you can use to build the rest of the examples in the Guides section of the docs, or start your own integration project!

In the following sections, we’ll walk through the steps to create the a development environment from scratch and learn the underlying concepts.

Set Up Dependencies

Node is one of the most common Javascript runtimes. For our purposes it will provide scripting we can use to compile and test our contracts. If you haven’t already, install NodeJS and its package manager NPM (instructions). Once those dependencies are set up, we can initialize our project:

$ npm init

Hardhat is an Ethereum development toolset that provides a number of powerful features including Solidity compilation, testing and deployment, all in a single convenient wrapper. We’ll use NPM to add Hardhat to our project:

$ npm add --save-dev hardhat

With Hardhat installed we can invoke it to scaffold our development environment. When you first run Hardhat you’ll have the option of starting with a templated Javascript or Typescript project or an empty project. Since Hardhat relies heavily on folder structure, we recommend starting with either of the templated options. Initialize Hardhat and follow the prompts to make your selection and answer yes to the follow up prompts:

$ npx hardhat init

Once the Hardhat initialization completes, take a look around at what got set up. The folder structure should be intuitive, ./contracts is where you’ll write your Solidity contracts, ./test is where you’ll write your tests and ./scripts is where you can write scripts to perform actions like deploying. Out of the box, Hardhat is configured to use this folder structure so don’t change it unless you know what you’re doing!

Next we’ll use NPM to add the Media Protocol contracts which will allow us to seamlessly integrate with the protocol in our new contracts:

$ git clone https://github.com/mediafoundation/media-protocol.git
info

Please note that this repository currently contains only the contract interfaces. The source code is not yet public due to ongoing audits. We apologize for any inconvenience this may cause and are working diligently to make it available as soon as possible.

The Media Protocol contracts were written using a specific version of the solidity compiler. Since we’re building integrations on these contracts we have to tell Hardhat to use the correct compiler to build these files. Go to the ./hardhat.config.js file and change the Solidity version to “0.8.17”:

// ...
module.exports = {
solidity: "0.8.17",
};

That’s it! You should now have a functional development environment to start building on chain Media Protocol integrations. Let’s run a quick test to confirm everything is set up properly.

Compile a Basic Contract

To confirm that our environment is configured correctly we’ll attempt to compile a basic Initialize Marketplace contract. Create a new file, ./contracts/InitializeMarketplace.sol and paste the following code into it:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "./media-protocol/contracts/interfaces/IMarketplaceStorage.sol";

contract SimpleInitializeMarketplace {
IMarketplaceStorage marketplaceStorage;
constructor(address _marketplaceAddress) {
marketplaceStorage = IMarketplaceStorage(_marketplaceStorageAddress);
}

function initializeMarketplace() external returns (uint marketplaceId) {
marketplaceId = marketplaceStorage.initializeMarketplace(
5000000000000000000, // marketplace min required liquidity
msg.sender, // marketplace owner
30000, // 3% fee
'{"name": "Test Marketplace"}' // metadata
);
}
}

To compile all the contracts in the ./contracts folder, we’ll use the Hardhat compile command:

$ npx hardhat compile

If the environment is compiled correctly you should see the message:

Compiled { x } Solidity files successfully

Local Node with a Mainnet Fork

When building and testing integrations with on chain protocols, developers often hit a problem: the liquidity on the live chain is critical to thoroughly testing their code but testing against a live network like Mainnet can be extremely expensive.

Luckily, Hardhat has a powerful feature that allows developers to run a local Ethereum test node that uses a fork of Mainnet. This allows us to test against simulated offers for free.

As of now, the Media Protocol contracts are deployed only on Goerli, Base Goerli, and LaTestnet. To test against these contracts, you will need to fork one of these networks. Forking a network requires an RPC endpoint that supports Forking. Here's a free Goerli Testnet RPC provided by Ankr:

You can use this RPC endpoint to start your Hardhat node with the following command:

$ npx hardhat node --fork https://rpc.ankr.com/eth_goerli

With your local node up and running, you can use the --network localhost flag in tests to point the Hardhat testing suite to that local node:

$ npx hardhat test --network localhost

Next Steps

With your environment set up you’re ready to start building. Jump over to the guides section to learn about the Media Protocol functions you can integrate with.