You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

1 Días
1 Hrs
52 Min
59 Seg

Compilando y desplegando contratos inteligentes

17/27
Resources

The process of developing a contract, going through the necessary configurations and testing of the contract, ends with the deployment of the contract to the desired blockchain.

Contract deployment with Hardhat

To deploy your contract, you will need to compile it. Hardhat also helps us with that thanks to the npx hardhat compile command. You will see that it creates several directories in your project with the bytecode of the developed contract. Remember that EVM doesn't understand anything about Solidity, contracts must be compiled to be interpreted correctly with the Ethereum network.

In addition to the contract bytecode, you will find a file called Application Binary Interface or ABI. The ABI, is a JSON that we can use from any client to know how to interact with the contract. What methods it has, what parameters it receives, etc. In the next classes, we will use it to type the information and communicate with the smart contract deployed on the blockchain.

After compiling your contract, configuring the hardhat.config.js file and developing the deployment script with Hardhat, the command npx hardhat run scripts/deploy.js --network goerli will be in charge of deploying the contract, in this case, in Goerli.

You will notice in the development console that, after the successful launch of the contract, it delivers a completely unique address that you will use to locate your smart contract on the blockchain in question where it has been deployed.

Each blockchain, whether it is the mainnet or a test network, will have its own browser that will allow you to visualize and get more information about what is happening on the network. By using Goerli's explorer, plus the address of the contract, you will be able to visualize the status of the contract and corroborate its correct deployment and all the transactions that interact with it.

Knowing how to use blockchain explorers will be key to become a great web developer3.

To this point, it only remains to congratulate you for having developed and deployed your first smart contract with Hardhat.


Contributed by: Kevin Fiorentino (Platzi Contributor).

Contributions 14

Questions 5

Sort by:

Want to see more contributions, questions and answers from the community?

En las ultimas versiones de hardhat con el código de despliegue que dan en este capitulo saca error. TypeError: no matching function (argument=“key”, value=“address”, code=INVALID_ARGUMENT, version=6.6.1)

Para arreglarlo hay que cambiar en deploy.js en la linea 9 counter.address por counter.getAddress()

Tomar en cuenta que la red de Goerli esta deprecada al dia de hoy (marzo 2023), por lo que se recomienda utilizar la red de Sepolia que funciona de la misma manera.

npx hardhat compile
npx hardhat run scripts/deploy.js --network sepolia

Cuando te devuelva la dirección del contrato, puedes validarlo en etherscan:
https://sepolia.etherscan.io/

Comandos importantes de esta clase:

  • npx hardhat compile

  • npx hardhat run scripts/deploy.js --network goerli

Si obtienes el error:

TypeError: (intermediate value) is not iterable
at main (/Users/…/scripts/deploy.js:4:22)
at processTicksAndRejections (node:internal/process/task_queues:95:5)

.

El error es en el archivo deploy.js, en la línea const [deployer] = await ethers.getSigner()

Se debe cambiar por const deployer = await ethers.getSigner()

Espero puedan agregar una clase usando Sepolia

Deploy en TS: ```js import { ethers } from 'hardhat' async function main() { const [ deployer ] = await ethers.getSigners() console.log('deployer', deployer) const Counter = await ethers.getContractFactory("Counter") const counter = await Counter.deploy(0) const contractAddress = await counter.getAddress() console.log('Counter Contract Address:', contractAddress) } main() .then(() => process.exit(0)) .catch((error) => { console.error(error) process.exit(1) } ) ```

Dejo mis apuntes, más el codigo de la clase.

Agosto 2024: Es necesario agregar en deploy.js una promera de esperar la direccion para que no quede <\<pending>> agrego el codigo como debe quedar: ```js const { ethers } = require('hardhat'); async function main() { const [deployer] = await ethers.getSigners(); console.log('Deployer', deployer); const Counter = await ethers.getContractFactory('Counter'); const counter = await Counter.deploy(0); const contractAddress = await counter.getAddress(); console.log('Counter Contract Address:', contractAddress); } main() .then(() => process.exit(0)) .catch(error => { console.error(error); process.exit(1) }); ```const { ethers } = require('hardhat'); async function main() {    const \[deployer] = await ethers.getSigners();    console.log('Deployer', deployer);     const Counter = await ethers.getContractFactory('Counter');    const counter = await Counter.deploy(0);     const contractAddress = await counter.getAddress();    console.log('Counter Contract Address:', contractAddress);} main()    .then(() => process.exit(0))    .catch(error => {        console.error(error);        process.exit(1)    });
A dia de hoy es mas complicado conseguir eth en sepolia :(
Una pregunta en cuanto a la parte del testing. No estoy seguro de en qué parte entre la compilación y el despliegue, se ejecuta el código de la carpeta test. Fui a buscar la documentación pero salí aún más confundido.
Buenos dias, se puede especificar un address en la configuracion de hardhat? Estoy intentando desplegar a testnet de celo pero me sale que la dir que despliega es otra que no tiene para pagar gas, también hice pruebas con ganache y si despliega bien pero me gustaría especificarle con cual address de todas debe desplegar. Es posible esto? Saludos.

Para los que usan ubuntu y les da error por la versión de node que usan les dejo este enlace para que puedan actualizarla

https://www.stewright.me/2023/04/install-nodejs-18-on-ubuntu-22-04/

En sepolia:

require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.18",
  networks: {
    sepolia: {
      url: " TU URL ",
      accounts: [" TU LLAVE PRIVADA "],
    }
  },
};