Skip to content

Commit

Permalink
fix bridge token, add simple token account tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
zfy0701 committed Dec 26, 2023
1 parent d083da2 commit e0b0328
Show file tree
Hide file tree
Showing 15 changed files with 9,061 additions and 106 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"type": "module",
"dependencies": {
"@sentio/sdk": "^2.28.4-rc.1",
"@sentio/sdk": "^2.29.0-rc.2",
"node-fetch": "^3.3.1"
},
"resolutions": {
Expand Down
79 changes: 41 additions & 38 deletions projects/bridgetoken/src/processor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { SimpleCoinInfo, whitelistCoins } from "@sentio/sdk/aptos/ext";
import {CHAIN_IDS} from "@sentio/sdk";
import { whitelistCoins } from "@sentio/sdk/aptos/ext";
import { AptosNetwork } from "@sentio/sdk/aptos";
import {account, coin, type_info} from "@sentio/sdk/aptos/builtin/0x1";
import {getPriceByType} from "@sentio/sdk/utils";
import {defaultMoveCoder, getAptosClient} from "@sentio/sdk/aptos";
import { SimpleCoinInfo } from "@sentio/sdk/move/ext";
import { MoveStructId, EntryFunctionPayloadResponse } from "@aptos-labs/ts-sdk"
import { scaleDown } from "@sentio/sdk";

const BRIDGE_TOKENS = new Map<string, SimpleCoinInfo>()
const PRICES = new Map<string, number>()
Expand All @@ -16,7 +19,7 @@ for (const token of whitelistCoins().values()) {

BRIDGE_TOKENS.set(token.token_type.type, token)

getPriceByType(CHAIN_IDS.APTOS_MAINNET, token.token_type.type, date).then((price) => {
getPriceByType(AptosNetwork.MAIN_NET, token.token_type.type, date).then((price) => {
price = price || 0
PRICES.set(token.token_type.type, price)
console.log("price", token.token_type.type, price)
Expand All @@ -25,39 +28,39 @@ for (const token of whitelistCoins().values()) {

const client = getAptosClient()!

// coin.bind().onEventDepositEvent(async (evt, ctx) => {
// const payload = ctx.transaction.payload as TransactionPayload_EntryFunctionPayload
// const token = BRIDGE_TOKENS.get(payload.type_arguments[0])
// if (!token) {
// return
// }
//
// const amount = scaleDown(evt.data_decoded.amount, token.decimals)
// const value = amount.multipliedBy(PRICES.get(token.token_type.type)!)
//
// // const value = await calculateValueInUsd(evt.data_decoded.amount, token, priceTimestamp)
// if (!value.isGreaterThan(0)) {
// return
// }
//
// ctx.logger.info("deposit", {value: value.toNumber(), token: token.symbol, bridge: token.bridge, account: evt.guid.account_address})
// }).onEventWithdrawEvent(async (evt, ctx) => {
// const payload = ctx.transaction.payload as TransactionPayload_EntryFunctionPayload
// const token = BRIDGE_TOKENS.get(payload.type_arguments[0])
// if (!token) {
// return
// }
//
// const amount = scaleDown(evt.data_decoded.amount, token.decimals)
// const value = amount.multipliedBy(PRICES.get(token.token_type.type)!)
// // const value = await calculateValueInUsd(evt.data_decoded.amount, token, priceTimestamp)
// if (!value.isGreaterThan(0)) {
// return
// }
// ctx.logger.info("withdraw", {value: value.negated().toNumber(), token: token.symbol, bridge: token.bridge, account: evt.guid.account_address})
// })

coin.bind()
coin.bind().onEventDepositEvent(async (evt, ctx) => {
const payload = ctx.transaction.payload as EntryFunctionPayloadResponse
const token = BRIDGE_TOKENS.get(payload.type_arguments[0])
if (!token) {
return
}

const amount = scaleDown(evt.data_decoded.amount, token.decimals)
const value = amount.multipliedBy(PRICES.get(token.token_type.type)!)

// const value = await calculateValueInUsd(evt.data_decoded.amount, token, priceTimestamp)
if (!value.isGreaterThan(0)) {
return
}

ctx.eventLogger.emit("deposit", {value: value.toNumber(), token: token.symbol, bridge: token.bridge, account: evt.guid.account_address})
}).onEventWithdrawEvent(async (evt, ctx) => {
const payload = ctx.transaction.payload as EntryFunctionPayloadResponse
const token = BRIDGE_TOKENS.get(payload.type_arguments[0])
if (!token) {
return
}

const amount = scaleDown(evt.data_decoded.amount, token.decimals)
const value = amount.multipliedBy(PRICES.get(token.token_type.type)!)
// const value = await calculateValueInUsd(evt.data_decoded.amount, token, priceTimestamp)
if (!value.isGreaterThan(0)) {
return
}
ctx.eventLogger.emit("withdraw", {value: value.negated().toNumber(), token: token.symbol, bridge: token.bridge, account: evt.guid.account_address})
})

// coin.bind()


// defaultMoveCoder().load(coin.ABI)
Expand All @@ -68,9 +71,9 @@ account.bind().onEventCoinRegisterEvent(async (call, ctx) => {
if (!token) {
return
}
const coinStore = `0x1::coin::CoinStore<${token.token_type.type}>`;
const coinStore = `0x1::coin::CoinStore<${token.token_type.type}>` as MoveStructId;

const res = await client.getAccountResource(accountAddress, coinStore)
const res = await client.getAccountResource({ accountAddress, resourceType: coinStore })
const decodedRes = await defaultMoveCoder().decodeResource<coin.CoinStore<any>>(res)
if (!decodedRes) {
console.log(res)
Expand Down
3 changes: 2 additions & 1 deletion projects/bridgetoken/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"exclude": [
"dist",
"tsconfig.json"
"tsconfig.json",
"jest.config.ts"
]
}
10 changes: 6 additions & 4 deletions projects/liquidswap/src/liquidswap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import {defaultMoveCoder, AptosResourcesProcessor, TypedMoveResource, MoveResource, AptosResourcesContext} from "@sentio/sdk/aptos";
import {defaultMoveCoder, AptosResourcesProcessor, TypedMoveResource, AptosResourcesContext} from "@sentio/sdk/aptos";
import { MoveResource } from "@aptos-labs/ts-sdk"


// v0
Expand Down Expand Up @@ -32,6 +33,7 @@ import {
tvlByPoolNew,
volume, volumeByCoin,
} from "./metrics.js"
import { SimpleCoinInfo } from "@sentio/sdk/move/ext";

// TODO to remove
export const accountTracker = AccountEventTracker.register("users")
Expand Down Expand Up @@ -362,7 +364,7 @@ for (const env of [v0, v05]) {
let priceInUsd: Map<string, BigDecimal> = new Map<string, BigDecimal>()

function calcPrice(coin: string, pools: TypedMoveResource<PoolType<any, any, any>>[]) {
/*

const coinInfo = getCoinInfo(coin)
if (coinInfo.symbol == "USDC") {
return BigDecimal(1)
Expand Down Expand Up @@ -414,8 +416,8 @@ for (const env of [v0, v05]) {
} else {
console.log(`failed to get price of coin[${coinInfo.symbol}]`)
}
return res*/
return BigDecimal(0)
return res
// return BigDecimal(0)
}

// loadAllTypes(defaultMoveCoder())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"bytecode": "0xa11ceb0b050000000501000202020a070c170823200a4305000000010003000100010001076c705f636f696e024c500b64756d6d795f6669656c6405a97986a9d031c4567e15b797be516910cfcb4156312482efc6a19c0a30c948000201020100",
"abi": {
"address": "0x5a97986a9d031c4567e15b797be516910cfcb4156312482efc6a19c0a30c948",
"name": "lp_coin",
"friends": [],
"exposed_functions": [],
"structs": [
{
"name": "LP",
"is_native": false,
"abilities": [],
"generic_type_params": [
{
"constraints": []
},
{
"constraints": []
},
{
"constraints": []
}
],
"fields": [
{
"name": "dummy_field",
"type": "bool"
}
]
}
]
}
}
]
4,274 changes: 4,274 additions & 0 deletions projects/simple-aptos-coin/abis/aptos/auxexchange.json

Large diffs are not rendered by default.

Loading

0 comments on commit e0b0328

Please sign in to comment.