bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 18쪽 중 15쪽
m.startBroadcast(deployerPrivateKey);
Token token = new Token(1000000); // Initial supply of 1,000,000 tokens
vm.stopBroadcast();
}
}
```
Set the private key:
```shell
export PRIVATE_KEY=<your private key>
```
Deploy the Contract:
* Use forge to deploy the contract to the Pharos Testnet:
```shell
forge script script/DeployToken.s.sol --rpc-url <PHAROS_RPC_URL> --broadcast
```
### Step 5: Verify the Token Contract
* For Atlantic Testnet
```shell
forge verify-contract \
<contract_address> \
--constructor-args <abi_encoded_args> \
src/Token.sol:Token \
--chain-id 688688 \
--verifier blockscout \
--verifier-url https://api.socialscan.io/pharos-atlantic-testnet/v1/explorer/command_api/contract
```
* For Testnet
```shell
forge verify-contract \
<contract_address> \
--constructor-args <abi_encoded_args> \
src/Token.sol:Token \
--chain-id 688689 \
--verifier blockscout \
--verifier-url https://api.socialscan.io/pharos-atlantic-testnet/v1/explorer/command_api/contract
```
> Note: Replace with your token contract address
### Step 6: Interact with the Token Contract
Open `script/InteractToken.s.sol` and add the following code:
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import {Script, console} from "forge-std/Script.sol";
import {Token} from "../src/Token.sol";
contract InteractToken is Script {
function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
Token token = Token(address(0x00)); // Replace with your token contract address
// Check balance
uint256 balance = token.balanceOf(msg.sender);
console.log("Balance:", balance);
// Transfer tokens
token.transfer(address(0x00), 100); // Replace with recipient address and amount
console.log("Tokens transferred");
vm.stopBroadcast();
}
}
```
Execute the script using forge:
```shell
forge script script/InteractToken.s.sol --rpc-url <PHAROS_RPC_URL> --broadcast
```
### 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 Foundry.
This guide provides a comprehensive introduction to creating and deploying a token on the Pharos blockchain using Foundry. If you encounter any issues, refer to the Troubleshooting section or consult the Foundry 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 Foundry
* [Foundry](https://book.getfoundry.sh/getting-started/installation)
### Setup 2: Set Up the Project
Clone the example repo:
```shell
git clone https://github.com/PharosNetwork/examples
cd examples/nft/foundry/contract
```
Install OpenZeppelin Contracts:
* Foundry uses forge to manage dependencies. Install OpenZeppelin contracts:
```shell
forge install OpenZeppelin/openzeppelin-contracts --no-git
```
### Setup 3: Write the NFT Contract
Create a New Solidity File:
* Create a new file for your NFT contract:
```shell
touch src/Token.sol
```
Write the Token Contract:
* Open `src/Token.sol` in your favorite text editor and add the following code:
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {ERC721} from "openzeppelin-contracts/contracts/token/ERC721/ERC721.sol";
contract Token is ERC721 {
constructor() ERC721("Token", "MTK") {}
}
```
Compile the Smart Contract:
* Use forge to compile the contract:
```shell
forge build
```
Test the Smart Contract
* Use forge to test the contract:
```shell
forge test
```
### Step 4: Deploy the Token Contract
Set the private key:
```shell
export PRIVATE_KEY=<your private key>
```
Create a Deployment Script:
* Create a new file for the deployment script:
```shell
touch script/DeployToken.s.sol
```
Write the Deployment Script:
* Open script/DeployToken.s.sol and add the following code:
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "forge-std/Script.sol";
import "../src/Token.sol";
contract DeployToken is Script {
function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
Token token = new Token(1000000); // Initial supply of 1,000,000 tokens
vm.stopBroadcast();
}
}
```
Deploy the Contract:
* Use forge to deploy the contract to the Pharos Testnet:
```shell
forge script script/DeployToken.s.sol --rpc-url <PHAROS_RPC_URL> --broadcast
```
### Step 5: Verify the Token Contract
* For Atlantic Testnet
```shell
forge verify-contract \
<contract_address> \
--constructor-args <abi_encoded_args> \
src/Token.sol:Token \
--chain-id 688688 \
--verifier blockscout \
--verifier-url https://api.socialscan.io/pharos-atlantic-testnet/v1/explorer/command_api/contract
```
* For Testnet
```shell
forge verify-contract \
<contract_address> \
--constructor-args <abi_encoded_args> \
src/Token.sol:Token \
--chain-id 688688 \
--verifier blockscout \
--verifier-url https://api.socialscan.io/pharos-testnet/v1/explorer/command_api/contract
```
> Note: Replace with your token contract address
### Step 6: Interact with the NFT Contract
Open `script/InteractToken.s.sol` and add the following code:
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import {Script, console} from "forge-std/Script.sol";
import {Token} from "../src/Token.sol";
contract InteractToken is Script {
function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
Token token = Token(address(0x00)); // Replace with your token contract address
// Check balance
uint256 balance = token.balanceOf(msg.sender);
console.log("Balance:", balance);
// Transfer tokens
token.transfer(address(0x00), 100); // Replace with recipient address and amount
console.log("Tokens transferred");
vm.stopBroadcast();
}
}
```
Execute the script using forge:
```shell
forge script script/InteractToken.s.sol --rpc-url <PHAROS_RPC_URL> --broadcast
```
### 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 Foundry.
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 Foundry 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 Foundry
* [Foundry](https://book.getfoundry.sh/getting-started/installation)
### Setup 2: Set Up the Project
Clone the example repo:
```shell
git clone https://github.com/PharosNetwork/examples
cd examples/uniswap/foundry/contract
```
Install OpenZeppelin Contracts:
* Foundry uses forge to manage dependencies. Install OpenZeppelin contracts:
```shell
forge install OpenZeppelin/openzeppelin-contracts --no-git
```
### Step 3: Write the Uniswap Contract
Create a New Solidity File:
* Create a new file for your uniswap contract:
```shell
touch src/Uniswap.sol
```
Write the uniswap Contract:
* Open `src/Uniswap.sol` in your favorite text editor and add the following code:
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "openzeppelin-contracts/contracts/token/ERC20/IERC20.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:
* Use forge to compile the contract:
```shell
forge build
```
Test the Smart Contract
* Use forge to test the contract:
```shell
forge test
```
### Step 4: Deploy the Uniswap Contract
Create a Deployment Script:
* Create a new file for the deployment script:
```shell
touch script/DeployUniswap.s.sol
```
Write the Deployment Script:
* Open script/DeployUniswap.s.sol and add the following code:
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "forge-std/Script.sol";
import "../src/Uniswap.sol";
contract DeployUniswap is Script {
function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
address tokenA = address(0xAA); // Replace with the address of token A
address tokenB = address(0xBB); // Replace with the address of token B
Uniswap uniswap = new Uniswap(tokenA, tokenB);
vm.stopBroadcast();
}
}
```
Set the private key:
```shell
export PRIVATE_KEY=<your private key>
```
Deploy the Contract:
* Use forge to deploy the contract to the Pharos Testnet:
```shell
forge script script/DeployUniswap.s.sol --rpc-url <PHAROS_RPC_URL> --broadcast
```
### Step 5: Interact with the Uniswap Contract
Open `script/InteractUniswap.s.sol` and add the following code:
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "forge-std/Script.sol";
import "../src/Uniswap.sol";
import "../test/Uniswap.t.sol";
contract DeployUniswap is Script {
function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
MockERC20 tokenA = new MockERC20("TokenA", "TKA");
MockERC20 tokenB = new MockERC20("TokenB", "TKB");
Uniswap uniswap = new Uniswap(address(tokenA), address(tokenB));
console.log("TokenA deployed at:", address(tokenA));
console.log("TokenB deployed at:", address(tokenB));
console.log("Uniswap deployed at:", address(uniswap));
vm.stopBroadcast();
}
}
```
Execute the script using forge:
```shell
forge script script/InteractUniswap.s.sol --rpc-url <PHAROS_RPC_URL> --broadcast
```
### 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 Foundry. If you encounter any issues, refer to the Foundry documentation or the troubleshooting section. Happy building! 🚀
# Hardhat
This guide is suitable for developers who want to start building dApps using Pharos and hardhat toolchains. If you are a new user of Ethereum, please consider researching the [Ethereum documentation](https://ethereum.org/en/developers/docs/) before continuing.
## Write Your First Transaction
This guide will walk you through the process of sending your first transaction on the Pharos blockchain using three popular libraries: `ethers.js`, `web3.js`, and `web3.py`. By the end of this guide, you will understand how to send a transaction programmatically using these libraries.
### Prerequisites
Before you begin, ensure you have the following:
* Git: Used for code management and obtain examples.
* Pharos Devnet/Testnet Access: Access to a Pharos node (local or remote) for interacting with the blockchain.
### Setup 1: Pick Your Favorite Contract Develop Framework and IDE
Install your preferred contract framework from the below list:
* ethers.js
* web3.js
* web3.py
### Setup 2: Set Up the Project
Clone the example repo and add dependencies.
```shell
git clone https://github.com/PharosNetwork/examples
cd examples/transaction/ethersjs
npm install
```
```javascript
const { ethers } = require("ethers");
// Connect to Pharos Testnet
const provider = new ethers.providers.JsonRpcProvider("<PHAROS_RPC_URL>");
// Note: Your wallet private key (for testnet only, never expose this in production)
const privateKey = "YOUR_PRIVATE_KEY";
const wallet = new ethers.Wallet(privateKey, provider);
// Transaction details
const tx = {
to: "RECIPIENT_ADDRESS", // Note: Replace with the recipient's address
value: ethers.utils.parseEther("0.1"), // Amount to send (0.1 PHAR)
};
// Send the transaction
wallet.sendTransaction(tx)
.then((transaction) => {
console.log("Transaction sent:", transaction.hash);
})
.catch((error) => {
console.error("Error sending transaction:", error);
});
```
### Step 3: Run the Script
Run the script using Node.js:
```shell
node index.js
```
### Key Points
* **Private Key**: Never expose your private key in production. Use environment variables or secure storage.
* **Recipient Address**: Replace RECIPIENT\_ADDRESS with the actual recipient's wallet address.
* **Gas Limit**: Ensure you include a sufficient gas limit for the transaction.
### Troubleshooting
* **Transaction Fails**: Ensure you have enough testnet tokens to cover the transaction fee.
* **Incorrect Network**: Double-check that you are connected to the Pharos Testnet.
* **Invalid Address**: Verify that the recipient's address is correct.
### Conclusion
Now that you’ve sent your first transaction using ethers.js, web3.js, and web3.py.
This guide provides a comprehensive introduction to sending transactions on the Pharos blockchain using three popular libraries. If you encounter any issues, refer to the Troubleshooting section or consult the respective library's documentation. Happy building! 🚀
# Write Your First dApp
This guide will walk you through the process of building and deploying your first end-to-end decentralized application (dApp) on the Pharos blockchain. By the end of this guide, you will have a fully functional dApp that includes a smart contract, a backend, and a frontend.
### 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/dapp/hardhat
npm install
```
### Setup 3: Deploy the Smart Contract
Set the private key:
```shell
npx hardhat vars set PRIVATE_KEY
```
Then
```shell
cd contract && npm install
npx hardhat ignition deploy ./ignition/modules/Counter.js --network pharos
```
### Setup 4: Build the Frontend
Connect the Frontend to the Smart Contract:
* Open `src/app.jsx` and replace its content with the following code:
```jsx
import React, { useState } from "react";
import { ethers } from "ethers";
const contractAddress = "YOUR_CONTRACT_ADDRESS"; // Replace with your contract address
const abi = [
{
"inputs": [],
"name": "get",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "x",
"type": "uint256"
}
],
"name": "set",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
function App() {
const [value, setValue] = useState("");
const [storedValue, setStoredValue] = useState("");
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
const handleSet = async () => {
await contract.set(value);
alert("Value set!");
};
const handleGet = async () => {
const result = await contract.get();
setStoredValue(result.toString());
};
return (
<div>
<h1>Simple Storage dApp</h1>
<input
type="text"
value={value}
onChange={(e) => setValue(e.target.value)}
placeholder="Enter a value"
/>
<button onClick={handleSet}>Set Value</button>
<button onClick={handleGet}>Get Value</button>
<p>Stored Value: {storedValue}</p>
</div>
);
}
export default App;
```
Replace Placeholder Values
* Replace `YOUR_CONTRACT_ADDRESS` with the address of your deployed contract.
### Step 5: Run the dApp
Start the React App
* Navigate to the frontend directory and start the app:
```shell
npm start
```
Interact with the dApp:
* Open your browser and navigate to `http://localhost:3000`.
* Use the input field and buttons to interact with your smart contract.
### Troubleshooting
* Contract Deployment Fails: Ensure you have enough testnet tokens to cover the deployment cost.
* Frontend Connection Issues: Verify that the contract address and ABI are correct.
### Conclusion
Now that you’ve built your first end-to-end dApp.
This guide provides a comprehensive introduction to building and deploying an end-to-end dApp on the Pharos blockchain. If you encounter any issues, refer to the Troubleshooting section or consult the Pharos documentation. Happy building! 🚀
# Write Your First Token
This guide will walk you through the process of creating and deploying your first token on the Pharos blockchain. 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.
* 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/token/hardhat/contract
``프로젝트로 돌아가기