forked from salazarguille/yearn-vaults-v2-subgraph
-
Notifications
You must be signed in to change notification settings - Fork 41
/
vaultWithdraw.ts
150 lines (140 loc) · 4.13 KB
/
vaultWithdraw.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { assert } from 'matchstick-as/assembly/index';
import {
getDayIDFromIndex,
getDayIndexFromTimestamp,
} from '../../src/utils/vault/vault-day-data';
import { defaults } from '../default';
/*
This function is used to verify subgraph state transitions that are triggered by withdrawals.
*/
export function validateWithdrawalStateTransition(
beneficiary: string,
vault: string,
txnHash: string,
expectedWithdrawalAmount: string,
expectedSharesBurned: string,
expectedPricePerShare: string,
transactionIndex: string | null,
logIndex: string | null
): void {
let positionId = beneficiary.concat('-').concat(vault);
assert.fieldEquals('AccountVaultPosition', positionId, 'balanceShares', '0');
assert.fieldEquals('AccountVaultPosition', positionId, 'balanceTokens', '0');
// todo: The AccountVaultPosition.balanceProfit field appears to returning inaccurate results, so cannot be tested.
/*
assert.fieldEquals(
'AccountVaultPosition',
positionId,
'balanceProfit',
"0"
);
*/
// Verify VaultUpdate
let txnIndexToUse = '1';
if (transactionIndex) {
txnIndexToUse = transactionIndex;
}
let logIndexToUse = '1';
if (logIndex) {
logIndexToUse = logIndex;
}
// from _getOrCreateTransaction
let transactionHashId = txnHash.concat('-').concat(logIndexToUse);
// from buildIdFromVaultAndTransaction
let vaultUpdateId = vault
.concat('-')
.concat(transactionHashId.concat('-').concat(txnIndexToUse));
assert.fieldEquals('VaultUpdate', vaultUpdateId, 'id', vaultUpdateId);
assert.fieldEquals(
'VaultUpdate',
vaultUpdateId,
'transaction',
transactionHashId
);
assert.fieldEquals('VaultUpdate', vaultUpdateId, 'vault', vault);
assert.fieldEquals('VaultUpdate', vaultUpdateId, 'tokensDeposited', '0');
assert.fieldEquals(
'VaultUpdate',
vaultUpdateId,
'tokensWithdrawn',
expectedWithdrawalAmount
);
assert.fieldEquals(
'VaultUpdate',
vaultUpdateId,
'pricePerShare',
expectedPricePerShare
);
assert.fieldEquals('VaultUpdate', vaultUpdateId, 'sharesMinted', '0');
assert.fieldEquals(
'VaultUpdate',
vaultUpdateId,
'sharesBurnt',
expectedSharesBurned
);
assert.fieldEquals('VaultUpdate', vaultUpdateId, 'totalFees', '0');
assert.fieldEquals('VaultUpdate', vaultUpdateId, 'newManagementFee', 'null');
assert.fieldEquals('VaultUpdate', vaultUpdateId, 'newPerformanceFee', 'null');
assert.fieldEquals('VaultUpdate', vaultUpdateId, 'newHealthCheck', 'null');
assert.fieldEquals('VaultUpdate', vaultUpdateId, 'newRewards', 'null');
assert.fieldEquals('VaultUpdate', vaultUpdateId, 'returnsGenerated', '0');
// todo: get newOrder from transaction
let newOrder = '1';
let positionUpdateId = beneficiary
.concat('-')
.concat(vault.concat('-').concat(newOrder));
assert.fieldEquals(
'AccountVaultPositionUpdate',
positionUpdateId,
'sharesBurnt',
expectedSharesBurned
);
assert.fieldEquals(
'AccountVaultPositionUpdate',
positionUpdateId,
'balanceShares',
'0'
);
assert.fieldEquals(
'AccountVaultPositionUpdate',
positionUpdateId,
'withdrawals',
expectedWithdrawalAmount
);
let withdrawlId = beneficiary
.concat('-')
.concat(transactionHashId)
.concat('-')
.concat(txnIndexToUse);
assert.fieldEquals('Withdrawal', withdrawlId, 'id', withdrawlId);
assert.fieldEquals(
'Withdrawal',
withdrawlId,
'tokenAmount',
expectedWithdrawalAmount
);
assert.fieldEquals(
'Withdrawal',
withdrawlId,
'sharesBurnt',
expectedSharesBurned
);
// Verify VaultDayData
let dayId = getDayIndexFromTimestamp(defaults.bigInt);
let vaultDayId = getDayIDFromIndex(vault, dayId);
assert.fieldEquals('VaultDayData', vaultDayId, 'id', vaultDayId);
assert.fieldEquals('VaultDayData', vaultDayId, 'totalReturnsGenerated', '0');
assert.fieldEquals('VaultDayData', vaultDayId, 'dayReturnsGenerated', '0');
assert.fieldEquals(
'VaultDayData',
vaultDayId,
'withdrawn',
expectedSharesBurned.toString()
);
assert.fieldEquals(
'VaultDayData',
vaultDayId,
'pricePerShare',
expectedPricePerShare.toString()
);
}