@dhedge/v2-sdk 1.7.2 → 1.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config.d.ts +0 -1
- package/dist/entities/pool.d.ts +16 -7
- package/dist/services/futures/constants.d.ts +3 -0
- package/dist/services/futures/index.d.ts +2 -0
- package/dist/services/futures/margin.d.ts +2 -0
- package/dist/services/futures/trade.d.ts +2 -0
- package/dist/services/velodrome/liquidity.d.ts +2 -2
- package/dist/test/constants.d.ts +24 -2
- package/dist/test/utils/token.d.ts +3 -0
- package/dist/utils/deadline.d.ts +2 -0
- package/dist/v2-sdk.cjs.development.js +4540 -5103
- package/dist/v2-sdk.cjs.development.js.map +1 -1
- package/dist/v2-sdk.cjs.production.min.js +1 -1
- package/dist/v2-sdk.cjs.production.min.js.map +1 -1
- package/dist/v2-sdk.esm.js +7341 -7904
- package/dist/v2-sdk.esm.js.map +1 -1
- package/package.json +5 -2
- package/src/abi/IDhedgeEasySwapper.json +99 -239
- package/src/abi/ISynthetiXFuturesMarketV2.json +531 -0
- package/src/config.ts +4 -5
- package/src/entities/pool.ts +68 -44
- package/src/services/futures/constants.ts +4 -0
- package/src/services/futures/index.ts +2 -0
- package/src/services/futures/margin.ts +10 -0
- package/src/services/futures/trade.ts +15 -0
- package/src/services/oneInch/protocols.ts +3 -4
- package/src/services/toros/easySwapper.ts +3 -2
- package/src/services/uniswap/V3Liquidity.ts +2 -3
- package/src/services/velodrome/liquidity.ts +7 -7
- package/src/test/constants.ts +27 -2
- package/src/test/futures.test.ts +48 -0
- package/src/test/oneInch.test.ts +32 -36
- package/src/test/pool.test.ts +42 -85
- package/src/test/toros.test.ts +50 -98
- package/src/test/uniswap.test.ts +65 -30
- package/src/test/utils/token.ts +31 -0
- package/src/test/velodrome.test.ts +74 -101
- package/src/test/wallet.ts +5 -3
- package/src/utils/deadline.ts +6 -0
- package/dist/services/claim-balancer/claim.service.d.ts +0 -17
- package/dist/services/claim-balancer/claim.worker.d.ts +0 -4
- package/dist/services/claim-balancer/ipfs.service.d.ts +0 -4
- package/dist/services/claim-balancer/types.d.ts +0 -54
- package/src/services/claim-balancer/MultiTokenClaim.json +0 -115
- package/src/services/claim-balancer/claim.service.ts +0 -262
- package/src/services/claim-balancer/claim.worker.ts +0 -32
- package/src/services/claim-balancer/ipfs.service.ts +0 -12
- package/src/services/claim-balancer/types.ts +0 -66
- package/src/test/1inch.test.ts +0 -54
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import axios from "axios";
|
|
3
|
-
import { chunk, flatten } from "lodash";
|
|
4
|
-
import merkleOrchardAbi from "../../abi/IBalancerMerkleOrchard.json";
|
|
5
|
-
import { ethers, Wallet } from "ethers";
|
|
6
|
-
|
|
7
|
-
import { getAddress } from "@ethersproject/address";
|
|
8
|
-
|
|
9
|
-
import { multicall } from "../../utils/contract";
|
|
10
|
-
import { bnum, loadTree, scale } from "../../utils";
|
|
11
|
-
|
|
12
|
-
import { ipfsService } from "./ipfs.service";
|
|
13
|
-
|
|
14
|
-
import {
|
|
15
|
-
ClaimProofTuple,
|
|
16
|
-
ClaimStatus,
|
|
17
|
-
ComputeClaimProofPayload,
|
|
18
|
-
MultiTokenPendingClaims,
|
|
19
|
-
Report,
|
|
20
|
-
Snapshot,
|
|
21
|
-
TokenClaimInfo
|
|
22
|
-
} from "./types";
|
|
23
|
-
|
|
24
|
-
import { Network } from "../../types";
|
|
25
|
-
import { networkChainIdMap, stakingAddress } from "../../config";
|
|
26
|
-
import { soliditySha3 } from "web3-utils";
|
|
27
|
-
import { Dapp } from "../..";
|
|
28
|
-
|
|
29
|
-
export class ClaimService {
|
|
30
|
-
network: Network;
|
|
31
|
-
signer: ethers.Wallet;
|
|
32
|
-
public constructor(network: Network, signer: Wallet) {
|
|
33
|
-
this.network = network;
|
|
34
|
-
this.signer = signer;
|
|
35
|
-
}
|
|
36
|
-
public async getMultiTokensPendingClaims(
|
|
37
|
-
account: string
|
|
38
|
-
): Promise<MultiTokenPendingClaims[]> {
|
|
39
|
-
const tokenClaimsInfo = await this.getTokenClaimsInfo();
|
|
40
|
-
if (tokenClaimsInfo != null) {
|
|
41
|
-
const multiTokenPendingClaims = await Promise.all(
|
|
42
|
-
tokenClaimsInfo.map(tokenClaimInfo =>
|
|
43
|
-
this.getTokenPendingClaims(tokenClaimInfo, getAddress(account))
|
|
44
|
-
)
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
const multiTokenPendingClaimsWithRewards = multiTokenPendingClaims.filter(
|
|
48
|
-
pendingClaim => Number(pendingClaim.availableToClaim) > 0
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
return multiTokenPendingClaimsWithRewards;
|
|
52
|
-
}
|
|
53
|
-
return [];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public async getTokenPendingClaims(
|
|
57
|
-
tokenClaimInfo: TokenClaimInfo,
|
|
58
|
-
account: string
|
|
59
|
-
): Promise<MultiTokenPendingClaims> {
|
|
60
|
-
const snapshot = await this.getSnapshot(tokenClaimInfo.manifest);
|
|
61
|
-
const weekStart = tokenClaimInfo.weekStart;
|
|
62
|
-
const claimStatus = await this.getClaimStatus(
|
|
63
|
-
Object.keys(snapshot).length,
|
|
64
|
-
account,
|
|
65
|
-
tokenClaimInfo
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
const pendingWeeks = claimStatus
|
|
69
|
-
.map((status, i) => [i + weekStart, status])
|
|
70
|
-
.filter(([, status]) => !status)
|
|
71
|
-
.map(([i]) => i) as number[];
|
|
72
|
-
|
|
73
|
-
const reports = await this.getReports(snapshot, pendingWeeks);
|
|
74
|
-
|
|
75
|
-
const claims = Object.entries(reports)
|
|
76
|
-
.filter((report: Report) => report[1][account])
|
|
77
|
-
.map((report: Report) => {
|
|
78
|
-
return {
|
|
79
|
-
id: report[0],
|
|
80
|
-
amount: report[1][account]
|
|
81
|
-
};
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
//console.log("claims", claims);
|
|
85
|
-
|
|
86
|
-
const availableToClaim = claims
|
|
87
|
-
.map(claim => parseFloat(claim.amount))
|
|
88
|
-
.reduce((total, amount) => total.plus(amount), bnum(0))
|
|
89
|
-
.toString();
|
|
90
|
-
|
|
91
|
-
return {
|
|
92
|
-
claims,
|
|
93
|
-
reports,
|
|
94
|
-
tokenClaimInfo,
|
|
95
|
-
availableToClaim
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
public async multiTokenClaimRewards(
|
|
100
|
-
account: string,
|
|
101
|
-
multiTokenPendingClaims: MultiTokenPendingClaims[]
|
|
102
|
-
): Promise<any> {
|
|
103
|
-
try {
|
|
104
|
-
const multiTokenClaims = await Promise.all(
|
|
105
|
-
multiTokenPendingClaims.map((tokenPendingClaims, tokenIndex) =>
|
|
106
|
-
this.computeClaimProofs(
|
|
107
|
-
tokenPendingClaims,
|
|
108
|
-
getAddress(account),
|
|
109
|
-
tokenIndex
|
|
110
|
-
)
|
|
111
|
-
)
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
return flatten(multiTokenClaims);
|
|
115
|
-
} catch (e) {
|
|
116
|
-
console.log("[Claim] Claim Rewards Error:", e);
|
|
117
|
-
return Promise.reject(e);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
private async computeClaimProofs(
|
|
122
|
-
tokenPendingClaims: MultiTokenPendingClaims,
|
|
123
|
-
account: string,
|
|
124
|
-
tokenIndex: number
|
|
125
|
-
): Promise<Promise<ClaimProofTuple[]>> {
|
|
126
|
-
return Promise.all(
|
|
127
|
-
tokenPendingClaims.claims.map(claim => {
|
|
128
|
-
const payload: ComputeClaimProofPayload = {
|
|
129
|
-
account,
|
|
130
|
-
distributor: tokenPendingClaims.tokenClaimInfo.distributor,
|
|
131
|
-
tokenIndex,
|
|
132
|
-
decimals: tokenPendingClaims.tokenClaimInfo.decimals,
|
|
133
|
-
// objects must be cloned
|
|
134
|
-
report: { ...tokenPendingClaims.reports[claim.id] },
|
|
135
|
-
claim: { ...claim }
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
return this.computeClaimProof(payload);
|
|
139
|
-
})
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
private computeClaimProof(
|
|
144
|
-
payload: ComputeClaimProofPayload
|
|
145
|
-
): ClaimProofTuple {
|
|
146
|
-
const {
|
|
147
|
-
report,
|
|
148
|
-
account,
|
|
149
|
-
claim,
|
|
150
|
-
distributor,
|
|
151
|
-
tokenIndex,
|
|
152
|
-
decimals
|
|
153
|
-
} = payload;
|
|
154
|
-
|
|
155
|
-
const claimAmount = claim.amount;
|
|
156
|
-
const merkleTree = loadTree(report, decimals);
|
|
157
|
-
|
|
158
|
-
const scaledBalance = scale(claimAmount, decimals).toString(10);
|
|
159
|
-
|
|
160
|
-
const proof = merkleTree.getHexProof(
|
|
161
|
-
soliditySha3(
|
|
162
|
-
{ t: "address", v: account },
|
|
163
|
-
{ t: "uint", v: scaledBalance }
|
|
164
|
-
)
|
|
165
|
-
);
|
|
166
|
-
return [
|
|
167
|
-
parseInt(claim.id),
|
|
168
|
-
scaledBalance,
|
|
169
|
-
distributor,
|
|
170
|
-
tokenIndex,
|
|
171
|
-
proof
|
|
172
|
-
] as ClaimProofTuple;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
private async getTokenClaimsInfo() {
|
|
176
|
-
let tokenClaims;
|
|
177
|
-
try {
|
|
178
|
-
const multiTokenClaim = await axios.get(
|
|
179
|
-
"https://raw.githubusercontent.com/balancer-labs/frontend-v2/develop/src/services/claim/MultiTokenClaim.json"
|
|
180
|
-
);
|
|
181
|
-
const chainId = networkChainIdMap[this.network];
|
|
182
|
-
tokenClaims = multiTokenClaim.data[chainId];
|
|
183
|
-
} catch (e) {
|
|
184
|
-
console.log("balancer multi token info error", e);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (tokenClaims != null) {
|
|
188
|
-
return (tokenClaims as TokenClaimInfo[]).map(tokenClaim => ({
|
|
189
|
-
...tokenClaim,
|
|
190
|
-
token: getAddress(tokenClaim.token),
|
|
191
|
-
decimals: 18
|
|
192
|
-
}));
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return null;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
private async getSnapshot(manifest: string) {
|
|
199
|
-
try {
|
|
200
|
-
const response = await axios.get<Snapshot>(manifest);
|
|
201
|
-
return response.data || {};
|
|
202
|
-
} catch (error) {
|
|
203
|
-
return {};
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
private async getClaimStatus(
|
|
208
|
-
totalWeeks: number,
|
|
209
|
-
account: string,
|
|
210
|
-
tokenClaimInfo: TokenClaimInfo
|
|
211
|
-
): Promise<ClaimStatus[]> {
|
|
212
|
-
const { token, distributor, weekStart } = tokenClaimInfo;
|
|
213
|
-
|
|
214
|
-
const claimStatusCalls = Array.from({ length: totalWeeks }).map((_, i) => [
|
|
215
|
-
stakingAddress[this.network][Dapp.BALANCER],
|
|
216
|
-
"isClaimed",
|
|
217
|
-
[token, distributor, weekStart + i, account]
|
|
218
|
-
]);
|
|
219
|
-
|
|
220
|
-
const rootCalls = Array.from({ length: totalWeeks }).map((_, i) => [
|
|
221
|
-
stakingAddress[this.network][Dapp.BALANCER],
|
|
222
|
-
"getDistributionRoot",
|
|
223
|
-
[token, distributor, weekStart + i]
|
|
224
|
-
]);
|
|
225
|
-
|
|
226
|
-
try {
|
|
227
|
-
const result = (await multicall<boolean | string>(
|
|
228
|
-
this.network,
|
|
229
|
-
this.signer,
|
|
230
|
-
merkleOrchardAbi.abi,
|
|
231
|
-
[...claimStatusCalls, ...rootCalls],
|
|
232
|
-
{},
|
|
233
|
-
true
|
|
234
|
-
)) as (boolean | string)[];
|
|
235
|
-
|
|
236
|
-
if (result.length > 0) {
|
|
237
|
-
const chunks = chunk(flatten(result), totalWeeks);
|
|
238
|
-
|
|
239
|
-
const claimedResult = chunks[0] as boolean[];
|
|
240
|
-
const distributionRootResult = chunks[1] as string[];
|
|
241
|
-
|
|
242
|
-
return claimedResult.filter(
|
|
243
|
-
(_, index) =>
|
|
244
|
-
distributionRootResult[index] !== ethers.constants.HashZero
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
} catch (e) {
|
|
248
|
-
console.log("[Claim] Claim Status Error:", e);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return [];
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
private async getReports(snapshot: Snapshot, weeks: number[]) {
|
|
255
|
-
const reports = await Promise.all<Report>(
|
|
256
|
-
weeks
|
|
257
|
-
.filter(week => snapshot[week] != null)
|
|
258
|
-
.map(week => ipfsService.get(snapshot[week]))
|
|
259
|
-
);
|
|
260
|
-
return Object.fromEntries(reports.map((report, i) => [weeks[i], report]));
|
|
261
|
-
}
|
|
262
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
// Shamelessly adapted from OpenZeppelin-contracts test utils
|
|
2
|
-
import { soliditySha3 } from "web3-utils";
|
|
3
|
-
import { loadTree, scale } from "../../utils";
|
|
4
|
-
|
|
5
|
-
import { ComputeClaimProofPayload } from "./types";
|
|
6
|
-
|
|
7
|
-
export class ClaimWorker {
|
|
8
|
-
public calcClaimProof(payload: ComputeClaimProofPayload): any {
|
|
9
|
-
const {
|
|
10
|
-
report,
|
|
11
|
-
account,
|
|
12
|
-
claim,
|
|
13
|
-
distributor,
|
|
14
|
-
tokenIndex,
|
|
15
|
-
decimals
|
|
16
|
-
} = payload;
|
|
17
|
-
|
|
18
|
-
const claimAmount = claim.amount;
|
|
19
|
-
const merkleTree = loadTree(report, decimals);
|
|
20
|
-
|
|
21
|
-
const scaledBalance = scale(claimAmount, decimals).toString(10);
|
|
22
|
-
|
|
23
|
-
const proof = merkleTree.getHexProof(
|
|
24
|
-
soliditySha3(
|
|
25
|
-
{ t: "address", v: account },
|
|
26
|
-
{ t: "uint", v: scaledBalance }
|
|
27
|
-
)
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
return [parseInt(claim.id), scaledBalance, distributor, tokenIndex, proof];
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
|
-
|
|
3
|
-
export default class IpfsService {
|
|
4
|
-
async get<T>(hash: string, protocol = "ipfs"): Promise<T> {
|
|
5
|
-
const { data } = await axios.get(
|
|
6
|
-
`https://cloudflare-ipfs.com/${protocol}/${hash}`
|
|
7
|
-
);
|
|
8
|
-
return data;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const ipfsService = new IpfsService();
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
export interface Claim {
|
|
2
|
-
id: string;
|
|
3
|
-
amount: string;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export type Snapshot = Record<number, string>;
|
|
7
|
-
|
|
8
|
-
export type TokenClaimInfo = {
|
|
9
|
-
label: string;
|
|
10
|
-
distributor: string;
|
|
11
|
-
token: string;
|
|
12
|
-
decimals: number;
|
|
13
|
-
manifest: string;
|
|
14
|
-
weekStart: number;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export type MultiTokenPendingClaims = {
|
|
18
|
-
claims: Claim[];
|
|
19
|
-
reports: Report;
|
|
20
|
-
tokenClaimInfo: TokenClaimInfo;
|
|
21
|
-
availableToClaim: string;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export type ClaimStatus = boolean;
|
|
25
|
-
|
|
26
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
27
|
-
export type Report = Record<string, any>;
|
|
28
|
-
|
|
29
|
-
export type MultiTokenCurrentRewardsEstimateResponse = {
|
|
30
|
-
success: boolean;
|
|
31
|
-
result: {
|
|
32
|
-
current_timestamp: string;
|
|
33
|
-
"liquidity-providers": Array<{
|
|
34
|
-
snapshot_timestamp: string;
|
|
35
|
-
address: string;
|
|
36
|
-
token_address: string;
|
|
37
|
-
chain_id: number;
|
|
38
|
-
current_estimate: string;
|
|
39
|
-
velocity: string;
|
|
40
|
-
week: number;
|
|
41
|
-
}>;
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export type MultiTokenCurrentRewardsEstimate = {
|
|
46
|
-
rewards: string;
|
|
47
|
-
velocity: string;
|
|
48
|
-
token: string;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export type ClaimProofTuple = [number, string, string, number, string[]]; // claimId, claimAmount, distributor, tokenIndex, proof
|
|
52
|
-
|
|
53
|
-
export type ComputeClaimProofPayload = {
|
|
54
|
-
report: Report;
|
|
55
|
-
account: string;
|
|
56
|
-
claim: Claim;
|
|
57
|
-
distributor: string;
|
|
58
|
-
tokenIndex: number;
|
|
59
|
-
decimals: number;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
63
|
-
export type ClaimWorkerMessage<P = any> = {
|
|
64
|
-
type: "computeClaimProof";
|
|
65
|
-
payload: P;
|
|
66
|
-
};
|
package/src/test/1inch.test.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { Dhedge } from "..";
|
|
2
|
-
import { Dapp, Network } from "../types";
|
|
3
|
-
import { TEST_POOL, USDC, WBTC } from "./constants";
|
|
4
|
-
import { getTxOptions } from "./txOptions";
|
|
5
|
-
|
|
6
|
-
import { wallet } from "./wallet";
|
|
7
|
-
|
|
8
|
-
let dhedge: Dhedge;
|
|
9
|
-
let options: any;
|
|
10
|
-
|
|
11
|
-
jest.setTimeout(100000);
|
|
12
|
-
|
|
13
|
-
describe("pool", () => {
|
|
14
|
-
beforeAll(async () => {
|
|
15
|
-
dhedge = new Dhedge(wallet, Network.OPTIMISM);
|
|
16
|
-
options = await getTxOptions(Network.OPTIMISM);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
// it("approves unlimited USDC on 1Inch", async () => {
|
|
20
|
-
// let result;
|
|
21
|
-
// const pool = await dhedge.loadPool(TEST_POOL);
|
|
22
|
-
// try {
|
|
23
|
-
// result = await pool.approve(
|
|
24
|
-
// Dapp.ONEINCH,
|
|
25
|
-
// USDC,
|
|
26
|
-
// ethers.constants.MaxInt256,
|
|
27
|
-
// options
|
|
28
|
-
// );
|
|
29
|
-
// console.log(result);
|
|
30
|
-
// } catch (e) {
|
|
31
|
-
// console.log(e);
|
|
32
|
-
// }
|
|
33
|
-
// expect(result).not.toBe(null);
|
|
34
|
-
// });
|
|
35
|
-
|
|
36
|
-
it("trades 1 USDC into WBTC on 1Inch", async () => {
|
|
37
|
-
let result;
|
|
38
|
-
const pool = await dhedge.loadPool(TEST_POOL);
|
|
39
|
-
try {
|
|
40
|
-
result = await pool.trade(
|
|
41
|
-
Dapp.ONEINCH,
|
|
42
|
-
USDC,
|
|
43
|
-
WBTC,
|
|
44
|
-
"1000000",
|
|
45
|
-
0.5,
|
|
46
|
-
options
|
|
47
|
-
);
|
|
48
|
-
console.log(result);
|
|
49
|
-
} catch (e) {
|
|
50
|
-
console.log(e);
|
|
51
|
-
}
|
|
52
|
-
expect(result).not.toBe(null);
|
|
53
|
-
});
|
|
54
|
-
});
|