Skip to content

Commit

Permalink
change logic to promise and minor edits
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisnsns committed Oct 30, 2023
1 parent 7f92bcc commit 4821ea0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 67 deletions.
32 changes: 16 additions & 16 deletions sdk/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export interface StreamInfo {
reason: ByteArray;
}

interface LogArgs {
token: string;
creator: string;
interface StreamCreated {
token: Address;
creator: Address;
stream_id: number;
amount_per_second: number;
start_time: number;
Expand All @@ -37,7 +37,7 @@ interface Log {
blockHash: string;
logIndex: number;
removed: boolean;
args: LogArgs;
args: StreamCreated;
eventName: string;
}

Expand Down Expand Up @@ -305,18 +305,18 @@ export default class StreamManager {
args: creator ? { creator } : {},
onLogs: (logs: Log[]) => {
logs
.map(
(log) =>
new Stream(
this,
log.args.creator as `0x${string}`,
log.args.stream_id,
log.args.token as `0x${string}`,
BigInt(log.args.amount_per_second),
this.publicClient,
this.walletClient
)
)
.map((log) => {
const stream = log.args as StreamCreated;
return new Stream(
this,
stream.creator,
stream.stream_id,
stream.token,
BigInt(stream.amount_per_second),
this.publicClient,
this.walletClient
);
})
.forEach(handleStream);
},
});
Expand Down
44 changes: 12 additions & 32 deletions ui/app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,45 +72,25 @@ function App() {
const { address } = useAccount();

const [stream, setStream] = useState<Stream | null>(null);
const [SM, setSM] = useState<StreamManager | null>(null);
const [createdStreams, setCreatedStreams] = useState<Stream[]>([]);

// Manage callback from onstreamcreate
const handleStreams = (newStream: Stream) => {
setCreatedStreams((prevStreams) => [...prevStreams, newStream]);
const addStream = (newStream: Stream) => {
setCreatedStreams((prevStreams) => [...prevStreams, newStream]);
};

// Fetch the StreamManager and its streams
useEffect(() => {
const fetchStreamManager = async () => {
if (SM === null) {
try {
const newSM = await StreamManager.fromAddress(
config.streamManagerAddress as `0x${string}`,
publicClient,
walletClient as WalletClient
);
setSM(newSM);
} catch (error) {
console.error("Stream Manager Error:", error);
}
}
};
fetchStreamManager();

// Fetch create streams when SM has been fetched
const interval: NodeJS.Timeout = setInterval(() => {
if (SM != null && createdStreams.length === 0) {
SM.onStreamCreated(handleStreams, address);
}
}, 3000);

return () => {
if (interval) {
clearInterval(interval);
}
};
}, [SM, createdStreams, handleStreams, address]);
StreamManager.fromAddress(
config.streamManagerAddress as `0x${string}`,
publicClient,
walletClient as WalletClient
)
.then((SM) => {
SM.onStreamCreated(addStream, address);
})
.catch(console.error);
}, [addStream, address]);

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion ui/lib/CancelStream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const CancelStream: React.FC<CancelStreamProps> = (props) => {
}, [startTime]);

// Calculate the time in seconds before a stream can be cancelled
const timeBeforeCancellability = startTime + minStreamLife - currentTime;
const timeBeforeCancellability = Math.max(0, startTime + minStreamLife - currentTime);

return (
<div className="stream-container">
Expand Down
25 changes: 7 additions & 18 deletions ui/lib/CreateStream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,13 @@ const CreateStream = (props: CreateStreamProps) => {
const publicClient = usePublicClient();
const walletClient = useWalletClient()?.data as WalletClient | undefined;

useEffect(() => {
const fetchStreamManager = async () => {
if (SM === null) {
try {
const newSM = await StreamManager.fromAddress(
props.streamManagerAddress,
publicClient,
walletClient
);
setSM(newSM);
} catch (error) {
console.error("Stream Manager Error:", error);
}
}
};

fetchStreamManager();
}, [SM, publicClient, walletClient]);
StreamManager.fromAddress(
props.streamManagerAddress as `0x${string}`,
publicClient,
walletClient as WalletClient
)
.then(setSM)
.catch(console.error);

const { config: approvalConfig } = usePrepareContractWrite({
address: selectedToken as `0x${string}`,
Expand Down

0 comments on commit 4821ea0

Please sign in to comment.