-
Notifications
You must be signed in to change notification settings - Fork 2
/
Synths.ts
173 lines (152 loc) · 5 KB
/
Synths.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import Asset from "./Asset";
import { ethers } from "ethers";
import { providers } from "@0xsequence/multicall";
import {
ERC20Ethers__factory,
LongShortPairEthers__factory,
} from "@uma/contracts-node";
import { defaultAssetsConfig } from "./config";
import { prepareLSPStateCall } from "../utils/helpers";
import {
SynthsAssetsConfig,
AssetsConfig,
InitOptions,
isAssetConfigLSP,
MulticallParameter,
} from "../types/assets.t";
const MulticallWrapper = providers.MulticallProvider as unknown as new (
provider: ethers.providers.BaseProvider,
multicall?: MulticallParameter
) => ethers.providers.JsonRpcProvider;
class Synths {
assets!: AssetsConfig;
#multicallProvider!: ethers.providers.Provider;
#signer!: ethers.Signer;
/**
* @notice Creates an instance of the Synths SDK.
* @param options - Ethers Synths configuration.
* @returns The Synths SDK instance.
* @throws "Synths not found in the current network".
*/
static async create(options: InitOptions): Promise<Synths> {
const synthsSdk = new Synths();
await synthsSdk.init(options);
return synthsSdk;
}
/**
* Connects the SDK to an asset.
* @param config - Ethers Asset configuration
*/
connectAsset(assetIdentifier: string): Asset {
const asset = Asset.connect({
signer: this.#signer,
multicallProvider: this.#multicallProvider,
assets: this.assets,
assetIdentifier: assetIdentifier,
});
return asset;
}
async getLSPPortfolio() {
try {
const portfolio: { [key: string]: Record<string, unknown> } = {};
for (const assetCycles in this.assets) {
for (const asset of this.assets[assetCycles]) {
if (isAssetConfigLSP(asset)) {
const userAddress = await this.#signer.getAddress();
const contract = LongShortPairEthers__factory.connect(
asset.lsp.address,
this.#multicallProvider
);
const call = prepareLSPStateCall(contract);
const [
expirationTimestamp,
collateralToken,
priceIdentifier,
pairName,
longToken,
shortToken,
collateralPerPair,
timerAddress,
] = await call;
console.log(priceIdentifier, collateralPerPair, timerAddress);
const collateralContract = ERC20Ethers__factory.connect(
collateralToken,
this.#multicallProvider
);
const longTokenContract = ERC20Ethers__factory.connect(
longToken,
this.#multicallProvider
);
const shortTokenContract = ERC20Ethers__factory.connect(
shortToken,
this.#multicallProvider
);
const currentUnixTs = Math.floor(Date.now() / 1000);
const expired = expirationTimestamp.gte(
ethers.BigNumber.from(currentUnixTs)
)
? true
: false;
const [
collateralSymbol,
longSymbol,
shortSymbol,
longBalance,
shortBalance,
] = await Promise.all([
collateralContract.symbol(),
longTokenContract.symbol(),
shortTokenContract.symbol(),
longTokenContract.balanceOf(userAddress),
shortTokenContract.balanceOf(userAddress),
]);
/// @todo Get lp amount of user
/// @todo Get tokenPrice
portfolio[longSymbol] = {
balance: longBalance,
collateralSymbol: collateralSymbol,
status: expired,
};
// get lp amount of user
// get tokenPrice
portfolio[shortSymbol] = {
balance: shortBalance,
collateralSymbol: collateralSymbol,
status: expired,
};
}
}
}
return portfolio;
} catch (e) {
console.error("error", e);
return;
}
}
/**
* Initializes the Synths SDK instance.
* @param options - Ethers Synths configuration
* @throws "Synths not found in the current network"
*/
private async init(options: InitOptions): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
this.#multicallProvider = new MulticallWrapper(options.ethersProvider);
this.#signer = options.ethersProvider.getSigner();
const synthsAssetsConfig: SynthsAssetsConfig = {
...defaultAssetsConfig,
...options.userAssetsConfig,
};
const chainId = await this.#signer.getChainId();
if (Object.keys(synthsAssetsConfig).includes(chainId.toString())) {
this.assets = synthsAssetsConfig[chainId];
} else {
throw new Error(
`Synths not found in the current network ${chainId}. Please check your configuration.`
);
}
if (!this.assets) {
throw new Error("Synths not found in the current network");
}
}
}
export default Synths;