DEV Community

Cover image for Hola mundo! En Optimism [ Optimistic Rollups ]
Ahmed Castro
Ahmed Castro

Posted on

Hola mundo! En Optimism [ Optimistic Rollups ]

Optimism es una solución de Layer 2 para Ethereum que busca solucionar problemas de escalabilidad. En Optimism podemos gozar de transacciones mas rápidas y mas baratas conservando siempre a Layer 1 (Ethereum mainnet) como proveedor de seguirdad. En este tutorial lanzaremos un "Hola mundo!" en Solidity mediante Hardhat en un blockchain local en nuestra computadora. Usaremos la Optimistic Virtual Machine (OVM) en vez de la Ethereum Virtual Machine para compilar nuestro contrato.

Antes de iniciar, asegurate de tener instalado node, docker y docker compose.

1. Dependencias

npm install -g yarn hardhat
git clone https://github.com/ethereum-optimism/optimism.git
cd optimism
yarn install
yarn build
Enter fullscreen mode Exit fullscreen mode

2. Optimism corriendo local

Esta parte tarda bastante así que debemos ser pacientes.

cd ops
export COMPOSE_DOCKER_CLI_BUILD=1
export DOCKER_BUILDKIT=1
sudo docker-compose build && echo Build complete
sudo docker-compose up
Enter fullscreen mode Exit fullscreen mode

3. Proyecto de Hardat

Creamos un proyecto de Hardhat como cualquier otro con la diferencia que usaremos la Optimism Virtual Machin (OVM) en vez de la Ethereum Virtual Machine (EVM).

mkdir MyHardhatProject
cd MyHardhatProject
npx hardhat
yarn add @eth-optimism/hardhat-ovm
Enter fullscreen mode Exit fullscreen mode

contracts/MyContract.sol

// SPDX-License-Identifier: MIT

pragma solidity 0.7.6;

contract MyContract {
    string public hello;

    constructor()
    {
        hello = "Hola mundo!";
    }

    function setHello(string memory _hello) public {
        hello = _hello;
    }
}
Enter fullscreen mode Exit fullscreen mode

hardhat.config.js

require("@nomiclabs/hardhat-waffle")
require('@eth-optimism/hardhat-ovm')

task("accounts", "Prints the list of accounts", async () => {
  const accounts = await ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});

module.exports = {
  solidity: "0.7.6",
  networks: {
    optimistic: {
       url: 'http://127.0.0.1:8545',
       accounts: { mnemonic: 'test test test test test test test test test test test junk' },
       gasPrice: 15000000,          
       ovm: true // This sets the network as using the ovm and ensure contract will be compiled against that.
    }
  }
};
Enter fullscreen mode Exit fullscreen mode

4. Compilar, deployar y probar

Compilamos.

npx hardhat compile
Enter fullscreen mode Exit fullscreen mode

Ingresamos en la consola.

npx hardhat console
Enter fullscreen mode Exit fullscreen mode

Deployamos.

const MyContract = await ethers.getContractFactory("MyContract")
const my_contract = await MyContract.deploy()
await my_contract.deployed()
Enter fullscreen mode Exit fullscreen mode

Y probamos nuestro contrato.

await my_contract.hello()
await my_contract.setHello("Probando...")
await my_contract.hello()
Enter fullscreen mode Exit fullscreen mode

Documentación oficial

Gracias por ver este tutorial!

Sígueme en dev.to y en Youtube para todo lo relacionado al desarrollo en Blockchain en Español.

Oldest comments (0)