Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement/tidy core logic #13

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

# Specify the RPC endpoint of your cluster
# For example, if your cluster's RPC endpoint is at "http://127.0.0.1:8529", set it as below
NIL_RPC_ENDPOINT: "http://127.0.0.1:8529"
NIL_RPC_ENDPOINT= "http://127.0.0.1:8529"

# Specify the private key used for signing transactions
# This should be a hexadecimal string corresponding to your account's private key
PRIVATE_KEY: ""
PRIVATE_KEY= ""

# Specify the wallet address associated with your private key
# Wallets can be created using the =nil; CLI
# This address will be used for transactions on the =nil; network
WALLET_ADDR: "0x"
WALLET_ADDR= "0x"
11 changes: 6 additions & 5 deletions contracts/UniswapV2Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ contract UniswapV2Factory is IUniswapV2Factory {
function createPair(
address tokenA,
address tokenB,
uint256 salt
uint256 salt,
uint256 shard
) public returns (address pair) {
require(tokenA != tokenB, "UniswapV2: IDENTICAL(_ADDRESSES");
(address token0, address token1) = tokenA < tokenB
Expand All @@ -39,8 +40,8 @@ contract UniswapV2Factory is IUniswapV2Factory {
require(
getPair[token0][token1] == address(0),
"UniswapV2: PAIR_EXISTS"
); // single check is sufficient
pair = address(deployPair(salt));
);
pair = address(deployPair(shard, salt));


getPair[token0][token1] = pair;
Expand Down Expand Up @@ -72,9 +73,9 @@ contract UniswapV2Factory is IUniswapV2Factory {
feeToSetter = _feeToSetter;
}

function deployPair(uint256 salt) private returns (address deployedAddress) {
function deployPair(uint256 shard, uint256 salt) private returns (address deployedAddress) {
bytes memory code = abi.encodePacked(type(UniswapV2Pair).creationCode, abi.encode(msg.sender));
address contractAddress = Nil.createAddress(1, code, salt);
address contractAddress = Nil.createAddress(shard, code, salt);
Nil.asyncCall(contractAddress, address(0), msg.sender, 0, 0, true, 0, abi.encodePacked(code, salt));
return contractAddress;
}
Expand Down
4 changes: 1 addition & 3 deletions contracts/UniswapV2Pair.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ contract UniswapV2Pair is NilCurrencyBase, IUniswapV2Pair {

if (_totalSupply == 0) {
liquidity = Math.sqrt(amount0.mul(amount1)).sub(MINIMUM_LIQUIDITY);
mintCurrencyInternal(MINIMUM_LIQUIDITY);
burnCurrencyInternal(MINIMUM_LIQUIDITY); // permanently lock the first MINIMUM_LIQUIDITY
totalSupply = totalSupply + liquidity; // permanently lock the first MINIMUM_LIQUIDITY
} else {
liquidity = Math.min(
amount0.mul(_totalSupply) / _reserve0,
Expand Down Expand Up @@ -147,7 +146,6 @@ contract UniswapV2Pair is NilCurrencyBase, IUniswapV2Pair {
"UniswapV2: INSUFFICIENT_LIQUIDITY_BURNED"
);
burnCurrencyInternal(liquidity);
totalSupply -= liquidity;
_safeTransfer(tokenId0, to, amount0);
_safeTransfer(tokenId1, to, amount1);

Expand Down
2 changes: 1 addition & 1 deletion contracts/UniswapV2Router01.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ contract UniswapV2Router01 is IUniswapV2Router01, NilCurrencyBase {
uint salt
) external override ensure(deadline) returns (uint amountA, uint amountB, uint liquidity) {
if (IUniswapV2Factory(factory).getTokenPair(tokenA, tokenB) == address(0)) {
IUniswapV2Factory(factory).createPair(tokenA, tokenB, salt);
IUniswapV2Factory(factory).createPair(tokenA, tokenB, 1, salt);
}
address pair = IUniswapV2Factory(factory).getTokenPair(tokenA, tokenB);
uint tokenAId = NilCurrencyBase(tokenA).getCurrencyId();
Expand Down
20 changes: 20 additions & 0 deletions contracts/UserWallet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// In another contract, e.g., MainContract.sol
import "@nilfoundation/smart-contracts/contracts/Wallet.sol";

contract UserWallet is NilCurrencyBase {
Wallet public wallet;

constructor(bytes memory _pubkey) {
wallet = new Wallet(_pubkey);
}
receive() external payable {}

/**
* @dev Sends currency to a specified address
* This is a workaround until we are able to send external messages to smart contracts
* For production, consider implementing access control, such as Ownable from OpenZeppelin
*/
function sendCurrencyPublic(address to, uint256 currencyId, uint256 amount) public {
sendCurrencyInternal(to, currencyId, amount);
}
}
3 changes: 2 additions & 1 deletion contracts/interfaces/IUniswapV2Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ interface IUniswapV2Factory {
function createPair(
address tokenA,
address tokenB,
uint256 salt
uint256 salt,
uint256 shard
) external returns (address pair);

function setFeeTo(address) external;
Expand Down
24 changes: 11 additions & 13 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import "@nomicfoundation/hardhat-chai-matchers";
import "@nomicfoundation/hardhat-ignition-ethers";
import "@nomicfoundation/hardhat-ethers";
import "@typechain/hardhat";
import "@nomicfoundation/hardhat-ignition-ethers";
import "@typechain/hardhat";
import "@nilfoundation/hardhat-plugin";
import type { NilHardhatUserConfig } from "@nilfoundation/hardhat-plugin";
import * as dotenv from "dotenv";
import "@nilfoundation/hardhat-plugin";

// Token Tasks
// Currency Tasks
import "./tasks/currency/info";
import "./tasks/currency/send";
import "./tasks/currency/mint";
import "./tasks/currency/mint-wallet";

// Other Tasks
import "./tasks/initialize";
import "./tasks/sync";
import "./tasks/skim";
import "./tasks/burn";
import "./tasks/get_pair";

import "./tasks/pair_reserves";
import "./tasks/flow_deploy_factory";
import "./tasks/flow_swap";
// Core Tasks
import "./tasks/core/pair/get-reserves";
import "./tasks/core/pair/mint";
import "./tasks/core/pair/burn";
import "./tasks/core/pair/swap";
import "./tasks/core/factory/get-pair";
import "./tasks/core/factory/create-pair";

dotenv.config();

Expand Down
7 changes: 7 additions & 0 deletions ignition/modules/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

module.exports = buildModule("DeployUser", (m) => {
const token = m.contract("UserWallet", [m.getParameter("publicKey", "")]);

return { token };
});
3 changes: 3 additions & 0 deletions ignition/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
},
"DeployUniswapV2Router01": {
"factory": "0x0000000000000000000000000000000000000000"
},
"DeployUser": {
"publicKey": "0x0000000000000000000000000000000000000000"
}
}
Loading
Loading