Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Dec 21, 2024
1 parent ae805c7 commit 6c8a85c
Show file tree
Hide file tree
Showing 11 changed files with 12,828 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RPC_URL=
PRIVATE_KEY=
28 changes: 28 additions & 0 deletions checkDeployment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require('dotenv').config();
const { ethers } = require('ethers');

const RPC_URL = process.env.RPC_URL;

const provider = new ethers.providers.JsonRpcProvider(RPC_URL);

// デプロイされたアドレス
const factoryAddress = '0xbF603B840D958FAD0021872506b64a66d613F824';
const pairAddress = '0xe86eF760CD9EB0D6627e6A7776b31e85F5c996F5';
const erc20Address = '0xF130DCa1d24b58378a6f0d521eB4189f54897372';

const checkDeployment = async () => {
try {
// コントラクトの検証
const codeFactory = await provider.getCode(factoryAddress);
const codePair = await provider.getCode(pairAddress);
const codeERC20 = await provider.getCode(erc20Address);

console.log(`Factory contract deployed: ${codeFactory !== '0x' ? 'Yes' : 'No'}`);
console.log(`Pair contract deployed: ${codePair !== '0x' ? 'Yes' : 'No'}`);
console.log(`ERC20 contract deployed: ${codeERC20 !== '0x' ? 'Yes' : 'No'}`);
} catch (error) {
console.error('Error checking deployment:', error);
}
};

checkDeployment();
43 changes: 43 additions & 0 deletions checkPair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require('dotenv').config();
const { ethers } = require('ethers');
const fs = require('fs');
const path = require('path');

// RPC URL
const RPC_URL = process.env.RPC_URL;

if (!RPC_URL) {
console.error('Please ensure RPC_URL is set in the .env file');
process.exit(1);
}

const provider = new ethers.providers.JsonRpcProvider(RPC_URL);

// Factory コントラクトのアドレスとABI
const factoryAddress = '0xbF603B840D958FAD0021872506b64a66d613F824'; // Replace with your Factory contract address
const factoryABI = JSON.parse(
fs.readFileSync(path.resolve(__dirname, 'build/UniswapV2Factory.json'), 'utf8')
).abi;

// トークンアドレス
const tokenA = '0x73d53c0e08baa93362c4825b836dbce45855abf4';
const tokenB = '0x6497ce5dbe25ad343bef8a5be82c19802ba7cd71';

const checkPair = async () => {
try {
const factoryContract = new ethers.Contract(factoryAddress, factoryABI, provider);

console.log(`Checking pair for tokens ${tokenA} and ${tokenB}...`);
const pairAddress = await factoryContract.getPair(tokenA, tokenB);

if (pairAddress !== ethers.constants.AddressZero) {
console.log(`Pair exists at address: ${pairAddress}`);
} else {
console.log('No pair exists for the given tokens.');
}
} catch (error) {
console.error('Error checking pair:', error);
}
};

checkPair();
53 changes: 53 additions & 0 deletions checkTransaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require('dotenv').config();
const { ethers } = require('ethers');
const fs = require('fs');
const path = require('path');

// Load environment variables
const RPC_URL = process.env.RPC_URL;

if (!RPC_URL) {
console.error('Please ensure RPC_URL is set in the .env file');
process.exit(1);
}

// Initialize provider
const provider = new ethers.providers.JsonRpcProvider(RPC_URL);

// Load compiled Factory contract
const loadContract = (fileName) => {
const filePath = path.resolve(__dirname, 'build', fileName);
const contractJson = fs.readFileSync(filePath, 'utf8');
return JSON.parse(contractJson);
};

// Specify the transaction hash
const txHash = '0xd74807dadf593dbd2d2c7bc9b191515b29f71c675743cd55fc7c2705413037d2'; // Replace with your transaction hash

const checkTransaction = async () => {
try {
const receipt = await provider.getTransactionReceipt(txHash);
console.log('Transaction Receipt:', receipt);

// Load Factory contract ABI
const factoryJson = loadContract('UniswapV2Factory.json');
const factoryAddress = '0x9184676B5E9a2Fc02e1aEb019cD4DAC60CA89e0C'; // Replace with your Factory contract address

const factoryContract = new ethers.Contract(factoryAddress, factoryJson.abi, provider);

// Parse logs to find the event
const event = receipt.logs.find(log =>
log.address.toLowerCase() === factoryContract.address.toLowerCase()
);

if (event) {
console.log('Event found:', event);
} else {
console.log('No relevant event found in the logs.');
}
} catch (error) {
console.error('Error checking transaction logs:', error);
}
};

checkTransaction();
72 changes: 72 additions & 0 deletions compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const path = require('path');
const fs = require('fs-extra');
const solc = require('solc');

// Set the path to the contracts directory
const contractsPath = path.resolve(__dirname, 'contracts');

// Set the path to the build directory
const buildPath = path.resolve(__dirname, 'build');

// Remove the existing build directory if it exists
fs.removeSync(buildPath);

// Create the build directory
fs.ensureDirSync(buildPath);

// Read all Solidity files in the contracts directory
const contractFiles = fs.readdirSync(contractsPath).filter(file => file.endsWith('.sol'));

// Read and compile each contract
contractFiles.forEach(file => {
const filePath = path.resolve(contractsPath, file);
const source = fs.readFileSync(filePath, 'utf8');

const input = {
language: 'Solidity',
sources: {
[file]: {
content: source,
},
},
settings: {
evmVersion: 'byzantium',
outputSelection: {
'*': {
'*': ['abi', 'evm.bytecode.object'],
},
},
},
};

console.log(`Compiling ${file}...`);

// Import callback to resolve relative paths
function findImports(importPath) {
const resolvedPath = path.resolve(contractsPath, importPath);
if (fs.existsSync(resolvedPath)) {
return { contents: fs.readFileSync(resolvedPath, 'utf8') };
} else {
return { error: `File not found: ${importPath}` };
}
}

const output = JSON.parse(solc.compile(JSON.stringify(input), { import: findImports }));

// Check for compilation errors
if (output.errors) {
output.errors.forEach(err => {
console.error(err.formattedMessage);
});
process.exit(1); // Exit on compilation error
}

// Save compiled contract output to the build directory
const contracts = output.contracts[file];
for (let contractName in contracts) {
const contractOutput = contracts[contractName];
const outputFilePath = path.resolve(buildPath, `${contractName}.json`);
fs.outputJsonSync(outputFilePath, contractOutput);
console.log(`Compiled and saved: ${contractName}`);
}
});
11 changes: 6 additions & 5 deletions contracts/UniswapV2Factory.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pragma solidity =0.5.16;

import './interfaces/IUniswapV2Factory.sol';
import './UniswapV2Pair.sol';

contract UniswapV2Factory is IUniswapV2Factory {
address public feeTo;
Expand All @@ -25,14 +24,16 @@ contract UniswapV2Factory is IUniswapV2Factory {
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'UniswapV2: ZERO_ADDRESS');
require(getPair[token0][token1] == address(0), 'UniswapV2: PAIR_EXISTS'); // single check is sufficient

bytes memory bytecode = type(UniswapV2Pair).creationCode;

bytes memory bytecode = abi.encodePacked(
hex"608060405234801561001057600080fd5b5060405161054e38038061054e8339818101604052602081101561003357600080fd5b50516040805160018054600160a060020a0319169190921790556100ca806100596000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063c5af0dff14602d575b600080fd5b60336035565b005b60405163ffffffff909216815260200160405180910390f35b600054600160a060020a031681565b60006020528060005260406000206000915090505481565b600054600160a060020a0316fffea165627a7a7230582096b0a552a5e7b4e0e09c29f88076495e27db7c899f2a28c3d08951cba82762030029"
);

assembly {
pair := create(0, add(bytecode, 32), mload(bytecode))
}
require(pair != address(0), 'UniswapV2: FAILED_TO_CREATE_PAIR');
require(pair != address(0), "UniswapV2: PAIR_CREATION_FAILED");

IUniswapV2Pair(pair).initialize(token0, token1);
getPair[token0][token1] = pair;
getPair[token1][token0] = pair; // populate mapping in the reverse direction
allPairs.push(pair);
Expand Down
43 changes: 43 additions & 0 deletions createPair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require('dotenv').config();
const { ethers } = require('ethers');
const fs = require('fs');
const path = require('path');

// RPC URLと秘密鍵
const RPC_URL = process.env.RPC_URL;
const PRIVATE_KEY = process.env.PRIVATE_KEY;

if (!RPC_URL || !PRIVATE_KEY) {
console.error('Please ensure RPC_URL and PRIVATE_KEY are set in the .env file');
process.exit(1);
}

const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);

// Factory コントラクトのアドレスとABI
const factoryAddress = '0xbF603B840D958FAD0021872506b64a66d613F824'; // Replace with your Factory contract address
const factoryABI = JSON.parse(
fs.readFileSync(path.resolve(__dirname, 'build/UniswapV2Factory.json'), 'utf8')
).abi;

// トークンアドレス
const tokenA = '0x73d53c0e08baa93362c4825b836dbce45855abf4';
const tokenB = '0x6497ce5dbe25ad343bef8a5be82c19802ba7cd71';

const createPair = async () => {
try {
const factoryContract = new ethers.Contract(factoryAddress, factoryABI, wallet);

console.log(`Creating pair for tokens ${tokenA} and ${tokenB}...`);
const tx = await factoryContract.createPair(tokenA, tokenB);
console.log(`Transaction sent: ${tx.hash}`);

const receipt = await tx.wait();
console.log(`Transaction confirmed: ${receipt.transactionHash}`);
} catch (error) {
console.error('Error creating pair:', error);
}
};

createPair();
63 changes: 63 additions & 0 deletions deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require('dotenv').config();
const { ethers } = require('ethers');
const fs = require('fs');
const path = require('path');

// Load environment variables
const RPC_URL = process.env.RPC_URL;
const PRIVATE_KEY = process.env.PRIVATE_KEY;

if (!RPC_URL || !PRIVATE_KEY) {
console.error('Please ensure RPC_URL and PRIVATE_KEY are set in the .env file');
process.exit(1);
}

// Initialize provider and wallet
const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);

// Load compiled contracts
const loadContract = (fileName) => {
const filePath = path.resolve(__dirname, 'build', fileName);
const contractJson = fs.readFileSync(filePath, 'utf8');
return JSON.parse(contractJson);
};

// Deploy a contract
const deployContract = async (contractName, args = []) => {
const contractJson = loadContract(`${contractName}.json`);
const contractFactory = new ethers.ContractFactory(
contractJson.abi,
contractJson.evm.bytecode.object,
wallet
);

console.log(`Deploying ${contractName}...`);
const contract = await contractFactory.deploy(...args);
await contract.deployed();
console.log(`${contractName} deployed at: ${contract.address}`);
return contract.address;
};

// Main deployment function
const main = async () => {
try {
// Deploy UniswapV2Factory
const factoryAddress = await deployContract('UniswapV2Factory', [wallet.address]);

// Deploy UniswapV2ERC20 (Optional, for testing or standalone token deployment)
const erc20Address = await deployContract('UniswapV2ERC20');

// Deploy UniswapV2Pair (Will typically be created by the factory)
const pairAddress = await deployContract('UniswapV2Pair');

console.log('Deployment complete!');
console.log('Factory Address:', factoryAddress);
console.log('ERC20 Address (if deployed):', erc20Address);
console.log('Pair Address (if deployed):', pairAddress);
} catch (error) {
console.error('Error during deployment:', error);
}
};

main();
Loading

0 comments on commit 6c8a85c

Please sign in to comment.