Skip to content

Commit 16b4702

Browse files
committed
bringing back the user model
1 parent 5fc8878 commit 16b4702

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

schema.graphql

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,15 @@ type PriceRateProvider @entity(immutable: false) {
157157

158158
type PoolShare @entity(immutable: false) {
159159
id: ID!
160-
userAddress: Bytes!
160+
userAddress: User!
161161
poolId: Pool!
162162
balance: BigDecimal!
163163
}
164164

165+
type User @entity(immutable: true) {
166+
id: ID!
167+
}
168+
165169
type AmpUpdate @entity(immutable: true) {
166170
id: ID!
167171
poolId: Pool!
@@ -182,7 +186,7 @@ type Swap @entity(immutable: true) {
182186
tokenAmountIn: BigDecimal!
183187
tokenAmountOut: BigDecimal!
184188
poolId: Pool!
185-
userAddress: Bytes!
189+
userAddress: User!
186190
timestamp: Int!
187191
block: BigInt
188192
tx: Bytes!
@@ -199,7 +203,7 @@ type JoinExit @entity(immutable: true) {
199203
sender: Bytes!
200204
amounts: [BigDecimal!]!
201205
pool: Pool!
202-
user: Bytes!
206+
user: User!
203207
timestamp: Int!
204208
tx: Bytes!
205209
block: BigInt

src/mappings/helpers/misc.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BigDecimal, Address, BigInt, Bytes } from '@graphprotocol/graph-ts';
2-
import { Pool, PoolToken, PoolShare, PriceRateProvider, Token, FXOracle } from '../../types/schema';
2+
import { Pool, PoolToken, PoolShare, PriceRateProvider, Token, User, FXOracle } from '../../types/schema';
33
import { ERC20 } from '../../types/Vault/ERC20';
44
import { Vault } from '../../types/Vault/Vault';
55
import { ONE_BD, SWAP_IN, SWAP_OUT, VAULT_ADDRESS, ZERO_ADDRESS, ZERO_BD } from './constants';
@@ -77,10 +77,12 @@ export function getPoolShare(poolId: string, lpAddress: Address): PoolShare {
7777
}
7878

7979
export function createPoolShareEntity(poolId: string, lpAddress: Address): PoolShare {
80+
createUserEntity(lpAddress);
81+
8082
let id = getPoolShareId(getPoolAddress(poolId), lpAddress);
8183
let poolShare = new PoolShare(id);
8284

83-
poolShare.userAddress = lpAddress;
85+
poolShare.userAddress = lpAddress.toHex();
8486
poolShare.poolId = poolId;
8587
poolShare.balance = ZERO_BD;
8688
poolShare.save();
@@ -200,6 +202,14 @@ export function getTokenPriceId(
200202
.concat(block.toString());
201203
}
202204

205+
export function createUserEntity(address: Address): void {
206+
let addressHex = address.toHex();
207+
if (User.load(addressHex) == null) {
208+
let user = new User(addressHex);
209+
user.save();
210+
}
211+
}
212+
203213
export function createToken(tokenAddress: Address): Token {
204214
let erc20token = ERC20.bind(tokenAddress);
205215
let token = new Token(tokenAddress.toHexString());

src/mappings/vault.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
updateTokenBalances,
1111
bytesToAddress,
1212
getPoolShare,
13+
createUserEntity,
1314
} from './helpers/misc';
1415
import { updatePoolWeights } from './helpers/weighted';
1516
import { SWAP_IN, SWAP_OUT, VAULT_ADDRESS, ZERO, ZERO_ADDRESS, ZERO_BD } from './helpers/constants';
@@ -83,7 +84,7 @@ function handlePoolJoined(event: PoolBalanceChanged): void {
8384
join.type = 'Join';
8485
join.amounts = joinAmounts;
8586
join.pool = event.params.poolId.toHexString();
86-
join.user = event.params.liquidityProvider;
87+
join.user = event.params.liquidityProvider.toHexString();
8788
join.timestamp = blockTimestamp;
8889
join.tx = transactionHash;
8990
join.block = event.block.number;
@@ -183,7 +184,7 @@ function handlePoolExited(event: PoolBalanceChanged): void {
183184
exit.type = 'Exit';
184185
exit.amounts = exitAmounts;
185186
exit.pool = event.params.poolId.toHexString();
186-
exit.user = event.params.liquidityProvider;
187+
exit.user = event.params.liquidityProvider.toHexString();
187188
exit.timestamp = blockTimestamp;
188189
exit.tx = transactionHash;
189190
exit.block = event.block.number;
@@ -220,6 +221,7 @@ function handlePoolExited(event: PoolBalanceChanged): void {
220221
************** SWAPS ***************
221222
************************************/
222223
export function handleSwapEvent(event: SwapEvent): void {
224+
createUserEntity(event.transaction.from);
223225
let poolId = event.params.poolId;
224226

225227
let pool = Pool.load(poolId.toHexString());
@@ -336,7 +338,7 @@ export function handleSwapEvent(event: SwapEvent): void {
336338
swap.tokenAmountOut = tokenAmountOut;
337339

338340
swap.caller = event.transaction.from;
339-
swap.userAddress = event.transaction.from;
341+
swap.userAddress = event.transaction.from.toHex();
340342
swap.poolId = poolId.toHex();
341343

342344
swap.timestamp = blockTimestamp;

0 commit comments

Comments
 (0)