hardhat 실습 2

유지민·2022년 12월 22일
0

hardhat

목록 보기
2/3

앞에 두개는 베포된게 아님 (주소값만 나옴)

이더리움에서 논스는 거래를 일으킨 숫자

거래가 0이라서 처음두번은 주소만드는 환경값이 똑같으니까 주소값이 같게 나옴
로컬에서 베포할떄는 거래가 추가되면서 다른값이 나옴

기본적으로 하드헷은 알케미, 이더스를 사용

npx hardhat help

하드헷은 자체적으로 verify가 가능하다.

npx hardhat verify --network goerli "여러분 contract address"

npx hardhat console

ethers.provider //= 알케미같이 도와주는 애 정보
console.log(await ethers.getSigner())
//Signer() = 지갑주소

npx hardhat --network goerli console

ethers.providers

순수 터미널에서 하드헷

npx hardhat --network goerli console //네트워크 접속
const provider_ = new ethers.providers.AlchemyProvider(network="goerli", "알케미 API KEY(주소말고 키만 기입)")	//프로바이더 설정
const PVK = "개인키"
const signer = new ethers.Wallet(PVK, provider_) //계정 등록
let addr = "컨트랙트주소"
let ABI = ~~~
const contract = new ethers.Contract(addr, ABI, signer) //컨트랙트 등록

contract.estimateGas.getA().then(console.log)	//
contract.address //

contract.getA().then(console.log)
contract.setA(15).then(console.log)

await contract.getA().then(console.log)

contract.setA(25, {gasPrice : 5500000000}).then(console.log)	//가스프라이스 설정해서 거래

vscode에서 세팅& 실행

.env 만들고 (env 파일은 ; 들어가면 에러남)

//.env파일

ABI = [ { "inputs": [], "name": "getA", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "_a", "type": "uint256" } ], "name": "setA", "outputs": [], "stateMutability": "nonpayable", "type": "function" } ]
//ABI

C_Addr = "Contract 주소"
Alchemy_KEY = "Alchemy_KEY"
PVK = "사용할 지갑 개인키"

interact.js 만들고

require("dotenv").config(); // dotenv 불러오기
const ethers = require("ethers"); // ethers 불러오기

const provider = new ethers.providers.AlchemyProvider(network = "goerli",process.env.Alchemy_KEY);
const signer = new ethers.Wallet(process.env.PVK, provider);

let ABI = process.env.ABI;
let addr = process.env.C_Addr;

const contract = new ethers.Contract(addr, ABI, signer);

contract.getA().then(console.log)

contract.setA(15).then(console.log)	//하드헷은 가스비 따로 지정 안해줘도 실행 가능..

await contract.getA().then(console.log)
  • 작성 후 split 터미널에서
node interact.js

실행

profile
개발 취준생

0개의 댓글