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

remove user wallet #15

Merged
merged 2 commits into from
Sep 24, 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
20 changes: 0 additions & 20 deletions contracts/UserWallet.sol

This file was deleted.

7 changes: 0 additions & 7 deletions ignition/modules/User.ts

This file was deleted.

3 changes: 0 additions & 3 deletions ignition/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@
},
"DeployUniswapV2Router01": {
"factory": "0x0000000000000000000000000000000000000000"
},
"DeployUser": {
"publicKey": "0x0000000000000000000000000000000000000000"
}
}
39 changes: 26 additions & 13 deletions tasks/core/pair/burn.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { shardNumber } from "@nilfoundation/hardhat-plugin/dist/utils/conversion";
import { waitTillCompleted } from "@nilfoundation/niljs";
import { task } from "hardhat/config";
import type { Currency, UniswapV2Pair } from "../../../typechain-types";
import type { UserWallet } from "../../../typechain-types";
import { createClient } from "../../util/client";

task("burn", "Burn liquidity tokens and print balances and reserves")
.addParam("pair", "The address of the pair contract")
.addParam("wallet", "The address to transfer the burned tokens to")
.setAction(async (taskArgs, hre) => {
const walletAddress = process.env.WALLET_ADDR;

if (!walletAddress) {
throw new Error("WALLET_ADDR is not set in environment variables");
}

const { wallet, publicClient } = await createClient();

// Destructure parameters for clarity
const pairAddress = taskArgs.pair;
const walletAddress = taskArgs.wallet;

// Attach to the Uniswap V2 Pair contract
const Pair = await hre.ethers.getContractFactory("UniswapV2Pair");
Expand Down Expand Up @@ -57,20 +65,25 @@ task("burn", "Burn liquidity tokens and print balances and reserves")
userBalanceToken1.toString(),
);

// Attach to the UserWallet contract
const UserFactory = await hre.ethers.getContractFactory("UserWallet");
const user = UserFactory.attach(walletAddress) as UserWallet;

const lpAddress = await pair.getCurrencyId();
const userLpBalance = await pair.getCurrencyBalanceOf(walletAddress);
console.log("Total LP balance for user wallet:", userLpBalance.toString());

// Send LP tokens to the user wallet
await user.sendCurrencyPublic(
pairAddress.toLowerCase(),
lpAddress,
userLpBalance,
);
const hash = await wallet.sendMessage({
// @ts-ignore
to: pairAddress,
feeCredit: BigInt(10_000_000),
value: BigInt(0),
refundTo: walletAddress,
tokens: [
{
id: lpAddress,
amount: BigInt(userLpBalance),
},
],
});

await waitTillCompleted(publicClient, shardNumber(walletAddress), hash);

// Execute burn
console.log("Executing burn...");
Expand Down
50 changes: 34 additions & 16 deletions tasks/core/pair/mint.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { shardNumber } from "@nilfoundation/hardhat-plugin/dist/utils/conversion";
import { waitTillCompleted } from "@nilfoundation/niljs";
import { task } from "hardhat/config";
import type {
Currency,
UniswapV2Pair,
UserWallet,
} from "../../../typechain-types";
import type { Currency, UniswapV2Pair } from "../../../typechain-types";
import { createClient } from "../../util/client";

task("mint", "Mint currencies and add liquidity to the pair")
.addParam("pair", "The address of the pair contract")
.addParam("wallet", "The address of the user contract")
.addParam("amount0", "The amount of the first currency to mint")
.addParam("amount1", "The amount of the second currency to mint")
.setAction(async (taskArgs, hre) => {
const walletAddress = process.env.WALLET_ADDR;

if (!walletAddress) {
throw new Error("WALLET_ADDR is not set in environment variables");
}

const { wallet, publicClient } = await createClient();

// Destructure parameters for clarity
const pairAddress = taskArgs.pair;
const walletAddress = taskArgs.wallet;
const amount0 = taskArgs.amount0;
const amount1 = taskArgs.amount1;

Expand All @@ -40,16 +45,29 @@ task("mint", "Mint currencies and add liquidity to the pair")
const currency1Id = await currency1.getCurrencyId();
console.log("Currency 1 ID:", currency1Id);

// Attach to the UserWallet contract
const UserFactory = await hre.ethers.getContractFactory("UserWallet");
const user = UserFactory.attach(walletAddress) as UserWallet;

// Send currency amounts to the pair contract
console.log(`Sending ${amount0} of currency0 to ${pairAddress}...`);
await user.sendCurrencyPublic(pairAddress, currency0Id, amount0);

console.log(`Sending ${amount1} of currency1 to ${pairAddress}...`);
await user.sendCurrencyPublic(pairAddress, currency1Id, amount1);
console.log(
`Sending ${amount0} currency0 and ${amount1} currency1 to ${pairAddress}...`,
);
const hash = await wallet.sendMessage({
// @ts-ignore
to: pairAddress,
feeCredit: BigInt(10_000_000),
value: BigInt(0),
refundTo: wallet.address,
tokens: [
{
id: currency0Id,
amount: BigInt(amount0),
},
{
id: currency1Id,
amount: BigInt(amount1),
},
],
});

await waitTillCompleted(publicClient, shardNumber(walletAddress), hash);

// Log balances in the pair contract
const pairCurrency0Balance =
Expand Down
51 changes: 32 additions & 19 deletions tasks/core/pair/swap.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { shardNumber } from "@nilfoundation/hardhat-plugin/dist/utils/conversion";
import { waitTillCompleted } from "@nilfoundation/niljs";
import { task } from "hardhat/config";
import type {
Currency,
UniswapV2Pair,
UserWallet,
} from "../../../typechain-types";
import type { Currency, UniswapV2Pair } from "../../../typechain-types";
import { createClient } from "../../util/client";

task("swap", "Swap currency0 for currency1 in the Uniswap pair")
.addParam("pair", "The address of the Uniswap pair contract")
.addParam("wallet", "The address of the user which is swapping")
.addParam("amount", "The amount of currency0 to swap")
.setAction(async (taskArgs, hre) => {
const walletAddress = process.env.WALLET_ADDR;

if (!walletAddress) {
throw new Error("WALLET_ADDR is not set in environment variables");
}

const { wallet, publicClient } = await createClient();

// Destructure parameters for clarity
const userWalletAddress = taskArgs.wallet.toLowerCase();
const pairAddress = taskArgs.pair.toLowerCase();
const swapAmount = BigInt(taskArgs.amount);

Expand Down Expand Up @@ -61,9 +66,9 @@ task("swap", "Swap currency0 for currency1 in the Uniswap pair")

// Log balances before the swap
const balanceCurrency0Before =
await currency0Contract.getCurrencyBalanceOf(userWalletAddress);
await currency0Contract.getCurrencyBalanceOf(walletAddress);
const balanceCurrency1Before =
await currency1Contract.getCurrencyBalanceOf(userWalletAddress);
await currency1Contract.getCurrencyBalanceOf(walletAddress);
console.log(
"Balance of currency0 before swap:",
balanceCurrency0Before.toString(),
Expand All @@ -73,28 +78,36 @@ task("swap", "Swap currency0 for currency1 in the Uniswap pair")
balanceCurrency1Before.toString(),
);

// Attach to the UserWallet contract
const UserWalletFactory = await hre.ethers.getContractFactory("UserWallet");
const userWallet = UserWalletFactory.attach(
userWalletAddress,
) as UserWallet;
const hash = await wallet.sendMessage({
// @ts-ignore
to: pairAddress,
feeCredit: BigInt(10_000_000),
value: BigInt(0),
refundTo: wallet.address,
tokens: [
{
id: currency0Id,
amount: BigInt(swapAmount),
},
],
});

await waitTillCompleted(publicClient, shardNumber(walletAddress), hash);

// Send currency0 to the pair contract
await userWallet.sendCurrencyPublic(pairAddress, currency0Id, swapAmount);
console.log(
`Sent ${swapAmount.toString()} of currency0 to the pair contract.`,
);

// Execute the swap
console.log("Executing swap...");
await pair.swap(0, expectedOutputAmount, userWalletAddress);
await pair.swap(0, expectedOutputAmount, walletAddress);
console.log("Swap executed successfully.");

// Log balances after the swap
const balanceCurrency0After =
await currency0Contract.getCurrencyBalanceOf(userWalletAddress);
await currency0Contract.getCurrencyBalanceOf(walletAddress);
const balanceCurrency1After =
await currency1Contract.getCurrencyBalanceOf(userWalletAddress);
await currency1Contract.getCurrencyBalanceOf(walletAddress);
console.log(
"Balance of currency0 after swap:",
balanceCurrency0After.toString(),
Expand Down
38 changes: 38 additions & 0 deletions tasks/util/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
HttpTransport,
LocalECDSAKeySigner,
PublicClient,
WalletV1,
} from "@nilfoundation/niljs";

export async function createClient(): Promise<{
wallet: WalletV1;
publicClient: PublicClient;
}> {
const walletAddress = process.env.WALLET_ADDR;

if (!walletAddress) {
throw new Error("WALLET_ADDR is not set in environment variables");
}

const publicClient = new PublicClient({
transport: new HttpTransport({
endpoint: walletAddress,
}),
shardId: 1,
});

const signer = new LocalECDSAKeySigner({
privateKey: `0x${process.env.PRIVATE_KEY}`,
});
const pubkey = await signer.getPublicKey();

const wallet = new WalletV1({
pubkey: pubkey,
address: walletAddress,
client: publicClient,
signer,
});

return { wallet, publicClient };
}
Loading