Skip to content

Commit

Permalink
remove user wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAleksaOpacic committed Sep 23, 2024
1 parent 4f37319 commit 6531643
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 69 deletions.
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"
}
}
40 changes: 29 additions & 11 deletions tasks/core/pair/burn.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { task } from "hardhat/config";
import type { Currency, UniswapV2Pair } from "../../../typechain-types";
import type { UserWallet } from "../../../typechain-types";
import {waitTillCompleted} from "@nilfoundation/niljs";
import {createClient} from "../../util/client";
import {shardNumber} from "@nilfoundation/hardhat-plugin/dist/utils/conversion";

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,19 +65,29 @@ 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
Expand Down
49 changes: 36 additions & 13 deletions tasks/core/pair/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ import { task } from "hardhat/config";
import type {
Currency,
UniswapV2Pair,
UserWallet,
} from "../../../typechain-types";
import {createClient} from "../../util/client";
import {waitTillCompleted} from "@nilfoundation/niljs";
import {shardNumber} from "@nilfoundation/hardhat-plugin/dist/utils/conversion";

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("pair", "The address of the pair 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 +48,31 @@ 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
50 changes: 35 additions & 15 deletions tasks/core/pair/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ import { task } from "hardhat/config";
import type {
Currency,
UniswapV2Pair,
UserWallet,
} from "../../../typechain-types";
import {createClient} from "../../util/client";
import {waitTillCompleted} from "@nilfoundation/niljs";
import {shardNumber} from "@nilfoundation/hardhat-plugin/dist/utils/conversion";

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 +69,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 +81,40 @@ 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
24 changes: 24 additions & 0 deletions tasks/util/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { HttpTransport, LocalECDSAKeySigner, PublicClient, WalletV1 } from "@nilfoundation/niljs";

export async function createClient(): Promise<{ wallet: WalletV1; publicClient: PublicClient }> {
const publicClient = new PublicClient({
transport: new HttpTransport({
endpoint: process.env.NIL_RPC_ENDPOINT!,
}),
shardId: 1,
});

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

const wallet = new WalletV1({
pubkey: pubkey,
address: process.env.WALLET_ADDR!,
client: publicClient,
signer,
});

return { wallet, publicClient };
}

0 comments on commit 6531643

Please sign in to comment.