-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathacpFare.ts
More file actions
126 lines (99 loc) · 3.16 KB
/
acpFare.ts
File metadata and controls
126 lines (99 loc) · 3.16 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 {
Address,
createPublicClient,
erc20Abi,
ethAddress,
http,
parseUnits,
} from "viem";
import AcpError from "./acpError";
import { AcpContractConfig, baseAcpConfig } from "./configs/acpConfigs";
class Fare {
constructor(public contractAddress: Address, public decimals: number) {}
formatAmount(amount: number) {
if (!Number.isFinite(amount) || amount < 0) {
throw new AcpError(
`Invalid amount: ${amount}. Amount must be a finite, non-negative number.`
);
}
const numStr = amount.toString();
const amountStr = numStr.includes('e')
? amount.toFixed(this.decimals)
: numStr;
return parseUnits(amountStr, this.decimals);
}
static async fromContractAddress(
contractAddress: Address,
config: AcpContractConfig = baseAcpConfig
) {
if (contractAddress === config.baseFare.contractAddress) {
return config.baseFare;
}
const publicClient = createPublicClient({
chain: config.chain,
transport: http(config.rpcEndpoint),
});
const decimals = await publicClient.readContract({
address: contractAddress,
abi: erc20Abi,
functionName: "decimals",
});
return new Fare(contractAddress, decimals as number);
}
}
abstract class FareAmountBase {
amount: bigint;
fare: Fare;
constructor(amount: bigint, fare: Fare) {
this.amount = amount;
this.fare = fare;
}
abstract add(other: FareAmountBase): FareAmountBase;
static async fromContractAddress(
amount: number | bigint,
contractAddress: Address,
config: AcpContractConfig = baseAcpConfig
): Promise<FareAmountBase> {
const fare = await Fare.fromContractAddress(contractAddress, config);
if (typeof amount === "number") {
return new FareAmount(amount, fare);
}
return new FareBigInt(amount, fare);
}
}
class FareAmount extends FareAmountBase {
constructor(fareAmount: number, fare: Fare) {
const truncateToTokenDecimals = (input: string, decimals: number): number => {
const [intPart, decPart = ""] = input.split(".");
if (decPart === "") {
return parseFloat(intPart);
}
const truncated = decPart.slice(0, decimals);
return parseFloat(`${intPart}.${truncated}`);
};
super(fare.formatAmount(truncateToTokenDecimals(fareAmount.toString(), fare.decimals)), fare);
}
add(other: FareAmountBase) {
if (this.fare.contractAddress !== other.fare.contractAddress) {
throw new Error("Token addresses do not match");
}
return new FareBigInt(this.amount + other.amount, this.fare);
}
}
class FareBigInt implements FareAmountBase {
amount: bigint;
fare: Fare;
constructor(amount: bigint, fare: Fare) {
this.amount = amount;
this.fare = fare;
}
add(other: FareAmountBase): FareAmountBase {
if (this.fare.contractAddress !== other.fare.contractAddress) {
throw new AcpError("Token addresses do not match");
}
return new FareBigInt(this.amount + other.amount, this.fare);
}
}
const wethFare = new Fare("0x4200000000000000000000000000000000000006", 18);
const ethFare = new Fare(ethAddress, 18);
export { Fare, FareAmountBase, FareAmount, FareBigInt, wethFare, ethFare };