基于区块链的毕业设计Ethanol – 乙醇

本文提供基于区块链的毕业设计国外最新区块链项目源码下载,包括solidity,eth,fabric等blockchain区块链,基于区块链的毕业设计Ethanol – 乙醇 是一篇很好的国外资料

Ethanol

Humane JS wrappers around web3 and solc.

Defining the RPC endpoint

The work starts with getting a Blockchain object:

const { Blockchain } = require("ethanol"); const bc = Blockchain.at("http://localhost:8545");

The argument to the at function is the URL of an RPC node representing the blockchain. Besides the http and https schemas the ipc schema can also be used:

const bc = Blockchain.at("ipc:///home/john/geth/geth.ipc");

The blockchain provides several query functions:

const latestBlockNumber = await bc.blockNumber(); const firstBlock = await bc.block(1); const someBalance = await bc.balanceOf( 	"0x5e94baef74b60e98116b971e9240d914f4059e27" );

Users

Because any transaction assumes a sender account, a user object is needed for most of the tasks. A default user can be obtained with the user function of a blockchain:

const alice = await bc.user();

If the blockchain is connected via IPC, then the user embedded in the local client (for example, geth) will be assumed. The local user is typically an unlocked account that can send signed transactions.

If the interface is HTTP, then a user with a hardcoded mnemonic will be assumed. A user with a specific BIP44 mnemonic can be obtained using the userFromMnemonic function:

const { Blockchain } = require("ethanol"); const bc = Blockchain.at("http://localhost:8545"); const bob = await bc.userFromMnemonic("science truck gospel ...", 0);

The second argument is optional and defines the index in the sequence of private keys generated by the mnemonic. The mnemonic and the index define the private key.

Users can report their address and balance, and transfer Ether (in Wei units):

const { Blockchain } = require("ethanol"); const bc = Blockchain.at("http://localhost:8545");  const alice = await bc.userFromMnemonic( 	"eeny meeny miny moe catch a tiger by the toe" ); const bob = await bc.user();  const address = alice.address(); const balance = await alice.balance();  // Give 10 Wei to Bob const transaction = await alice.give(bob, 10);

Transactions

Transaction objects are returned for plain ether transfers, and also for contract calls and deployments.

const transaction1 = await alice.give(bob, 10); const transaction2 = await alice.call(contract, "funcName");

The transaction’s success function returns a promise which resolves when the transaction is mined successfully or rejects when the transaction is rejected:

const transaction = await alice.call(contract, "funcName"); try { 	await transaction.success(); } catch (error) { 	console.log("transaction failed:", error); }

An often used shorthand for making a transaction and waiting for its completion is:

await alice.give(bob, 1).then(t => t.success());

This additional step has to be explicit to allow an application choose whether to wait or not for the actual result.

Calling contracts

In order to work with a contract one needs to know its ABI (which is typically saved as JSON) and its address. The contract function on a blockchain object returns a “deployed contract” object.

const abi = JSON.parse(fs.readFileSync("contract.abi")); const address = "0x1234...";  const contract = bc.contract(abi, address);

Only a user can call a contract function, so a user object is required. User instances have read and call methods to statically read the contract’s variables, make dry function runs or make actual calls:

const val = await alice.read(contract, "variableName"); const result = await alice.read(contract, "functionName", ["arg1", "arg2"]); const transaction = await alice.call(contract, "functionName");

The result of the read promise is the value of the variable being read or the return value of the function. The result of the call promise is a contract call transaction which, like other transactions, has the success function:

const transaction = await alice.call(contract, "funcName"); try { 	await transaction.success(); } catch (error) { 	// transaction failed }

In addition to the generic success promise, call transactions can return the contract’s event logs for that particular call:

const transaction = await alice.call(contract, "funcName"); try { 	const logs = await transaction.logs(); } catch (error) { 	// transaction failed }

Obtaining logs assumes waiting for the transaction to be mined, so it’s not necessary to await for transaction.success in this case.

A transaction can also be obtained from a contract using the transaction’s hash value.

const transaction = await contract.transaction("0x1234abcd...");

The contract is necessary because it provides context (like an ABI definition) to the transaction so that the logs function will work. The transaction function will also verify that the transaction exists and belongs to the same contract.

Getting contract event logs

Given a deployed contract object, it’s possible to request its logs for a given range of block numbers:

const contract = bc.contract(abi, address); const block = await bc.blockNumber(); const logs = await contract.history("ContractEventName", block - 100, block);

Even item is an instance of the ContractEvent object which has the name method that has the event name and also the values method that returns the event’s arguments as an object:

for (const event of logs) { 	console.log(`event ${event.name()} with values ${event.values}`); }

Deploying contracts

A contract image is a combination of abi and bin properties which have the corresponding outputs of a Solidity compiler. It will usually be obtained from files:

const abi = fs.readFileSync("hello-world.abi"); const bin = fs.readFileSync("hello-world.bin"); const image = { abi, bin };

Given an existing contract image, it can be deployed from a user’s account:

const transaction = await user.deploy(image, [arg1, arg2]); const contract = await transaction.contract();

The deploy function accepts a list of arguments which will be passed to the contract’s constructor. The returned promise will turn into a DeployedContract instance which can be used as usual.

Building contracts

A contract can be compiled using the Compiler object:

const { Compiler } = require("ethanol"); const com = new Compiler(); const image = await com.compile("hello-world.sol");

The Compiler object will call the host system’s solc compiler through the command line.

The constructor takes a map of compiler options which exactly correspond to command line options of the solc compiler. For example, to get binaries compatible with older versions of the blockchain:

const com = new Compiler({ "evm-version": "spuriousDragon" }); const image = await com.compile("hello-world.sol");

乙醇

关于web3和solc的人道JS包装。

定义RPC端点

工作从获取区块链blockchain对象开始:

const { Blockchain } = require("ethanol"); const bc = Blockchain.at("http://localhost:8545");

at函数的参数是表示区块链blockchain的RPC节点的URL。除了http和https模式之外,ipc模式也可以使用:

const bc = Blockchain.at("ipc:///home/john/geth/geth.ipc");

区块链blockchain提供了几个查询功能:

const latestBlockNumber = await bc.blockNumber(); const firstBlock = await bc.block(1); const someBalance = await bc.balanceOf( 	"0x5e94baef74b60e98116b971e9240d914f4059e27" );

用户

因为任何交易都假设一个发送方账户,所以大多数任务都需要一个用户对象。通过区块链blockchain的用户功能可以获得一个默认用户:

const alice = await bc.user();

如果区块链blockchain通过IPC连接,则假定嵌入本地客户端(例如geth)的用户。本地用户通常是一个未锁定的帐户,可以发送签名的事务。

如果接口是HTTP,则假定用户具有硬编码助记符。具有特定BIP44助记符的用户可以使用userfrom助记符函数获得:

const { Blockchain } = require("ethanol"); const bc = Blockchain.at("http://localhost:8545"); const bob = await bc.userFromMnemonic("science truck gospel ...", 0);

第二个参数是可选的,它在助记符生成的私钥序列中定义索引。助记符和索引定义私钥。

用户可以报告其地址和余额,并传输以太(以Wei为单位):

const { Blockchain } = require("ethanol"); const bc = Blockchain.at("http://localhost:8545");  const alice = await bc.userFromMnemonic( 	"eeny meeny miny moe catch a tiger by the toe" ); const bob = await bc.user();  const address = alice.address(); const balance = await alice.balance();  // Give 10 Wei to Bob const transaction = await alice.give(bob, 10);

事务

对于纯以太传输,以及对于合同调用和部署,返回事务对象。

const transaction1 = await alice.give(bob, 10); const transaction2 = await alice.call(contract, "funcName");

事务的success函数返回一个promise,该承诺在事务被成功挖掘时解析,或者在事务被拒绝时拒绝:

const transaction = await alice.call(contract, "funcName"); try { 	await transaction.success(); } catch (error) { 	console.log("transaction failed:", error); }

一个通常用于创建事务并等待其完成的速记是:

await alice.give(bob, 1).then(t => t.success());

此附加步骤必须显式,以允许应用程序选择是否等待或不等待实际结果。

调用合同

为了处理合同,需要知道它的ABI(通常保存为JSON)和地址。区块链blockchain对象上的契约函数返回“部署的契约”对象。

const abi = JSON.parse(fs.readFileSync("contract.abi")); const address = "0x1234...";  const contract = bc.contract(abi, address);

只有用户可以调用协定函数,因此需要用户对象。用户实例具有read和call方法来静态地读取契约的变量、运行dry函数或进行实际调用:

const val = await alice.read(contract, "variableName"); const result = await alice.read(contract, "functionName", ["arg1", "arg2"]); const transaction = await alice.call(contract, "functionName");

read promise的结果是正在读取的变量的值或函数的返回值。调用承诺的结果是一个契约调用事务,与其他事务一样,它具有成功函数:

const transaction = await alice.call(contract, "funcName"); try { 	await transaction.success(); } catch (error) { 	// transaction failed }

除了一般的成功承诺之外,调用事务还可以返回该特定调用的契约的事件日志:

const transaction = await alice.call(contract, "funcName"); try { 	const logs = await transaction.logs(); } catch (error) { 	// transaction failed }

获取日志假定等待事务被挖掘,所以没必要等待事务处理成功在这种情况下。

也可以使用事务的哈希值从合约中获取事务。

const transaction = await contract.transaction("0x1234abcd...");

契约是必要的,因为它为事务提供了上下文(如ABI定义),以便logs函数可以工作。事务函数还将验证事务是否存在并属于同一个合同。

获取合同事件日志

给定一个已部署的contract对象,它可以请求给定块编号范围的日志:

const contract = bc.contract(abi, address); const block = await bc.blockNumber(); const logs = await contract.history("ContractEventName", block - 100, block);

Even item是ContractEvent对象的一个实例,该对象的name方法具有事件名称,并且values方法将事件的参数作为对象返回:

for (const event of logs) { 	console.log(`event ${event.name()} with values ${event.values}`); }

部署合同

契约映像是具有Solidity编译器相应输出的abi和bin属性。它通常从文件中获取:

const abi = fs.readFileSync("hello-world.abi"); const bin = fs.readFileSync("hello-world.bin"); const image = { abi, bin };

给定一个现有的合同映像,它可以从用户的帐户进行部署:

const transaction = await user.deploy(image, [arg1, arg2]); const contract = await transaction.contract();

deploy函数接受一个参数列表,这些参数将传递给契约的构造函数。返回的promise将变成一个可以正常使用的DeployedContract实例。

构建合同

可以使用编译器对象编译协定:

const { Compiler } = require("ethanol"); const com = new Compiler(); const image = await com.compile("hello-world.sol");

编译器对象将通过命令行调用主机系统的solc编译器。

构造函数获取与solc编译器的命令行选项完全对应的编译器选项映射。例如,要获得与区块链blockchain旧版本兼容的二进制文件:

const com = new Compiler({ "evm-version": "spuriousDragon" }); const image = await com.compile("hello-world.sol");

部分转自网络,侵权联系删除区块链源码网

www.interchains.cc

https://www.interchains.cc/17711.html

区块链毕设网(www.interchains.cc)全网最靠谱的原创区块链毕设代做网站 部分资料来自网络,侵权联系删除! 最全最大的区块链源码站 !
区块链知识分享网, 以太坊dapp资源网, 区块链教程, fabric教程下载, 区块链书籍下载, 区块链资料下载, 区块链视频教程下载, 区块链基础教程, 区块链入门教程, 区块链资源 » 基于区块链的毕业设计Ethanol – 乙醇

提供最优质的资源集合

立即查看 了解详情