Share – 分享区块链毕设代写

区块链毕设代写本文提供国外最新区块链项目源码下载,包括solidity,eth,fabric等blockchain区块链,Share – 分享区块链毕设代写 是一篇很好的国外资料

Share

Solidity contracts for IP copyright sharding on EVM.

Setup and Testing

We use Ganache to run a local ETH blockchain instance and Truffle for deploying contracts and interacting with the local EVM. We also use The Graph for indexing blockchain events. This requires installing a local graph node using Docker for testing.

Running the Ganache EVM Local Blockchain

ganache-cli -h 0.0.0.0 

Migrating Contracts

cd share emacs truffle-config.js 

Enable:

development: {   host: "0.0.0.0",     // Localhost (default: none)                                  port: 8545,            // Standard Ethereum port (default: none)                   network_id: "*",       // Any network (default: none)                            } 

Note that 0.0.0.0:8545 matches the Ganache address:port binding.

Note Contract Addresses

Note the contract addresses logged on deployment:

Deploying 'MockERC20'                                                                                                                                       ---------------------                                                                                                                                       > transaction hash:    0x746c8fa07f757f72d33eb3c073a52eb3d5d1766ad66c594c60b6b873267fb625 > Blocks: 0            Seconds: 0                                                                                                                        > contract address:    0x7e37293114b9C1999b121298fb307BEd7d94eFCE                                                                                        > block number:        2                                                                                                                                  > block timestamp:     1629341200                                            > account:             0xC6e905c949ea72430051d6EeEeAbFda8ebD6feF7            > balance:             99.97039534                                           > gas used:            1233341 (0x12d1bd)                                    > gas price:           20 gwei       > value sent:          0 ETH         > total cost:          0.02466682 ETH                                         Deploying 'ShareContract'            -------------------------            > transaction hash:    0xa8e4525c8593d8e2e80a4fd0bd397e2273cf85f332fa429c88bd3fce4c2396d4                                 > Blocks: 0            Seconds: 0                                            > contract address:    0x969BD42CB868125CFc4a095F61e7dc2890869916            > block number:        3             > block timestamp:     1629341200                                            > account:             0xC6e905c949ea72430051d6EeEeAbFda8ebD6feF7            > balance:             99.9569096                                            > gas used:            674287 (0xa49ef)                                      > gas price:           20 gwei       > value sent:          0 ETH         > total cost:          0.01348574 ETH                                         > Saving migration to chain.         > Saving artifacts                   -------------------------------------                                        > Total cost:           0.0430904 ETH 

During testing, these contract addresses need to be set in the share/subgraphs/un7.yaml file. TODO(brandon): Automate this.

Interacting with EVM using Truffle

Basic Example

truffle console # from a directory with the correct truffle config accounts let instance = await ContractName.deployed() instance.<method_name>() 

Running a Graph Node

The Graph is a collection of user run off chain webservers that offer to index blockchain events for future querying in exchange for an API fee. We need this in SHARE because we want to perform queries such as the number of shares owned by a user during a given time window, which determines the withdrawal cap. Within a graph node there are “subgraphs” which are essentially instructions for indexing specific contract events, deployed by a contract developer.

During development, we bring up our own graph node using Docker, and deploy a local SHARE subgraph which maps transaction events to a counter in the graph database.

Installing and launching

Download the graph node code from the graph GitHub repository. We’ll configure it to point to the local blockchain instance using the setup bash script as shown below, and then bring it up using docker. Note: Before bringing up the node with docker, open the docker-compose.yaml file and make sure that the following has been done (either by setup.sh or manually):

Change ethereum: 'mainnet:http://host.docker.internal:8545' to ethereum: 'mainnet:0.0.0.0:8545'  

Reference

git clone https://github.com/graphprotocol/graph-node/ cd graph-node/docker ./setup.sh docker-compose up 

Now your local graph node should be up and ready to index blockchain events.

Understanding Subgraphs

Subgraphs are a strange name but essentially they map to the GraphQL database paradigm. There’s one large “graph”, e.g. database, comprising of many “subgraphs”, e.g. application specific rows/entries. We define at least three entities:

  1. A Subgraph Schema –– This schema defines the data types that we care about and enables the graph toolchain to perform code generation given the types.

  2. Subgraph Mappings –– This is a Javascript (Typescript) file where functions are implemented which operate on blockchain event data and map the data to some indexing operation in GraphQL.

  3. A Subgraph Project –– This is the project which gets deployed into the graph instance and tells it where to find the schema and mappings above.

We instantiate a subgraph project using:

graph init --from-example <GITHUB_USERNAME>/<SUBGRAPH_NAME> 

And modify the resulting templates accordingly. Deploying the subgraph into the graph instance is somewhat confusing as there’s a dependency on yarn, e.g:

yarn && yarn codegen yarn create-local yarn deploy 

There is no documented explanation as to how these yarn commands have any association with the entities listed above, but somehow this magically deploys the subgraph into the local graph instance. In production, we’ll use the hosted subgraph studio to deploy our subgraph after testing.

UPDATE: It turns out that when you run:

$ graph init --from-example <GITHUB_USERNAME>/<SUBGRAPH_NAME> [<DIRECTORY>] 

A package.json file read by yarn gets created. This file defines “scripts” as follows:

{   "name": "share-testonly",   "version": "0.1.0",   "scripts": {     "build-contract": "solc contracts/Gravity.sol --abi -o abis --overwrite && solc contracts/Gravity.sol -- bin -o bin --overwrite",     "create": "graph create un7c0rn/share-testonly --node https://api.thegraph.com/deploy/",     "create-local": "graph create un7c0rn/share-testonly --node http://127.0.0.1:8020",     "codegen": "graph codegen",     "build": "graph build",     "deploy": "graph deploy un7c0rn/share-testonly --ipfs https://api.thegraph.com/ipfs/ --node https://api. thegraph.com/deploy/",     "deploy-local": "graph deploy un7c0rn/share-testonly --ipfs http://localhost:5001 --node http://127.0.0. 1:8020"   } 

Meaning that:

yarn create maps to graph create un7c0rn/share-testonly --node https://api.thegraph.com/deploy/

yarn create-local maps to graph create un7c0rn/share-testonly --node http://127.0.0.1:8020

yarn codegen maps to graph codegen

yarn build maps to graph build

yarn deploy-local maps to graph deploy un7c0rn/share-testonly --ipfs http://localhost:5001 --node http://127.0.0.1:8020

The yarn commands are being resolved using the package.json configuration file and map to graph CLI commands.

Using Code Generation

Given a subgraph schema, e.g. un7.yaml we perform code generation as follows:

graph codegen un7.yaml 

This produces a generated folder with subdirectories that include the AssemblyScript code files needed to execute our mapping.ts implementation in a deployed graph node.

Raw Commands for Deploying Subgraph

The yarn scripts can be distilled down to:

graph init share/un7 yarn add --dev @graphprotocol/graph-ts graph deploy share/un7 --ipfs http://localhost:5001 --node http://127.0.0.1:8020 un7.yaml 

Using CURL to Query Your Subgraph

Once the subgraph is deployed it can be queried using CURL as follows:

curl -X POST -H “Content-Type: application/json” -d ‘{“query” : “{gravatar(id:”0x0″){id displayName}}”}’ http://127.0.0.1:8000/subgraphs/name/un7c0rn/share-testonly

If you use truffle to interact with the contract of interest, each event will cause the index to change.

Contracts

The primary contract is a simple off-chain royalty receiver. It is meant to distribute funds generated for accessing on-chain IP which are entitled to “mechanical royalties”, e.g. audio/video with a pay-per-view or pay-per-stream model. This is independent of royalties received for sale of the underlying token.

The contract maintains the following state variables:

  1. balance (int) e.g. this contract is payable.
  2. cumulative_royalty_value (int).
  3. cumulative_withdrawal_amount (mapping addr : int).
  4. shares (mapping addr : int) (this value is derived using the ERC20 contract of the share token associated with the fractionalized non-fungible token of the IP) – it is not stored in this contract but rather computed at runtime.

Pseudocode:

_RIGHTSHOLDER_CONTRACT_ADDR = ''  function balance() { 	 return this.balance; }  function () payable { // anonymous callback to receive ETH. 	 uint256 amount = msg.value 	 cumulative_royalty_value += amount. 	 // note that _all_ funds sent to this contract are considered royalties and can only 	 // be withdrawn by copyright shareholders. }  function shares(address _shareholder) { 	 shareholders_contract = ERC20(_RIGHTSHOLDER_CONTRACT_ADDR) 	 return shareholders_contract.balanceOf(_shareholder) }  function withdraw(address _shareholder, int amount) {          shareholders_contract = ERC20(_RIGHTSHOLDER_CONTRACT_ADDR) 	 require (shareholders_contract.balanceOf(_shareholder) > 0, "You are not a shareholder."); 	 drawn = this.cumulative_withdrawal_amount[_shareholder] 	 shareholder_value = self.cumulative_royalty_value / shares(_shareholder) 	 max_draw = 0 if drawn >= (shareholder_value) else (shareholder_value - drawn) 	 require (amount < max_draw, "The amount you've requested to withdraw exceeds your available shareholder royalty value. Request a smaller amount or wait for more royalties to accrue."); 	 require (amount < self.balance, "The amount you've requested exceeds the current royalty balance on the contract. Wait for more royalties to accrue."); // in theory this shouldn't happen – double check 	 _shareholder.call{value: amount}(); 	 this.cumulative_withdrawal_amount[_shareholder] += amount; } 

ABIs

SHARE uses the NIFTEX ShardedWallet contract. It is necessary to store the ABI (Application Binary Interface) for the contract in order to interact with it from a subgraph schema. The NIFTEX ShardedWallet ABI can be found here:

https://etherscan.io/address/0x177f51a18668bdcc4d14dbf63d3248c40f427848#code

Merge pull request


共享

EVM上IP版权分片的实体合同

设置和测试

我们使用Ganache运行本地ETH区块链blockchain实例,并使用Truffle部署合同并与本地EVM交互。我们还使用该图对区块链blockchain事件进行索引。这需要使用Docker安装本地图形节点进行测试

运行Ganache EVM本地区块链blockchain

ganache-cli -h 0.0.0.0 

迁移合同

cd share emacs truffle-config.js 

启用:

development: {   host: "0.0.0.0",     // Localhost (default: none)                                  port: 8545,            // Standard Ethereum port (default: none)                   network_id: "*",       // Any network (default: none)                            } 

注意,0.0.0.0:8545与Ganache地址:端口绑定匹配

注意合同地址

注意部署中记录的契约地址:

Deploying 'MockERC20'                                                                                                                                       ---------------------                                                                                                                                       > transaction hash:    0x746c8fa07f757f72d33eb3c073a52eb3d5d1766ad66c594c60b6b873267fb625 > Blocks: 0            Seconds: 0                                                                                                                        > contract address:    0x7e37293114b9C1999b121298fb307BEd7d94eFCE                                                                                        > block number:        2                                                                                                                                  > block timestamp:     1629341200                                            > account:             0xC6e905c949ea72430051d6EeEeAbFda8ebD6feF7            > balance:             99.97039534                                           > gas used:            1233341 (0x12d1bd)                                    > gas price:           20 gwei       > value sent:          0 ETH         > total cost:          0.02466682 ETH                                         Deploying 'ShareContract'            -------------------------            > transaction hash:    0xa8e4525c8593d8e2e80a4fd0bd397e2273cf85f332fa429c88bd3fce4c2396d4                                 > Blocks: 0            Seconds: 0                                            > contract address:    0x969BD42CB868125CFc4a095F61e7dc2890869916            > block number:        3             > block timestamp:     1629341200                                            > account:             0xC6e905c949ea72430051d6EeEeAbFda8ebD6feF7            > balance:             99.9569096                                            > gas used:            674287 (0xa49ef)                                      > gas price:           20 gwei       > value sent:          0 ETH         > total cost:          0.01348574 ETH                                         > Saving migration to chain.         > Saving artifacts                   -------------------------------------                                        > Total cost:           0.0430904 ETH 

在测试期间,需要在share/subgraphs/un7.yaml文件中设置这些契约地址。托多(布兰登):自动化这个

使用块菌与EVM交互

基本示例

truffle console # from a directory with the correct truffle config accounts let instance = await ContractName.deployed() instance.<method_name>() 

运行一个图形节点

该图是用户运行链外Web服务器的集合,这些服务器提供对区块链blockchain事件进行索引,以便将来查询,以换取API费用。我们在共享中需要这个,因为我们想要执行查询,例如在给定的时间窗口内用户拥有的共享数量,这决定了取款上限。在一个图节点中有一些“子图”,它们本质上是索引特定契约事件的指令,由契约开发人员部署

在开发过程中,我们使用Docker启动自己的graph节点,并部署一个本地共享子图,该子图将事务事件映射到graph数据库中的计数器

安装和启动

从graph GitHub存储库下载graph节点代码。我们将使用如下所示的setup bash脚本将其配置为指向本地区块链blockchain实例,然后使用docker将其调出。注意:在使用docker启动节点之前,请打开docker-compose.yaml文件,并确保(通过setup.sh或手动)已完成以下操作:

Change ethereum: 'mainnet:http://host.docker.internal:8545' to ethereum: 'mainnet:0.0.0.0:8545'  

参考

git clone https://github.com/graphprotocol/graph-node/ cd graph-node/docker ./setup.sh docker-compose up 

现在,您的本地图形节点应已启动并准备好索引区块链blockchain事件

了解子图形

子图是一个奇怪的名称,但本质上它们映射到GraphQL数据库范式。有一个大的“图”,例如数据库,由许多“子图”组成,例如特定于应用程序的行/条目。我们至少定义了三个实体:

  1. 子图映射–这是一个Javascript(Typescript)文件,其中实现了对区块链blockchain事件数据进行操作的函数,并将数据映射到GraphQL中的某个索引操作
  2. 子图项目–这是一个项目,它被部署到图实例中,并告诉它在哪里可以找到上面的模式和映射

    累计使用费值(int)

  3. 共享(映射地址:int)(该值是使用与IP的分馏不可替换令牌相关联的共享令牌的ERC20契约导出的)-它不存储在该契约中,而是在运行时计算

我们使用:

graph init --from-example <GITHUB_USERNAME>/<SUBGRAPH_NAME> 

实例化一个子图项目,并相应地修改生成的模板。将子图部署到graph实例中有些混乱,因为存在对Thread的依赖,例如:

yarn && yarn codegen yarn create-local yarn deploy 

没有文档说明这些Thread命令如何与上面列出的实体关联,但不知何故,这神奇地将子图部署到了本地graph实例中。在生产中,我们将使用托管子图studio在测试后部署子图

更新:当您运行时:

$ graph init --from-example <GITHUB_USERNAME>/<SUBGRAPH_NAME> [<DIRECTORY>] 

会创建一个由Thread读取的package.json文件。该文件对“脚本”的定义如下:

{   "name": "share-testonly",   "version": "0.1.0",   "scripts": {     "build-contract": "solc contracts/Gravity.sol --abi -o abis --overwrite && solc contracts/Gravity.sol -- bin -o bin --overwrite",     "create": "graph create un7c0rn/share-testonly --node https://api.thegraph.com/deploy/",     "create-local": "graph create un7c0rn/share-testonly --node http://127.0.0.1:8020",     "codegen": "graph codegen",     "build": "graph build",     "deploy": "graph deploy un7c0rn/share-testonly --ipfs https://api.thegraph.com/ipfs/ --node https://api. thegraph.com/deploy/",     "deploy-local": "graph deploy un7c0rn/share-testonly --ipfs http://localhost:5001 --node http://127.0.0. 1:8020"   } 

意思是:

创建映射到图形创建un7c0rn/share testonly–nodehttps://api.thegraph.com/deploy/

创建本地映射到图形创建un7c0rn/share testonly–节点http://127.0.0.1:8020

纱线codegen映射到图形codegen

纱线构建映射到graph build

将本地映射部署到graph deploy un7c0rn/share testonly–ipfshttp://localhost:5001 –节点http://127.0.0.1:8020

正在使用package.json配置文件和map to graph CLI命令解析纱线命令

部署子图的原始命令

给定一个子图模式,例如un7.yaml,我们将按如下方式执行代码生成:

graph codegen un7.yaml 

这将生成一个包含子目录的生成文件夹,其中包含在部署的图形节点中执行mapping.ts实现所需的AssemblyScript代码文件

使用CURL查询子图

纱线脚本可以归结为:

graph init share/un7 yarn add --dev @graphprotocol/graph-ts graph deploy share/un7 --ipfs http://localhost:5001 --node http://127.0.0.1:8020 un7.yaml 

合同

ABIs

子图模式––此模式定义了数据类型这是我们关心的,并使graph工具链能够在给定类型的情况下执行代码生成

  • 子图映射–这是一个Javascript(Typescript)文件,其中实现了对区块链blockchain事件数据进行操作的函数,并将数据映射到GraphQL中的某个索引操作
  • 子图项目–这是一个项目,它被部署到图实例中,并告诉它在哪里可以找到上面的模式和映射

    累计使用费值(int)

  • 累计提款金额(映射地址:int)

  • 共享(映射地址:int)(该值是使用与IP的分馏不可替换令牌相关联的共享令牌的ERC20契约导出的)-它不存储在该契约中,而是在运行时计算

    一旦部署了子图,就可以使用CURL进行如下查询:

    CURL-X POST-H“Content Type:application/json”-d'{“query”:“{gravatar(id:“0x0”){id displayName}}”http://127.0.0.1:8000/subgraphs/name/un7c0rn/share-如果您使用块菌进行交互,则仅测试有了利益合约,每个事件都会导致指数发生变化

    主合同是一个简单的链外特许权使用费接收人。其目的是分配为访问连锁IP而产生的资金,这些资金有权获得“机械特许权使用费”,例如,采用按次付费或按流付费模式的音频/视频。这与出售标的代币所收取的版税无关

    Contracts

    契约维护以下状态变量:

    伪代码:

    1. balance (int) e.g. this contract is payable.
    2. cumulative_royalty_value (int).
    3. cumulative_withdrawal_amount (mapping addr : int).
    4. shares (mapping addr : int) (this value is derived using the ERC20 contract of the share token associated with the fractionalized non-fungible token of the IP) – it is not stored in this contract but rather computed at runtime.

    共享使用NIFTEX ShardedWallet契约。为了从子图模式与契约交互,有必要存储契约的ABI(应用程序二进制接口)。NIFTEX ShardedWallet ABI可在此处找到:

    _RIGHTSHOLDER_CONTRACT_ADDR = ''  function balance() { 	 return this.balance; }  function () payable { // anonymous callback to receive ETH. 	 uint256 amount = msg.value 	 cumulative_royalty_value += amount. 	 // note that _all_ funds sent to this contract are considered royalties and can only 	 // be withdrawn by copyright shareholders. }  function shares(address _shareholder) { 	 shareholders_contract = ERC20(_RIGHTSHOLDER_CONTRACT_ADDR) 	 return shareholders_contract.balanceOf(_shareholder) }  function withdraw(address _shareholder, int amount) {          shareholders_contract = ERC20(_RIGHTSHOLDER_CONTRACT_ADDR) 	 require (shareholders_contract.balanceOf(_shareholder) > 0, "You are not a shareholder."); 	 drawn = this.cumulative_withdrawal_amount[_shareholder] 	 shareholder_value = self.cumulative_royalty_value / shares(_shareholder) 	 max_draw = 0 if drawn >= (shareholder_value) else (shareholder_value - drawn) 	 require (amount < max_draw, "The amount you've requested to withdraw exceeds your available shareholder royalty value. Request a smaller amount or wait for more royalties to accrue."); 	 require (amount < self.balance, "The amount you've requested exceeds the current royalty balance on the contract. Wait for more royalties to accrue."); // in theory this shouldn't happen – double check 	 _shareholder.call{value: amount}(); 	 this.cumulative_withdrawal_amount[_shareholder] += amount; } 

    ABIs

    https://etherscan.io/address/0x177f51a18668bdcc4d14dbf63d3248c40f427848#code

    https://etherscan.io/address/0x177f51a18668bdcc4d14dbf63d3248c40f427848#code

  • Merge pull request

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

    www.interchains.cc

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

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

    提供最优质的资源集合

    立即查看 了解详情