BLOCKCHAIN/Ethereum

[Ethereum] Ethereum Project Infrastructure

2022. 9. 9. 20:02
목차
  1. 1 Single Run Compilation
  2. 2 Test File Setup

1 Single Run Compilation

 

2 Test File Setup

const assert = require("assert");
const ganache = require("ganache-cli");
const Web3 = require("web3");
const web3 = new Web3(ganache.provider());

/* 컴파일과 컴파일펙토리에 대한 각각의 빌드 */
const compiledFactory = require("../ethereum/build/CampaignFactory.json");
const compiledCampaign = require("../ethereum/build/Campaign.json");

let accounts;
let factory;
let campaignAddress;
let campaign;

beforeEach(async () => {
  /* 계정 목록 */
  accounts = await web3.eth.getAccounts();

  /* 팩토리 계약 인스턴스 배포, JSON으로 파싱 */
  factory = await new web3.eth.Contract(JSON.parse(compiledFactory.interface))
    .deploy({ data: compiledFactory.bytecode })
    .send({ from: accounts[0], gas: '1000000' });

  /* 데이터를 수정하기 위한 트랜잭션 보내기 - Factory 없이는 결과를 return 받지 못함 */
  await factory.methods.createCampaign('100').send({
    /* accounts가 0인 계정은 관리자 계정*/
    from: accounts[0],
    gas: '1000000',
  });

  /* 실제 인스턴스 생성, 위까진 캠페인이 로컬 블록체인에 존재 */
  [campaignAddress] = await factory.methods.getDeployedCampaigns().call();
  campaign = await new web3.eth.Contract(
    JSON.parse(compiledCampaign.interface),
    campaignAddress
  );
});

/* json 파일의 test */
describe("Campaigns", () => {
  it("deploys a factory and a campaign", () => {
    assert.ok(factory.options.address);
    assert.ok(campaign.options.address);
  });

/* 매핑에 접근, 캠패인에서 manager 메서드를 호출하면 주소 비교 */
  it("marks caller as the campaign manager", async() => {
    const manager = await campaign.methods.manager().call();
    assert.equal(accounts[0], manager);
  });

  /* 기여자가 기여할 수 있게 하기위한 확인 */
  it("allows people to contribute money and marks them as approvers", async() => {
    await campaign.methods.contribute().send({
      value: "200",
      from: accounts[1],
    });
    const isContributor = await campaign.methods.approvers(accounts[1]).call();
    assert(isContributor);
  });

  /* Minimum에 대한 확인 */
  it("requires a minimum contribution", async() => {
    try {
      await campaign.methods.contribute().send({
        value: "5",
        from: accounts[1],
      });
      assert(false);
    } catch (err) {
      assert(err);
    }
  });

  it("allows a manager to make a payment request", async () => {
    await campaign.methods
      .createRequest("Buy batteries", '100', accounts[1])
      .send({
        from: accounts[0],
        gas: '1000000',
      });
    const request = await campaign.methods.requests(0).call();

    assert.equal("Buy batteries", request.description);
  });

  it("processes requests", async () => {
    await campaign.methods.contribute().send({
      from: accounts[0],
      value: web3.utils.toWei('10', "ether"),
    });

    /* 10ehter를 수령했을 때 request를 생성 */
    await campaign.methods
    /* 송금에 적절한 금액을 설정 - 5ether */
      .createRequest("A", web3.utils.toWei("5", "ether"), accounts[1])
      .send({ from: accounts[0], gas: '1000000' });

      /* 요청 승인 */
    await campaign.methods.approveRequest(0).send({
      from: accounts[0],
      gas: "1000000",
    });

    /* 실제 요청 완료 */
    await campaign.methods.finalizeRequest(0).send({
      from: accounts[0],
      gas: "1000000",
    });

    /* web3에서 모든 계정을 검색 + 현재 잔액 */
    let balance = await web3.eth.getBalance(accounts[1]);
    balance = web3.utils.fromWei(balance, "ether");
    /* balnce 값을 Float로 변환 */
    balance = parseFloat(balance);
    console.log(balance);
    assert(balance > 104);
  });
});

 

 

 

  1. 1 Single Run Compilation
  2. 2 Test File Setup
'BLOCKCHAIN/Ethereum' 카테고리의 다른 글
  • [Ethereum] Advanced Multi-Page Front-Ends (2)
  • [Ethereum] Advanced Multi-Page Front-Ends (1)
  • [Ethereum] Real Projects with Ethereum (2)
  • [Ethereum] Real Projects with Ethereum (1)
yuujoeng
yuujoeng
IT and Information Security
yuujoeng
알감자는 공부중
yuujoeng
전체
오늘
어제
  • 🎁 (201)
    • SECURITY (80)
      • 관리보안 (27)
      • System (10)
      • System | Wargame (30)
      • Android (9)
      • Reversing (3)
      • AWS (1)
    • BLOCKCHAIN (45)
      • BlockChain (22)
      • Ethereum (23)
    • PROGRAMMING (30)
      • Web (16)
      • Android (6)
      • Spring (8)
    • IT (0)
      • Article (40)
      • RaspberryPi (5)

블로그 메뉴

  • HOME
  • TIKKLE

인기 글

hELLO · Designed By 정상우.
yuujoeng
[Ethereum] Ethereum Project Infrastructure
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.