Blockchain code Metaverse VR

Crypto×VR×SmartContract(396)

スポンサーリンク

//tips

//smart contract

campaign.test.jsの作成。これでcampaignコントラクトがきちんと機能するかをテストする。

const assert=require('assert');
const ganache=require('ganache-cli');
const Web3=require('web3');

const web3=new Web3(ganache.provider());

//コンパイルしたabiにアクセス
const compiledFactory=require('../ethereum/build/CampaignFactory.json');
const compiledCampaign=require('../ethereum/build/Campaign.json');

let accounts;//local ganacheネットワークのもの
let factory;
let campaignAddress;
let campaign;

beforeEach(async()=>{
accounts= await web3.eth.getAccounts();
//contractのconstructoerはweb3.eth.の一部
//インターフェースとなるabiのcompiledFactoryを使用してネットワークに接続
factory=await new web3.eth.Contract(JSON.parse(compiledFactory.interface))//これ自体ではまだインスタンス化されていないjsオブジェクト
.deploy({data:compiledFactory.bytecode})
.send({from:accounts[0],gas:1000000});

//同時にcampaignインスタンス生成

await factory.methods.createCampaign('100').send({
from:accounts[0],
gas:'1000000'
});//基本的にはsendによるtransactionはトランザクション番号しか返さない
//生成campaignのアドレスを整理、管理を行う

[campaignAddress]=await factory.methods.getDeployedCampaigns().call();//最初のエレメントを抽出
//campaignAddress=addresses[0]

campaign=await new web3.eth.Contract(
JSON.parse(compiledCampaign.interface),
campaignAddress//すでにdeployされているので.send()する必要がない
);

})

describe('Campaigns',()=>{
it('deploys a factory and a campaign',()=>{
assert.ok(factory.options.address);
assert.ok(campaign.options.address);

})

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);
});

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')
});

await campaign.methods
.createRequest('A',web3.utils.toWei('5','ether'),accounts[1])//requestの作成
.send({
from:accounts[0],gas:'1000000'
})

await campaign.methods.approveRequest(0).send({//requestの承認
from:accounts[0],
gas:'1000000'

});

await campaign.methods.finalizeRequest(0).send({
from:accounts[0],
gas:'1000000'
});
let balance=await web3.eth.getBalance(accounts[1]);
balance=web3.utils.fromWei(balance,'ether');
balance=parseFloat(balance);

assert(balance>104);
//ただ、ここではaccounts[1]にどれほど残高があるのかの正確な認識ができないので
//良いテストとは言えない

});

});

これらのテストが成功したので、ここからは実際のデプロイに移っていく。deploy.jsはlotteryのものと同じで良い。

早速 npm install @truffle/hdwallet-providerを実行し、同様の環境を整える。

人気の記事

1

皆さん、ついに、エアラインでも、サブスクリプションが始まったのはご存じですか? まだ実験段階ですが、ANAが、定額全国住み放題サービスを提供する「ADDress」と組んで、国内線を4回まで定額利用可能 ...

2

無料でネットショップを開けるアプリとして多くの人に驚きを与えたBASE株式会社が、2019年10月25日東証マザーズに上場しました。2020年2月時点で90万店を超えるショップを抱えるまでに成長してい ...

3

2011年にサービスを開始してから圧倒的な成長率を誇るインテリア通販サイト 【FLYMEe/フライミー】を皆さんご存じでしょうか。 「自分のイメージするインテリア、本当に欲しいインテリアがどこにあるの ...

4

ついに、noteの月間アクティブユーザー数が4400万人(2020年3月時点)に到達しました。 そもそも、「note」とは、クリエイターが、文章やマンガ、写真、音声を投稿することができ、ユーザーはその ...

5

ボードゲームカフェが1日2回転で儲かるという記事をみつけたので興味を持ち、調べてみました。 まずは、需要がどれくらいあるのか、市場のようすからみていきましょう。 世界最大のボードゲーム市場はドイツで、 ...

-Blockchain, code, Metaverse, VR
-, ,

Copyright© BUSINESS HUNTER , 2023 All Rights Reserved Powered by AFFINGER5.