2020-12-11 11:37:09 +01:00
|
|
|
import { BigInt, Address, BigDecimal } from '@graphprotocol/graph-ts'
|
|
|
|
import {
|
|
|
|
LOG_CALL,
|
|
|
|
LOG_JOIN,
|
|
|
|
LOG_EXIT,
|
|
|
|
LOG_SWAP,
|
|
|
|
Transfer
|
2021-02-09 11:13:31 +01:00
|
|
|
} from '../@types/templates/Pool/Pool'
|
2020-11-20 13:12:02 +01:00
|
|
|
|
|
|
|
import {
|
2020-12-02 11:07:09 +01:00
|
|
|
PoolFactory,
|
|
|
|
Pool,
|
|
|
|
PoolToken,
|
|
|
|
PoolShare,
|
2020-12-09 12:50:17 +01:00
|
|
|
Datatoken,
|
|
|
|
PoolTransaction
|
2021-02-09 11:13:31 +01:00
|
|
|
} from '../@types/schema'
|
2020-11-20 13:12:02 +01:00
|
|
|
import {
|
|
|
|
hexToDecimal,
|
|
|
|
tokenToDecimal,
|
|
|
|
createPoolShareEntity,
|
|
|
|
createPoolTokenEntity,
|
|
|
|
ZERO_BD,
|
2020-12-09 11:54:13 +01:00
|
|
|
MINUS_1_BD,
|
2020-12-02 11:07:09 +01:00
|
|
|
decrPoolCount,
|
|
|
|
updatePoolTransactionToken,
|
2020-12-03 10:05:34 +01:00
|
|
|
createPoolTransaction,
|
|
|
|
OCEAN,
|
2020-12-11 11:37:09 +01:00
|
|
|
debuglog,
|
|
|
|
updatePoolTokenBalance
|
|
|
|
} from '../helpers'
|
2020-11-20 13:12:02 +01:00
|
|
|
|
|
|
|
/************************************
|
|
|
|
********** Pool Controls ***********
|
|
|
|
************************************/
|
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
export function handleSetSwapFee(
|
|
|
|
event: LOG_CALL,
|
|
|
|
swapFeeStr: string = null
|
|
|
|
): void {
|
|
|
|
const poolId = event.address.toHex()
|
|
|
|
const pool = Pool.load(poolId)
|
2020-11-20 13:12:02 +01:00
|
|
|
if (!swapFeeStr) {
|
|
|
|
swapFeeStr = event.params.data.toHexString().slice(-40)
|
|
|
|
}
|
|
|
|
pool.swapFee = hexToDecimal(swapFeeStr, 18)
|
|
|
|
pool.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleSetController(event: LOG_CALL): void {
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolId = event.address.toHex()
|
|
|
|
const pool = Pool.load(poolId)
|
|
|
|
pool.controller = Address.fromString(
|
|
|
|
event.params.data.toHexString().slice(-40)
|
|
|
|
)
|
2020-11-20 13:12:02 +01:00
|
|
|
pool.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleSetPublicSwap(event: LOG_CALL): void {
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolId = event.address.toHex()
|
|
|
|
const pool = Pool.load(poolId)
|
2021-01-19 12:16:03 +01:00
|
|
|
pool.publicSwap = event.params.data.toHexString().slice(-1) == '1'
|
2020-11-20 13:12:02 +01:00
|
|
|
pool.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleFinalize(event: LOG_CALL): void {
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolId = event.address.toHex()
|
|
|
|
const pool = Pool.load(poolId)
|
2020-11-20 13:12:02 +01:00
|
|
|
pool.finalized = true
|
|
|
|
pool.symbol = 'BPT'
|
|
|
|
pool.publicSwap = true
|
|
|
|
pool.save()
|
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
const factory = PoolFactory.load('1')
|
2020-11-20 13:12:02 +01:00
|
|
|
factory.finalizedPoolCount = factory.finalizedPoolCount + 1
|
|
|
|
factory.save()
|
|
|
|
}
|
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
export function _handleRebind(
|
|
|
|
event: LOG_CALL,
|
|
|
|
poolId: string,
|
|
|
|
tokenAddress: string,
|
|
|
|
balanceStr: string,
|
|
|
|
denormWeightStr: string
|
|
|
|
): void {
|
|
|
|
const pool = Pool.load(poolId)
|
|
|
|
const decimals = BigInt.fromI32(18).toI32()
|
|
|
|
|
2021-01-19 14:02:15 +01:00
|
|
|
if (tokenAddress != OCEAN) {
|
2020-12-02 11:07:09 +01:00
|
|
|
pool.datatokenAddress = tokenAddress
|
2020-11-20 13:12:02 +01:00
|
|
|
}
|
2020-12-02 17:20:29 +01:00
|
|
|
pool.tokenCount += BigInt.fromI32(1)
|
2020-12-11 11:37:09 +01:00
|
|
|
const address = Address.fromString(tokenAddress)
|
|
|
|
const denormWeight = hexToDecimal(denormWeightStr, decimals)
|
|
|
|
const poolTokenId = poolId.concat('-').concat(address.toHexString())
|
2020-11-20 13:12:02 +01:00
|
|
|
let poolToken = PoolToken.load(poolTokenId)
|
|
|
|
if (poolToken == null) {
|
|
|
|
createPoolTokenEntity(poolTokenId, poolId, address.toHexString())
|
|
|
|
poolToken = PoolToken.load(poolTokenId)
|
|
|
|
pool.totalWeight += denormWeight
|
|
|
|
} else {
|
2020-12-11 11:37:09 +01:00
|
|
|
const oldWeight = poolToken.denormWeight
|
2020-11-20 13:12:02 +01:00
|
|
|
if (denormWeight > oldWeight) {
|
|
|
|
pool.totalWeight = pool.totalWeight + (denormWeight - oldWeight)
|
|
|
|
} else {
|
|
|
|
pool.totalWeight = pool.totalWeight - (oldWeight - denormWeight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 11:54:13 +01:00
|
|
|
poolToken.denormWeight = denormWeight
|
2020-12-11 11:37:09 +01:00
|
|
|
const balance = hexToDecimal(balanceStr, decimals)
|
2020-12-03 10:05:34 +01:00
|
|
|
updatePoolTokenBalance(poolToken as PoolToken, balance, '_handleRebind')
|
2020-12-02 11:07:09 +01:00
|
|
|
|
2020-11-20 13:12:02 +01:00
|
|
|
poolToken.save()
|
|
|
|
if (balance.equals(ZERO_BD)) {
|
|
|
|
decrPoolCount(pool.finalized)
|
|
|
|
pool.active = false
|
|
|
|
}
|
|
|
|
pool.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleRebind(event: LOG_CALL): void {
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolId = event.address.toHex()
|
2020-11-20 13:12:02 +01:00
|
|
|
_handleRebind(
|
2020-12-11 11:37:09 +01:00
|
|
|
event,
|
|
|
|
poolId,
|
|
|
|
event.params.data.toHexString().slice(34, 74),
|
|
|
|
event.params.data.toHexString().slice(74, 138),
|
|
|
|
event.params.data.toHexString().slice(138)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleSetup(event: LOG_CALL): void {
|
2021-01-19 12:16:03 +01:00
|
|
|
if (PoolTransaction.load(event.transaction.hash.toHexString()) != null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolId = event.address.toHex()
|
|
|
|
debuglog('handleSetup: ', event, [])
|
|
|
|
const data = event.params.data.toHexString()
|
|
|
|
// First 2 chars are 0x
|
|
|
|
// Next there is 8 chars
|
|
|
|
// Next starts the data each params occupies exactly 64 chars
|
|
|
|
// Each value is padded with 0s to the left
|
|
|
|
// For an Address, need to remove the leading 24 zeros, because the address itself is 40 chars
|
|
|
|
// For numbers we donot need to remove the leading zeros because they have no effect being on the left of the number
|
|
|
|
|
|
|
|
// skip 8 then take the last 40 (2 + 8 + 24 = 34) to (2 + 8 + 64 = 74)
|
|
|
|
const dataTokenAddress = Address.fromString(data.slice(34, 74)).toHexString()
|
|
|
|
|
|
|
|
const dataTokenAmount = data.slice(74, 138) // 74+64
|
|
|
|
const dataTokenWeight = data.slice(138, 202) // (74+64,74+(2*64)
|
|
|
|
const baseTokenAddress = Address.fromString(
|
|
|
|
data.slice(202 + 24, 266)
|
|
|
|
).toHexString() // (74+(2*64)+24, 74+(3*64))
|
|
|
|
const baseTokenAmount = data.slice(266, 330) // (74+(3*64),74+(4*64))
|
|
|
|
const baseTokenWeight = data.slice(330, 394) // (74+(4*64),74+(5*64))
|
|
|
|
const swapFee = data.slice(394) // (74+(5*64), END)
|
|
|
|
|
|
|
|
const poolTokenId = poolId.concat('-').concat(baseTokenAddress)
|
|
|
|
const poolToken = PoolToken.load(poolTokenId)
|
|
|
|
if (poolToken != null) return
|
|
|
|
|
|
|
|
_handleRebind(
|
|
|
|
event,
|
|
|
|
poolId,
|
|
|
|
dataTokenAddress,
|
|
|
|
dataTokenAmount,
|
|
|
|
dataTokenWeight
|
|
|
|
)
|
|
|
|
_handleRebind(
|
|
|
|
event,
|
|
|
|
poolId,
|
|
|
|
baseTokenAddress,
|
|
|
|
baseTokenAmount,
|
|
|
|
baseTokenWeight
|
|
|
|
)
|
|
|
|
handleSetSwapFee(event, swapFee)
|
|
|
|
handleFinalize(event)
|
|
|
|
createPoolTransaction(event, 'setup', event.transaction.from.toHex())
|
|
|
|
|
|
|
|
// update base token
|
|
|
|
let amount = hexToDecimal(baseTokenAmount, 18)
|
|
|
|
|
|
|
|
updatePoolTransactionToken(
|
|
|
|
event.transaction.hash.toHexString(),
|
|
|
|
poolTokenId,
|
|
|
|
amount,
|
|
|
|
PoolToken.load(poolTokenId).balance,
|
|
|
|
ZERO_BD
|
|
|
|
)
|
|
|
|
// update the datatoken
|
|
|
|
amount = hexToDecimal(dataTokenAmount, 18)
|
|
|
|
updatePoolTransactionToken(
|
|
|
|
event.transaction.hash.toHexString(),
|
|
|
|
poolId.concat('-').concat(dataTokenAddress),
|
|
|
|
amount,
|
|
|
|
PoolToken.load(poolId.concat('-').concat(dataTokenAddress)).balance,
|
|
|
|
ZERO_BD
|
2020-11-20 13:12:02 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************
|
|
|
|
********** JOINS & EXITS ***********
|
|
|
|
************************************/
|
|
|
|
|
|
|
|
export function handleJoinPool(event: LOG_JOIN): void {
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolId = event.address.toHex()
|
2020-11-20 13:12:02 +01:00
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
const pool = Pool.load(poolId)
|
2021-01-19 12:16:03 +01:00
|
|
|
if (pool.finalized == false) {
|
2020-12-02 11:07:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.joinCount = pool.joinCount.plus(BigInt.fromI32(1))
|
2020-11-20 13:12:02 +01:00
|
|
|
pool.save()
|
2020-12-11 11:37:09 +01:00
|
|
|
const ptx = event.transaction.hash.toHexString()
|
|
|
|
const poolTx = PoolTransaction.load(ptx)
|
2020-12-02 17:20:29 +01:00
|
|
|
if (poolTx != null) {
|
2020-12-11 11:37:09 +01:00
|
|
|
debuglog(
|
|
|
|
'!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!! JOIN JOIN JOIN !!!!!!!!!!!! PoolTransaction EXISTS: ',
|
|
|
|
event,
|
|
|
|
[]
|
|
|
|
)
|
2020-12-02 17:20:29 +01:00
|
|
|
return
|
|
|
|
}
|
2020-11-20 13:12:02 +01:00
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
const address = event.params.tokenIn.toHex()
|
|
|
|
const poolTokenId = poolId.concat('-').concat(address)
|
|
|
|
const poolToken = PoolToken.load(poolTokenId)
|
2020-12-02 11:07:09 +01:00
|
|
|
if (poolToken == null) {
|
2020-12-11 11:37:09 +01:00
|
|
|
debuglog(
|
|
|
|
'!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!! JOIN JOIN JOIN !!!!!!!!!!!! NO PoolToken: ',
|
|
|
|
event,
|
|
|
|
[address, poolTokenId]
|
|
|
|
)
|
2020-11-20 13:12:02 +01:00
|
|
|
return
|
|
|
|
}
|
2020-11-26 12:10:45 +01:00
|
|
|
|
2021-01-15 14:52:07 +01:00
|
|
|
const datatoken: Datatoken | null =
|
2020-12-11 11:37:09 +01:00
|
|
|
poolToken.tokenId != null ? Datatoken.load(poolToken.tokenId) : null
|
|
|
|
const decimals =
|
|
|
|
datatoken == null ? BigInt.fromI32(18).toI32() : datatoken.decimals
|
|
|
|
const tokenAmountIn = tokenToDecimal(
|
|
|
|
event.params.tokenAmountIn.toBigDecimal(),
|
|
|
|
decimals
|
|
|
|
)
|
|
|
|
updatePoolTokenBalance(
|
|
|
|
poolToken as PoolToken,
|
|
|
|
poolToken.balance.plus(tokenAmountIn),
|
|
|
|
'handleJoinPool'
|
|
|
|
)
|
|
|
|
debuglog(
|
|
|
|
'!!!!!!!!!!!!!!!!!! JOIN JOIN JOIN : (token, amountIn, amountIn) ',
|
|
|
|
event,
|
|
|
|
[address, tokenAmountIn.toString(), event.params.tokenAmountIn.toString()]
|
|
|
|
)
|
2020-12-03 10:05:34 +01:00
|
|
|
|
2020-11-20 13:12:02 +01:00
|
|
|
poolToken.save()
|
2020-12-02 11:07:09 +01:00
|
|
|
createPoolTransaction(event, 'join', event.params.caller.toHexString())
|
|
|
|
updatePoolTransactionToken(
|
2020-12-11 11:37:09 +01:00
|
|
|
event.transaction.hash.toHexString(),
|
|
|
|
poolTokenId,
|
|
|
|
tokenAmountIn,
|
|
|
|
poolToken.balance,
|
2020-12-02 11:07:09 +01:00
|
|
|
tokenAmountIn.times(pool.swapFee)
|
|
|
|
)
|
2020-11-20 13:12:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function handleExitPool(event: LOG_EXIT): void {
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolId = event.address.toHex()
|
2020-11-20 13:12:02 +01:00
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
const address = event.params.tokenOut.toHex()
|
|
|
|
const poolTokenId = poolId.concat('-').concat(address.toString())
|
|
|
|
const poolToken = PoolToken.load(poolTokenId)
|
2020-11-20 13:12:02 +01:00
|
|
|
if (!poolToken) {
|
2020-12-11 11:37:09 +01:00
|
|
|
debuglog(
|
|
|
|
'!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!! EXIT EXIT EXIT !!!!!!!!!!!! NO PoolToken: ',
|
|
|
|
event,
|
|
|
|
[address, poolTokenId]
|
|
|
|
)
|
2020-12-09 11:54:13 +01:00
|
|
|
return
|
2020-11-20 13:12:02 +01:00
|
|
|
}
|
2020-11-26 12:10:45 +01:00
|
|
|
|
2021-01-15 14:50:24 +01:00
|
|
|
const datatoken: Datatoken | null =
|
2020-12-11 11:37:09 +01:00
|
|
|
poolToken.tokenId != null ? Datatoken.load(poolToken.tokenId) : null
|
|
|
|
const decimals =
|
|
|
|
datatoken == null ? BigInt.fromI32(18).toI32() : datatoken.decimals
|
|
|
|
const tokenAmountOut = tokenToDecimal(
|
|
|
|
event.params.tokenAmountOut.toBigDecimal(),
|
|
|
|
decimals
|
|
|
|
)
|
|
|
|
const newAmount = poolToken.balance.minus(tokenAmountOut)
|
2020-12-03 10:05:34 +01:00
|
|
|
updatePoolTokenBalance(poolToken as PoolToken, newAmount, 'handleExitPool')
|
2020-11-20 13:12:02 +01:00
|
|
|
poolToken.save()
|
2020-12-11 11:37:09 +01:00
|
|
|
debuglog(
|
|
|
|
'!!!!!!!!!!!!!!!!!! EXIT EXIT EXIT : (token, amountOut, amountOut)',
|
|
|
|
event,
|
|
|
|
[address, tokenAmountOut.toString(), event.params.tokenAmountOut.toString()]
|
|
|
|
)
|
|
|
|
const pool = Pool.load(poolId)
|
2020-12-02 11:07:09 +01:00
|
|
|
pool.exitCount = pool.exitCount.plus(BigInt.fromI32(1))
|
2020-11-20 13:12:02 +01:00
|
|
|
if (newAmount.equals(ZERO_BD)) {
|
|
|
|
decrPoolCount(pool.finalized)
|
|
|
|
pool.active = false
|
|
|
|
}
|
|
|
|
pool.save()
|
|
|
|
|
2020-12-02 11:07:09 +01:00
|
|
|
createPoolTransaction(event, 'exit', event.params.caller.toHexString())
|
|
|
|
updatePoolTransactionToken(
|
2020-12-11 11:37:09 +01:00
|
|
|
event.transaction.hash.toHexString(),
|
|
|
|
poolTokenId,
|
|
|
|
tokenAmountOut.times(MINUS_1_BD),
|
|
|
|
poolToken.balance,
|
2020-12-02 11:07:09 +01:00
|
|
|
tokenAmountOut.times(pool.swapFee)
|
|
|
|
)
|
2020-11-20 13:12:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************
|
|
|
|
************** SWAPS ***************
|
|
|
|
************************************/
|
|
|
|
|
|
|
|
export function handleSwap(event: LOG_SWAP): void {
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolId = event.address.toHex()
|
|
|
|
const ptx = event.transaction.hash.toHexString()
|
2020-11-20 13:12:02 +01:00
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
const tokenIn = event.params.tokenIn.toHex()
|
|
|
|
const poolTokenInId = poolId.concat('-').concat(tokenIn.toString())
|
|
|
|
const poolTokenIn = PoolToken.load(poolTokenInId)
|
2020-11-20 13:12:02 +01:00
|
|
|
if (!poolTokenIn) {
|
2020-12-11 11:37:09 +01:00
|
|
|
debuglog(
|
|
|
|
'!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!! SWAP SWAP SWAP !!!!!!!!!!!! NO PoolToken: ',
|
|
|
|
event,
|
|
|
|
[tokenIn, poolTokenInId]
|
|
|
|
)
|
2020-11-20 13:12:02 +01:00
|
|
|
return
|
|
|
|
}
|
2020-12-11 11:37:09 +01:00
|
|
|
const dtIn = Datatoken.load(tokenIn)
|
|
|
|
const tokenAmountIn = tokenToDecimal(
|
|
|
|
event.params.tokenAmountIn.toBigDecimal(),
|
|
|
|
dtIn == null ? 18 : dtIn.decimals
|
|
|
|
)
|
|
|
|
const newAmountIn = poolTokenIn.balance.plus(tokenAmountIn)
|
|
|
|
updatePoolTokenBalance(
|
|
|
|
poolTokenIn as PoolToken,
|
|
|
|
newAmountIn,
|
|
|
|
'handleSwap.tokenIn'
|
|
|
|
)
|
2020-11-20 13:12:02 +01:00
|
|
|
poolTokenIn.save()
|
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
const tokenOut = event.params.tokenOut.toHex()
|
|
|
|
const poolTokenOutId = poolId.concat('-').concat(tokenOut.toString())
|
|
|
|
const poolTokenOut = PoolToken.load(poolTokenOutId)
|
|
|
|
const dtOut = Datatoken.load(tokenOut)
|
|
|
|
const tokenAmountOut = tokenToDecimal(
|
|
|
|
event.params.tokenAmountOut.toBigDecimal(),
|
|
|
|
dtOut == null ? 18 : dtOut.decimals
|
|
|
|
)
|
|
|
|
const newAmountOut = poolTokenOut.balance.minus(tokenAmountOut)
|
|
|
|
updatePoolTokenBalance(
|
|
|
|
poolTokenOut as PoolToken,
|
|
|
|
newAmountOut,
|
|
|
|
'handleSwap.tokenOut'
|
|
|
|
)
|
2020-11-20 13:12:02 +01:00
|
|
|
poolTokenOut.save()
|
2020-12-11 11:37:09 +01:00
|
|
|
debuglog(
|
|
|
|
'!!!!!!!!!!!!!!!!!! SWAP SWAP SWAP : (tokenIn, tokenOut, amountIn, amountIn, amountOut, amountOut)',
|
|
|
|
event,
|
|
|
|
[
|
|
|
|
tokenIn,
|
|
|
|
tokenOut,
|
|
|
|
tokenAmountIn.toString(),
|
|
|
|
event.params.tokenAmountIn.toString(),
|
|
|
|
tokenAmountOut.toString(),
|
|
|
|
event.params.tokenAmountOut.toString()
|
|
|
|
]
|
|
|
|
)
|
|
|
|
const pool = Pool.load(poolId)
|
2020-11-20 13:12:02 +01:00
|
|
|
|
2020-12-09 11:54:13 +01:00
|
|
|
pool.swapCount = pool.swapCount.plus(BigInt.fromI32(1))
|
2020-11-20 13:12:02 +01:00
|
|
|
if (newAmountIn.equals(ZERO_BD) || newAmountOut.equals(ZERO_BD)) {
|
|
|
|
decrPoolCount(pool.finalized)
|
|
|
|
pool.active = false
|
|
|
|
}
|
|
|
|
pool.save()
|
|
|
|
|
2020-12-02 11:07:09 +01:00
|
|
|
createPoolTransaction(event, 'swap', event.params.caller.toHexString())
|
|
|
|
updatePoolTransactionToken(
|
2020-12-11 11:37:09 +01:00
|
|
|
ptx,
|
|
|
|
poolTokenIn.id,
|
|
|
|
tokenAmountIn,
|
|
|
|
poolTokenIn.balance,
|
|
|
|
tokenAmountIn.times(pool.swapFee)
|
|
|
|
)
|
2020-12-02 11:07:09 +01:00
|
|
|
updatePoolTransactionToken(
|
2020-12-11 11:37:09 +01:00
|
|
|
ptx,
|
|
|
|
poolTokenOut.id,
|
|
|
|
tokenAmountOut.times(MINUS_1_BD),
|
|
|
|
poolTokenOut.balance,
|
|
|
|
BigDecimal.fromString('0.0')
|
|
|
|
)
|
2020-11-20 13:12:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************
|
|
|
|
*********** POOL SHARES ************
|
|
|
|
************************************/
|
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
export function handleTransfer(event: Transfer): void {
|
|
|
|
const poolId = event.address.toHex()
|
2020-11-20 13:12:02 +01:00
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
|
2020-11-20 13:12:02 +01:00
|
|
|
|
2021-01-19 12:16:03 +01:00
|
|
|
const isMint = event.params.from.toHex() == ZERO_ADDRESS
|
|
|
|
const isBurn = event.params.to.toHex() == ZERO_ADDRESS
|
2020-11-20 13:12:02 +01:00
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolShareFromId = poolId.concat('-').concat(event.params.from.toHex())
|
2020-11-20 13:12:02 +01:00
|
|
|
let poolShareFrom = PoolShare.load(poolShareFromId)
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolShareFromBalance =
|
|
|
|
poolShareFrom == null ? ZERO_BD : poolShareFrom.balance
|
2020-11-20 13:12:02 +01:00
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolShareToId = poolId.concat('-').concat(event.params.to.toHex())
|
2020-11-20 13:12:02 +01:00
|
|
|
let poolShareTo = PoolShare.load(poolShareToId)
|
2020-12-11 11:37:09 +01:00
|
|
|
const poolShareToBalance = poolShareTo == null ? ZERO_BD : poolShareTo.balance
|
2020-11-20 13:12:02 +01:00
|
|
|
|
2020-12-11 11:37:09 +01:00
|
|
|
const pool = Pool.load(poolId)
|
|
|
|
const poolTx = PoolTransaction.load(event.transaction.hash.toHexString())
|
|
|
|
const value = tokenToDecimal(event.params.value.toBigDecimal(), 18)
|
2021-02-09 11:13:31 +01:00
|
|
|
debuglog('poolShare Transfer event: (from, to, value)', event,
|
|
|
|
[event.params.from.toHex(), event.params.to.toHex(), value.toString()])
|
2020-11-20 13:12:02 +01:00
|
|
|
|
|
|
|
if (isMint) {
|
|
|
|
if (poolShareTo == null) {
|
|
|
|
createPoolShareEntity(poolShareToId, poolId, event.params.to.toHex())
|
|
|
|
poolShareTo = PoolShare.load(poolShareToId)
|
|
|
|
}
|
2020-12-02 11:07:09 +01:00
|
|
|
poolShareTo.balance += value
|
2020-11-20 13:12:02 +01:00
|
|
|
poolShareTo.save()
|
2020-12-02 11:07:09 +01:00
|
|
|
pool.totalShares += value
|
|
|
|
if (poolTx != null) {
|
|
|
|
poolTx.sharesTransferAmount = value
|
|
|
|
poolTx.sharesBalance = poolShareTo.balance
|
|
|
|
}
|
2021-02-09 11:13:31 +01:00
|
|
|
debuglog(
|
|
|
|
'pool shares mint: (id, value, totalShares, shareToBalance, toAddress)',
|
|
|
|
event,
|
|
|
|
[
|
|
|
|
poolId,
|
|
|
|
value.toString(),
|
|
|
|
pool.totalShares.toString(),
|
|
|
|
poolShareTo.balance.toString(),
|
|
|
|
poolShareTo.userAddress
|
|
|
|
]
|
|
|
|
)
|
2020-11-20 13:12:02 +01:00
|
|
|
} else if (isBurn) {
|
|
|
|
if (poolShareFrom == null) {
|
2020-12-02 11:07:09 +01:00
|
|
|
createPoolShareEntity(poolShareFromId, poolId, event.params.from.toHex())
|
|
|
|
poolShareFrom = PoolShare.load(poolShareFromId)
|
|
|
|
}
|
|
|
|
poolShareFrom.balance -= value
|
2020-11-20 13:12:02 +01:00
|
|
|
poolShareFrom.save()
|
2020-12-02 11:07:09 +01:00
|
|
|
pool.totalShares -= value
|
|
|
|
if (poolTx != null) {
|
|
|
|
poolTx.sharesTransferAmount = -value
|
|
|
|
poolTx.sharesBalance = poolShareFrom.balance
|
|
|
|
}
|
2021-02-09 11:13:31 +01:00
|
|
|
debuglog(
|
|
|
|
'pool shares burn: (id, value, totalShares, shareFromBalance, fromAddress)',
|
|
|
|
event,
|
|
|
|
[
|
|
|
|
poolId,
|
|
|
|
value.toString(),
|
|
|
|
pool.totalShares.toString(),
|
|
|
|
poolShareFrom.balance.toString(),
|
|
|
|
poolShareFrom.userAddress
|
|
|
|
]
|
|
|
|
)
|
2020-11-20 13:12:02 +01:00
|
|
|
} else {
|
|
|
|
if (poolShareTo == null) {
|
|
|
|
createPoolShareEntity(poolShareToId, poolId, event.params.to.toHex())
|
|
|
|
poolShareTo = PoolShare.load(poolShareToId)
|
|
|
|
}
|
2020-12-02 11:07:09 +01:00
|
|
|
poolShareTo.balance += value
|
2020-11-20 13:12:02 +01:00
|
|
|
poolShareTo.save()
|
|
|
|
|
|
|
|
if (poolShareFrom == null) {
|
|
|
|
createPoolShareEntity(poolShareFromId, poolId, event.params.from.toHex())
|
|
|
|
poolShareFrom = PoolShare.load(poolShareFromId)
|
|
|
|
}
|
2020-12-02 11:07:09 +01:00
|
|
|
poolShareFrom.balance -= value
|
2020-11-20 13:12:02 +01:00
|
|
|
poolShareFrom.save()
|
2021-02-09 11:13:31 +01:00
|
|
|
debuglog(
|
|
|
|
'pool shares transfer: ' +
|
|
|
|
'(id, value, totalShares, shareToBalance, shareFromBalance, toAddress, fromAddress)', event,
|
|
|
|
[poolId, value.toString(), pool.totalShares.toString(),
|
|
|
|
poolShareTo.balance.toString(), poolShareFrom.balance.toString(),
|
|
|
|
poolShareTo.userAddress, poolShareFrom.userAddress
|
|
|
|
])
|
2020-11-20 13:12:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2021-01-19 14:02:15 +01:00
|
|
|
poolShareTo != null &&
|
2020-12-11 11:37:09 +01:00
|
|
|
poolShareTo.balance.notEqual(ZERO_BD) &&
|
|
|
|
poolShareToBalance.equals(ZERO_BD)
|
2020-11-20 13:12:02 +01:00
|
|
|
) {
|
2020-12-02 11:07:09 +01:00
|
|
|
pool.holderCount += BigInt.fromI32(1)
|
2020-11-20 13:12:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2021-01-19 14:02:15 +01:00
|
|
|
poolShareFrom != null &&
|
2020-12-11 11:37:09 +01:00
|
|
|
poolShareFrom.balance.equals(ZERO_BD) &&
|
|
|
|
poolShareFromBalance.notEqual(ZERO_BD)
|
2020-11-20 13:12:02 +01:00
|
|
|
) {
|
2020-12-02 11:07:09 +01:00
|
|
|
pool.holderCount -= BigInt.fromI32(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (poolTx != null) {
|
|
|
|
poolTx.save()
|
2020-11-20 13:12:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pool.save()
|
2020-12-11 11:37:09 +01:00
|
|
|
}
|