Skip to content

Commit

Permalink
working demo
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisnsns committed Dec 19, 2023
1 parent 8647b26 commit 3a20baf
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ui/app/src/CreatePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const StreamPage = () => {
<div className="create-stream-component">
<CreateStream
streamManagerAddress={sm as `0x${string}`}
amountPerSecond={BigInt(10000000000000)}
amountPerSecond={BigInt(30000000000000)}
registerStream={addStreams}
renderReasonCode={renderReasonCode}
handleTransactionStatus={handleTransactionStatus}
Expand Down
73 changes: 61 additions & 12 deletions ui/lib/CreateStream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,49 @@ const CreateStream = (props: CreateStreamProps) => {
}
}, [props.tokenList, targetChainId]);

const [allowance, setAllowance] = useState<number>(0);

const [allowance, setAllowance] = useState<number | null>(null);
const [isAllowanceSufficient, setIsAllowanceSufficient] =
useState<boolean>(false);

// ABI used to fetch the current user allowance
const erc20ABI = [
{
constant: true,
inputs: [
{ name: "_owner", type: "address" },
{ name: "_spender", type: "address" },
],
name: "allowance",
outputs: [{ name: "", type: "uint256" }],
type: "function",
},
];

// Fetch current user allowance
const { data: allowanceData } = useContractRead({
address: selectedToken?.address as Address,
functionName: "allowance",
abi: erc20ABI,
args: [address, props.streamManagerAddress],
watch: true,
});

console.log("allowance", allowanceData);
useEffect(() => {
if (allowanceData) {
const fetchedAllowance = Number(allowanceData.toString());
setAllowance(fetchedAllowance);
}
}, [allowanceData]);

// Effect hook to check if the allowance is sufficient
useEffect(() => {
if (allowance !== null && txCost !== undefined) {
setIsAllowanceSufficient(allowance >= txCost);
}
}, [allowance, txCost]);

console.log("allowance", allowance);
console.log("cost", txCost);

// Select the payment token among tokens with the same chainID
const Step1 = () => {
Expand Down Expand Up @@ -416,15 +449,31 @@ const CreateStream = (props: CreateStreamProps) => {
}
disabled={isSuccess}
/>
<button
className="button-validate-transaction"
onClick={approveStream}
disabled={isSuccess}
style={{ backgroundColor: isSuccess ? "grey" : "initial" }}
>
{`Approve ${Math.floor(transactionAmount + 1)}`}&nbsp;
{`${selectedToken?.symbol}`}
</button>

{isAllowanceSufficient && (
<>
<button
className="button-validate-transaction allowance"
onClick={() => validateStep(2)}
>
{`Approve ${Math.floor(transactionAmount + 1)}`}&nbsp;
{`${selectedToken?.symbol}`}
</button>
</>
)}
{!isAllowanceSufficient && (
<>
<button
className="button-validate-transaction"
onClick={approveStream}
disabled={isSuccess}
style={{ backgroundColor: isSuccess ? "grey" : "initial" }}
>
{`Approve ${Math.floor(transactionAmount + 1)}`}&nbsp;
{`${selectedToken?.symbol}`}
</button>
</>
)}
{isLoading && (
<div className="validate-transaction-message">
Waiting for your confirmation.
Expand Down

0 comments on commit 3a20baf

Please sign in to comment.