-
Notifications
You must be signed in to change notification settings - Fork 2
/
FlowCore.ts
156 lines (133 loc) · 4.72 KB
/
FlowCore.ts
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
import BigNumber from "bignumber.js";
import _ from "lodash";
import { getAccount, readContract, readContracts, waitForTransactionReceipt, writeContract } from "wagmi/actions";
import { config } from "../components/Web3";
import { ABI, SEPOLIA_CHAIN_ID, contracts } from "../constants";
import type { IAddress, ICreateAndDepositFlow, IWithdrawFlow } from "../types";
import { erroneous, expect } from "../utils";
class FlowCore {
static async doCreateAndDeposit(
state: {
ratePerSecond: string | undefined;
initialDeposit: string | undefined;
recipient: string | undefined;
token: string | undefined;
transferability: boolean;
},
log: (value: string) => void,
) {
try {
if (
!expect(state.ratePerSecond, "ratePerSecond") ||
!expect(state.initialDeposit, "initialDeposit") ||
!expect(state.recipient, "recipient") ||
!expect(state.token, "token") ||
!expect(state.transferability, "transferability")
) {
return;
}
const decimals = await readContract(config, {
address: state.token as IAddress,
abi: ABI.ERC20.abi,
functionName: "decimals",
});
/** We use BigNumber to convert float values to decimal padded BigInts */
const padding = new BigNumber(10).pow(new BigNumber(decimals.toString()));
const ratePerSecond = BigInt(new BigNumber(state.ratePerSecond).times(padding).toFixed());
const initialDeposit = BigInt(new BigNumber(state.initialDeposit).times(padding).toFixed());
const sender = await getAccount(config).address;
if (!expect(sender, "sender")) {
return;
}
const payload: ICreateAndDepositFlow = [
sender as IAddress,
state.recipient as IAddress,
ratePerSecond,
state.token as IAddress,
state.transferability,
initialDeposit,
];
console.info("Payload", payload);
const hash = await writeContract(config, {
address: contracts[SEPOLIA_CHAIN_ID].SablierFlow,
abi: ABI.SablierFlow.abi,
functionName: "createAndDeposit",
args: payload,
});
if (hash) {
log(`FL Stream sent to the blockchain with hash: ${hash}.`);
}
const receipt = await waitForTransactionReceipt(config, { hash });
if (receipt?.status === "success") {
log(`FL Stream successfully created.`);
} else {
log(`FL Stream creation failed.`);
}
} catch (error) {
erroneous(error);
}
}
static async doWithdraw(
state: {
streamId: string | undefined;
amount: string | undefined;
},
log: (value: string) => void,
) {
try {
if (!expect(state.streamId, "streamId") || !expect(state.amount, "amount")) {
return;
}
const [asset, to] = await readContracts(config, {
contracts: [
{
address: contracts[SEPOLIA_CHAIN_ID].SablierFlow,
abi: ABI.SablierFlow.abi,
functionName: "getToken",
args: [BigInt(state.streamId)],
},
{
address: contracts[SEPOLIA_CHAIN_ID].SablierFlow,
abi: ABI.SablierFlow.abi,
functionName: "getRecipient",
args: [BigInt(state.streamId)],
},
],
allowFailure: false,
});
const decimals = await readContract(config, {
address: asset,
abi: ABI.ERC20.abi,
functionName: "decimals",
});
/** We use BigNumber to convert float values to decimal padded BigInts */
const padding = new BigNumber(10).pow(new BigNumber(decimals.toString()));
const sender = await getAccount(config).address;
if (!expect(sender, "sender")) {
return;
}
const amount = BigInt(new BigNumber(state.amount).times(padding).toFixed());
/** The public withdraw is locked to the recipient's address. Recipients themselves can chose a different withdraw address. */
const payload: IWithdrawFlow = [BigInt(state.streamId), to, amount];
console.info("Payload", payload);
const hash = await writeContract(config, {
address: contracts[SEPOLIA_CHAIN_ID].SablierFlow,
abi: ABI.SablierFlow.abi,
functionName: "withdraw",
args: payload,
});
if (hash) {
log(`Withdrawal for Stream #${state.streamId} sent to the blockchain with hash: ${hash}.`);
}
const receipt = await waitForTransactionReceipt(config, { hash });
if (receipt?.status === "success") {
log(`Withdrawal for Stream #${state.streamId} successful.`);
} else {
log(`Withdrawal for Stream #${state.streamId} failed.`);
}
} catch (error) {
erroneous(error);
}
}
}
export default FlowCore;