Tag: Rely on Project feature to share test address interaction with the system technology
Despite the recent troubles, Ethereum is still the most important system for implementing smart contracts in the blockchain space, and that seems unlikely to change very quickly.
In my view, the technology itself has great potential, from an academic point of view is very interesting, but as mentioned earlier issues, before the many problems demonstrated, blockchain technology, smart contracts, especially with solidity ethereum ecosystem is very immature, And there are no ready-made prime time/production use cases.
However, this is a great opportunity to learn and learn about this technology and be able to prepare ahead of time before the application crashes.
In one of my previous articles, I created a small application that contained a simple winner-takes-all crowdfunding contract. In this article, we will accept the contract and refer to the Contract.sol contract to see if we can use the go language for deployment and interaction.
Why use the Go language? One of the reasons is that the go language has a powerful role to play. The most widely used Ethereum client is written in the go language, meaning there is a good ecosystem to interact with Ethereum and smart contracts, such as generating reusable code from a shared library.
In this example, instead of using a real blockchain as a deployment target, we use the simulated backend provided by go-ethereum so that we can safely test and experiment without spending any money.
The smart contract itself is simple (I won't go into details about its function or how it works), as long as it is sufficient to explain that 3 parameters are deployed in the contract, and 3 parameters are as follows:
Minimum entry fee for the project
Deadline for submission of new projects
Deadline for supporting projects
In the first phase, the project can be submitted with a name and URL, and the parameters include at least a minimum fee. In the second phase, the project can be supported by sending an address to the partner.
However, in this article, we will focus on:
Deployment contract
Read the data from the contract
Interaction with a contract (transaction)
Instantiating a deployed contract by address
We'll do this with 70 lines of code. Let's get started!
To be able to keep up, you need something. First of all, the most important thing is that you need to solc the solidity compiler.
Then, use Go-ethereum and build it:
We can go through the Solc and Geth Devtools and start with the go-version of the build contract. Our smart contract for the Sol file:
The generated code looks like this.
As you can see, we have a way of deploying and instantiating contracts, as well as mapping all public contract methods.
The next step is to deploy the contract to the analog back end.
To do this, some settings are required. As mentioned above, for the sake of simplicity, we will use Simulatedbackend as our target block chain, at the end of this article there will be a short section on how to use testnet or even real Etherey block links.
With some go-ethereum dependencies, we can start setting up: Board practices and batching
We simply create a key, create an Ethernet Genesis account, and start the emulated backend, which returns a bind. Contractbackend. Now we can start deploying the contract using the generated Deploywinnertakesall method.
We pass a Auth object that represents our identity, parameters include the backend sim and Minimum Entry fee,project Deadline and Campaign Deadline, each of which uses a bigint. The method returns the address to which the contract will be deployed, as well as a handle to the contract and an error. A transaction object is also returned, but we will not handle it here.
Now that the contract has been deployed, we should be able to interact with it. For example, we can check whether the deadlines we send are set correctly in the contract:
However, if you do this, return < nil >. That's because our contract hasn't really been mined yet. If we use the real network as the backend, we have to wait for it to happen, but on our analog back end, we can do this:
We return to the date of our deployment:
All right. This allows us to read the data disclosed in the contract. Now we want to interact with it.
In this case, the simplest approach is to propose a new project by sending the name and URL of an item with at least the lowest input cost:
Of course, we need to re-extract:
But we get the following output:
That's great--that means our project was created. This allows us to deploy, read and write contracts.
But if the contract is already deployed, we just want to interact with it? Fortunately, the generated code includes a Newwinnertakesall method that uses only the address of the deployed contract to instantiate the contract:
We get the same return value as the contract we deployed and can interact in the same way as this version, which is implemented through address instantiation.
We've done all the steps and we need to interact with a contract meaningfully, but only on the back end of the simulation. In order to use testnet or real ethereum blockchain, we only need to adapt to the following several things:
This will result in the Auth object we created above. Of course, do not use plaintext or passwords in your code, but instead load them in a secure manner.
If the contract is already deployed, we do not need to create newipcclient, but can dial directly to the node:
You can find the code and the complete example here.
Summarize
As I said at the beginning of this article, it is too early for me to rely on reliable smart contracts to handle application crashes, but the potential for this and other methods of block-based smart contracts is huge, so it's worth knowing the technology around it.
The go language is suitable for tasks that interact with ethereumbased-based smart contracts because there are a lot of reusable code from Geth, and even some documentation on how to get started. This can of course be implemented in other languages. (using WEB3), but if you like, it seems to be a reliable choice.
Use the go language to interact with Ethereum smart contracts