update consume price and use pool contract call.

This commit is contained in:
ssallam 2020-12-08 13:32:15 +01:00
parent bc33ba658e
commit 827d51b3e1
6 changed files with 52 additions and 79372 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
import { BigInt, BigDecimal } from '@graphprotocol/graph-ts' import { BigInt, BigDecimal } from '@graphprotocol/graph-ts'
import { OrderStarted, Transfer } from '../types/templates/Datatoken/Datatoken' import { OrderStarted, Transfer } from '../types/templates/DataToken/DataToken'
import { log } from '@graphprotocol/graph-ts' import { log } from '@graphprotocol/graph-ts'
import { import {

View File

@ -1,7 +1,8 @@
import { BigInt } from '@graphprotocol/graph-ts' import { BigInt } from '@graphprotocol/graph-ts'
import { TokenRegistered } from '../types/DTFactory/DTFactory' import { TokenRegistered } from '../types/DTFactory/DTFactory'
import { DatatokenFactory, Datatoken } from '../types/schema' import { DatatokenFactory, Datatoken as DatatokenEntity } from '../types/schema'
import { Datatoken as DatatokenContract } from '../types/templates' import { DataToken as DatatokenDataSource } from '../types/templates'
import { import {
createUserEntity, createUserEntity,
tokenToDecimal, tokenToDecimal,
@ -19,7 +20,7 @@ export function handleNewToken(event: TokenRegistered): void {
factory.txCount = BigInt.fromI32(0) factory.txCount = BigInt.fromI32(0)
} }
let datatoken = new Datatoken(event.params.tokenAddress.toHexString()) let datatoken = new DatatokenEntity(event.params.tokenAddress.toHexString())
log.error('************************ handleNewToken: datatokenId {}', [datatoken.id.toString()]) log.error('************************ handleNewToken: datatokenId {}', [datatoken.id.toString()])
datatoken.factoryID = event.address.toHexString() datatoken.factoryID = event.address.toHexString()
@ -45,5 +46,5 @@ export function handleNewToken(event: TokenRegistered): void {
factory.tokenCount = factory.tokenCount + 1 factory.tokenCount = factory.tokenCount + 1
factory.save() factory.save()
DatatokenContract.create(event.params.tokenAddress) DatatokenDataSource.create(event.params.tokenAddress)
} }

View File

@ -3,10 +3,12 @@ import {
BigInt, BigInt,
Bytes, Bytes,
dataSource, dataSource,
Address,
ethereum ethereum
} from '@graphprotocol/graph-ts' } from '@graphprotocol/graph-ts'
import { import {
Pool, Pool as PoolEntity,
User, User,
PoolToken, PoolToken,
PoolShare, PoolShare,
@ -18,11 +20,16 @@ import {
PoolTransactionTokenValues PoolTransactionTokenValues
} from '../types/schema' } from '../types/schema'
import { log } from '@graphprotocol/graph-ts' import { log } from '@graphprotocol/graph-ts'
import { Pool } from '../types/templates/Pool/Pool'
export let ZERO_BD = BigDecimal.fromString('0.0') export let ZERO_BD = BigDecimal.fromString('0.0')
export let MINUS_1 = BigDecimal.fromString('-1.0') export let MINUS_1 = BigDecimal.fromString('-1.0')
export let ONE = BigDecimal.fromString('1.0') export let ONE = BigDecimal.fromString('1.0')
export let ONE_INT = BigInt.fromI32(10).pow(18 as u8)
export let BONE = BigDecimal.fromString('1000000000000000000')
export let ENABLE_DEBUG = false export let ENABLE_DEBUG = false
let network = dataSource.network() let network = dataSource.network()
@ -41,7 +48,7 @@ export function debuglog(message: string, event: ethereum.Event, args: Array<str
for (let i=0; i < args.length; i++) { for (let i=0; i < args.length; i++) {
message = message.concat(' {}') message = message.concat(' {}')
} }
log.info(message, args) log.error('********** ' + message, args)
} }
export function hexToDecimal(hexString: String, decimals: i32): BigDecimal { export function hexToDecimal(hexString: String, decimals: i32): BigDecimal {
@ -61,6 +68,13 @@ export function tokenToDecimal(amount: BigDecimal, decimals: i32): BigDecimal {
return amount.div(scale) return amount.div(scale)
} }
export function decimalToBigInt(value: BigDecimal): BigInt {
value.truncate(18)
let scale = BigInt.fromI32(10).pow((value.exp.toI32() + 18) as u8)
return value.digits.times(scale)
}
export function updatePoolTokenBalance(poolToken: PoolToken, balance: BigDecimal, source: string): void { export function updatePoolTokenBalance(poolToken: PoolToken, balance: BigDecimal, source: string): void {
debuglog('updating poolToken balance (source, oldBalance, newBalance, poolId) ', null, debuglog('updating poolToken balance (source, oldBalance, newBalance, poolId) ', null,
[source, poolToken.balance.toString(), balance.toString(), poolToken.poolId]) [source, poolToken.balance.toString(), balance.toString(), poolToken.poolId])
@ -122,7 +136,7 @@ export function updatePoolTransactionToken(
export function createPoolTransaction(event: ethereum.Event, event_type: string, userAddress: string): void { export function createPoolTransaction(event: ethereum.Event, event_type: string, userAddress: string): void {
let poolId = event.address.toHex() let poolId = event.address.toHex()
let pool = Pool.load(poolId) let pool = PoolEntity.load(poolId)
let ptx = event.transaction.hash.toHexString() let ptx = event.transaction.hash.toHexString()
let ocnToken = PoolToken.load(poolId.concat('-').concat(OCEAN)) let ocnToken = PoolToken.load(poolId.concat('-').concat(OCEAN))
@ -147,19 +161,36 @@ export function createPoolTransaction(event: ethereum.Event, event_type: string,
poolTx.sharesBalance = ZERO_BD poolTx.sharesBalance = ZERO_BD
poolTx.spotPrice = calcSpotPrice( poolTx.spotPrice = calcSpotPrice(
ocnToken.denormWeight, dtToken.denormWeight, ocnToken.balance, dtToken.balance, pool.swapFee) ocnToken.denormWeight, dtToken.denormWeight, ocnToken.balance, dtToken.balance, pool.swapFee)
// poolTx.consumePrice = calcInGivenOut(
// ocnToken.denormWeight, dtToken.denormWeight, ocnToken.balance, dtToken.balance,
// ONE, pool.swapFee)
pool.datatokenReserve = dtToken.balance pool.datatokenReserve = dtToken.balance
pool.oceanReserve = ocnToken.balance pool.oceanReserve = ocnToken.balance
pool.spotPrice = poolTx.spotPrice pool.spotPrice = poolTx.spotPrice
let p = Pool.bind(Address.fromString(poolId))
let result = p.try_calcInGivenOut(
decimalToBigInt(ocnToken.balance), decimalToBigInt(ocnToken.denormWeight),
decimalToBigInt(dtToken.balance), decimalToBigInt(dtToken.denormWeight),
ONE_INT, decimalToBigInt(pool.swapFee)
)
pool.consumePrice = result.reverted ? BigDecimal.fromString('-1') : bigIntToDecimal(result.value, 18)
debuglog(
'args to calcInGivenOut (ocnBalance, ocnWeight, dtBalance, dtWeight, dtAmount, swapFee, result)', null,
[
decimalToBigInt(ocnToken.balance).toString(),
decimalToBigInt(ocnToken.denormWeight).toString(),
decimalToBigInt(dtToken.balance).toString(),
decimalToBigInt(dtToken.denormWeight).toString(),
ONE_INT.toString(),
decimalToBigInt(pool.swapFee).toString(),
result.reverted ? 'failed' : result.value.toString()
]
)
pool.save() pool.save()
debuglog('updated pool reserves (source, dtBalance, ocnBalance, dtReserve, ocnReserve): ', debuglog('updated pool reserves (source, dtBalance, ocnBalance, dtReserve, ocnReserve): ',
event, event,
['createPoolTransaction', dtToken.balance.toString(), ocnToken.balance.toString(), ['createPoolTransaction', dtToken.balance.toString(), ocnToken.balance.toString(),
pool.datatokenReserve.toString(), pool.oceanReserve.toString()]) pool.datatokenReserve.toString(), pool.oceanReserve.toString()])
// pool.consumePrice = ZERO_BD
poolTx.tx = event.transaction.hash poolTx.tx = event.transaction.hash
poolTx.event = event_type poolTx.event = event_type
@ -183,20 +214,6 @@ export function calcSpotPrice(
return ratio.times(scale) return ratio.times(scale)
} }
// export function calcInGivenOut(
// wIn: BigDecimal, wOut: BigDecimal, balanceIn: BigDecimal, balanceOut: BigDecimal,
// amountOut: BigDecimal, swapFee: BigDecimal): BigDecimal {
//
// let weightRatio = wOut.div(wIn)
// let diff = balanceOut.minus(amountOut)
// let y = balanceOut.div(diff)
// y.toString()
//
// let foo = BigDecimal.fromString(Math.pow(y, weightRatio).toString()) - ONE
// return balanceIn.times(foo / (ONE - swapFee))
// }
export function decrPoolCount(finalized: boolean): void { export function decrPoolCount(finalized: boolean): void {
let factory = PoolFactory.load('1') let factory = PoolFactory.load('1')
factory.poolCount -= 1 factory.poolCount -= 1

View File

@ -96,20 +96,20 @@ templates:
handler: handleTransfer handler: handleTransfer
- kind: ethereum/contract - kind: ethereum/contract
name: Datatoken name: DataToken
network: mainnet network: mainnet
source: source:
abi: Datatoken abi: DataToken
mapping: mapping:
kind: ethereum/events kind: ethereum/events
apiVersion: 0.0.4 apiVersion: 0.0.4
language: wasm/assemblyscript language: wasm/assemblyscript
file: ./src/mappings/datatoken.ts file: ./src/mappings/datatoken.ts
entities: entities:
- Datatoken - DataToken
- DatatokenFactory - DatatokenFactory
abis: abis:
- name: Datatoken - name: DataToken
file: ./abis/DataTokenTemplate.json file: ./abis/DataTokenTemplate.json
eventHandlers: eventHandlers:
- event: Transfer(indexed address,indexed address,uint256) - event: Transfer(indexed address,indexed address,uint256)