-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (77 loc) · 1.87 KB
/
index.js
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
/* Variables
chain button - .network_button attr network
amount field - .borrow_amount
main = #borrow_form
*/
const borrowApp = Vue.createApp({
data: () => ({
amount: 0,
chain: "matic",
lendingToken: "USDC",
collateralToken: "",
collateralTokens: [],
requiredCollateral: 0,
}),
watch: {
amount() {
this.getCollateralAmount();
},
chain() {
this.getCollateralToken();
this.getCollateralAmount();
},
lendingToken() {
this.getCollateralAmount();
},
collateralToken() {
this.getCollateralAmount();
},
},
computed: {
collSymbol() {
const s = this.collateralTokens.find(
(t) => t.address === this.collateralToken
);
if (s) return s.symbol;
return "";
},
},
methods: {
changeChain(chain) {
this.chain = chain;
},
async getCollateralToken() {
try {
const res = await fetch(
`https://dash.internal.teller.org/get_collateral_tokens?chain=${this.chain}&lendingTokenSymbol=${this.lendingToken}`
);
const data = await res.json();
this.collateralTokens = data;
} catch (err) {
alert(err.message);
}
},
async getCollateralAmount() {
if (
!this.collateralToken ||
!this.amount ||
!this.chain ||
!this.lendingToken
) {
return;
}
try {
const res =
await fetch(`https://dash.internal.teller.org/get_collateral_amount?collateralTokenAddress=${this.collateralToken}&amount=${this.amount}&chain=${this.chain}&lendingTokenSymbol=${this.lendingToken}
`);
const data = await res.json();
this.requiredCollateral =
Math.round(data.required_collateral * 100) / 100;
} catch (err) {}
},
},
mounted() {
this.getCollateralToken();
},
});
borrowApp.mount("#borrow_form");