Skip to content

Commit

Permalink
Adding 1155 / Governor and unit test example for 1155 (like a product…
Browse files Browse the repository at this point in the history
… shop)
  • Loading branch information
juanfranblanco committed Mar 25, 2022
1 parent fc098ac commit 7bc84cf
Show file tree
Hide file tree
Showing 20 changed files with 3,498 additions and 907 deletions.
3 changes: 0 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"telemetry.enableCrashReporter": false,

"solidity.remappingsWindows": ["@openzeppelin/contracts/=H:\\JuanFran\\Documents\\Source\\Repos\\Nethereum.ERC721.Template\\node_modules\\@openzeppelin\\contracts"],
"solidity.remappings": ["@openzeppelin/contracts/=H:\\JuanFran\\Documents\\Source\\Repos\\Nethereum.ERC721.Template\\node_modules\\@openzeppelin\\contract"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<None Update="Documents\TitlePlanImage.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="ShopImages\hard-drive-by-vincent-botta-from-unsplash.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
115 changes: 115 additions & 0 deletions ERC721ContractLibrary.Testing/MyErc1155Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using ERC721ContractLibrary.Contracts.MyERC1155;
using ERC721ContractLibrary.Contracts.MyERC1155.ContractDefinition;
using ERC721ContractLibrary.Contracts.MyERC721;
using ERC721ContractLibrary.Contracts.MyERC721.ContractDefinition;
using Nethereum.Util;
using Nethereum.XUnitEthereumClients;
using Newtonsoft.Json;
using Xunit;

namespace ERC721ContractLibrary.Testing
{
[Collection(EthereumClientIntegrationFixture.ETHEREUM_CLIENT_COLLECTION_DEFAULT)]
public class MyErc1155Test
{
private readonly EthereumClientIntegrationFixture _ethereumClientIntegrationFixture;

public MyErc1155Test(EthereumClientIntegrationFixture ethereumClientIntegrationFixture)
{
_ethereumClientIntegrationFixture = ethereumClientIntegrationFixture;
}


[Fact]
public async void ShouldDeployAndMintoken()
{
//Using rinkeby to demo opensea, if we dont want to use the configured client
//please input your infura id in appsettings.test.json
var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Rinkeby);
//var web3 = _ethereumClientIntegrationFixture.GetWeb3(); //if you want to use your local node (ie geth, uncomment this, see appsettings.test.json for further info)
//example of configuration as legacy (not eip1559) to work on L2s
web3.Eth.TransactionManager.UseLegacyAsDefault = true;


var ercERC1155Deployment = new MyERC1155Deployment();
//Deploy the 1155 contract (shop)
var deploymentReceipt = await MyERC1155Service.DeployContractAndWaitForReceiptAsync(web3, ercERC1155Deployment);

//creating a new service with the new contract address
var erc1155Service = new MyERC1155Service(web3, deploymentReceipt.ContractAddress);

//uploading to ipfs our documents
var nftIpfsService = new NFTIpfsService("https://ipfs.infura.io:5001");
var imageIpfs = await nftIpfsService.AddFileToIpfsAsync("ShopImages/hard-drive-by-vincent-botta-from-unsplash.jpg");


//adding all our document ipfs links to the metadata and the description
var metadataNFT = new ProductNFTMetadata()
{
ProductId = 12131,
Name = "4TB Black Performance Desktop Hard Drive",
Image = "ipfs://" + imageIpfs.Hash, //The image is what is displayed in market places like opean sea
Description = @"Capacity: 4TB
Interface: SATA 6Gb / s
Form Factor: 3.5 Inch
Easy Backup and Upgrade
5 Year Warranty (Photo by: Vincent Botta https://unsplash.com/@0asa)",
ExternalUrl = "https://github.com/Nethereum/ERC721ContractLibrary.Template",
Decimals = 0
};
var stockHardDrive = 100;
//Adding the metadata to ipfs
var metadataIpfs =
await nftIpfsService.AddNftsMetadataToIpfsAsync(metadataNFT, metadataNFT.ProductId + ".json");

var addressToRegisterOwnership = "0xe612205919814b1995D861Bdf6C2fE2f20cDBd68";

//Adding the product information
var tokenUriReceipt = await erc1155Service.SetTokenUriRequestAndWaitForReceiptAsync(metadataNFT.ProductId,
"ipfs://" + metadataIpfs.Hash);

var mintReceipt = await erc1155Service.MintRequestAndWaitForReceiptAsync(addressToRegisterOwnership, metadataNFT.ProductId, stockHardDrive, new byte[]{});


// the balance should be
var balance = await erc1155Service.BalanceOfQueryAsync(addressToRegisterOwnership, (BigInteger)metadataNFT.ProductId);

Assert.Equal(stockHardDrive, balance);

var addressOfToken = await erc1155Service.UriQueryAsync(metadataNFT.ProductId);

Assert.Equal("ipfs://" + metadataIpfs.Hash, addressOfToken);

//Url format https://testnets.opensea.io/assets/[nftAddress]/[id]
//opening opensea testnet to visualise the nft
var ps = new ProcessStartInfo("https://testnets.opensea.io/assets/" + deploymentReceipt.ContractAddress + "/" + metadataNFT.ProductId)
{
UseShellExecute = true,
Verb = "open"
};
Process.Start(ps);

//lets sell 2 hard drives
var transfer = await erc1155Service.SafeTransferFromRequestAndWaitForReceiptAsync(addressToRegisterOwnership, addressToRegisterOwnership, (BigInteger)metadataNFT.ProductId, 2, new byte[]{});
Assert.False(transfer.HasErrors());

}


public class ProductNFTMetadata : Nft1155Metadata
{
public int ProductId { get; set; }
}




}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
using System.IO;
using System.Net;
using System.Threading;
using ERC721ContractLibrary.Contracts.ERC721EnumerableUriStorage;
using ERC721ContractLibrary.Contracts.ERC721EnumerableUriStorage.ContractDefinition;
using ERC721ContractLibrary.Contracts.MyERC721;
using ERC721ContractLibrary.Contracts.MyERC721.ContractDefinition;
using Nethereum.BlockchainProcessing.ProgressRepositories;
using Newtonsoft.Json;


namespace ERC721ContractLibrary.Testing
{
[Collection(EthereumClientIntegrationFixture.ETHEREUM_CLIENT_COLLECTION_DEFAULT)]
public class ERC721EnumerableUriStorageServiceTest
public class MyErc721Test
{
private readonly EthereumClientIntegrationFixture _ethereumClientIntegrationFixture;

public ERC721EnumerableUriStorageServiceTest(EthereumClientIntegrationFixture ethereumClientIntegrationFixture)
public MyErc721Test(EthereumClientIntegrationFixture ethereumClientIntegrationFixture)
{
_ethereumClientIntegrationFixture = ethereumClientIntegrationFixture;
}
Expand All @@ -45,13 +45,13 @@ public async void ShouldDeployAndMintNFTToken()

//creating our deployment information (this includes the bytecode already)
//This example creates an NFT Property (Real state) registry
var erc721Deployment = new ERC721EnumerableUriStorageDeployment() { Name = "Property Registry", Symbol = "PR" };
var erc721Deployment = new MyERC721Deployment() { Name = "Property Registry", Symbol = "PR" };

//Deploy the erc721Minter
var deploymentReceipt = await ERC721EnumerableUriStorageService.DeployContractAndWaitForReceiptAsync(web3, erc721Deployment);
var deploymentReceipt = await MyERC721Service.DeployContractAndWaitForReceiptAsync(web3, erc721Deployment);

//creating a new service with the new contract address
var erc721Service = new ERC721EnumerableUriStorageService(web3, deploymentReceipt.ContractAddress);
var erc721Service = new MyERC721Service(web3, deploymentReceipt.ContractAddress);

//uploading to ipfs our documents
var nftIpfsService = new NFTIpfsService("https://ipfs.infura.io:5001");
Expand Down Expand Up @@ -81,7 +81,7 @@ public async void ShouldDeployAndMintNFTToken()
var addressToRegisterOwnership = "0xe612205919814b1995D861Bdf6C2fE2f20cDBd68";

//Minting the nft with the url to the ipfs metadata
var mintReceipt = await erc721Service.MintRequestAndWaitForReceiptAsync(
var mintReceipt = await erc721Service.SafeMintRequestAndWaitForReceiptAsync(
addressToRegisterOwnership, "ipfs://" + metadataIpfs.Hash);

//we have just minted our first nft so the nft will have the id of 0.
Expand All @@ -103,16 +103,7 @@ public async void ShouldDeployAndMintNFTToken()
Process.Start(ps);

var transfer = await erc721Service.TransferFromRequestAndWaitForReceiptAsync(ownerOfToken, ownerOfToken, 0);
try
{
var transfer2 = await erc721Service.TransferFromRequestAndWaitForReceiptAsync(ownerOfToken,
"0x12890d2cce102216644c59daE5baed380d84830c", 0);
}
catch(Exception ex)
{
//The example creates a non transferable token.
Assert.NotNull(ex);
}
Assert.False(transfer.HasErrors());

}

Expand Down
8 changes: 8 additions & 0 deletions ERC721ContractLibrary.Testing/NFTIpfsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public class NftMetadata
public string Image { get; set; }
}

public class Nft1155Metadata:NftMetadata
{

[JsonProperty("decimals")]
public int Decimals { get; set; }

}

public class NFTIpfsService
{
private readonly string _userName;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This file was deleted.

Large diffs are not rendered by default.

Loading

0 comments on commit 7bc84cf

Please sign in to comment.