add FixedRateExchange (#53)

* add FixedRateExchange

* fix lint

* remove duplicate code.

* fix style

* remove newline.

Co-authored-by: ssallam <36411297+ssallam@users.noreply.github.com>
This commit is contained in:
Alex Coseru 2021-03-10 23:36:51 +02:00 committed by GitHub
parent 63edacb77a
commit 7784de4a97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 15939 additions and 0 deletions

15678
abis/FixedRateExchange.json Normal file

File diff suppressed because one or more lines are too long

View File

@ -200,4 +200,39 @@ type User @entity {
poolTransactionsTokenValues: [PoolTransactionTokenValues!] @derivedFrom(field: "userAddress")
tokenTransactions: [TokenTransaction!] @derivedFrom(field: "userAddress")
orders: [TokenOrder!] @derivedFrom(field: "payer")
freSwaps: [FixedRateExchangeSwap!] @derivedFrom(field: "by")
}
type FixedRateExchange @entity {
id: ID! # fixed rate exchange id
exchangeOwner: User!
datatoken: Datatoken!
baseToken: String!
rate: BigDecimal!
active: Boolean!
updates: [FixedRateExchangeUpdate!] @derivedFrom(field: "exchangeId")
swaps: [FixedRateExchangeSwap!] @derivedFrom(field: "exchangeId")
}
type FixedRateExchangeUpdate @entity {
id: ID!
exchangeId: FixedRateExchange!
oldRate: BigDecimal!
newRate: BigDecimal!
oldActive: Boolean!
newActive: Boolean!
block: Int!
timestamp: Int!
tx: Bytes!
}
type FixedRateExchangeSwap @entity {
id: ID!
exchangeId: FixedRateExchange!
by: User!
baseTokenAmount: BigDecimal!
dataTokenAmount: BigDecimal!
block: Int!
timestamp: Int!
tx: Bytes!
}

View File

@ -0,0 +1,114 @@
import { BigInt, ethereum } from '@graphprotocol/graph-ts'
import {
ExchangeCreated,
ExchangeActivated,
ExchangeDeactivated,
ExchangeRateChanged,
Swapped
} from '../@types/FixedRateExchange/FixedRateExchange'
import {
FixedRateExchange,
FixedRateExchangeUpdate,
FixedRateExchangeSwap
} from '../@types/schema'
import { tokenToDecimal } from '../helpers'
export function handleExchangeCreated(event: ExchangeCreated): void {
const fixedrateexchange = new FixedRateExchange(
event.params.exchangeId.toHexString()
)
fixedrateexchange.exchangeOwner = event.params.exchangeOwner.toHexString()
fixedrateexchange.datatoken = event.params.dataToken.toHexString()
fixedrateexchange.baseToken = event.params.baseToken.toHexString()
fixedrateexchange.active = false
fixedrateexchange.rate = tokenToDecimal(
event.params.fixedRate.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
fixedrateexchange.save()
}
function _processActiveUpdated(
event: ethereum.Event,
exchangeId: string,
active: boolean
): void {
const tx = event.transaction.hash
const id = tx.toHexString().concat('-').concat(exchangeId)
const fixedrateexchange = FixedRateExchange.load(exchangeId)
const freupdate = new FixedRateExchangeUpdate(id)
freupdate.exchangeId = exchangeId
freupdate.oldRate = fixedrateexchange.rate
freupdate.newRate = fixedrateexchange.rate
freupdate.oldActive = fixedrateexchange.active
freupdate.newActive = active
freupdate.block = event.block.number.toI32()
freupdate.timestamp = event.block.timestamp.toI32()
freupdate.tx = tx
freupdate.save()
fixedrateexchange.active = active
fixedrateexchange.save()
}
export function handleExchangeActivated(event: ExchangeActivated): void {
_processActiveUpdated(event, event.params.exchangeId.toHexString(), true)
}
export function handleExchangeDeactivated(event: ExchangeDeactivated): void {
_processActiveUpdated(event, event.params.exchangeId.toHexString(), false)
}
export function handleExchangeRateChanged(event: ExchangeRateChanged): void {
const tx = event.transaction.hash
const id = tx
.toHexString()
.concat('-')
.concat(event.params.exchangeId.toHexString())
const fixedrateexchange = FixedRateExchange.load(
event.params.exchangeId.toHexString()
)
const freupdate = new FixedRateExchangeUpdate(id)
freupdate.exchangeId = fixedrateexchange.id
freupdate.oldRate = fixedrateexchange.rate
freupdate.newRate = tokenToDecimal(
event.params.newRate.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
freupdate.oldActive = fixedrateexchange.active
freupdate.newActive = fixedrateexchange.active
freupdate.block = event.block.number.toI32()
freupdate.timestamp = event.block.timestamp.toI32()
freupdate.tx = tx
freupdate.save()
fixedrateexchange.rate = freupdate.newRate
fixedrateexchange.save()
}
export function handleSwapped(event: Swapped): void {
const tx = event.transaction.hash
const id = tx
.toHexString()
.concat('-')
.concat(event.params.exchangeId.toHexString())
const freSwap = new FixedRateExchangeSwap(id)
freSwap.exchangeId = event.params.exchangeId.toHexString()
freSwap.by = event.params.by.toHexString()
freSwap.baseTokenAmount = tokenToDecimal(
event.params.baseTokenSwappedAmount.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
freSwap.dataTokenAmount = tokenToDecimal(
event.params.dataTokenSwappedAmount.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
freSwap.block = event.block.number.toI32()
freSwap.timestamp = event.block.timestamp.toI32()
freSwap.tx = tx
freSwap.save()
}

View File

@ -66,6 +66,34 @@ dataSources:
handler: handleMetadataCreated
- event: MetadataUpdated(indexed address,indexed address,bytes,bytes)
handler: handleMetadataUpdated
- kind: ethereum/contract
name: FixedRateExchange
network: polygon
source:
address: '0x2112Eb973af1DBf83a4f11eda82f7a7527D7Fde5'
abi: FixedRateExchange
startBlock: 11005247
mapping:
kind: ethereum/events
apiVersion: 0.0.4
language: wasm/assemblyscript
file: ./src/mappings/fixedrateexchange.ts
entities:
- FixedRateExchange
abis:
- name: FixedRateExchange
file: ./abis/FixedRateExchange.json
eventHandlers:
- event: ExchangeCreated(indexed bytes32,indexed address,indexed address,address,uint256)
handler: handleExchangeCreated
- event: ExchangeActivated(indexed bytes32,indexed address)
handler: handleExchangeActivated
- event: ExchangeDeactivated(indexed bytes32,indexed address)
handler: handleExchangeDeactivated
- event: ExchangeRateChanged(indexed bytes32,indexed address,uint256)
handler: handleExchangeRateChanged
- event: Swapped(indexed bytes32,indexed address,uint256,uint256)
handler: handleSwapped
templates:
- kind: ethereum/contract
name: Pool

View File

@ -66,6 +66,34 @@ dataSources:
handler: handleMetadataCreated
- event: MetadataUpdated(indexed address,indexed address,bytes,bytes)
handler: handleMetadataUpdated
- kind: ethereum/contract
name: FixedRateExchange
network: rinkeby
source:
address: '0xeD1DfC5F3a589CfC4E8B91C1fbfC18FC6699Fbde'
abi: FixedRateExchange
startBlock: 7298808
mapping:
kind: ethereum/events
apiVersion: 0.0.4
language: wasm/assemblyscript
file: ./src/mappings/fixedrateexchange.ts
entities:
- FixedRateExchange
abis:
- name: FixedRateExchange
file: ./abis/FixedRateExchange.json
eventHandlers:
- event: ExchangeCreated(indexed bytes32,indexed address,indexed address,address,uint256)
handler: handleExchangeCreated
- event: ExchangeActivated(indexed bytes32,indexed address)
handler: handleExchangeActivated
- event: ExchangeDeactivated(indexed bytes32,indexed address)
handler: handleExchangeDeactivated
- event: ExchangeRateChanged(indexed bytes32,indexed address,uint256)
handler: handleExchangeRateChanged
- event: Swapped(indexed bytes32,indexed address,uint256,uint256)
handler: handleSwapped
templates:
- kind: ethereum/contract
name: Pool

View File

@ -66,6 +66,34 @@ dataSources:
handler: handleMetadataCreated
- event: MetadataUpdated(indexed address,indexed address,bytes,bytes)
handler: handleMetadataUpdated
- kind: ethereum/contract
name: FixedRateExchange
network: ropsten
source:
address: '0xA7a711A09396DF82D9be46A26B48BafdB9BB4fA6'
abi: FixedRateExchange
startBlock: 9227595
mapping:
kind: ethereum/events
apiVersion: 0.0.4
language: wasm/assemblyscript
file: ./src/mappings/fixedrateexchange.ts
entities:
- FixedRateExchange
abis:
- name: FixedRateExchange
file: ./abis/FixedRateExchange.json
eventHandlers:
- event: ExchangeCreated(indexed bytes32,indexed address,indexed address,address,uint256)
handler: handleExchangeCreated
- event: ExchangeActivated(indexed bytes32,indexed address)
handler: handleExchangeActivated
- event: ExchangeDeactivated(indexed bytes32,indexed address)
handler: handleExchangeDeactivated
- event: ExchangeRateChanged(indexed bytes32,indexed address,uint256)
handler: handleExchangeRateChanged
- event: Swapped(indexed bytes32,indexed address,uint256,uint256)
handler: handleSwapped
templates:
- kind: ethereum/contract
name: Pool

View File

@ -66,6 +66,34 @@ dataSources:
handler: handleMetadataCreated
- event: MetadataUpdated(indexed address,indexed address,bytes,bytes)
handler: handleMetadataUpdated
- kind: ethereum/contract
name: FixedRateExchange
network: mainnet
source:
address: '0x608d05214E42722B94a54cF6114d4840FCfF84e1'
abi: FixedRateExchange
startBlock: 11105610
mapping:
kind: ethereum/events
apiVersion: 0.0.4
language: wasm/assemblyscript
file: ./src/mappings/fixedrateexchange.ts
entities:
- FixedRateExchange
abis:
- name: FixedRateExchange
file: ./abis/FixedRateExchange.json
eventHandlers:
- event: ExchangeCreated(indexed bytes32,indexed address,indexed address,address,uint256)
handler: handleExchangeCreated
- event: ExchangeActivated(indexed bytes32,indexed address)
handler: handleExchangeActivated
- event: ExchangeDeactivated(indexed bytes32,indexed address)
handler: handleExchangeDeactivated
- event: ExchangeRateChanged(indexed bytes32,indexed address,uint256)
handler: handleExchangeRateChanged
- event: Swapped(indexed bytes32,indexed address,uint256,uint256)
handler: handleSwapped
templates:
- kind: ethereum/contract
name: Pool