Skip to content

Commit

Permalink
fix token seller scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
dvpublic committed May 1, 2024
1 parent 90f8cc9 commit 1e69a08
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 46 deletions.
1 change: 0 additions & 1 deletion contracts/token-seller/Univ3TokenSellerResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ contract Univ3TokenSellerResolver {
string public constant VERSION = "1.0.0";
//endregion ---------------------- Constants


//region ---------------------- Variables

IUniv3TokenSeller public tokenSeller;
Expand Down
12 changes: 7 additions & 5 deletions scripts/token-seller/DeployTokenSeller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ async function main() {
const net = await ethers.provider.getNetwork();
console.log(net, `network name="${network.name}"`);

const tokenA = FtmAddresses.WFTM_TOKEN;
const tokenB = FtmAddresses.USDC_LZ_TOKEN;
const tokenA = "0xe4436821e403e78a6dd62f7a9f5611f97a18f44c"; // sacra (token to sell)
const tokenB = "0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83"; // ftm

const POOL = "0xf6021f2f63ab3bcdddbaf32939cd66a8b9e4c21c"; // SpookySwap pool
const POOL = "0x56e837286dc7366ef6d6464d332ac6f9d32bc5a0"; // SpookySwap pool
const tickRangeSize = 10;
const SIGNER_AMOUNT_A = "1";
const thresholdRatio = "0.1";
const thresholdLastRefreshPoolSec = 100;
const thresholdLastRefreshPoolSec = 60;
const tickLimit = 40810;

const tokenSeller = await DeployTokenSellerUtils.deployTokenSeller(
signer,
Expand All @@ -31,7 +32,8 @@ async function main() {
SIGNER_AMOUNT_A,
tickRangeSize,
thresholdRatio,
thresholdLastRefreshPoolSec
thresholdLastRefreshPoolSec,
tickLimit
);
console.log("tokenSeller", tokenSeller);
writeFileSyncRestoreFolder(destPathTxt, `tokenSeller: ${tokenSeller}\n`, { encoding: 'utf8', flag: 'a' });
Expand Down
15 changes: 11 additions & 4 deletions scripts/token-seller/DeployTokenSellerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {SignerWithAddress} from "@nomiclabs/hardhat-ethers/signers";
import {parseUnits} from "ethers/lib/utils";
import {Univ3TokenSellerLib} from "../../typechain/contracts/token-seller/Univ3TokenSeller";
import TokenSellerConfigStruct = Univ3TokenSellerLib.TokenSellerConfigStruct;
import {BigNumber} from "ethers";
import {Misc} from "../utils/Misc";

export class DeployTokenSellerUtils {
public static async deployTokenSeller(
Expand All @@ -15,24 +17,29 @@ export class DeployTokenSellerUtils {
tickRangeSize: number = 1,
thresholdRatio: string = "0.1",
thresholdLastRefreshPoolSec: number = 1000,
tickLimit: number = 0
): Promise<string> {
const decimalsA = await IERC20Metadata__factory.connect(tokenA, signer).decimals();

const slot0 = await UniswapV3Pool__factory.connect(pool, signer).slot0();
const poolTokenA = await UniswapV3Pool__factory.connect(pool, signer).token0();
const poolSwapTokens = poolTokenA.toLowerCase() === tokenA;
const poolToken0 = await UniswapV3Pool__factory.connect(pool, signer).token0();
const poolSwapTokens = poolToken0.toLowerCase() !== tokenA.toLowerCase();

const sqrtPriceX96Limit = poolSwapTokens
? slot0.sqrtPriceX96.mul(100) // upper limit
: slot0.sqrtPriceX96.div(100); // lowest limit

// const q96 = BigNumber.from(2).pow(96);
// const priceReal = (sqrtPriceX96Limit.mul(Misc.ONE18).mul(sqrtPriceX96Limit).div(q96).div(q96)).toNumber();
// console.log("priceLimit", priceReal);

const config: TokenSellerConfigStruct = {
amountA: parseUnits(amountA, decimalsA),
thresholdLastRefreshPoolSec,
thresholdRatio: parseUnits(thresholdRatio, 5),
thresholdTokenB: 0,
tickRangeSize,
tickLimit: 0, // auto calculated
tickLimit,
}

const tokenSeller = (await DeployerUtils.deployContract(
Expand All @@ -42,7 +49,7 @@ export class DeployTokenSellerUtils {
tokenB,
pool,
config,
sqrtPriceX96Limit,
tickLimit === 0 ? sqrtPriceX96Limit : 0,
signer.address
)) as Univ3TokenSeller;

Expand Down
26 changes: 19 additions & 7 deletions scripts/token-seller/GetStatus.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {parseUnits} from "ethers/lib/utils";
import {formatUnits, parseUnits} from "ethers/lib/utils";
import {
IERC20Metadata__factory,
MockToken__factory,
UniswapV3Callee__factory,
UniswapV3Callee__factory, UniswapV3Pool__factory,
Univ3TokenSeller__factory
} from "../../typechain";
import {ethers, network} from "hardhat";
Expand All @@ -18,7 +18,8 @@ async function main() {
const POOL = "0x11aB7c23B6d05Aa08D69A1eD669Ad19f3eAC52d9";
const TOKEN_SELLER = "0x8E294388A3E91cdac5FEA95b5b75f509b85dC5E2";

const signer = (await ethers.getSigners())[0];
// const signer = (await ethers.getSigners())[0];
const signer = await Misc.impersonate("0xF1dCce3a6c321176C62b71c091E3165CC9C3816E");
console.log("signer", signer.address);

const net = await ethers.provider.getNetwork();
Expand All @@ -33,13 +34,24 @@ async function main() {
const decimalsB = await tokenB.decimals();

const tokenSeller = Univ3TokenSeller__factory.connect(TOKEN_SELLER, signer);
const pool = UniswapV3Pool__factory.connect(POOL, signer);
const owner = await tokenSeller.owner();

console.log("Need to refresh", await tokenSeller.needToRefresh());
console.log("TokenSeller.state", await tokenSeller.state());
const state = await tokenSeller.state();
const slot0 = await pool.slot0();
const config = await tokenSeller.config();

console.log("owner", owner);
console.log("BalanceA", await tokenA.balanceOf(owner));
console.log("BalanceB", await tokenB.balanceOf(owner));
console.log("TokenSeller.config", config);
console.log("TokenSeller.config.tickLimit", config.tickLimit);
console.log("TokenSeller.state", state);

console.log("Need to refresh", await tokenSeller.needToRefresh());
console.log("BalanceA", +formatUnits(await tokenA.balanceOf(signer.address), decimalsA));
console.log("BalanceB", +formatUnits(await tokenB.balanceOf(signer.address), decimalsB));
console.log("TokenSeller.state.lowerTick", state.lowerTick);
console.log("TokenSeller.state.upperTick", state.upperTick);
console.log("slot0Before.tick", slot0.tick);
}

main()
Expand Down
45 changes: 35 additions & 10 deletions scripts/token-seller/MakeTestSwap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {parseUnits} from "ethers/lib/utils";
import {IERC20Metadata__factory, MockToken__factory, UniswapV3Callee__factory} from "../../typechain";
import {formatUnits, parseUnits} from "ethers/lib/utils";
import {
MockToken__factory,
UniswapV3Callee__factory, UniswapV3Pool__factory,
Univ3TokenSeller__factory
} from "../../typechain";
import {ethers, network} from "hardhat";
import {Misc} from "../utils/Misc";
import {RunHelper} from "../utils/RunHelper";
import {Misc} from "../utils/Misc";

/**
* npx hardhat run scripts/token-seller/MakeTestSwap.ts
Expand All @@ -12,6 +16,7 @@ async function main() {
const TOKEN_B = "0xF992a90B4F2D59B65548bF9766a02ee8170E7F07";
const POOL = "0x11aB7c23B6d05Aa08D69A1eD669Ad19f3eAC52d9";
const UNISWAP_V3_CALEE = "0x848A49De3f59C62A337463f1D856Ad30FE85675e";
const TOKEN_SELLER = "0x8E294388A3E91cdac5FEA95b5b75f509b85dC5E2";

const signer = (await ethers.getSigners())[0];
// const signer = await Misc.impersonate("0xF1dCce3a6c321176C62b71c091E3165CC9C3816E");
Expand All @@ -20,18 +25,25 @@ async function main() {
const net = await ethers.provider.getNetwork();
console.log(net, `network name="${network.name}"`);

const swapTokenA = false;

const tokenA = MockToken__factory.connect(TOKEN_A, signer);
const tokenB = MockToken__factory.connect(TOKEN_B, signer);
const uniswapV3Callee = UniswapV3Callee__factory.connect(UNISWAP_V3_CALEE, signer);
const tokenSeller = Univ3TokenSeller__factory.connect(TOKEN_SELLER, signer);
const pool = UniswapV3Pool__factory.connect(POOL, signer);

const decimalsA = await tokenA.decimals();
const decimalsB = await tokenB.decimals();

const swapAmount = "1000";
const swapTokenA = true;
const swapAmount = "2000000";
const amountToSwap = parseUnits(swapAmount, swapTokenA ? decimalsA : decimalsB);

// await RunHelper.runAndWait2ExplicitSigner(
// signer,
// tokenB.connect(signer).populateTransaction.mint(signer.address, amountToSwap)
// )
// return

if (swapTokenA) {
await RunHelper.runAndWait2ExplicitSigner(
signer,
Expand All @@ -44,8 +56,10 @@ async function main() {
);
}

console.log("BalanceA", await tokenA.balanceOf(signer.address));
console.log("BalanceB", await tokenB.balanceOf(signer.address));
const stateBefore = await tokenSeller.state();
const slot0Before = await pool.slot0();
const balanceABefore = +formatUnits(await tokenA.balanceOf(signer.address), decimalsA);
const balanceBBefore = +formatUnits(await tokenB.balanceOf(signer.address), decimalsB);

await RunHelper.runAndWait2ExplicitSigner(
signer,
Expand All @@ -57,9 +71,20 @@ async function main() {
)
);

console.log("BalanceA.after", await tokenA.balanceOf(signer.address));
console.log("BalanceB.after", await tokenB.balanceOf(signer.address));
const stateAfter = await tokenSeller.state();
const slot0After = await pool.slot0();

console.log("BalanceA", balanceABefore);
console.log("BalanceB", balanceBBefore);
console.log("stateBefore.lowerTick", stateBefore.lowerTick);
console.log("stateBefore.upperTick", stateBefore.upperTick);
console.log("slot0Before.tick", slot0Before.tick);

console.log("BalanceA.after", +formatUnits(await tokenA.balanceOf(signer.address), decimalsA));
console.log("BalanceB.after", +formatUnits(await tokenB.balanceOf(signer.address), decimalsB));
console.log("stateAfter.lowerTick", stateAfter.lowerTick);
console.log("stateAfter.upperTick", stateAfter.upperTick);
console.log("slot0After.tick", slot0After.tick);
}

main()
Expand Down
37 changes: 37 additions & 0 deletions scripts/token-seller/SetTokenSeller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {parseUnits} from "ethers/lib/utils";
import {
IERC20Metadata__factory,
MockToken__factory,
UniswapV3Callee__factory,
Univ3TokenSeller__factory, Univ3TokenSellerResolver__factory
} from "../../typechain";
import {ethers, network} from "hardhat";
import {Misc} from "../utils/Misc";
import {RunHelper} from "../utils/RunHelper";

/**
* npx hardhat run scripts/token-seller/SetTokenSeller.ts
*/
async function main() {
const NEW_TOKEN_SELLER = "0x8E294388A3E91cdac5FEA95b5b75f509b85dC5E2";
const RESOLVER = "0xd2538f5f7c328a7027a5e0dea1938931601d1684";

const signer = (await ethers.getSigners())[0];
console.log("signer", signer.address);

const net = await ethers.provider.getNetwork();
console.log(net, `network name="${network.name}"`);

const resolver = Univ3TokenSellerResolver__factory.connect(RESOLVER, signer);
await RunHelper.runAndWait2ExplicitSigner(
signer,
resolver.populateTransaction.setTokenSeller(NEW_TOKEN_SELLER)
);
}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
2 changes: 1 addition & 1 deletion scripts/utils/EnvSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class EnvSetup {

ftmForkBlock: {
type: 'number',
default: 80210808,
default: 80265574,
},

/////// NETWORK EXPLORERS
Expand Down
45 changes: 27 additions & 18 deletions test/token-seller/SpookySwapTokenSellerTest2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ describe('SpookySwapTokenSellerTest2', () => {
const MAX_AMOUNT_TO_SWAP_A = "100";
const MAX_AMOUNT_TO_SWAP_B = "100";

const TOKEN_A = "0xF21ABFC5D68819EA0044fdB256d0AA854DC3034b";
const TOKEN_B = "0xF992a90B4F2D59B65548bF9766a02ee8170E7F07";
const POOL = "0x11aB7c23B6d05Aa08D69A1eD669Ad19f3eAC52d9";
// const TOKEN_A = "0xF21ABFC5D68819EA0044fdB256d0AA854DC3034b";
// const TOKEN_B = "0xF992a90B4F2D59B65548bF9766a02ee8170E7F07";
// const POOL = "0x11aB7c23B6d05Aa08D69A1eD669Ad19f3eAC52d9";

const POOL = "0x56e837286dc7366ef6d6464d332ac6f9d32bc5a0";
const TOKEN_A = "0xe4436821e403e78a6dd62f7a9f5611f97a18f44c"; // sacra (token to sell)
const TOKEN_B = "0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83"; // ftm

let snapshotBefore: string;
let signer: SignerWithAddress;
Expand Down Expand Up @@ -64,6 +68,11 @@ describe('SpookySwapTokenSellerTest2', () => {
saveToCsv(states, "");
});

it("calculate prices and ticks", async () => {
const tickCurrent = 40810;
const tickLimit = 46600;
});

//region Data types
interface ISwapParams {
tag: string;
Expand Down Expand Up @@ -295,9 +304,9 @@ describe('SpookySwapTokenSellerTest2', () => {
}
//endregion Stat utils

async function getToken(token: string, signer: string, amount: BigNumber) {
await MockToken__factory.connect(token, await Misc.impersonate(signer)).mint(signer, amount);
}
// async function getToken(token: string, signer: string, amount: BigNumber) {
// await MockToken__factory.connect(token, await Misc.impersonate(signer)).mint(signer, amount);
// }

describe("refreshPool (init => {swap => refreshPool} * N => withdraw)", () => {
const SWAP_SETS: ISwapParams[] = [
Expand All @@ -315,26 +324,21 @@ describe('SpookySwapTokenSellerTest2', () => {
console.log("Set up token seller");
snapshot2 = await TimeUtils.snapshot();

// await TokenUtils.getToken(tokenA.address, signer.address, parseUnits(SIGNER_BALANCE_A, decimalsA));
// await TokenUtils.getToken(tokenA.address, signer2.address, parseUnits(MAX_AMOUNT_TO_SWAP_A, decimalsA));
// await TokenUtils.getToken(tokenB.address, signer2.address, parseUnits(MAX_AMOUNT_TO_SWAP_B, decimalsB));
await TokenUtils.getToken(tokenA.address, signer.address, parseUnits(SIGNER_BALANCE_A, decimalsA));
await TokenUtils.getToken(tokenA.address, signer2.address, parseUnits(MAX_AMOUNT_TO_SWAP_A, decimalsA));
await TokenUtils.getToken(tokenB.address, signer2.address, parseUnits(MAX_AMOUNT_TO_SWAP_B, decimalsB));

await getToken(tokenA.address, signer.address, parseUnits(SIGNER_BALANCE_A, decimalsA));
await getToken(tokenA.address, signer2.address, parseUnits(MAX_AMOUNT_TO_SWAP_A, decimalsA));
await getToken(tokenB.address, signer2.address, parseUnits(MAX_AMOUNT_TO_SWAP_B, decimalsB));
// await getToken(tokenA.address, signer.address, parseUnits(SIGNER_BALANCE_A, decimalsA));
// await getToken(tokenA.address, signer2.address, parseUnits(MAX_AMOUNT_TO_SWAP_A, decimalsA));
// await getToken(tokenB.address, signer2.address, parseUnits(MAX_AMOUNT_TO_SWAP_B, decimalsB));

tokenSeller = Univ3TokenSeller__factory.connect(
await DeployTokenSellerUtils.deployTokenSeller(signer, tokenA.address, tokenB.address, POOL, SIGNER_AMOUNT_A, tickRangeSize),
signer
);
console.log("5");
poolSwapTokens = await tokenSeller.poolSwapTokens();
console.log("6");

await tokenA.transfer(tokenSeller.address, parseUnits(SIGNER_AMOUNT_A, decimalsA));
console.log("7");
await tokenSeller.openPosition();
console.log("8");

const poolInfo = await pool.slot0();
init = {
Expand All @@ -348,13 +352,18 @@ describe('SpookySwapTokenSellerTest2', () => {
balancesAbSigner1: await getBalancesAB(signer1.address),
balancesAbSigner2: await getBalancesAB(signer2.address),
}
console.log("9");
console.log("init", init);

const config = await tokenSeller.config();
derivedValues = {
tickLimit: config.tickLimit,
tickSpacing: await tokenSeller.tickSpacing(),
}

console.log("config", config);
console.log("poolSwapTokens", await tokenSeller.poolSwapTokens());
console.log("tokenA", await tokenSeller.tokenA());
console.log("tokenB", await tokenSeller.tokenB());
});
after(async function () {
await TimeUtils.rollback(snapshot2);
Expand Down

0 comments on commit 1e69a08

Please sign in to comment.