Deploy solidity contract with c#

I am developing an application on&nbsp;<strong><em>Ethereum</em></strong>&nbsp;blockchain. I use&nbsp;<strong><em>Solidity</em></strong>&nbsp;for contract and&nbsp;<strong><em>Nethereum c#</em></strong>&nbsp;library to connect to the contract. I am somehow new in Ethereum and it is my first app in blockchain!! I want to deploy my app in one&nbsp;<strong><em>TESTChain</em></strong>&nbsp;first,

I am developing an application on Ethereum blockchain. I use Solidity for contract and Nethereum c# library to connect to the contract. I am somehow new in Ethereum and it is my first app in blockchain!! I want to deploy my app in one TESTChain first,

The problem is that The C# code does not reply anything after send request for deploying of contract. here is my code

the contract is created well on remix and in running tab it works

  public class DeploymentTest : ContractDeploymentMessage
        {
      public static string BYTECODE = @"0x....";// removed it because of long string
  public DeploymentTest() : base(BYTECODE) { }
    }

  public async Task ConnectToTest()
        {
            var url = "https://github.com/Nethereum/TestChains";
            var pass = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
            var acc = new Account(pass);
            var chain = new Web3(acc, url);
            var deploymentMessage = new DeploymentTest();
            var deploymentHandler =  chain.Eth.GetContractDeploymentHandler&lt;DeploymentTest&gt;();

             // this point just waited and does not return anything
            var transactionReceipt = await deploymentHandler.SendRequestAndWaitForReceiptAsync(deploymentMessage);
            var contractAddress = transactionReceipt.ContractAddress;

            }

Can any one help me, I don't insist in deploying with code, it can be happened by anyway but after deploying I need to connect it and call some functions. Thanks

EDITED

I downloaded the TESTChain network for windows and it works fine, it seems the default port is 8545 for http:localhost. but it still does not connect to chain

    var web3test = new Web3(); // also tried new Web3(http://localhost:8545);
var isMining = await web3test.Eth.Mining.IsMining.SendRequestAsync();
var accounts = await web3test.Eth.Accounts.SendRequestAsync();

is that problem with my connection? I disabled my MacAfee firewall completely.

EDITED 2

I changed my machine and it works on my another laptop, I cannot find the problem. I do not know if I just Uninstall the mcaffe will it be ok or not? has anybody any idea about the firewall prolems or other issues?


Learning Solidity

Solidity is a high-level language used to implement smart contracts. This is an object oriented language designed to target the Ethereum Virtual Machine. Let's explore it!

Introduction

Solidity is a high-level language used to implement smart contracts. This is an object oriented language designed to target the Ethereum Virtual Machine. Let's explore it!

Let's go!!!

Let's create a file called Contract.sol

First, you must define the version you are using. This is an information the compiler needs.

pragma solidity ^0.4.22;

All code in Ethereum belongs to a Contract. Let's create a contract and define a few variables inside it.

pragma solidity ^0.4.22;

contract DeveloperFactory {
// Let's create a Developer!
uint dnaDigits = 16;
uint ageDigits = 2;
}

Solidity is a typed language. uint stand for Unsigned Integer ( non negative integers ). These variables are state variables. They will be permanently stored in the contract storage ( in the Ethereum Blockchain ). Our Developer has a dna of 16 digits and an age of 2 digits.

Let's keep going!

Struct and arrays

pragma solidity ^0.4.22;

contract DeveloperFactory {
// Let's create a Developer!
uint dnaDigits = 16;
uint ageDigits = 2;

struct Developer {
    string name;
    uint dna;
    uint age;
}

The struct variable allows us to define more complex data structures. Here the Developer struc takes a string called name, a uint called dna and a uint called age.

Solidity also has arrays. You can create dynamic or fixed arrays. Here, our Developer array is dynamic because we do not specify a length. So we can keep adding Developers to our army without any limitations.

Developer[5] public developers is a fixed array that can contain 5 Developer struct.

Functions

A function would look something like this:

pragma solidity ^0.4.22;

contract DeveloperFactory {
// Let's create a Developer!
uint maxAge = 100;
uint minAge = 5;

struct Developer {
    string name;
    uint id;
    uint age;
}

Developer[] public developers;

function _createDeveloper( string _name, uint _id, uint _age ) private{
    developers.push( Developer( _name, _id, _age ) );
}

function _generateRandomId( string _str ) private pure returns (uint){
    uint rand = uint(keccak256(_str));
    return rand;
}

function createRandomDeveloper( string _name, uint _age ) public view {
    require(_age &gt; minAge);
    require(_age &lt; maxAge);
    uint randId = _generateRandomId( _name );
    _createDeveloper(_name, randId, _age );
}

}

We create functions with the function keyword. Functions can take parameters. By default, functions are public. I added the private keyword to make this function private. I also chose to add an underscore before a private function or variable to distinguish them from public variables. You don't have to do this, I just find it easier to read.

Ethereum has the hash function keccak256 built in. This is a version of SHA3. Pass it any string and you get a 256-bit hexadecimal number.

As you can see, we are typecasting the keccak256 value into a uint value and we return it.

Aside from the private keyword, there are several things you can add to a function:

  • The function returns something: Use the returns keyword and specify the type the function returns. Here, it will return a uint type.
  • The view keyword means that our function needs to look at some of our contract's variables, but not modify them. Our function createRandomDeveloper needs to look at minAge and maxAge variables.
  • The pure keyword means that the function is not accessing any data in the app. It only returns something depending on its parameters. Our _generateRandomId is pure.

We have three functions. _generateRandomId generates a random Id for our developer by using the keccak256 built in function. _createDeveloper creates and pushes a new Developer struct into our developers array. createRandomDeveloper is the only public function. It checks if the age provided is correct. The require statements will throw errors if it is not the case ( age greater than 100 and lower than 5 in our case ). So, this last function is the one that can be called from outside our contract.

Events

You can also create events so you can communicate what happens on the blockchain to your front-end app. Your app would then listen to those events and react accordingly.

pragma solidity ^0.4.22;

contract DeveloperFactory {
// Let's create a Developer!

event NewDeveloper(uint devId, string name, uint age);

uint maxAge = 100;
uint minAge = 5;

struct Developer {
    string name;
    uint id;
    uint age;
}

Developer[] public developers;

function _createDeveloper( string _name, uint _id, uint _age ) private{
    uint id = developers.push( Developer( _name, _id, _age ) ) - 1;
    newDeveloper(id, _name, _age);
}

function _generateRandomId( string _str ) private pure returns (uint){
    uint rand = uint(keccak256(_str));
    return rand;
}

function createRandomDeveloper( string _name, uint _age ) public view {
    require(_age &gt; minAge);
    require(_age &lt; maxAge);
    uint randId = _generateRandomId( _name );
    _createDeveloper(_name, randId, _age );
}

}

Mappings

After struct and arrays, we can also store data in mappings. Mappings are a key-value store. For example:

mapping(uint => string) public keyUintStringValue;
mapping(address => uint) public addressToUint;

Here, we have two public mappings. The first one has a uint key and a stringvalue. The second has an address key and a uint value.

Addresses

The Ethereum blockchain is made up of addresses. Each account has an unique address. It takes the following form: 0x0cE440255306E921F41612C46F1v6df9Cc969183. Each address has a certain amount of Ether, which is the cryptocurrency used on the blockchain, and can receive or send Ether to other addresses.

With that in mind, let's create a new mapping for our DeveloperFactory:

pragma solidity ^0.4.22;

contract DeveloperFactory {

event NewDeveloper(uint devId, string name, uint age);

uint maxAge = 100;
uint minAge = 5;

struct Developer {
    string name;
    uint id;
    uint age;
}

Developer[] public developers;

mapping (address =&gt; uint) public ownerDevCount;
mapping (uint =&gt; address) public devToOwner;

function _createDeveloper( string _name, uint _id, uint _age ) private{
    uint id = developers.push( Developer( _name, _id, _age ) ) - 1;
    NewDeveloper(id, _name, _age);
}

function _generateRandomId( string _str ) private pure returns (uint){
    uint rand = uint(keccak256(_str));
    return rand;
}

function createRandomDeveloper( string _name, uint _age ) public view {
    require(_age &gt; minAge);
    require(_age &lt; maxAge);
    uint randId = _generateRandomId( _name );
    _createDeveloper(_name, randId, _age );
}

}

In the first mapping, we will keep track of the number of devs each account ( address ) created. In the second, we will keep track of the owners for each dev.

msg.sender

Each contract is passive, they don't do anything until someone triggers them. msg.sender is a global variable that allows us to know which address is responsible for the triggering. It could be a account or another smart contract.

With that information, we can update our mappings appropriately. In the _createDeveloper function, we will increase the ownerDevCount for this particular address. In the devToOwner mapping, we will indicate that the newly created developer is owned by the msg.sender address.

pragma solidity ^0.4.22;

contract DeveloperFactory {

event NewDeveloper(uint devId, string name, uint age);

uint maxAge = 100;
uint minAge = 5;

struct Developer {
    string name;
    uint id;
    uint age;
}

Developer[] public developers;

mapping (address =&gt; uint) public ownerDevCount;
mapping (uint =&gt; address) public devToOwner;

function _createDeveloper( string _name, uint _id, uint _age ) private{
    uint id = developers.push( Developer( _name, _id, _age ) ) - 1;
    ownerDevCount[msg.sender]++;
    devToOwner[id] = msg.sender;
    NewDeveloper(id, _name, _age);
}

function _generateRandomId( string _str ) private pure returns (uint){
    uint rand = uint(keccak256(_str));
    return rand;
}

function createRandomDeveloper( string _name, uint _age ) public view {
    require(_age &gt; minAge);
    require(_age &lt; maxAge);
    uint randId = _generateRandomId( _name );
    _createDeveloper(_name, randId, _age );
}

}

Notice the ++ notation to increase the ownerDevCount[msg.sender], just live Javascript!

Inheritance and import

Contracts can inherit from other contracts. If you open a new file call DeveloperLearning.sol, you can make it inherit from DeveloperFactory.sol:

pragma solidity ^0.4.22;

import "./DeveloperFactory.sol";

contract DeveloperLearning is DeveloperFactory {
// I have now access to DeveloperFactory functions
}

Notice how we imported the DeveloperFactory contract ( assuming it was in the same folder ). To declare inheritance, we use the keyword is.

payable

In order for a contract to receive ether, we need to have the payable keyword to a function. The amount sent will be accessible in the global variable msg.value. So we could make sure that a certain amount of ether is sent to the contract before the creation of a developer:

pragma solidity ^0.4.22;

contract DeveloperFactory {

event NewDeveloper(uint devId, string name, uint age);

uint maxAge = 100;
uint minAge = 5;

struct Developer {
    string name;
    uint id;
    uint age;
}

Developer[] public developers;

mapping (address =&gt; uint) public ownerDevCount;
mapping (uint =&gt; address) public devToOwner;

function _createDeveloper( string _name, uint _id, uint _age ) private{
    uint id = developers.push( Developer( _name, _id, _age ) ) - 1;
    ownerDevCount[msg.sender]++;
    devToOwner[id] = msg.sender;
    NewDeveloper(id, _name, _age);
}

function _generateRandomId( string _str ) private pure returns (uint){
    uint rand = uint(keccak256(_str));
    return rand;
}

function createRandomDeveloper( string _name, uint _age ) public payable {
    require(_age &gt; minAge);
    require(_age &lt; maxAge);
    require(msg.value == 5);
    uint randId = _generateRandomId( _name );
    _createDeveloper(_name, randId, _age );
}

}

Memory and Storage

In Solidity, there are two places where variables are stored: in storage or in memory. A variable stored in memory is temporary, it exists while the function is used, then it is discarded. A variable stored in storage exists permanently on the blockchain. Most of the time, you don't have to worry about where to store your variables, as Solidity handles it for you.

For example, state variables ( maxAge, minAge, Developer ), declared outside of functions, are stored in storage. Variables like randId, id, rand are stored in memory.

But, in some cases, you want to explicitly declare where to store certain variables. Solidity allows you to do that with memory and storagekeywords.




how to initialize Empty array in stuct [Solidity]

I am suffering to initialize an empty array for struct when a struct is made.

I am suffering to initialize an empty array for struct when a struct is made.

pragma solidity ^0.5.1;

contract Board {

//storage
Post[] posts;

//struct
struct Post {
    address author;
    string title;
    string content;
    Comment[] comments;
}

struct Comment {
    address author;
    string comment;
}

//add-post
function addPost(address _author, string memory _title, string memory _content) public {
    posts.push(Post(_author, _title, _content, /* HERE IS THE PROBLEM POINT */));
}

}

I want to initialize the comments(struct member) with Empty Array(Type: Comment). Which code should I use for The Problem Point??

Can Blockchain and Privacy Save Facebook?

Can Blockchain and Privacy Save Facebook?

There’s no doubt that Facebook is going big with private communication. But to truly succeed with this new business model, the company must rethink the way it monetizes content across its platform.

There’s no doubt that Facebook is going big with private communication. But to truly succeed with this new business model, the company must rethink the way it monetizes content across its platform.

One solution would be developing a third-party app ecosystem around content — an ecosystem where financial services and payments are key. Blockchain technology, built and maintained by the company’s in-house blockchain group, would be the ideal engine to power these new services, everything from Facebook’s private messaging to the transactions that take place across the platform.

In this post, we’ll examine how the company arrived at this new model, why it’s poised to succeed and the implications for Facebook and beyond.

Rewind: Zuckerberg’s Privacy Pivot

In the fall of 2015, Mark Zuckerberg and Facebook were on top of the world. The company’s advertising business was growing at a double-digit percent clip, the stock was at an all-time high.

Then the 2016 election happened, and with it came an onslaught of fake news. Over the next two years, Facebook faced increasing regulatory scrutiny. The integrity of the content on its platform started to matter. In the public’s eyes, Facebook had moved from a technology platform to a robust media outlet, and like any media outlet, it had to take responsibility for its content.

So what did Facebook do? For starters, the company announced hiring thousands of editors to help reduce fake news on its platform.

This new overhead challenged their road-tested ad-based business model and, as venture capitalist Hunter Walk pointed out, taking more content ownership also meant lower software margins. Facebook was under pressure from both regulators to take responsibility and shareholders to grow its valuation.

Then, early this month, Mark unveiled his big pivot. He introduced a revised vision for Facebook — one that centered around private, secure communication in smaller communities.

‘I believe the future of communication will increasingly shift to private, encrypted services.’ — Mark Zuckerberg

Facebook’s secure messaging could be the next “super app”

In non-emerging markets, such as the United States, where credit card adoption is prevalent, the Google and Apple app stores solve third-party app discovery and payment friction. Consumers give their credit card details to an app store, and this information becomes available for use across the app ecosystem.

Like Google and Apple, Facebook’s secure messaging platform could enable third-party apps to plug their services into and around the chat application. By doing this, it would make services, like blockchain-enable payments, available across the app ecosystem.

Taking it one step further, Facebook could leverage blockchain for financial services directly in platform and help facilitate online payments and other financial services, especially in emerging markets that are rapidly transforming from cash to digital payments.

The super app model is thriving in Asia

Mobile super apps are the de facto method for commerce in Asia. Consumers in the region use mobile apps like Gojek, Grab and Alipay — $10B, $14B and $150 billion market caps (respectively) — to access and pay for apps and services ranging from music and transportation to gaming and virtual goods. Not only is this momentum expected to drive future growth in Asia, it will likely spread globally.

These super app companies capitalized on cash to digital and ecommerce trends in the region. Credit cards are not dominant in Asia, and Apple and Google’s app store publishing restrictions are not economically advantageous for developers (30 percent is cut for anything bought through app stores). Further, Asian consumers pre-pay for bandwidth and are thus conservative with mobile data usage.

This creates a lot of friction in payments, especially mobile payments, and means that new services entrants need to take a hyper-local, country-by-country approach in reducing payments friction through their mobile apps.

These Asian super apps did just that. They solved the payment problem for mobile consumers because they enabled consumers with easy ways to deposit money into their apps via cash and bank transfers (since credit cards are not as prevalent in this region).

Asian super apps established their markets far before blockchain and cryptocurrencies were broadly established. However, for new super apps starting today, it makes more sense to tap into blockchain for payments and other financial services than building infrastructure from scratch.

Facebook could also shift monetization from ads to ecommerce via blockchain-based payments and services

‘Beyond that, significant thought needs to go into all of the services we build on top of that foundation — from how people do payments and financial transactions, to the role of businesses and advertising, to how we can offer a platform for other private services.’ — Mark Zuckerberg

Facebook’s third-party ecosystem built around secure messaging will be the next super app. The company’s blockchain-based payment service will help customers load funds into the new super app, and use it for ecommerce payments and in-app payments, much like the developed world already use the Apple and Google app stores to make payments on our mobile devices today.

But here’s where Facebook, by leveraging blockchain infrastructure and crypto exchanges, may have an added advantage. The company can activate its already massive user base in the region and solve the same last-mile payments problem by partnering with regional crypto exchanges as on and off ramps to the Facebook super app.

Tapping into blockchain and crypto technology in this way could help Facebook scale in emerging markets and take market share away from incumbent providers. Thus, reinventing Facebook as we know it today.

Originally published on https://hackernoon.com