-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathStreamRateIndicator.tsx
196 lines (171 loc) · 5.71 KB
/
StreamRateIndicator.tsx
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { Skeleton } from "@/components/ui/skeleton";
import { useWallet } from "@aptos-labs/wallet-adapter-react";
import { useEffect, useState } from "react";
import { Button } from "./ui/button";
import { InfoCircledIcon } from "@radix-ui/react-icons";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Stream } from "@/app/payments/CreatedStreamList";
/*
Finds the best unit to display the stream rate in by changing the bottom of the unit from seconds
to minutes, hours, days, etc.
*/
function displayStreamRate(streamRatePerSecond: number) {
if (streamRatePerSecond == 0) {
return "0 APT / s";
}
if (Math.abs(streamRatePerSecond) >= 1) {
return `${streamRatePerSecond.toLocaleString(undefined, {
maximumFractionDigits: 3,
})} APT / s`;
}
streamRatePerSecond *= 60; // to minutes
if (Math.abs(streamRatePerSecond) >= 1) {
return `${streamRatePerSecond.toLocaleString(undefined, {
maximumFractionDigits: 3,
})} APT / min`;
}
streamRatePerSecond *= 60; // to hours
if (Math.abs(streamRatePerSecond) >= 1) {
return `${streamRatePerSecond.toLocaleString(undefined, {
maximumFractionDigits: 3,
})} APT / hr`;
}
streamRatePerSecond *= 24; // to days
if (Math.abs(streamRatePerSecond) >= 1) {
return `${streamRatePerSecond.toLocaleString(undefined, {
maximumFractionDigits: 3,
})} APT / day`;
}
streamRatePerSecond *= 7; // to weeks
if (Math.abs(streamRatePerSecond) >= 1) {
return `${streamRatePerSecond.toLocaleString(undefined, {
maximumFractionDigits: 3,
})} APT / week`;
}
streamRatePerSecond *= 4; // to months
if (Math.abs(streamRatePerSecond) >= 1) {
return `${streamRatePerSecond.toLocaleString(undefined, {
maximumFractionDigits: 3,
})} APT / month`;
}
streamRatePerSecond *= 12; // to years
return `${streamRatePerSecond.toLocaleString(undefined, {
maximumFractionDigits: 3,
})} APT / year`;
}
export default function StreamRateIndicator() {
// wallet adapter state
const { isLoading, account, connected } = useWallet();
// stream rate state
const [streamRate, setStreamRate] = useState(0);
/*
Calculates and sets the stream rate
*/
useEffect(() => {
calculateStreamRate().then((streamRate) => {
setStreamRate(streamRate);
});
});
/*
Calculates the stream rate by adding up all of the streams the user is receiving and subtracting
all of the streams the user is sending.
*/
const calculateStreamRate = async () => {
/*
TODO #1: Fetch the receiver and sender streams using getReceiverStreams and getSenderStreams.
Then, calculate the stream rate by calculating and adding up the rate of APT per second
for each receiver stream and subtracting the rate of APT per second for each sender stream.
Return the stream rate.
*/
let aptPerSec = 0;
return aptPerSec;
};
const getSenderStreams = async () => {
/*
TODO #2: Validate the account is defined before continuing. If not, return.
*/
/*
TODO #3: Make a request to the view function `get_senders_streams` to retrieve the streams sent by
the user.
*/
/*
TODO #4: Parse the response from the view request and create the streams array using the given
data. Return the new streams array.
HINT:
- Remember to convert the amount to floating point number
*/
return [];
};
const getReceiverStreams = async () => {
/*
TODO #5: Validate the account is defined before continuing. If not, return.
*/
/*
TODO #6: Make a request to the view function `get_receivers_streams` to retrieve the streams sent by
the user.
*/
/*
TODO #7: Parse the response from the view request and create an object containing an array of
pending, completed, and active streams using the given data. Return the new object.
HINT:
- Remember to convert the amount to floating point number
- Remember to convert the timestamps to milliseconds
- Mark a stream as pending if the start timestamp is 0
- Mark a stream as completed if the start timestamp + duration is less than the current time
- Mark a stream as active if it is not pending or completed
*/
return {
pending: [],
completed: [],
active: [],
};
};
if (!connected) {
return null;
}
return (
<Dialog>
<DialogTrigger asChild>
<Button className="bg-neutral-500 hover:bg-neutral-500 px-3">
<div className="flex flex-row gap-3 items-center">
<InfoCircledIcon className="h-4 w-4 text-neutral-100" />
<span
className={
"font-matter " +
(streamRate > 0
? "text-green-400"
: streamRate < 0
? "text-red-400"
: "")
}
>
{isLoading || !connected ? (
<Skeleton className="h-4 w-24" />
) : (
displayStreamRate(streamRate)
)}
</span>
</div>
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Your current stream rate</DialogTitle>
<DialogDescription>
This is the current rate at which you are streaming and being
streamed APT. This rate is calculated by adding up all of the
streams you are receiving and subtracting all of the streams you are
sending.
</DialogDescription>
</DialogHeader>
</DialogContent>
</Dialog>
);
}