In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to compile and deploy test TNS tokens" related knowledge, the editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "how to compile and deploy test TNS tokens" article can help you solve the problem.
Compile
At this point we have a file that contains some Solidity code. But in order for the ethernet virtual machine to understand it, we need to convert it into machine code. In addition, in order to communicate with it from a Web application, we need an ABI (application binary interface), which is a generally readable description of a function that exists in a smart contract-- whether it's a token or something more complex. We can use Truffle's compiler to create machine code for EVM and ABI immediately.
In the project folder, run:
Truffle compile
This command will look inside the accounts subfolder, compile them all, and put their compiled versions into the build subfolder. Please note that if you use the alternative development process in the previous section, all parent contracts of our TNSToken contract inheritance feature will also be compiled one by one in its own file.
Feel free to check the contents of the generated JSON file. Our TNSToken should have more than 10000 lines of JSON code.
Deploy to Ganache
Now let's see if we can deploy it into our simulator Ganache block chain. If Ganache is not already running in the tab of the terminal or in the application of the operating system, run the following command:
Ganache-cli
Or run the application to get a screen like this:
Then, back to the folder where we just compiled the contract, we have to add a migration migration. Create the file migrations/2_deploy_tnstoken.js. If you are not familiar with migration in the Truffle ecosystem, please refer to this guide.
We put the following into the file:
Var Migrations = artifacts.require (". / Migrations.sol"); var TNSToken = artifacts.require (". / TNSToken.sol"); module.exports = function (deployer, network, accounts) {deployer.deploy (TNSToken, {from: accounts [0]});}
First, complete the import by requesting the Migrations.sol. This is required for each migration. Next, deploying the token means that we need to import its Solidity code, and we do this through TNSToken.sol (the code we wrote in the previous section). Finally, it's just the part that migrates between function (deployer, network, accounts) {and the last}.
In this case, we tell the deployer to deploy TNSToken and pass in the from parameter to set the initial token holder. The addresses used here are random addresses generated by Ganache, but by using the accounts array that is automatically sent to the deployer, we ensure that we have access to the list of accounts that exist in the running node (whether it's a real-time Geth node or Ganache). In my specific example, the address of account [0] is 0xdFb659D556d926dd3585d0f18d4F8eD7939E8061, which is also evident in the screenshot above.
Let's not forget to configure the development environment in truffle.js:
Module.exports = {networks: {development: {host: "127.0.0.1", port: 7545, network_id: "*"}
Note: take care of the port and IP; yours may be different!
Finally, in the project folder, run truffle migrate. You should see something like this:
Note the etheric place address next to TNStoken: 0x3134bcded93e810e1025ee814e87eff252cff422. This is where our tokens are deployed. Now let's see how it actually works.
Test token
In this case, automatic testing is not required. Token contracts are highly standardized and combat-tested. Automated testing will come in handy if some of the features we use are beyond the scope of traditional tokens. However, it is sufficient to test it by sending it to and from the address.
Open a wallet UI like MyEtherWallet and select a custom network from the menu in the upper right corner. In the dialog box, enter the information given to you by your private block chain-Ganache or the actual PoA block chain, which you can run according to part 1 of this tutorial series. In my example, the address is 127.0.0.1 and the port is 7545.
Configure the network in MEW
Open the wallet that you set to the from value in the deployment script. If you are using Ganache, you will see its private key printed on the Ganache UI screen or in the ganache output of the terminal.
Finally, you need to inform MEW that the token exists. We do this by adding custom tokens.
Add custom tokens in MEW
After adding tokens, you will immediately notice that the account now has a balance of 100 million and can send them in the currency drop-down menu. We try to send some to another address.
Continue sending, and then restore the original account to 100 million. We just made sure that the basic functions of tokens work as expected.
Deploy to a real-time network
If it is not deployed on a real-time network, this will not be a real token test. However, let's not use the main network, but a test network like Rinkeby.
At truffle.js, let's add a new network rinkeby so that our file looks like this:
Require ('dotenv'). Config (); const WalletProvider = require ("truffle-wallet-provider"); const Wallet = require (' ethereumjs-wallet'); const Web3 = require ("web3"); const w3 = new Web3 (); const PRIVKEY = process.env ["PRIVKEY"]; const INFURAKEY = process.env ["INFURAKEY"] Module.exports = {networks: {development: {host: "127.0.0.1", port: 7545, network_id: "*"}, rinkeby: {provider: function () {return new WalletProvider (Buffer.from (PRIVKEY, "hex")), "https://rinkeby.infura.io/"+INFURAKEY) }, gas: 4600000, gasPrice: w3.utils.toWei ("50", "gwei"), network_id: "3",}}
Oops! What is all this?
Let's comb it line by line.
The first few lines import some node modules, so we can use the following function. If you get any missing module messages, simply run npm install web3-wallet-provider truffle-wallet-provider web3 dotenv-save should solve the problem.
Next, we load the private key of our running contract wallet (so the wallet will get 100 million tokens; we can't use the from value here) project folder from the .env file in the root directory. If it does not exist, create it. The file also has an Infura.io access key, which is a Web site that hosts Ethernet Square nodes and allows applications to connect to them, so developers do not need to run the full Ethernet Square node on their computers.
.env files are hidden by default and can be ignored in .gitignore, so there is no danger of private key disclosure-a very important precaution! This is what the file contains:
PRIVKEY= "YOUR_PRIVATE_KEY"; INFURAKEY= "YOUR_INFURA_ACCESS_KEY"
You can register here to get the Infura key. If you just install Metamask, switch it to Rinkeby, and then go to export the private key, you can easily get the private key. However, any method is feasible, so choose any method you like. You can also use the private key of Ganache. A private key can unlock the same wallet-testnet,rinkeby,mainnet-on all ethernet networks, and you can name it.
Go back to our configuration file. We have a new network entry: rinkeby. This is the name of the Ethernet Square test network we are going to deploy, and the code within the provider is basically copied and pasted, telling Truffle to "grab my private key, hexadecimal code it into an unlocked wallet, and then interact with Infura through it."
Finally, we defined the gas limit we want to spend when executing this contract (4.6 million, which can be changed if needed), how much gas is needed (50 Gwei is actually quite expensive, but the Ethernet we are playing with is simulated, so it doesn't matter), and set the network ID to 4, because this is the label of Rinkeby testnet.
There's one more thing we need to do. The migration file we wrote earlier is for the starting address, but the address of the Rinkeby is different. Does this mean that we need to deploy scripts based on network changes? Of course not! Let's change the 2_deploy_tnstoken.js file to look like this:
Var Migrations = artifacts.require (". / Migrations.sol"); var TNSToken = artifacts.require (". / TNSToken.sol"); module.exports = function (deployer, network, accounts) {if (network = = "development") {deployer.deploy (TNSToken, {from: accounts [0});} else {deployer.deploy (TNSToken);}}
As you can see, the deployment script is a simple JavaScript, and the second parameter provided to the deployer is always the name of the network-- we can use it to distinguish between them.
If we try to run the migration using truffle migrate-- network rinkeby now, it will fail if the address we use is new:
This is because the address does not cost Ethernet when deploying the contract. But it's easy to solve. Just go to Rinkeby Faucet to get some for free.
Now rerun the migration, and the token contract will be deployed in real time on the Rinkeby network. It can be tested like the Ganache use case above. Everything should be exactly the same, and only now you can test it with your friends and colleagues. A lot of progress!
Rewards: validation and ENS
For additional trustpoints, it is recommended that you verify the token on Etherscan and register the ENS domain for it.
Verification
Validation means submitting the source code of the token to Etherscan so that it can be compared with what is deployed on the network, thus verifying that it has no back door. This is done on the Code tab of the token address. Because our tokens use some third-party code, and we can't easily pull that code into the code window of the verification screen, we need to modify the contract. To do this, we will use a tool called truffle-flattener:
Npm install-global truffle-flattener
The tool copies the source code for all dependencies and tokens into a file. We can run it like this:
Truffle-flattener contracts/TNSToken.sol > >. / contracts/TNSTokenFlat.sol
Now there should be a new file in the contracts folder, almost the same as our source code, but with the dependent code pasted (for example, SafeMath.sol will be pasted at the top of the file).
Paste the contents of the new file into the code window on the Verify screen, set the compiler to the version of truffle obtained by running truffle version, and set Optimization to No. Click verify and publish, and once the process is complete, the address screen of your token will display new tabs: read contract and write contract, and the Code tab will have a green check mark indicating that the code has been verified. This provides an additional point of trust for the community.
ENS
ENS is the name system of Etay Square. It is used to provide a human-readable name for Tai Fong, so you don't have to remember 0xmumbojumbo strings, but can remember things like bitfalls.eth. Then, you can even register a subdomain name like token.bitfalls.eth. The process of registering an ENS domain is not easy and takes time, so if you are willing to do so, I recommend that you read this guide and follow the instructions here.
This is the end of the introduction to "how to compile and deploy test TNS tokens". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.