Skip to content

Commit

Permalink
Merge pull request #142 from invariant-labs/airdrop-entrypoint
Browse files Browse the repository at this point in the history
add airdrop entrypoint and refactor setup script
  • Loading branch information
zielvna authored Oct 2, 2024
2 parents d926a30 + 04d56d7 commit 5e98d0b
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 74 deletions.
6 changes: 6 additions & 0 deletions contracts/scripts/token_tx.ral
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
TxScript Withdraw(token: TokenFaucet, amount: U256) {
token.withdraw(amount)
}

TxScript Airdrop(tokenOne: TokenFaucet, amountOne: U256, tokenTwo: TokenFaucet, amountTwo: U256, tokenThree: TokenFaucet, amountThree: U256) {
tokenOne.withdraw(amountOne)
tokenTwo.withdraw(amountTwo)
tokenThree.withdraw(amountThree)
}
4 changes: 2 additions & 2 deletions 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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@invariant-labs/alph-sdk",
"version": "0.0.12",
"version": "0.0.13",
"collaborators": [
"Invariant Labs"
],
Expand Down
16 changes: 8 additions & 8 deletions scripts/package-lock.json

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

10 changes: 6 additions & 4 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
"build": "tsc",
"start": "tsc && node dist/index.js",
"deploy-tokens": "npx tsc && node dist/deploy-tokens.js",
"setup": "npx tsc && node dist/setup.js",
"replace-code": "npx tsc && node dist/replace-code.js"
"replace-code": "npx tsc && node dist/replace-code.js",
"setup-testnet": "npx tsc && node dist/setup-testnet.js",
"setup-devnet": "npx tsc && node dist/setup-devnet.js"
},
"dependencies": {
"@invariant-labs/alph-sdk": "file:../.",
"@invariant-labs/alph-sdk": "file:..",
"invariant-old": "npm:@invariant-labs/alph-sdk@=0.0.8",
"dotenv": "^16.4.5"
},
"devDependencies": {
"@types/chai": "^4.3.17",
"chai": "^5.1.1",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0", "eslint": "^8.37.0",
"@typescript-eslint/parser": "^7.13.0",
"eslint": "^8.37.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"typescript": "^5.4.5"
Expand Down
153 changes: 153 additions & 0 deletions scripts/src/setup-devnet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import {
FEE_TIERS,
Invariant,
Network,
FungibleToken,
PoolKey,
calculateTick,
newPoolKey,
priceToSqrtPrice,
toPercentage,
setOfficialNodeProvider,
TokenAmount,
ALPH_TOKEN_ID,
Price,
Percentage,
getLiquidityByX,
SqrtPrice,
getSigner,
ONE_ALPH
} from '@invariant-labs/alph-sdk'
import dotenv from 'dotenv'

dotenv.config()

const main = async () => {
setOfficialNodeProvider(Network.Devnet)

const account = await getSigner(ONE_ALPH * 100n)

console.log(`Deployer: ${account.address}`)
const invariant = await Invariant.deploy(account, toPercentage(1n, 2n))
console.log(`Invariant: ${invariant.instance.address.toString()}`)

for (const feeTier of FEE_TIERS) {
await invariant.addFeeTier(account, feeTier)
}
console.log('Successfully added fee tiers')

const BTCTokenID = await FungibleToken.deploy(account, 0n as TokenAmount, 'Bitcoin', 'BTC', 8n)
const ETHTokenID = await FungibleToken.deploy(account, 0n as TokenAmount, 'Ether', 'ETH', 18n)
const USDCTokenID = await FungibleToken.deploy(account, 0n as TokenAmount, 'USDC', 'USDC', 6n)
const USDTTokenID = await FungibleToken.deploy(
account,
0n as TokenAmount,
'Tether USD',
'USDT',
6n
)
const SOLTokenID = await FungibleToken.deploy(account, 0n as TokenAmount, 'Solana', 'SOL', 9n)
const decimals = {
[BTCTokenID]: 8n,
[ETHTokenID]: 18n,
[USDCTokenID]: 6n,
[USDTTokenID]: 6n,
[SOLTokenID]: 9n,
[ALPH_TOKEN_ID]: 18n
}
console.log(
`BTC: ${BTCTokenID}, ETH: ${ETHTokenID}, USDC: ${USDCTokenID}, USDT: ${USDTTokenID}, SOL: ${SOLTokenID}`
)

const response = await fetch(
'https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=bitcoin,ethereum,alephium,solana'
)
const data = await response.json()
const prices = {
[BTCTokenID]: data.find((coin: any) => coin.id === 'bitcoin').current_price,
[ETHTokenID]: data.find((coin: any) => coin.id === 'ethereum').current_price,
[USDCTokenID]: 1,
[USDTTokenID]: 1,
[SOLTokenID]: data.find((coin: any) => coin.id === 'solana').current_price,
[ALPH_TOKEN_ID]: data.find((coin: any) => coin.id === 'alephium').current_price
}
console.log(
`BTC: ${prices[BTCTokenID]}, ETH: ${prices[ETHTokenID]}, USDC: ${prices[USDCTokenID]}, USDT: ${prices[USDTTokenID]}, SOL: ${prices[SOLTokenID]}, ALPH: ${prices[ALPH_TOKEN_ID]}`
)

const poolKeys: PoolKey[] = [
newPoolKey(BTCTokenID, ETHTokenID, FEE_TIERS[1]),
newPoolKey(BTCTokenID, USDCTokenID, FEE_TIERS[1]),
newPoolKey(BTCTokenID, USDTTokenID, FEE_TIERS[1]),
newPoolKey(BTCTokenID, SOLTokenID, FEE_TIERS[1]),
newPoolKey(ETHTokenID, USDCTokenID, FEE_TIERS[1]),
newPoolKey(ETHTokenID, USDTTokenID, FEE_TIERS[1]),
newPoolKey(ETHTokenID, SOLTokenID, FEE_TIERS[1]),
newPoolKey(USDCTokenID, USDTTokenID, FEE_TIERS[1]),
newPoolKey(USDCTokenID, SOLTokenID, FEE_TIERS[1]),
newPoolKey(USDTTokenID, SOLTokenID, FEE_TIERS[1])
]
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)) as Price)
await invariant.createPool(account, poolKey, poolSqrtPrice)
} catch (e) {
console.log('Create pool error', poolKey, e)
}
}
console.log('Successfully added pools')

const token = FungibleToken.load()
await token.mint(account, (2n ** 96n - 1n) as TokenAmount, BTCTokenID)
await token.mint(account, (2n ** 96n - 1n) as TokenAmount, ETHTokenID)
await token.mint(account, (2n ** 96n - 1n) as TokenAmount, USDCTokenID)
await token.mint(account, (2n ** 96n - 1n) as TokenAmount, USDTTokenID)
await token.mint(account, (2n ** 96n - 1n) as TokenAmount, SOLTokenID)
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
const lowerSqrtPrice = priceToSqrtPrice(BigInt(Math.round(price * 0.95)) as Price)
const upperSqrtPrice = priceToSqrtPrice(BigInt(Math.round(price * 1.05)) as Price)
const poolSqrtPrice = priceToSqrtPrice(BigInt(Math.round(price)) as Price)
try {
const lowerTick = calculateTick(lowerSqrtPrice, FEE_TIERS[1].tickSpacing)
const upperTick = calculateTick(upperSqrtPrice, FEE_TIERS[1].tickSpacing)
const tokenXAmount = BigInt(
Math.round((5000 / prices[poolKey.tokenX]) * 10 ** Number(decimals[poolKey.tokenX]))
)
const { l: liquidity } = getLiquidityByX(
tokenXAmount as TokenAmount,
lowerTick,
upperTick,
poolSqrtPrice as SqrtPrice,
true
)
const approvedAmountX = await token.getBalanceOf(account.address, poolKey.tokenX)
const approvedAmountY = await token.getBalanceOf(account.address, poolKey.tokenY)
await invariant.createPosition(
account,
poolKey,
lowerTick,
upperTick,
liquidity,
approvedAmountX,
approvedAmountY,
poolSqrtPrice,
0n as Percentage
)
} catch (e) {
console.log('Create position error', poolKey, e)
}
}
console.log('Successfully created positions')

process.exit(0)
}

main()
78 changes: 29 additions & 49 deletions scripts/src/setup.ts → scripts/src/setup-testnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
TokenAmount,
ALPH_TOKEN_ID,
Price,
Liquidity,
Percentage
Percentage,
getLiquidityByX,
SqrtPrice
} from '@invariant-labs/alph-sdk'
import dotenv from 'dotenv'

Expand All @@ -36,7 +37,7 @@ const main = async () => {
console.log('Successfully added fee tiers')

const BTCTokenID = await FungibleToken.deploy(account, 0n as TokenAmount, 'Bitcoin', 'BTC', 8n)
const ETHTokenID = await FungibleToken.deploy(account, 0n as TokenAmount, 'Ether', 'ETH', 12n)
const ETHTokenID = await FungibleToken.deploy(account, 0n as TokenAmount, 'Ether', 'ETH', 18n)
const USDCTokenID = await FungibleToken.deploy(account, 0n as TokenAmount, 'USDC', 'USDC', 6n)
const USDTTokenID = await FungibleToken.deploy(
account,
Expand All @@ -45,11 +46,10 @@ const main = async () => {
'USDT',
6n
)

const SOLTokenID = await FungibleToken.deploy(account, 0n as TokenAmount, 'Solana', 'SOL', 9n)
const decimals = {
[BTCTokenID]: 8n,
[ETHTokenID]: 12n,
[ETHTokenID]: 18n,
[USDCTokenID]: 6n,
[USDTTokenID]: 6n,
[SOLTokenID]: 9n,
Expand All @@ -75,24 +75,19 @@ const main = async () => {
`BTC: ${prices[BTCTokenID]}, ETH: ${prices[ETHTokenID]}, USDC: ${prices[USDCTokenID]}, USDT: ${prices[USDTTokenID]}, SOL: ${prices[SOLTokenID]}, ALPH: ${prices[ALPH_TOKEN_ID]}`
)

const poolKeys: [PoolKey, bigint][] = [
// [newPoolKey(ALPH_TOKEN_ID, BTCTokenID, FEE_TIERS[1]), 10804609546189987720n],
// [newPoolKey(ALPH_TOKEN_ID, ETHTokenID, FEE_TIERS[1]), 4711830510277394610468n],
// [newPoolKey(ALPH_TOKEN_ID, USDCTokenID, FEE_TIERS[1]), 272063075569508447756n],
// [newPoolKey(ALPH_TOKEN_ID, USDTTokenID, FEE_TIERS[1]), 272063075569508447756n],
// [newPoolKey(ALPH_TOKEN_ID, SOLTokenID, FEE_TIERS[1]), 37143700245489847211n],
[newPoolKey(BTCTokenID, ETHTokenID, FEE_TIERS[1]), 130559235944405760n],
[newPoolKey(BTCTokenID, USDCTokenID, FEE_TIERS[1]), 7865049221247086n],
[newPoolKey(BTCTokenID, USDTTokenID, FEE_TIERS[1]), 7865049221247086n],
[newPoolKey(BTCTokenID, SOLTokenID, FEE_TIERS[1]), 977937074251981n],
[newPoolKey(ETHTokenID, USDCTokenID, FEE_TIERS[1]), 3454809855596621497n],
[newPoolKey(ETHTokenID, USDTTokenID, FEE_TIERS[1]), 3454809855596621497n],
[newPoolKey(ETHTokenID, SOLTokenID, FEE_TIERS[1]), 423131631710393596n],
[newPoolKey(USDCTokenID, USDTTokenID, FEE_TIERS[1]), 9999818389598293n],
[newPoolKey(USDCTokenID, SOLTokenID, FEE_TIERS[1]), 24911294718392400n],
[newPoolKey(USDTTokenID, SOLTokenID, FEE_TIERS[1]), 24911294718392400n]
const poolKeys: PoolKey[] = [
newPoolKey(BTCTokenID, ETHTokenID, FEE_TIERS[1]),
newPoolKey(BTCTokenID, USDCTokenID, FEE_TIERS[1]),
newPoolKey(BTCTokenID, USDTTokenID, FEE_TIERS[1]),
newPoolKey(BTCTokenID, SOLTokenID, FEE_TIERS[1]),
newPoolKey(ETHTokenID, USDCTokenID, FEE_TIERS[1]),
newPoolKey(ETHTokenID, USDTTokenID, FEE_TIERS[1]),
newPoolKey(ETHTokenID, SOLTokenID, FEE_TIERS[1]),
newPoolKey(USDCTokenID, USDTTokenID, FEE_TIERS[1]),
newPoolKey(USDCTokenID, SOLTokenID, FEE_TIERS[1]),
newPoolKey(USDTTokenID, SOLTokenID, FEE_TIERS[1])
]
for (const [poolKey] of poolKeys) {
for (const poolKey of poolKeys) {
const price =
(1 / (prices[poolKey.tokenY] / prices[poolKey.tokenX])) *
10 ** (Number(decimals[poolKey.tokenY]) - Number(decimals[poolKey.tokenX])) *
Expand All @@ -112,21 +107,7 @@ const main = async () => {
await token.mint(account, (2n ** 96n - 1n) as TokenAmount, USDCTokenID)
await token.mint(account, (2n ** 96n - 1n) as TokenAmount, USDTTokenID)
await token.mint(account, (2n ** 96n - 1n) as TokenAmount, SOLTokenID)

const BTCBefore = await token.getBalanceOf(account.address, BTCTokenID)
const ETHBefore = await token.getBalanceOf(account.address, ETHTokenID)
const USDCBefore = await token.getBalanceOf(account.address, USDCTokenID)
const USDTBefore = await token.getBalanceOf(account.address, USDTTokenID)
const SOLBefore = await token.getBalanceOf(account.address, SOLTokenID)
// const ALPHBefore = await token.getBalanceOf(account.address, ALPH_TOKEN_ID)

console.log(
`BTC: ${BTCBefore}, ETH: ${ETHBefore}, USDC: ${
USDCBefore
}, USDT: ${USDTBefore}, SOL: ${SOLBefore}`
)

for (const [poolKey, amount] of poolKeys) {
for (const poolKey of poolKeys) {
const price =
(1 / (prices[poolKey.tokenY] / prices[poolKey.tokenX])) *
10 ** (Number(decimals[poolKey.tokenY]) - Number(decimals[poolKey.tokenX])) *
Expand All @@ -137,14 +118,24 @@ const main = async () => {
try {
const lowerTick = calculateTick(lowerSqrtPrice, FEE_TIERS[1].tickSpacing)
const upperTick = calculateTick(upperSqrtPrice, FEE_TIERS[1].tickSpacing)
const tokenXAmount = BigInt(
Math.round((5000 / prices[poolKey.tokenX]) * 10 ** Number(decimals[poolKey.tokenX]))
)
const { l: liquidity } = getLiquidityByX(
tokenXAmount as TokenAmount,
lowerTick,
upperTick,
poolSqrtPrice as SqrtPrice,
true
)
const approvedAmountX = await token.getBalanceOf(account.address, poolKey.tokenX)
const approvedAmountY = await token.getBalanceOf(account.address, poolKey.tokenY)
await invariant.createPosition(
account,
poolKey,
lowerTick,
upperTick,
amount as Liquidity,
liquidity,
approvedAmountX,
approvedAmountY,
poolSqrtPrice,
Expand All @@ -154,17 +145,6 @@ const main = async () => {
console.log('Create position error', poolKey, e)
}
}
const BTCAfter = await token.getBalanceOf(account.address, BTCTokenID)
const ETHAfter = await token.getBalanceOf(account.address, ETHTokenID)
const USDCAfter = await token.getBalanceOf(account.address, USDCTokenID)
const USDTAfter = await token.getBalanceOf(account.address, USDTTokenID)
const SOLAfter = await token.getBalanceOf(account.address, SOLTokenID)
// const ALPHAfter = await token.getBalanceOf(account.address, ALPH_TOKEN_ID)
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)
Expand Down
Loading

0 comments on commit 5e98d0b

Please sign in to comment.