Skip to content

Commit 7457712

Browse files
authored
WINK (#1707)
* WINK * Updated using SDK * Updated apy to apyReward, added poolMeta and rewardTokens * Updated LockUSDW APY
1 parent 287ef8e commit 7457712

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

src/adaptors/wink/index.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
const utils = require('../utils');
2+
const sdk = require('@defillama/sdk');
3+
const ethers = require('ethers');
4+
const { getProvider } = require('@defillama/sdk/build/general');
5+
const { default: BigNumber } = require('bignumber.js');
6+
const superagent = require('superagent');
7+
8+
const WINK_TOKEN_ADDRESS = '0x8c3441E7B9aA8A30a542DDE048dd067DE2802E9B'
9+
10+
const USDW_TOKEN_ADDRESS = '0xab670FDfb0060BDC6508B84a309ff41b56CCAf3f'
11+
12+
const LOCK_WINK_ADDRESS = '0x49C4EeC1d4fFFcdFF415E0757F01Cc50eeF5d4FD'
13+
const LOCK_WINK_ABI = [{"inputs": [],"name": "rebaser","outputs": [{"internalType": "address","name": "","type": "address"}],"stateMutability": "view","type": "function"}]
14+
15+
const LOCK_USDW_ADDRESS = '0x231fB0E6AD5d975151fC8d5b5C5EB164D265fE85'
16+
const LOCK_USDW_ABI = [{"inputs": [{"internalType": "enum LockUSDW.LockPeriod","name": "","type": "uint8"}],"name": "lockAPY","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},]
17+
18+
const S_USDW_ADDRESS = '0xfB379c1f5431E8065e987B36C9BDAF93cba18740'
19+
const S_USDW_ABI = [{"inputs": [],"name": "ssr","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"}]
20+
21+
22+
const chain = 'polygon';
23+
const provider = getProvider(chain);
24+
25+
const YEAR = 365 * 24 * 60 * 60;
26+
const REBASE_OBSERVATION_PERIOD = 60 * 60;
27+
28+
const winkTokenInterface = new ethers.utils.Interface([
29+
'event Transfer(address indexed from, address indexed to, uint256 value)',
30+
]);
31+
32+
async function getPrices(chain, addresses) {
33+
const priceKeys = [...new Set(addresses)].map(
34+
(address) => `${chain}:${address}`
35+
);
36+
return (
37+
await superagent.get(
38+
`https://coins.llama.fi/prices/current/${priceKeys
39+
.join(',')
40+
.toLowerCase()}`
41+
)
42+
).body.coins;
43+
}
44+
45+
const getRebaserTopic = async () => {
46+
return (
47+
await sdk.api.abi.call({
48+
abi: LOCK_WINK_ABI.find((abi) => abi.name == 'rebaser'),
49+
target: LOCK_WINK_ADDRESS,
50+
chain,
51+
})
52+
).output.replace('0x', '0x000000000000000000000000');
53+
}
54+
55+
const getLockWinkBalance = async () => {
56+
return (
57+
await sdk.api.abi.call({
58+
abi: 'erc20:balanceOf',
59+
target: WINK_TOKEN_ADDRESS,
60+
params: [LOCK_WINK_ADDRESS],
61+
chain,
62+
})
63+
).output
64+
}
65+
66+
const getLockWinkApy = async (fromBlock, toBlock, lockedAmount) => {
67+
68+
const rebases = (
69+
await sdk.api2.util.getLogs({
70+
target: WINK_TOKEN_ADDRESS,
71+
toBlock,
72+
fromBlock,
73+
keys: [],
74+
topics: [
75+
winkTokenInterface.getEventTopic('Transfer'),
76+
await getRebaserTopic()
77+
],
78+
chain,
79+
})
80+
).output
81+
82+
const rebaseAmounts = rebases.reduce((acc, rebase) => acc.plus(rebase.data), new BigNumber(0))
83+
84+
const annualRebased = rebaseAmounts.times(YEAR).div(REBASE_OBSERVATION_PERIOD)
85+
86+
return annualRebased.times(100).div(lockedAmount).toNumber()
87+
}
88+
89+
const ssrToApy = (ssr) => {
90+
return Math.round(100 * ((Number(ssr) / 10**27)**(YEAR) - 1) * 100) / 100;
91+
}
92+
93+
const getLockUsdwApy = async () => {
94+
return ssrToApy((
95+
await sdk.api.abi.call({
96+
abi: LOCK_USDW_ABI.find((abi) => abi.name == 'lockAPY'),
97+
target: LOCK_USDW_ADDRESS,
98+
params: [0],
99+
chain,
100+
})
101+
).output)
102+
}
103+
104+
const getLockUsdwBalance = async () => {
105+
return (
106+
await sdk.api.abi.call({
107+
abi: 'erc20:balanceOf',
108+
target: USDW_TOKEN_ADDRESS,
109+
params: [LOCK_USDW_ADDRESS],
110+
chain,
111+
})
112+
).output
113+
}
114+
115+
const getSusdwApy = async () => {
116+
return ssrToApy((
117+
await sdk.api.abi.call({
118+
abi: S_USDW_ABI.find((abi) => abi.name == 'ssr'),
119+
target: S_USDW_ADDRESS,
120+
chain,
121+
})
122+
).output)
123+
}
124+
125+
const getSusdwBalance = async () => {
126+
return (
127+
await sdk.api.abi.call({
128+
abi: 'erc20:balanceOf',
129+
target: USDW_TOKEN_ADDRESS,
130+
params: [S_USDW_ADDRESS],
131+
chain,
132+
})
133+
).output
134+
}
135+
136+
const poolsFunction = async () => {
137+
138+
const prices = await getPrices(chain, [WINK_TOKEN_ADDRESS, USDW_TOKEN_ADDRESS]);
139+
140+
const usdwData = prices[`${chain}:${USDW_TOKEN_ADDRESS.toLowerCase()}`];
141+
const winkData = prices[`${chain}:${WINK_TOKEN_ADDRESS.toLowerCase()}`];
142+
143+
const currentBlock = await sdk.api.util.getLatestBlock(chain);
144+
const toBlock = currentBlock.number;
145+
const pastTimestamp = currentBlock.timestamp - REBASE_OBSERVATION_PERIOD;
146+
const [fromBlock] = await utils.getBlocksByTime([pastTimestamp], chain);
147+
148+
const lockWinkBalance = await getLockWinkBalance()
149+
const lockUsdwBalance = await getLockUsdwBalance()
150+
const susdwBalance = await getSusdwBalance()
151+
152+
return [{
153+
pool: LOCK_WINK_ADDRESS,
154+
chain: utils.formatChain(chain),
155+
project: 'wink',
156+
symbol: 'LockWINK',
157+
tvlUsd: new BigNumber(lockWinkBalance).times(winkData.price).div(1e18).toNumber(),
158+
apyReward: await getLockWinkApy(fromBlock, toBlock, lockWinkBalance),
159+
rewardTokens: [WINK_TOKEN_ADDRESS],
160+
poolMeta: '3 to 24 months lock'
161+
}, {
162+
pool: LOCK_USDW_ADDRESS,
163+
chain: utils.formatChain(chain),
164+
project: 'wink',
165+
symbol: 'LockUSDW',
166+
tvlUsd: new BigNumber(lockUsdwBalance).times(usdwData.price).div(1e18).toNumber(),
167+
apyReward: await getLockUsdwApy(),
168+
rewardTokens: [USDW_TOKEN_ADDRESS],
169+
poolMeta: '3 to 24 months lock'
170+
}, {
171+
pool: S_USDW_ADDRESS,
172+
chain: utils.formatChain(chain),
173+
project: 'wink',
174+
symbol: 'sUSDW',
175+
tvlUsd: new BigNumber(susdwBalance).times(usdwData.price).div(1e18).toNumber(),
176+
apyReward: await getSusdwApy(),
177+
rewardTokens: [USDW_TOKEN_ADDRESS],
178+
poolMeta: 'Liquid staking'
179+
}]
180+
};
181+
182+
module.exports = {
183+
timetravel: false,
184+
apy: poolsFunction, // Main function, returns pools
185+
url: 'https://wink.finance/', // Link to page with pools (Only required if you do not provide url's for each pool)
186+
};

0 commit comments

Comments
 (0)