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

add mainnet addresses to sdk and setup mainnet script to scripts #292

Merged
merged 2 commits into from
Sep 2, 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
3 changes: 2 additions & 1 deletion scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down
156 changes: 156 additions & 0 deletions scripts/src/setup-mainnet.ts
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 2 additions & 2 deletions sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@invariant-labs/a0-sdk",
"version": "0.2.17",
"version": "0.2.19",
"collaborators": [
"Invariant Labs"
],
Expand Down
45 changes: 37 additions & 8 deletions sdk/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,53 @@ 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
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()
Expand Down
10 changes: 5 additions & 5 deletions sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Loading