From c132a1cd3fb43a1c198aabc275beba86db9274fc Mon Sep 17 00:00:00 2001 From: zielvna Date: Wed, 28 Aug 2024 19:06:30 +0200 Subject: [PATCH] add mainnet addresses to sdk and setup mainnet script to scripts --- scripts/package.json | 3 +- scripts/src/setup-mainnet.ts | 156 +++++++++++++++++++++++++++++++++++ sdk/package-lock.json | 4 +- sdk/package.json | 2 +- sdk/src/consts.ts | 45 ++++++++-- sdk/src/index.ts | 10 +-- 6 files changed, 203 insertions(+), 17 deletions(-) create mode 100644 scripts/src/setup-mainnet.ts diff --git a/scripts/package.json b/scripts/package.json index 70e61be0..b4049550 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -14,7 +14,8 @@ "query-tickmap": "npx tsc && node --experimental-wasm-modules target/query-tickmap.js", "swap": "npx tsc && node --experimental-wasm-modules target/swap.js", "setup": "npx tsc && node --experimental-wasm-modules target/setup.js", - "replace-code": "npx tsc && node --experimental-wasm-modules target/replace-code.js" + "replace-code": "npx tsc && node --experimental-wasm-modules target/replace-code.js", + "setup-mainnet": "npx tsc && node --experimental-wasm-modules target/setup-mainnet.js" }, "keywords": [], "author": "", diff --git a/scripts/src/setup-mainnet.ts b/scripts/src/setup-mainnet.ts new file mode 100644 index 00000000..84aaf40a --- /dev/null +++ b/scripts/src/setup-mainnet.ts @@ -0,0 +1,156 @@ +import { + FEE_TIERS, + Invariant, + Keyring, + Network, + PSP22, + PoolKey, + calculateTick, + initPolkadotApi, + newPoolKey, + priceToSqrtPrice, + toPercentage +} from '@invariant-labs/a0-sdk' +import dotenv from 'dotenv' + +dotenv.config() + +const main = async () => { + const network = Network.Mainnet + const api = await initPolkadotApi(network) + + const keyring = new Keyring({ type: 'sr25519' }) + const mnemonic = process.env.DEPLOYER_MNEMONIC ?? '' + const account = keyring.addFromMnemonic(mnemonic) + console.log(`Deployer: ${account.address}, Mnemonic: ${mnemonic}`) + + const invariant = await Invariant.deploy(api, network, account, toPercentage(1n, 2n), { + storageDepositLimit: 100000000000, + refTime: 100000000000, + proofSize: 100000000000 + }) + console.log(`Invariant: ${invariant.contract.address.toString()}`) + + for (const feeTier of FEE_TIERS) { + await invariant.addFeeTier(account, feeTier) + } + console.log('Successfully added fee tiers') + + const BTCAddress = await PSP22.deploy(api, account, 0n, 'Bitcoin', 'BTC', 8n) + const ETHAddress = await PSP22.deploy(api, account, 0n, 'Ether', 'ETH', 12n) + const USDCAddress = await PSP22.deploy(api, account, 0n, 'USDC', 'USDC', 6n) + const USDTAddress = await PSP22.deploy(api, account, 0n, 'Tether USD', 'USDT', 6n) + const SOLAddress = await PSP22.deploy(api, account, 0n, 'Solana', 'SOL', 9n) + const decimals = { + [BTCAddress]: 8n, + [ETHAddress]: 12n, + [USDCAddress]: 6n, + [USDTAddress]: 6n, + [SOLAddress]: 9n + } + console.log( + `BTC: ${BTCAddress}, ETH: ${ETHAddress}, USDC: ${USDCAddress}, USDT: ${USDTAddress}, SOL: ${SOLAddress}` + ) + + const response = await fetch( + 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin,ethereum,solana' + ) + const data = await response.json() + const prices = { + [BTCAddress]: data.find((coin: any) => coin.id === 'bitcoin').current_price, + [ETHAddress]: data.find((coin: any) => coin.id === 'ethereum').current_price, + [USDCAddress]: 1, + [USDTAddress]: 1, + [SOLAddress]: data.find((coin: any) => coin.id === 'solana').current_price + } + console.log( + `BTC: ${prices[BTCAddress]}, ETH: ${prices[ETHAddress]}, USDC: ${prices[USDCAddress]}, USDT: ${prices[USDTAddress]}, SOL: ${prices[SOLAddress]}` + ) + + const poolKeys: [PoolKey, bigint][] = [ + [newPoolKey(BTCAddress, ETHAddress, FEE_TIERS[1]), 130559235944405760n], + [newPoolKey(BTCAddress, USDCAddress, FEE_TIERS[1]), 7865049221247086n], + [newPoolKey(BTCAddress, USDTAddress, FEE_TIERS[1]), 7865049221247086n], + [newPoolKey(BTCAddress, SOLAddress, FEE_TIERS[1]), 977937074251981n], + [newPoolKey(ETHAddress, USDCAddress, FEE_TIERS[1]), 3454809855596621497n], + [newPoolKey(ETHAddress, USDTAddress, FEE_TIERS[1]), 3454809855596621497n], + [newPoolKey(ETHAddress, SOLAddress, FEE_TIERS[1]), 423131631710393596n], + [newPoolKey(USDCAddress, USDTAddress, FEE_TIERS[1]), 9999818389598293n], + [newPoolKey(USDCAddress, SOLAddress, FEE_TIERS[1]), 24911294718392400n], + [newPoolKey(USDTAddress, SOLAddress, FEE_TIERS[1]), 24911294718392400n] + ] + for (const [poolKey] of poolKeys) { + const price = + (1 / (prices[poolKey.tokenY] / prices[poolKey.tokenX])) * + 10 ** (Number(decimals[poolKey.tokenY]) - Number(decimals[poolKey.tokenX])) * + 10 ** 24 + try { + const poolSqrtPrice = priceToSqrtPrice(BigInt(Math.round(price))) + await invariant.createPool(account, poolKey, poolSqrtPrice) + } catch (e) { + console.log('Create pool error', poolKey, e) + } + } + console.log('Successfully added pools') + + const psp22 = await PSP22.load(api, network, { + storageDepositLimit: 100000000000, + refTime: 100000000000, + proofSize: 100000000000 + }) + await psp22.mint(account, 2n ** 96n - 1n, BTCAddress) + await psp22.mint(account, 2n ** 96n - 1n, ETHAddress) + await psp22.mint(account, 2n ** 96n - 1n, USDCAddress) + await psp22.mint(account, 2n ** 96n - 1n, USDTAddress) + await psp22.mint(account, 2n ** 96n - 1n, SOLAddress) + await psp22.approve(account, invariant.contract.address.toString(), 2n ** 96n - 1n, BTCAddress) + await psp22.approve(account, invariant.contract.address.toString(), 2n ** 96n - 1n, ETHAddress) + await psp22.approve(account, invariant.contract.address.toString(), 2n ** 96n - 1n, USDCAddress) + await psp22.approve(account, invariant.contract.address.toString(), 2n ** 96n - 1n, USDTAddress) + await psp22.approve(account, invariant.contract.address.toString(), 2n ** 96n - 1n, SOLAddress) + + const BTCBefore = await psp22.balanceOf(account.address, BTCAddress) + const ETHBefore = await psp22.balanceOf(account.address, ETHAddress) + const USDCBefore = await psp22.balanceOf(account.address, USDCAddress) + const USDTBefore = await psp22.balanceOf(account.address, USDTAddress) + const SOLBefore = await psp22.balanceOf(account.address, SOLAddress) + for (const [poolKey, amount] of poolKeys) { + const price = + (1 / (prices[poolKey.tokenY] / prices[poolKey.tokenX])) * + 10 ** (Number(decimals[poolKey.tokenY]) - Number(decimals[poolKey.tokenX])) * + 10 ** 24 + const lowerSqrtPrice = priceToSqrtPrice(BigInt(Math.round(price * 0.95))) + const upperSqrtPrice = priceToSqrtPrice(BigInt(Math.round(price * 1.05))) + const poolSqrtPrice = priceToSqrtPrice(BigInt(Math.round(price))) + try { + const lowerTick = calculateTick(lowerSqrtPrice, FEE_TIERS[1].tickSpacing) + const upperTick = calculateTick(upperSqrtPrice, FEE_TIERS[1].tickSpacing) + await invariant.createPosition( + account, + poolKey, + lowerTick, + upperTick, + amount, + poolSqrtPrice, + 0n + ) + } catch (e) { + console.log('Create position error', poolKey, e) + } + } + const BTCAfter = await psp22.balanceOf(account.address, BTCAddress) + const ETHAfter = await psp22.balanceOf(account.address, ETHAddress) + const USDCAfter = await psp22.balanceOf(account.address, USDCAddress) + const USDTAfter = await psp22.balanceOf(account.address, USDTAddress) + const SOLAfter = await psp22.balanceOf(account.address, SOLAddress) + console.log( + `BTC: ${BTCBefore - BTCAfter}, ETH: ${ETHBefore - ETHAfter}, USDC: ${ + USDCBefore - USDCAfter + }, USDT: ${USDTBefore - USDTAfter}, SOL: ${SOLBefore - SOLAfter}` + ) + console.log('Successfully created positions') + + process.exit(0) +} + +main() diff --git a/sdk/package-lock.json b/sdk/package-lock.json index 3a91aa4d..d171e355 100644 --- a/sdk/package-lock.json +++ b/sdk/package-lock.json @@ -1,12 +1,12 @@ { "name": "@invariant-labs/a0-sdk", - "version": "0.2.17", + "version": "0.2.19", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@invariant-labs/a0-sdk", - "version": "0.2.17", + "version": "0.2.19", "license": "ISC", "dependencies": { "@invariant-labs/a0-sdk-wasm": "^0.1.23", diff --git a/sdk/package.json b/sdk/package.json index c8278696..6d586f9f 100644 --- a/sdk/package.json +++ b/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@invariant-labs/a0-sdk", - "version": "0.2.17", + "version": "0.2.19", "collaborators": [ "Invariant Labs" ], diff --git a/sdk/src/consts.ts b/sdk/src/consts.ts index e4959b4c..4b68be16 100644 --- a/sdk/src/consts.ts +++ b/sdk/src/consts.ts @@ -25,6 +25,7 @@ import { getTokenAmountDenominator, getTokenAmountScale } from '@invariant-labs/a0-sdk-wasm/invariant_a0_wasm.js' +import { Network } from './network' export const MAX_REF_TIME = 259058343000 export const DEFAULT_REF_TIME = 1250000000000 @@ -32,17 +33,45 @@ export const DEFAULT_PROOF_SIZE = 1250000000000 export const CONCENTRATION_FACTOR = 1.00001526069123 export const TESTNET = 'alephzero-testnet' -export const MAINNET = 'alephzero-mainnet' +export const MAINNET = 'alephzero' export const DEFAULT_LOCAL = 'ws://127.0.0.1:9944' -export const TESTNET_WAZERO_ADDRESS = '5EFDb7mKbougLtr5dnwd5KDfZ3wK55JPGPLiryKq4uRMPR46' +export const WAZERO_ADDRESS = { + [Network.Testnet]: '5EFDb7mKbougLtr5dnwd5KDfZ3wK55JPGPLiryKq4uRMPR46', + [Network.Mainnet]: '5CtuFVgEUz13SFPVY6s2cZrnLDEkxQXc19aXrNARwEBeCXgg', + [Network.Local]: '' +} -export const TESTNET_INVARIANT_ADDRESS = '5HJJ5K4vGixAZo3fpG6niXKKRgvsxsur9CBuiVQGW9AHrnSo' -export const TESTNET_BTC_ADDRESS = '5GPoVZGgTGvXNK85MUYzVCtWgKDT4UPqQti4X5tZGm7ntxPz' -export const TESTNET_ETH_ADDRESS = '5FJvhnohVmEZNVxZatASSgFxpUNe1Nqxccd1gLxHrZoMGdy1' -export const TESTNET_USDC_ADDRESS = '5Hj9dcaNhAMuhY8ju7crf1Uj4nJexVJWBdRf2WZGE3a78j3G' -export const TESTNET_USDT_ADDRESS = '5G91YrSRyJhuu6BswzSxcS5QTkoEwhhZpFay3LHMSFZBue4r' -export const TESTNET_SOL_ADDRESS = '5DGCxfxuKiE2JasJLstVSaYBXvQJQK7tr87ndWtgYtCqv8vs' +export const INVARIANT_ADDRESS = { + [Network.Testnet]: '5HJJ5K4vGixAZo3fpG6niXKKRgvsxsur9CBuiVQGW9AHrnSo', + [Network.Mainnet]: '5CvocBcChFccUkNGZpYf1mThQQDaY7ZxXEmdTXbTLqt1SaYQ', + [Network.Local]: '' +} +export const BTC_ADDRESS = { + [Network.Testnet]: '5GPoVZGgTGvXNK85MUYzVCtWgKDT4UPqQti4X5tZGm7ntxPz', + [Network.Mainnet]: '5HW9QeCifdKt8gXwXVSE8z56njDQBhGfses1KJNFL68qius9', + [Network.Local]: '' +} +export const ETH_ADDRESS = { + [Network.Testnet]: '5FJvhnohVmEZNVxZatASSgFxpUNe1Nqxccd1gLxHrZoMGdy1', + [Network.Mainnet]: '5EEzffpXkfYKkdtmqNh9UNctYTjmbi9GfKKAWRTKKFh6F1FU', + [Network.Local]: '' +} +export const USDC_ADDRESS = { + [Network.Testnet]: '5Hj9dcaNhAMuhY8ju7crf1Uj4nJexVJWBdRf2WZGE3a78j3G', + [Network.Mainnet]: '5GDsB8Qm6CAoBi7rmM6TCKMQQUg8CiRzuH9YVyfcrwDKWoqB', + [Network.Local]: '' +} +export const USDT_ADDRESS = { + [Network.Testnet]: '5G91YrSRyJhuu6BswzSxcS5QTkoEwhhZpFay3LHMSFZBue4r', + [Network.Mainnet]: '5HX57YoV7h51NEKhpfXZAJk8RzLX4Uutp36S23RDMPZ424LY', + [Network.Local]: '' +} +export const SOL_ADDRESS = { + [Network.Testnet]: '5DGCxfxuKiE2JasJLstVSaYBXvQJQK7tr87ndWtgYtCqv8vs', + [Network.Mainnet]: '5F2xiTnahG1tFY3ZHyghh25JsjuCaamRnN7ddQjPEzwvdd3j', + [Network.Local]: '' +} export const FEE_GROWTH_DENOMINATOR = getFeeGrowthDenominator() export const FIXED_POINT_DENOMINATOR = getFixedPointDenominator() diff --git a/sdk/src/index.ts b/sdk/src/index.ts index 9ef64e55..93854ccf 100644 --- a/sdk/src/index.ts +++ b/sdk/src/index.ts @@ -49,11 +49,11 @@ export { MAX_SQRT_PRICE, MIN_SQRT_PRICE, TESTNET, - TESTNET_BTC_ADDRESS, - TESTNET_ETH_ADDRESS, - TESTNET_INVARIANT_ADDRESS, - TESTNET_USDC_ADDRESS, - TESTNET_WAZERO_ADDRESS + BTC_ADDRESS, + ETH_ADDRESS, + INVARIANT_ADDRESS, + USDC_ADDRESS, + WAZERO_ADDRESS } from './consts.js' export { Invariant } from './invariant.js' export { Network } from './network.js'