Skip to content

Commit

Permalink
updated: using forwarder for flow operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Salmandabbakuti committed Jun 29, 2024
1 parent 43bf8b8 commit 9e0f10c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
54 changes: 45 additions & 9 deletions client/app/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import {
} from "@ant-design/icons";
import {
flowCraftContract as contract,
cfav1ForwarderContract,
calculateFlowRateInTokenPerMonth,
calculateFlowRateInWeiPerSecond
} from "./utils";
import { SUPPORTED_SUPERTOKEN_ADDRESS, FLOWCRAFT_ADDRESS } from "./utils/constants";

dayjs.extend(relativeTime);

Expand All @@ -50,7 +52,12 @@ export default function Home() {
return message.error("Please connect wallet first");
if (!flowInfo) return message.error("No flow found open to contract");
try {
const tx = await contract.connect(signer).deleteFlowToContract();
const tx = await cfav1ForwarderContract.connect(signer).deleteFlow(
SUPPORTED_SUPERTOKEN_ADDRESS,
account,
FLOWCRAFT_ADDRESS,
"0x"
);
await tx.wait();
message.success("Flow deleted successfully");
} catch (err) {
Expand All @@ -64,23 +71,52 @@ export default function Home() {
return message.error("Please connect wallet first");
if (!flowRateInput) return message.error("Please enter flow rate");
try {
setLoading({ mintItem: true });
const flowRateInWeiPerSecond =
calculateFlowRateInWeiPerSecond(flowRateInput);
console.log("flowRateInWeiPerSecond: ", flowRateInWeiPerSecond);
const tx = await contract
const tx = await cfav1ForwarderContract
.connect(signer)
.createFlowToContract(flowRateInWeiPerSecond);
.createFlow(
SUPPORTED_SUPERTOKEN_ADDRESS,
account,
FLOWCRAFT_ADDRESS,
flowRateInWeiPerSecond,
"0x"
);
await tx.wait();
message.success("Flow opened to contract successfully");
setLoading({ mintItem: false });
} catch (err) {
setLoading({ mintItem: false });
message.error("Failed to open flow to contract");
console.error("failed to open flow to contract: ", err);
}
};

const handleUpdateFlowToContract = async () => {
if (!account || !signer)
return message.error("Please connect wallet first");
if (!flowRateInput) return message.error("Please enter flow rate");
try {
const flowRateInWeiPerSecond =
calculateFlowRateInWeiPerSecond(flowRateInput);
console.log("flowRateInWeiPerSecond: ", flowRateInWeiPerSecond);
const tx = await cfav1ForwarderContract
.connect(signer)
.updateFlow(
SUPPORTED_SUPERTOKEN_ADDRESS,
account,
FLOWCRAFT_ADDRESS,
flowRateInWeiPerSecond,
"0x"
);
await tx.wait();
message.success("Flow updated to contract successfully");
} catch (err) {
message.error("Failed to update flow to contract");
console.error("failed to update flow to contract: ", err);
}
};


const getFlowInfoToContract = async () => {
if (!account || !signer)
return message.error("Please connect wallet first");
Expand Down Expand Up @@ -181,7 +217,7 @@ export default function Home() {
{
// if flowInfo is not null, show the buttons(edit,delete)
// else show the button to open a new stream
flowInfo ? (
flowInfo?.currentFlowRate > 0 ? (
<>
<Popconfirm
title="Enter FlowRate"
Expand All @@ -193,7 +229,7 @@ export default function Home() {
addonAfter="fDAIx/mo"
/>
}
onConfirm={handleCreateFlowToContract}
onConfirm={handleUpdateFlowToContract}
>
<Button
type="primary"
Expand Down Expand Up @@ -233,7 +269,7 @@ export default function Home() {
</Space>
}
>
{flowInfo ? (
{flowInfo?.currentFlowRate > 0 ? (
<>
<Statistic
style={{ textAlign: "center", marginBottom: 20 }}
Expand Down
5 changes: 4 additions & 1 deletion client/app/utils/constants.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export const FLOWCRAFT_ADDRESS = "0x0BbF700909f861A0Fa1Ea7C53f330e05A67E1505";
// OP Sepolia
export const FLOWCRAFT_ADDRESS = "0x0BbF700909f861A0Fa1Ea7C53f330e05A67E1505";
export const CFAV1_FORWARDER_ADDRESS = "0xcfA132E353cB4E398080B9700609bb008eceB125";
export const SUPPORTED_SUPERTOKEN_ADDRESS = "0xd6faf98befa647403cc56bdb598690660d5257d2"; //fDAIx
13 changes: 12 additions & 1 deletion client/app/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Contract } from "@ethersproject/contracts";
import { formatEther, parseEther } from "@ethersproject/units";
import { FLOWCRAFT_ADDRESS } from "./constants";
import { FLOWCRAFT_ADDRESS, CFAV1_FORWARDER_ADDRESS } from "./constants";

const FLOWCRAFT_ABI = [
"function createFlowToContract(int96 _flowRate)",
Expand All @@ -12,6 +12,17 @@ const FLOWCRAFT_ABI = [

export const flowCraftContract = new Contract(FLOWCRAFT_ADDRESS, FLOWCRAFT_ABI);

const cfav1ForwarderABI = [
"function createFlow(address token, address sender, address receiver, int96 flowrate, bytes userData) returns (bool)",
"function updateFlow(address token, address sender, address receiver, int96 flowrate, bytes userData) returns (bool)",
"function deleteFlow(address token, address sender, address receiver, bytes userData) returns (bool)"
];
// load contracts
export const cfav1ForwarderContract = new Contract(
CFAV1_FORWARDER_ADDRESS,
cfav1ForwarderABI
);

export const calculateFlowRateInTokenPerMonth = (amount) => {
if (isNaN(amount)) return 0;
// convert from wei/sec to token/month for displaying in UI
Expand Down

0 comments on commit 9e0f10c

Please sign in to comment.