Skip to content

Commit

Permalink
sui token balanceChanges
Browse files Browse the repository at this point in the history
  • Loading branch information
ye-sentio committed Oct 31, 2023
1 parent 3da571f commit 8e8a76a
Show file tree
Hide file tree
Showing 8 changed files with 559 additions and 282 deletions.
11 changes: 11 additions & 0 deletions projects/kriya-lp-token/src/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const KRIYA_LP_TOKENS = [

]
// 0x074c421ceea5db8c393d4b5520d612782ccf10b51856d844d998a3fbc4896170 ; cUSDCe-SUI
// 0x5af4976b871fa1813362f352fa4cada3883a96191bb7212db1bd5d13685ae305;wUSDCe-SUI
// 0x517ee525c34fdfb2240342bd43fc07e1ec253c2442a7edd2482e6973700c6ef5;USDCbnb-SUI
// 0xbb4a712b3353176092cdfe3dd2d1251b725f9372e954248e5dd2eb2ab6a5f21a;wUSDCe-BUCK
// 0x1a32f76a27f49ba590ffebfd906bd4d6733e67090c5aec9bdd50d2c34e5db763;cUSDCe-cUSDTe
// 0x43ca1a6de20d7feabcaa460ac3798a6fdc754d3a83b49dff93221612c1370dcc;WETH-wUSDCe
// 0xd0086b7713e0487bbf5bb4a1e30a000794e570590a6041155cdbebee3cb1cb77;wUSDCe-wUSDTe
// 0x3c334f9d1b969767007d26bc886786f9f197ffb14771f7903cd8772c46d08dea;BUCK-SUI
212 changes: 212 additions & 0 deletions projects/kriya-lp-token/src/helper/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import { SuiObjectProcessor, SuiContext, SuiObjectContext } from "@sentio/sdk/sui"
import { getPriceByType, token } from "@sentio/sdk/utils"
import * as constant from '../constant.js'
import { SuiNetwork } from "@sentio/sdk/sui"

//get coin address without suffix
export function getCoinObjectAddress(type: string) {
let coin_a_address = ""
let coin_b_address = ""
const regex = /0x[a-fA-F0-9]+:/g
const matches = type.match(regex)
if (matches && matches.length >= 2) {
coin_a_address = matches[1].slice(0, -1)
coin_b_address = matches[2].slice(0, -1)
}
return [coin_a_address, coin_b_address]
}

//get full coin address with suffix
export function getCoinFullAddress(type: string) {
let coin_a_address = ""
let coin_b_address = ""
const regex_a = /<[^,]+,/g;
const regex_b = /0x[^\s>]+>/g;
const matches_a = type.match(regex_a)
const matches_b = type.match(regex_b)
if (matches_a) {
coin_a_address = matches_a[0].slice(1, -1)
}
if (matches_b) {
coin_b_address = matches_b[0].slice(0, -1)
}
return [coin_a_address, coin_b_address]
}

interface poolInfo {
symbol_a: string,
symbol_b: string,
decimal_a: number,
decimal_b: number,
pairName: string,
type: string
}


let poolInfoMap = new Map<string, Promise<poolInfo>>()
let IDOPoolInfoMap = new Map<string, Promise<poolInfo>>()
let coinInfoMap = new Map<string, Promise<token.TokenInfo>>()

export async function buildCoinInfo(ctx: SuiContext | SuiObjectContext, coinAddress: string): Promise<token.TokenInfo> {
let [symbol, name, decimal] = ["unk", "unk", 0]
try {
const metadata = await ctx.client.getCoinMetadata({ coinType: coinAddress })
//@ts-ignore
symbol = metadata.symbol
//@ts-ignore
decimal = metadata.decimals
//@ts-ignore
name = metadata.name
console.log(`build coin metadata ${symbol} ${decimal} ${name}`)
}
catch (e) {
console.log(`${e.message} get coin metadata error ${coinAddress}`)
}

return {
symbol,
name,
decimal
}
}

export const getOrCreateCoin = async function (ctx: SuiContext | SuiObjectContext, coinAddress: string): Promise<token.TokenInfo> {
let coinInfo = coinInfoMap.get(coinAddress)
if (!coinInfo) {
coinInfo = buildCoinInfo(ctx, coinAddress)
coinInfoMap.set(coinAddress, coinInfo)
// console.log("set coinInfoMap for " + coinAddress)
let i = 0
let msg = `set coinInfoMap for ${(await coinInfo).name}`
for (const key of coinInfoMap.keys()) {
const coinInfo = await coinInfoMap.get(key)
msg += `\n${i}:${coinInfo?.name},${coinInfo?.decimal} `
i++
}
console.log(msg)
}
return coinInfo
}

export async function buildPoolInfo(ctx: SuiContext | SuiObjectContext, pool: string): Promise<poolInfo> {
let [symbol_a, symbol_b, decimal_a, decimal_b, pairName, type, fee_label] = ["", "", 0, 0, "", "", "", "NaN"]
try {
const obj = await ctx.client.getObject({ id: pool, options: { showType: true, showContent: true } })
//@ts-ignore
type = obj.data.type
//@ts-ignore
if (obj.data.content.fields.fee_rate) {
//@ts-ignore
fee_label = (Number(obj.data.content.fields.fee_rate) / 10000).toFixed(2) + "%"
}
else {
console.log(`no fee label ${pool}`)
}
let [coin_a_full_address, coin_b_full_address] = ["", ""]
if (type) {
[coin_a_full_address, coin_b_full_address] = getCoinFullAddress(type)
}
const coinInfo_a = await getOrCreateCoin(ctx, coin_a_full_address)
const coinInfo_b = await getOrCreateCoin(ctx, coin_b_full_address)
symbol_a = coinInfo_a.symbol
symbol_b = coinInfo_b.symbol
decimal_a = coinInfo_a.decimal
decimal_b = coinInfo_b.decimal
pairName = symbol_a + "-" + symbol_b + " " + fee_label
console.log(`build pool ${pairName}`)
} catch (e) {
console.log(`${e.message} get pool object error ${pool}`)
}
return {
symbol_a,
symbol_b,
decimal_a,
decimal_b,
pairName,
type
}
}

export const getOrCreatePool = async function (ctx: SuiContext | SuiObjectContext, pool: string): Promise<poolInfo> {
let infoPromise = poolInfoMap.get(pool)
if (!infoPromise) {
infoPromise = buildPoolInfo(ctx, pool)
poolInfoMap.set(pool, infoPromise)
// console.log("set poolInfoMap for " + pool)
let i = 0
let msg = `set poolInfoMap for ${(await infoPromise).pairName}`
for (const key of poolInfoMap.keys()) {
const poolInfo = await poolInfoMap.get(key)
msg += `\n${i}:${poolInfo?.pairName} `
i++
}
console.log(msg)
}
return infoPromise
}

export async function buildIDOPoolInfo(ctx: SuiContext | SuiObjectContext, pool: string): Promise<poolInfo> {
let [symbol_a, symbol_b, decimal_a, decimal_b, pairName, type] = ["", "", 0, 0, "", "", ""]
try {
const obj = await ctx.client.getObject({ id: pool, options: { showType: true, showContent: true } })
//@ts-ignore
type = obj.data.type

let [coin_a_full_address, coin_b_full_address] = ["", ""]
if (type) {
[coin_a_full_address, coin_b_full_address] = getCoinFullAddress(type)
}
const coinInfo_a = await getOrCreateCoin(ctx, coin_a_full_address)
const coinInfo_b = await getOrCreateCoin(ctx, coin_b_full_address)
symbol_a = coinInfo_a.symbol
symbol_b = coinInfo_b.symbol
decimal_a = coinInfo_a.decimal
decimal_b = coinInfo_b.decimal
pairName = symbol_a + "-" + symbol_b + " IDO"
console.log(`build IDO pool ${pairName}`)
} catch (e) {
console.log(`${e.message} get IDO pool object error ${pool}`)
}
return {
symbol_a,
symbol_b,
decimal_a,
decimal_b,
pairName,
type
}
}
export const getOrCreatIDOPool = async function (ctx: SuiContext | SuiObjectContext, pool: string): Promise<poolInfo> {
let infoPromise = IDOPoolInfoMap.get(pool)
if (!infoPromise) {
infoPromise = buildIDOPoolInfo(ctx, pool)
IDOPoolInfoMap.set(pool, infoPromise)
// console.log("set poolInfoMap for " + pool)
let i = 0
let msg = `set IDO PoolInfoMap for ${(await infoPromise).pairName}`
for (const key of IDOPoolInfoMap.keys()) {
const poolInfo = await IDOPoolInfoMap.get(key)
msg += `\n${i}:${poolInfo?.pairName} `
i++
}
console.log(msg)
}
return infoPromise
}



export async function getPoolPrice(ctx: SuiContext | SuiObjectContext, pool: string) {
const obj = await ctx.client.getObject({ id: pool, options: { showType: true, showContent: true } })
//@ts-ignore
const current_sqrt_price = Number(obj.data.content.fields.current_sqrt_price)
if (!current_sqrt_price) { console.log(`get pool price error at ${ctx}`) }
const poolInfo = await getOrCreatePool(ctx, pool)
const pairName = poolInfo.pairName
const coin_b2a_price = 1 / (Number(current_sqrt_price) ** 2) * (2 ** 128) * 10 ** (poolInfo.decimal_b - poolInfo.decimal_a)
const coin_a2b_price = 1 / coin_b2a_price
ctx.meter.Gauge("a2b_price").record(coin_a2b_price, { pairName })
ctx.meter.Gauge("b2a_price").record(coin_b2a_price, { pairName })
return coin_a2b_price
}

73 changes: 71 additions & 2 deletions projects/kriya-lp-token/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,74 @@ SuiGlobalProcessor.bind({
network: SuiNetwork.MAIN_NET
})
.onTransactionBlock(async (tx, ctx) => {
//do nothing
}, {})
const balanceChanges = ctx.transaction.balanceChanges

if (balanceChanges) {
for (let i = 0; i < balanceChanges.length; i++) {
const amount = balanceChanges[i].amount
const coinType = balanceChanges[i].coinType
const owner = balanceChanges[i].owner
ctx.eventLogger.emit("balanceChanges", {
amount,
coinType,
owner
})
}
}

const objectChanges = ctx.transaction.objectChanges
if (objectChanges) {
for (let i = 0; i < objectChanges.length; i++) {
console.log("objectChanges", JSON.stringify(objectChanges[i]))
await processObjectChanges(ctx, objectChanges[i])
}
}

}, {}, { resourceChanges: true })




async function processObjectChanges(ctx: any, objectChange: any) {
switch (objectChange.type) {
case "transferred":
ctx.eventLogger.emit("transferred", {
objectId: objectChange.objectId,
objectType: objectChange.objectType,
recipient: objectChange.recipient,
sender: objectChange.sender,
version: objectChange.version
})
break
case "mutated":
ctx.eventLogger.emit("mutated", {
objectId: objectChange.objectId,
objectType: objectChange.objectType,
owner: objectChange.owner,
sender: objectChange.sender,
previousVersion: objectChange.previousVersion,
version: objectChange.version
})
break
case "deleted":
ctx.eventLogger.emit("deleted", {
objectId: objectChange.objectId,
objectType: objectChange.objectType,
sender: objectChange.sender,
version: objectChange.version
})
break
case "created":
ctx.eventLogger.emit("deleted", {
objectId: objectChange.objectId,
objectType: objectChange.objectType,
owner: objectChange.owner,
sender: objectChange.sender,
version: objectChange.version
})
break
default://do nothing for wrapped & published
break
}
}

Loading

0 comments on commit 8e8a76a

Please sign in to comment.