Skip to content

Commit

Permalink
Merge pull request #144 from invariant-labs/add-execute-methods
Browse files Browse the repository at this point in the history
add execute methods and create pool and position script
  • Loading branch information
zielvna authored Oct 9, 2024
2 parents 5e98d0b + a5b071f commit 4332e68
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 338 deletions.
23 changes: 23 additions & 0 deletions contracts/scripts/invariant_tx.ral
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,26 @@ TxScript Swap(invariant: Invariant, poolKey: PoolKey, xToY: Bool, amount: TokenA
let _ = invariant.swap{callerAddress!() -> swappedToken: swappedAmount}(poolKey, xToY, amount, byAmountIn, sqrtPriceLimit)
}

TxScript CreatePoolAndPosition(
invariant: Invariant,
poolKey: PoolKey,
initSqrtPrice: SqrtPrice,
initTick: I256,
lowerTick: I256,
upperTick: I256,
liquidityDelta: Liquidity,
slippageLimitLower: SqrtPrice,
slippageLimitUpper: SqrtPrice
) {
invariant.createPool{callerAddress!() -> ALPH: 5 * mapEntryDeposit!() }(poolKey.tokenX, poolKey.tokenY, poolKey.feeTier, initSqrtPrice, initTick)

let amountX = tokenRemaining!(callerAddress!(), poolKey.tokenX)
let amountY = tokenRemaining!(callerAddress!(), poolKey.tokenY)
if(poolKey.tokenX == ALPH) {
invariant.createPosition{callerAddress!() -> ALPH: amountX, poolKey.tokenY: amountY}
(poolKey, lowerTick, upperTick, liquidityDelta, slippageLimitLower, slippageLimitUpper)
} else {
invariant.createPosition{callerAddress!() -> poolKey.tokenX: amountX, poolKey.tokenY: amountY, ALPH: mapEntryDeposit!() * 6}
(poolKey, lowerTick, upperTick, liquidityDelta, slippageLimitLower, slippageLimitUpper)
}
}
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.13",
"version": "0.0.17",
"collaborators": [
"Invariant Labs"
],
Expand Down
5 changes: 5 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
FeeGrowth,
FixedPoint,
Liquidity,
Options,
Percentage,
Price,
SqrtPrice,
Expand Down Expand Up @@ -91,3 +92,7 @@ export const SOL_ID = {
[Network.Testnet]: 'c925472266dbd7f2e01313ad787cfb12b7f4be6776e9a3b02daed9dde7706200',
[Network.Mainnet]: ''
}

export const CONFIRMATIONS = 1
export const REQUEST_INTERVAL = 1000
export const DEFAULT_OPTIONS: Options = { waitForTxConfirmation: true }
88 changes: 33 additions & 55 deletions src/fungible-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {
SignerProvider,
stringToHex,
TransactionBuilder,
waitForTxConfirmation,
web3
} from '@alephium/web3'
import { Network } from './network'
import { Airdrop, TokenFaucet, Withdraw } from '../artifacts/ts'
import { balanceOf, signAndSend, waitTxConfirmed } from './utils'
import { MAX_U256 } from './consts'
import { TokenAmount } from './types'
import { CONFIRMATIONS, DEFAULT_OPTIONS, MAX_U256, REQUEST_INTERVAL } from './consts'
import { Options, TokenAmount } from './types'

export type TokenMetaData = {
symbol: string
Expand Down Expand Up @@ -59,25 +60,22 @@ export class FungibleToken {
return new FungibleToken()
}

async mintTx(signer: SignerProvider, value: TokenAmount, tokenId: string) {
const builder = TransactionBuilder.from(web3.getCurrentNodeProvider())
const bytecode = Withdraw.script.buildByteCodeToDeploy({
token: tokenId,
amount: value
async mint(
signer: SignerProvider,
value: TokenAmount,
tokenId: string,
options: Options = DEFAULT_OPTIONS
) {
const result = await Withdraw.execute(signer, {
initialFields: { token: tokenId, amount: value },
attoAlphAmount: DUST_AMOUNT
})

const { address, publicKey } = await signer.getSelectedAccount()

const unsignedTxBuild = await builder.buildExecuteScriptTx(
{ signerAddress: address, bytecode, attoAlphAmount: DUST_AMOUNT },
publicKey
)
return unsignedTxBuild
}
if (options.waitForTxConfirmation) {
await waitForTxConfirmation(result.txId, CONFIRMATIONS, REQUEST_INTERVAL)
}

async mint(signer: SignerProvider, value: TokenAmount, tokenId: string) {
const tx = await this.mintTx(signer, value, tokenId)
return await signAndSend(signer, tx)
return result.txId
}

async getAllBalances(tokens: string[], owner: Address): Promise<Map<string, TokenAmount>> {
Expand Down Expand Up @@ -122,52 +120,32 @@ export class FungibleToken {
return BigInt((await nodeProvider.fetchFungibleTokenMetaData(tokenId)).decimals)
}

async airdropTx(
async airdrop(
signer: SignerProvider,
valueOne: TokenAmount,
tokenOneId: string,
valueTwo: TokenAmount,
tokenTwoId: string,
valueThree: TokenAmount,
tokenThreeId: string
tokenThreeId: string,
options: Options = DEFAULT_OPTIONS
) {
const builder = TransactionBuilder.from(web3.getCurrentNodeProvider())
const bytecode = Airdrop.script.buildByteCodeToDeploy({
tokenOne: tokenOneId,
amountOne: valueOne,
tokenTwo: tokenTwoId,
amountTwo: valueTwo,
tokenThree: tokenThreeId,
amountThree: valueThree
const result = await Airdrop.execute(signer, {
initialFields: {
tokenOne: tokenOneId,
amountOne: valueOne,
tokenTwo: tokenTwoId,
amountTwo: valueTwo,
tokenThree: tokenThreeId,
amountThree: valueThree
},
attoAlphAmount: DUST_AMOUNT * 3n
})

const { address, publicKey } = await signer.getSelectedAccount()

const unsignedTxBuild = await builder.buildExecuteScriptTx(
{ signerAddress: address, bytecode, attoAlphAmount: DUST_AMOUNT * 3n },
publicKey
)
return unsignedTxBuild
}
if (options.waitForTxConfirmation) {
await waitForTxConfirmation(result.txId, CONFIRMATIONS, REQUEST_INTERVAL)
}

async airdrop(
signer: SignerProvider,
valueOne: TokenAmount,
tokenOneId: string,
valueTwo: TokenAmount,
tokenTwoId: string,
valueThree: TokenAmount,
tokenThreeId: string
) {
const tx = await this.airdropTx(
signer,
valueOne,
tokenOneId,
valueTwo,
tokenTwoId,
valueThree,
tokenThreeId
)
return await signAndSend(signer, tx)
return result.txId
}
}
Loading

0 comments on commit 4332e68

Please sign in to comment.