-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathgetAvailableAmount.ts
More file actions
126 lines (110 loc) · 3.42 KB
/
Copy pathgetAvailableAmount.ts
File metadata and controls
126 lines (110 loc) · 3.42 KB
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
import { ERC20StreamingEnforcer } from '@metamask/delegation-abis';
import type { Address, Client, Hex } from 'viem';
import { readContract, getBlock } from 'viem/actions';
export type ReadGetAvailableAmountParameters = {
client: Client;
contractAddress: Address;
delegationManager: Address;
delegationHash: Hex;
terms: Hex;
};
export const read = async ({
client,
contractAddress,
delegationManager,
delegationHash,
terms,
}: ReadGetAvailableAmountParameters) => {
// Get current block timestamp from blockchain
const currentBlock = await getBlock(client);
const currentTimestamp = currentBlock.timestamp;
// First, get the current state from the contract
const allowanceState = await readContract(client, {
address: contractAddress,
abi: ERC20StreamingEnforcer.abi,
functionName: 'streamingAllowances',
args: [delegationManager, delegationHash],
});
const [initialAmount, maxAmount, amountPerSecond, startTime, spent] =
allowanceState;
// Check if state exists (startTime != 0)
if (startTime !== 0n) {
// State exists, calculate available amount using the stored state
const availableAmount = getAvailableAmount({
initialAmount,
maxAmount,
amountPerSecond,
startTime,
spent,
currentTimestamp,
});
return {
availableAmount,
};
}
// State doesn't exist, decode terms and simulate with spent = 0
const decodedTerms = await readContract(client, {
address: contractAddress,
abi: ERC20StreamingEnforcer.abi,
functionName: 'getTermsInfo',
args: [terms],
});
const [
,
decodedInitialAmount,
decodedMaxAmount,
decodedAmountPerSecond,
decodedStartTime,
] = decodedTerms;
// Simulate using decoded terms with spent = 0
const availableAmount = getAvailableAmount({
initialAmount: decodedInitialAmount,
maxAmount: decodedMaxAmount,
amountPerSecond: decodedAmountPerSecond,
startTime: decodedStartTime,
spent: 0n,
currentTimestamp,
});
return {
availableAmount,
};
};
/**
* Replicates the internal _getAvailableAmount logic from the smart contract.
*
* @param allowance - The allowance object containing all parameters.
* @param allowance.initialAmount - The initial amount available.
* @param allowance.maxAmount - The maximum amount allowed.
* @param allowance.amountPerSecond - The amount streamed per second.
* @param allowance.startTime - The start time of the streaming.
* @param allowance.spent - The amount already spent.
* @param allowance.currentTimestamp - The current timestamp.
* @returns The available amount that can be spent.
*/
function getAvailableAmount(allowance: {
initialAmount: bigint;
maxAmount: bigint;
amountPerSecond: bigint;
startTime: bigint;
spent: bigint;
currentTimestamp: bigint;
}): bigint {
// If current time is before start time, nothing is available
if (allowance.currentTimestamp < allowance.startTime) {
return 0n;
}
// Calculate elapsed time since start
const elapsed = allowance.currentTimestamp - allowance.startTime;
// Calculate total unlocked amount
let unlocked = allowance.initialAmount + allowance.amountPerSecond * elapsed;
// Cap by max amount
if (unlocked > allowance.maxAmount) {
unlocked = allowance.maxAmount;
}
// If spent >= unlocked, nothing available
if (allowance.spent >= unlocked) {
return 0n;
}
// Return available amount
return unlocked - allowance.spent;
}