Pharos 백서

Pharos

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 18쪽 중 16쪽

` Install OpenZeppelin Contracts: ```shell npm install ``` ### Setup 3: Write the Token Contract Create a New Solidity File: * Create a new file for your token contract: ```shell touch contracts/Token.sol ``` Write the Token Contract: * Open `contracts/Token.sol` in your favorite text editor and add the following code: ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract Token is ERC20 { constructor(uint256 initialSupply) ERC20("Token", "MTK") { _mint(msg.sender, initialSupply); } } ``` Compile the Smart Contract: ```shell npx hardhat compile ``` Test the Smart Contract ```shell npx hardhat test ``` ### Step 4: Deploy the Token Contract Set the private key: ```shell npx hardhat vars set PRIVATE_KEY ``` Deploy the Contract: ```shell npx hardhat ignition deploy ./ignition/modules/Token.js --network pharos ``` ### Step 5: Verify the Token Contract Add the following content to your `hardhat.config.js` file * For Atlantic Testnet | Field | Value | | ------------- | -------------------------------------------------------------------------------------- | | chain\_name | pharos | | chain\_id | 688689 | | rpc\_endpoint | <https://atlantic.dplabs-internal.com>" | | api\_host | <https://api.socialscan.io/pharos-atlantic-testnet/v1/explorer/command\\_api/contract> | | explorer\_url | <https://atlantic.pharosscan.xyz/> | ```js require("@nomicfoundation/hardhat-toolbox"); /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.28", networks: { pharos: { url: "https://atlantic.dplabs-internal.com", accounts: [vars.get("PRIVATE_KEY")], }, }, etherscan: { customChains: [ { network: "pharos", chainId: 688689, urls: { apiURL: "https://api.socialscan.io/pharos-atlantic-testnet/v1/explorer/command_api/contract", browserURL: "https://atlantic.pharosscan.xyz/", }, }, ], apiKey: { pharos: "Put a random string", // Note we don't need a apiKey here, just leave a random string }, } }; ``` * For Testnet | Field | Value | | ------------- | ----------------------------------------------------------------------------- | | chain\_name | pharos | | chain\_id | 688688 | | rpc\_endpoint | <https://testnet.dplabs-internal.com> | | api\_host | <https://api.socialscan.io/pharos-testnet/v1/explorer/command\\_api/contract> | | explorer\_url | <https://testnet.pharosscan.xyz/> | ```js require("@nomicfoundation/hardhat-toolbox"); /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.28", networks: { pharos: { url: "https://testnet.dplabs-internal.com", accounts: [vars.get("PRIVATE_KEY")], }, }, etherscan: { customChains: [ { network: "pharos", chainId: 688688, // Or use Pharos Atlantic Testnet chain id 688689 urls: { apiURL: "https://api.socialscan.io/pharos-testnet/v1/explorer/command_api/contract", browserURL: "https://testnet.pharosscan.xyz/", }, }, ], apiKey: { pharos: "Put a random string", // Note we don't need a apiKey here, just leave a random string }, } }; ``` Run the hardhat verify command ```shell npx hardhat verify --network pharos <contract_address> <constructor_args_params> ``` ### Troubleshooting * **Contract Deployment Fails**: Ensure you have enough testnet tokens to cover the deployment cost. * **Interaction Issues**: Verify that the contract address and ABI are correct. * **Insufficient Balance**: Ensure your wallet has enough tokens to transfer. ### Conclusion Now that you’ve created and deployed your first token using Hardhat. This guide provides a comprehensive introduction to creating and deploying a token on the Pharos blockchain using Hardhat. If you encounter any issues, refer to the Troubleshooting section or consult the Hardhat documentation. Happy building! 🚀 # Write Your First NFT ### Prerequisites Before you begin, ensure you have the following: * Git: Used for code management and obtain examples. * Node.js: Install it from [nodejs.org](https://nodejs.org/). * Pharos Devnet/Testnet Access: Access to a Pharos node (local or remote) for interacting with the blockchain. ### Setup 1: Install Hardhat * [Hardhat](https://hardhat.org/hardhat-runner/docs/getting-started#installation) ### Setup 2: Set Up the Project Clone the example repo: ```shell git clone https://github.com/PharosNetwork/examples cd examples/nft/hardhat/contract ``` Install OpenZeppelin Contracts: ```shell npm install ``` ### Setup 3: Write the NFT Contract Create a New Solidity File: * Create a new file for your NFT contract: ```shell touch contracts/Token.sol ``` Write the Token Contract: * Open `contracts/Token.sol` in your favorite text editor and add the following code: ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract Token is ERC721 { uint256 id; constructor() ERC721("Token", "MTK") {} function mint(address to) public returns (uint256) { id += 1; _mint(to, id); return id; } } ``` Compile the Smart Contract: ```shell npx hardhat compile ``` Test the Smart Contract ```shell npx hardhat test ``` ### Step 4: Deploy the Token Contract Set the private key: ```shell npx hardhat vars set PRIVATE_KEY ``` Deploy the Contract: ```shell npx hardhat ignition deploy ./ignition/modules/Token.js --network pharos ``` ### Step 5: Verify the Token Contract Add the following content to your `hardhat.config.js` file * For Atlantic Testnet | Field | Value | | ------------- | -------------------------------------------------------------------------------------- | | chain\_name | pharos | | chain\_id | 688689 | | rpc\_endpoint | <https://atlantic.dplabs-internal.com>" | | api\_host | <https://api.socialscan.io/pharos-atlantic-testnet/v1/explorer/command\\_api/contract> | | explorer\_url | <https://atlantic.pharosscan.xyz/> | ```js require("@nomicfoundation/hardhat-toolbox"); /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.28", networks: { pharos: { url: "https://atlantic.dplabs-internal.com", accounts: [vars.get("PRIVATE_KEY")], }, }, etherscan: { customChains: [ { network: "pharos", chainId: 688689, urls: { apiURL: "https://api.socialscan.io/pharos-atlantic-testnet/v1/explorer/command_api/contract", browserURL: "https://atlantic.pharosscan.xyz/", }, }, ], apiKey: { pharos: "Put a random string", // Note we don't need a apiKey here, just leave a random string }, } }; ``` * For Testnet | Field | Value | | ------------- | ----------------------------------------------------------------------------- | | chain\_name | pharos | | chain\_id | 688688 | | rpc\_endpoint | <https://testnet.dplabs-internal.com> | | api\_host | <https://api.socialscan.io/pharos-testnet/v1/explorer/command\\_api/contract> | | explorer\_url | <https://testnet.pharosscan.xyz/> | ```js require("@nomicfoundation/hardhat-toolbox"); /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.28", networks: { pharos: { url: "https://testnet.dplabs-internal.com", accounts: [vars.get("PRIVATE_KEY")], }, }, etherscan: { customChains: [ { network: "pharos", chainId: 688688, // Or use Pharos Atlantic Testnet chain id 688689 urls: { apiURL: "https://api.socialscan.io/pharos-testnet/v1/explorer/command_api/contract", browserURL: "https://testnet.pharosscan.xyz/", }, }, ], apiKey: { pharos: "Put a random string", // Note we don't need a apiKey here, just leave a random string }, } }; ``` Run the hardhat verify command ```shell npx hardhat verify --network pharos <contract_address> <constructor_args_params> ``` ### Troubleshooting * **Contract Deployment Fails**: Ensure you have enough testnet tokens to cover the deployment cost. * **Interaction Issues**: Verify that the contract address and ABI are correct. * **Insufficient Balance**: Ensure your wallet has enough tokens to transfer. ### Conclusion Now that you’ve created and deployed your first NFT using Hardhat. This guide provides a comprehensive introduction to to creating and deploying an NFT on the Pharos blockchain. If you encounter any issues, refer to the Troubleshooting section or consult the Hardhat documentation. Happy building! 🚀 # Write Your First Uniswap Contract This guide will walk you through the process of creating and deploying a simple Uniswap-like decentralized exchange (DEX) contract on the Pharos blockchain. By the end of this guide, you will have a basic understanding of how automated market makers (AMMs) work and how to implement a simple DEX. ### Prerequisites Before you begin, ensure you have the following: * Git: Used for code management and obtain examples. * Node.js: Install it from [nodejs.org](https://nodejs.org/). * Pharos Devnet/Testnet Access: Access to a Pharos node (local or remote) for interacting with the blockchain. ### Setup 1: Install Hardhat * [Hardhat](https://hardhat.org/hardhat-runner/docs/getting-started#installation) ### Setup 2: Set Up the Project Clone the example repo: ```shell git clone https://github.com/PharosNetwork/examples cd examples/uniswap/hardhat/contract ``` Install OpenZeppelin Contracts: ```shell npm install ``` ### Step 3: Write the Uniswap Contract Create a New Solidity File: * Create a new file for your uniswap contract: ```shell touch contracts/Uniswap.sol ``` Write the uniswap Contract: * Open `contracts/Uniswap.sol` in your favorite text editor and add the following code: ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract Uniswap { address public tokenA; address public tokenB; uint256 public reserveA; uint256 public reserveB; constructor(address _tokenA, address _tokenB) { tokenA = _tokenA; tokenB = _tokenB; } function addLiquidity(uint256 amountA, uint256 amountB) external { require(IERC20(tokenA).transferFrom(msg.sender, address(this), amountA), "Transfer failed"); require(IERC20(tokenB).transferFrom(msg.sender, address(this), amountB), "Transfer failed"); reserveA += amountA; reserveB += amountB; } function removeLiquidity(uint256 amountA, uint256 amountB) external { require(reserveA >= amountA && reserveB >= amountB, "Insufficient reserves"); reserveA -= amountA; reserveB -= amountB; require(IERC20(tokenA).transfer(msg.sender, amountA), "Transfer failed"); require(IERC20(tokenB).transfer(msg.sender, amountB), "Transfer failed"); } function swapAToB(uint256 amountA) external { uint256 amountB = (amountA * reserveB) / reserveA; require(IERC20(tokenA).transferFrom(msg.sender, address(this), amountA), "Transfer failed"); require(IERC20(tokenB).transfer(msg.sender, amountB), "Transfer failed"); reserveA += amountA; reserveB -= amountB; } function swapBToA(uint256 amountB) external { uint256 amountA = (amountB * reserveA) / reserveB; require(IERC20(tokenB).transferFrom(msg.sender, address(this), amountB), "Transfer failed"); require(IERC20(tokenA).transfer(msg.sender, amountA), "Transfer failed"); reserveB += amountB; reserveA -= amountA; } } ``` Compile the Smart Contract: ```shell npx hardhat compile ``` Test the Smart Contract ```shell npx hardhat test ``` ### Step 4: Deploy the Uniswap Contract Set the private key: ```shell npx hardhat vars set PRIVATE_KEY ``` Deploy the Contract: ```shell npx hardhat ignition deploy ./ignition/modules/Token.js --network pharos ``` ### Troubleshooting * **Contract Deployment Fails**: Ensure you have enough testnet tokens to cover the deployment cost. * **Interaction Issues**: Verify that the contract address and ABI are correct. * **Test Failures**: Check the test output for detailed error messages and adjust the tests accordingly. ### Conclusion This guide provides a comprehensive introduction to creating and deploying a Uniswap-like contract on the Pharos blockchain using Hardhat. If you encounter any issues, refer to the Hardhat documentation or the troubleshooting section. Happy building! 🚀 # Rust This guide is suitable for developers who want to start building dApps using Pharos and Rust WASM toolchains. If you are a new user of Ethereum, please consider researching the [Ethereum documentation](https://ethereum.org/en/developers/docs/) before continuing. This guide will walk you through the process of creating and deploying your first token on the Pharos blockchain using Rust and WASM toolchains. By the end of this guide, you will have a fully functional token contract and understand how to interact with it. ### Prerequisites Before you begin, ensure you have the following: * Git: Used for code management and obtain examples. * Python: Used to interact with the Pharos network and execute contract testing scripts. * Rust: Install it from [rust-lang.org](https://www.rust-lang.org/tools/install). * Pharos Devnet/Testnet Access: Access to a Pharos node (local or remote) for interacting with the blockchain. ### Setup 1: Install WASM Toolchains With Cargo Install the pharos wasm toolchain plugin using the Cargo tool: ```shell cargo install --git https://github.com/PharosNetwork/pharos-cargo-stylus ``` Add the `wasm32-unknown-unknown` build target to your Rust compiler: ```shell rustup target add wasm32-unknown-unknown ``` You should now have it available as a Cargo subcommand: ```shell cargo stylus --help Cargo subcommand for developing Pharos Stylus projects ``` > Note: Pharos built the chain execution client from scratch using C++, and we design, developed and open-sourced the [Dora](https://github.com/dp-labs/dora) VM, which combines EVM and WASM in a deeply compiled VM arch with native interoperability. The Rust toolchain and Rust SDK are forked from Stylus toolchain and SDK to facilitate easier adoption, the change here is the removal of Arbitrum ink billing and the simplification of the activation and compilation process on Pharos. ### Setup 3: Write the Token Contract * Create a new file for your token contract: ```shell touch src/lib.rs ``` Write the Token Contract: * Open `src/lib.rs` in your favorite text editor and add the following code: ```rust // Only run this as a WASM if the export-abi feature is not set. #![cfg_attr(not(any(feature = "export-abi", test)), no_main)] extern crate alloc; // Modules and imports mod erc20; use crate::erc20::{Erc20, Erc20Error, Erc20Params}; use alloy_primitives::{Address, U256}; use stylus_sdk::{msg, prelude::*}; /// Immutable definitions struct StylusTestTokenParams; impl Erc20Params for StylusTestTokenParams { const NAME: &'static str = "StylusTestToken"; const SYMBOL: &'static str = "STTK"; const DECIMALS: u8 = 18; } // Define the entrypoint as a Solidity storage object. The sol_storage! macro // will generate Rust-equivalent structs with all fields mapped to Solidity-equivalent // storage slots and types. sol_storage! { #[entrypoint] struct StylusTestToken { // Allows erc20 to access StylusTestToken's storage and make calls #[borrow] Erc20<StylusTestTokenParams> erc20; } } #[public] #[inherit(Erc20<StylusTestTokenParams>)] impl StylusTestToken { /// Mints tokens pub fn mint(&mut self, value: U256) -> Result<(), Erc20Error> { self.erc20.mint(msg::sender(), value)?; Ok(()) } /// Mints tokens to another address pub fn mint_to(&mut self, to: Address, value: U256) -> Result<(), Erc20Error> { self.erc20.mint(to, value)?; Ok(()) } /// Burns tokens pub fn burn(&mut self, value: U256) -> Result<(), Erc20Error> { self.erc20.burn(msg::sender(), value)?; Ok(()) } } ``` Compile the Smart Contract: * Use cargo to compile the contract: ```shell cargo stylus check --endpoint=<PHAROS_RPC_URL> ``` ### Step 4: Deploy the Token Contract ```shell cargo stylus deploy --private-key=<YOUR_PRIVATE_KEY> --endpoint=<PHAROS_RPC_URL> ``` ### Step 5: Interact with the Token Contract Open `scripts/main.py` and replace your deployed contract address to the variable `CONTRACT_ADDRESS`: ```python from web3 import Web3 # Replace Pharos RPC URL RPC_URL = "<Pharos RPC URL>" # Note: Replace this address to your contract address CONTRACT_ADDRESS = Web3.to_checksum_address("0x8ca06d75562be39f645b3b1ead98b15ddfd8c06f") PRIVATE_KEY = "Replace your private key" contract_abi = [ { "inputs": [], "name": "name", "outputs": [{"internalType": "string", "name": "", "type": "string"}], "stateMutability": "pure", "type": "function", }, { "inputs": [], "name": "symbol", "outputs": [{"internalType": "string", "name": "", "type": "string"}], "stateMutability": "pure", "type": "function", }, { "inputs": [], "name": "decimals", "outputs": [{"internalType": "uint8", "name": "", "type": "uint8"}], "stateMutability": "pure", "type": "function", }, { "inputs": [], "name": "totalSupply", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "stateMutability": "view", "type": "function", }, { "inputs": [{"internalType": "address", "name": "owner", "type": "address"}], "name": "balanceOf", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "stateMutability": "view", "type": "function", }, { "inputs": [{"internalType": "address", "name": "to", "type": "address"}, {"internalType": "uint256", "name": "value", "type": "uint256"}], "name": "transfer", "outputs": [{"internalType": "bool", "name": "", "type": "bool"}], "stateMutability": "nonpayable", "type": "function", }, { "inputs": [{"internalType": "address", "name": "from", "type": "address"}, {"internalType": "address", "name": "to", "type": "address"}, {"internalType": "uint256", "name": "value", "type": "uint256"}], "name": "transferFrom", "outputs": [{"internalType": "bool", "name": "", "type": "bool"}], "stateMutability": "nonpayable", "type": "function", }, { "inputs": [{"internalType": "address", "name": "spender", "type": "address"}, {"internalType": "uint256", "name": "value", "type": "uint256"}], "name": "approve", "outputs": [{"internalType": "bool", "name": "", "type": "bool"}], "stateMutability": "nonpayable", "type": "function", }, { "inputs": [{"internalType": "address", "name": "owner", "type": "address"}, {"internalType": "address", "name": "spender", "type": "address"}], "name": "allowance", "outputs": [{"internalType": "uint256", "name": "", "type": "uint256"}], "stateMutability": "view", "type": "function", }, { "inputs": [{"internalType": "uint256", "name": "value", "type": "uint256"}], "name": "mint", "outputs": [], "stateMutability": "nonpayable", "type": "function", }, { "inputs": [{"internalType": "address", "name": "to", "type": "address"}, {"internalType": "uint256", "name": "value", "type": "uint256"}], "name": "mintTo", "outputs": [], "stateMutability": "nonpayable", "type": "function", }, { "inputs": [{"internalType": "uint256", "name": "value", "type": "uint256"}], "name": "burn", "outputs": [], "stateMutability": "nonpayable", "type": "function", }, ] w3 = Web3(Web3.HTTPProvider(RPC_URL)) print(f"Connected to Ethereum: {w3.is_connected()}") contract = w3.eth.contract(address=CONTRACT_ADDRESS, abi=contract_abi) your_address = w3.eth.account.from_key(PRIVATE_KEY).address print(f"Your Address: {your_address}") print(f"Your Balance: {contract.functions.balanceOf(your_address).call()}") ``` Execute the script using python: ```shell python3 -m pip install web3 && python3 scripts/main.py ``` ### Troubleshooting * **Contract Deployment Fails**: Ensure you have enough testnet tokens to cover the deployment cost. * **Interaction Issues**: Verify that the contract address and ABI are correct. * **Insufficient Balance**: Ensure your wallet has enough tokens to transfer. ### Next Steps Now that you’ve created and deployed your first token using Rust. This guide provides a comprehensive introduction to creating and deploying a token on the Pharos blockchain using Rust. If you encounte
프로젝트로 돌아가기