@atomiqlabs/chain-solana 10.0.0-dev.3 → 11.0.0
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/LICENSE +201 -201
- package/dist/index.d.ts +29 -29
- package/dist/index.js +45 -45
- package/dist/solana/SolanaChainType.d.ts +10 -10
- package/dist/solana/SolanaChainType.js +2 -2
- package/dist/solana/SolanaChains.d.ts +20 -20
- package/dist/solana/SolanaChains.js +25 -25
- package/dist/solana/SolanaInitializer.d.ts +18 -18
- package/dist/solana/SolanaInitializer.js +63 -63
- package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +228 -228
- package/dist/solana/btcrelay/SolanaBtcRelay.js +441 -441
- package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +29 -29
- package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +34 -34
- package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +46 -46
- package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +78 -78
- package/dist/solana/btcrelay/program/programIdl.json +671 -671
- package/dist/solana/chain/SolanaAction.d.ts +26 -26
- package/dist/solana/chain/SolanaAction.js +86 -86
- package/dist/solana/chain/SolanaChainInterface.d.ts +58 -58
- package/dist/solana/chain/SolanaChainInterface.js +112 -112
- package/dist/solana/chain/SolanaModule.d.ts +14 -14
- package/dist/solana/chain/SolanaModule.js +13 -13
- package/dist/solana/chain/modules/SolanaAddresses.d.ts +8 -8
- package/dist/solana/chain/modules/SolanaAddresses.js +22 -22
- package/dist/solana/chain/modules/SolanaBlocks.d.ts +28 -28
- package/dist/solana/chain/modules/SolanaBlocks.js +72 -72
- package/dist/solana/chain/modules/SolanaEvents.d.ts +25 -25
- package/dist/solana/chain/modules/SolanaEvents.js +58 -58
- package/dist/solana/chain/modules/SolanaFees.d.ts +121 -121
- package/dist/solana/chain/modules/SolanaFees.js +379 -379
- package/dist/solana/chain/modules/SolanaSignatures.d.ts +23 -23
- package/dist/solana/chain/modules/SolanaSignatures.js +39 -39
- package/dist/solana/chain/modules/SolanaSlots.d.ts +31 -31
- package/dist/solana/chain/modules/SolanaSlots.js +68 -68
- package/dist/solana/chain/modules/SolanaTokens.d.ts +136 -136
- package/dist/solana/chain/modules/SolanaTokens.js +248 -248
- package/dist/solana/chain/modules/SolanaTransactions.d.ts +124 -124
- package/dist/solana/chain/modules/SolanaTransactions.js +332 -332
- package/dist/solana/events/SolanaChainEvents.d.ts +88 -88
- package/dist/solana/events/SolanaChainEvents.js +256 -256
- package/dist/solana/events/SolanaChainEventsBrowser.d.ts +85 -85
- package/dist/solana/events/SolanaChainEventsBrowser.js +194 -194
- package/dist/solana/program/SolanaProgramBase.d.ts +40 -40
- package/dist/solana/program/SolanaProgramBase.js +43 -43
- package/dist/solana/program/SolanaProgramModule.d.ts +8 -8
- package/dist/solana/program/SolanaProgramModule.js +11 -11
- package/dist/solana/program/modules/SolanaProgramEvents.d.ts +59 -59
- package/dist/solana/program/modules/SolanaProgramEvents.js +103 -103
- package/dist/solana/swaps/SolanaSwapData.d.ts +59 -59
- package/dist/solana/swaps/SolanaSwapData.js +267 -267
- package/dist/solana/swaps/SolanaSwapModule.d.ts +10 -10
- package/dist/solana/swaps/SolanaSwapModule.js +11 -11
- package/dist/solana/swaps/SolanaSwapProgram.d.ts +202 -202
- package/dist/solana/swaps/SolanaSwapProgram.js +470 -463
- package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -11
- package/dist/solana/swaps/SwapTypeEnum.js +42 -42
- package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +94 -94
- package/dist/solana/swaps/modules/SolanaDataAccount.js +231 -231
- package/dist/solana/swaps/modules/SolanaLpVault.d.ts +71 -71
- package/dist/solana/swaps/modules/SolanaLpVault.js +173 -173
- package/dist/solana/swaps/modules/SwapClaim.d.ts +129 -129
- package/dist/solana/swaps/modules/SwapClaim.js +291 -291
- package/dist/solana/swaps/modules/SwapInit.d.ts +217 -217
- package/dist/solana/swaps/modules/SwapInit.js +519 -519
- package/dist/solana/swaps/modules/SwapRefund.d.ts +82 -82
- package/dist/solana/swaps/modules/SwapRefund.js +252 -252
- package/dist/solana/swaps/programIdl.json +945 -945
- package/dist/solana/swaps/programTypes.d.ts +943 -943
- package/dist/solana/swaps/programTypes.js +945 -945
- package/dist/solana/wallet/SolanaKeypairWallet.d.ts +9 -9
- package/dist/solana/wallet/SolanaKeypairWallet.js +33 -33
- package/dist/solana/wallet/SolanaSigner.d.ts +10 -10
- package/dist/solana/wallet/SolanaSigner.js +16 -16
- package/dist/utils/Utils.d.ts +53 -53
- package/dist/utils/Utils.js +170 -170
- package/package.json +41 -41
- package/src/index.ts +36 -36
- package/src/solana/SolanaChainType.ts +25 -25
- package/src/solana/SolanaChains.ts +23 -23
- package/src/solana/SolanaInitializer.ts +102 -102
- package/src/solana/btcrelay/SolanaBtcRelay.ts +589 -588
- package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +57 -57
- package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +102 -102
- package/src/solana/btcrelay/program/programIdl.json +670 -670
- package/src/solana/chain/SolanaAction.ts +108 -108
- package/src/solana/chain/SolanaChainInterface.ts +174 -174
- package/src/solana/chain/SolanaModule.ts +20 -20
- package/src/solana/chain/modules/SolanaAddresses.ts +20 -20
- package/src/solana/chain/modules/SolanaBlocks.ts +78 -78
- package/src/solana/chain/modules/SolanaEvents.ts +56 -56
- package/src/solana/chain/modules/SolanaFees.ts +450 -450
- package/src/solana/chain/modules/SolanaSignatures.ts +39 -39
- package/src/solana/chain/modules/SolanaSlots.ts +82 -82
- package/src/solana/chain/modules/SolanaTokens.ts +307 -307
- package/src/solana/chain/modules/SolanaTransactions.ts +370 -370
- package/src/solana/events/SolanaChainEvents.ts +299 -299
- package/src/solana/events/SolanaChainEventsBrowser.ts +256 -256
- package/src/solana/program/SolanaProgramBase.ts +79 -79
- package/src/solana/program/SolanaProgramModule.ts +15 -15
- package/src/solana/program/modules/SolanaProgramEvents.ts +140 -140
- package/src/solana/swaps/SolanaSwapData.ts +379 -379
- package/src/solana/swaps/SolanaSwapModule.ts +16 -16
- package/src/solana/swaps/SolanaSwapProgram.ts +697 -692
- package/src/solana/swaps/SwapTypeEnum.ts +29 -29
- package/src/solana/swaps/modules/SolanaDataAccount.ts +307 -307
- package/src/solana/swaps/modules/SolanaLpVault.ts +215 -215
- package/src/solana/swaps/modules/SwapClaim.ts +389 -389
- package/src/solana/swaps/modules/SwapInit.ts +663 -663
- package/src/solana/swaps/modules/SwapRefund.ts +312 -312
- package/src/solana/swaps/programIdl.json +944 -944
- package/src/solana/swaps/programTypes.ts +1885 -1885
- package/src/solana/wallet/SolanaKeypairWallet.ts +36 -36
- package/src/solana/wallet/SolanaSigner.ts +23 -23
- package/src/utils/Utils.ts +180 -180
|
@@ -1,216 +1,216 @@
|
|
|
1
|
-
import {SolanaSwapModule} from "../SolanaSwapModule";
|
|
2
|
-
import {SolanaAction} from "../../chain/SolanaAction";
|
|
3
|
-
import {PublicKey, SystemProgram} from "@solana/web3.js";
|
|
4
|
-
import {
|
|
5
|
-
Account,
|
|
6
|
-
getAssociatedTokenAddress,
|
|
7
|
-
getAssociatedTokenAddressSync,
|
|
8
|
-
TOKEN_PROGRAM_ID
|
|
9
|
-
} from "@solana/spl-token";
|
|
10
|
-
import {SolanaTx} from "../../chain/modules/SolanaTransactions";
|
|
11
|
-
import {toBigInt, toBN, tryWithRetries} from "../../../utils/Utils";
|
|
12
|
-
import {SwapProgram} from "../programTypes";
|
|
13
|
-
import { IntermediaryReputationType } from "@atomiqlabs/base";
|
|
14
|
-
import { IdlAccounts } from "@coral-xyz/anchor";
|
|
15
|
-
import {SolanaTokens} from "../../chain/modules/SolanaTokens";
|
|
16
|
-
|
|
17
|
-
export class SolanaLpVault extends SolanaSwapModule {
|
|
18
|
-
|
|
19
|
-
private static readonly CUCosts = {
|
|
20
|
-
WITHDRAW: 50000,
|
|
21
|
-
DEPOSIT: 50000
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Action for withdrawing funds from the LP vault
|
|
26
|
-
*
|
|
27
|
-
* @param signer
|
|
28
|
-
* @param token
|
|
29
|
-
* @param amount
|
|
30
|
-
* @constructor
|
|
31
|
-
* @private
|
|
32
|
-
*/
|
|
33
|
-
private async Withdraw(signer: PublicKey, token: PublicKey, amount: bigint): Promise<SolanaAction> {
|
|
34
|
-
const ata = getAssociatedTokenAddressSync(token, signer);
|
|
35
|
-
return new SolanaAction(signer, this.root,
|
|
36
|
-
await this.swapProgram.methods
|
|
37
|
-
.withdraw(toBN(amount))
|
|
38
|
-
.accounts({
|
|
39
|
-
signer,
|
|
40
|
-
signerAta: ata,
|
|
41
|
-
userData: this.program.SwapUserVault(signer, token),
|
|
42
|
-
vault: this.program.SwapVault(token),
|
|
43
|
-
vaultAuthority: this.program.SwapVaultAuthority,
|
|
44
|
-
mint: token,
|
|
45
|
-
tokenProgram: TOKEN_PROGRAM_ID
|
|
46
|
-
})
|
|
47
|
-
.instruction(),
|
|
48
|
-
SolanaLpVault.CUCosts.WITHDRAW
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Action for depositing funds to the LP vault
|
|
54
|
-
*
|
|
55
|
-
* @param signer
|
|
56
|
-
* @param token
|
|
57
|
-
* @param amount
|
|
58
|
-
* @constructor
|
|
59
|
-
* @private
|
|
60
|
-
*/
|
|
61
|
-
private async Deposit(signer: PublicKey, token: PublicKey, amount: bigint): Promise<SolanaAction> {
|
|
62
|
-
const ata = getAssociatedTokenAddressSync(token, signer);
|
|
63
|
-
return new SolanaAction(signer, this.root,
|
|
64
|
-
await this.swapProgram.methods
|
|
65
|
-
.deposit(toBN(amount))
|
|
66
|
-
.accounts({
|
|
67
|
-
signer,
|
|
68
|
-
signerAta: ata,
|
|
69
|
-
userData: this.program.SwapUserVault(signer, token),
|
|
70
|
-
vault: this.program.SwapVault(token),
|
|
71
|
-
vaultAuthority: this.program.SwapVaultAuthority,
|
|
72
|
-
mint: token,
|
|
73
|
-
systemProgram: SystemProgram.programId,
|
|
74
|
-
tokenProgram: TOKEN_PROGRAM_ID
|
|
75
|
-
})
|
|
76
|
-
.instruction(),
|
|
77
|
-
SolanaLpVault.CUCosts.DEPOSIT
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Returns intermediary's reputation & vault balance for a specific token
|
|
83
|
-
*
|
|
84
|
-
* @param address
|
|
85
|
-
* @param token
|
|
86
|
-
*/
|
|
87
|
-
public async getIntermediaryData(address: PublicKey, token: PublicKey): Promise<{
|
|
88
|
-
balance: bigint,
|
|
89
|
-
reputation: IntermediaryReputationType
|
|
90
|
-
}> {
|
|
91
|
-
const data: IdlAccounts<SwapProgram>["userAccount"] = await this.swapProgram.account.userAccount.fetchNullable(
|
|
92
|
-
this.program.SwapUserVault(address, token)
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
if(data==null) return null;
|
|
96
|
-
|
|
97
|
-
const response: any = [];
|
|
98
|
-
|
|
99
|
-
for(let i=0;i<data.successVolume.length;i++) {
|
|
100
|
-
response[i] = {
|
|
101
|
-
successVolume: data.successVolume[i],
|
|
102
|
-
successCount: data.successCount[i],
|
|
103
|
-
failVolume: data.failVolume[i],
|
|
104
|
-
failCount: data.failCount[i],
|
|
105
|
-
coopCloseVolume: data.coopCloseVolume[i],
|
|
106
|
-
coopCloseCount: data.coopCloseCount[i]
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return {
|
|
111
|
-
balance: toBigInt(data.amount),
|
|
112
|
-
reputation: response
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Returns intermediary's reputation for a specific token
|
|
118
|
-
*
|
|
119
|
-
* @param address
|
|
120
|
-
* @param token
|
|
121
|
-
*/
|
|
122
|
-
public async getIntermediaryReputation(address: PublicKey, token: PublicKey): Promise<IntermediaryReputationType> {
|
|
123
|
-
const intermediaryData = await this.getIntermediaryData(address, token);
|
|
124
|
-
return intermediaryData?.reputation;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Returns the balance of the token an intermediary has in his LP vault
|
|
129
|
-
*
|
|
130
|
-
* @param address
|
|
131
|
-
* @param token
|
|
132
|
-
*/
|
|
133
|
-
public async getIntermediaryBalance(address: PublicKey, token: PublicKey): Promise<bigint> {
|
|
134
|
-
const intermediaryData = await this.getIntermediaryData(address, token);
|
|
135
|
-
const balance: bigint = intermediaryData?.balance;
|
|
136
|
-
|
|
137
|
-
this.logger.debug("getIntermediaryBalance(): token LP balance fetched, token: "+token.toString()+
|
|
138
|
-
" address: "+address+" amount: "+(balance==null ? "null" : balance.toString()));
|
|
139
|
-
|
|
140
|
-
return balance;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Creates transactions for withdrawing funds from the LP vault, creates ATA if it doesn't exist and unwraps
|
|
145
|
-
* WSOL to SOL if required
|
|
146
|
-
*
|
|
147
|
-
* @param signer
|
|
148
|
-
* @param token
|
|
149
|
-
* @param amount
|
|
150
|
-
* @param feeRate
|
|
151
|
-
*/
|
|
152
|
-
public async txsWithdraw(signer: PublicKey, token: PublicKey, amount: bigint, feeRate?: string): Promise<SolanaTx[]> {
|
|
153
|
-
const ata = await getAssociatedTokenAddress(token, signer);
|
|
154
|
-
|
|
155
|
-
feeRate = feeRate || await this.getFeeRate(signer, token);
|
|
156
|
-
|
|
157
|
-
const action = new SolanaAction(signer, this.root);
|
|
158
|
-
if(!await this.root.Tokens.ataExists(ata)) {
|
|
159
|
-
action.add(this.root.Tokens.InitAta(signer, signer, token));
|
|
160
|
-
}
|
|
161
|
-
action.add(await this.Withdraw(signer, token, amount));
|
|
162
|
-
const shouldUnwrap = token.equals(SolanaTokens.WSOL_ADDRESS);
|
|
163
|
-
if(shouldUnwrap) action.add(this.root.Tokens.Unwrap(signer));
|
|
164
|
-
|
|
165
|
-
this.logger.debug("txsWithdraw(): withdraw TX created, token: "+token.toString()+
|
|
166
|
-
" amount: "+amount.toString(10)+" unwrapping: "+shouldUnwrap);
|
|
167
|
-
|
|
168
|
-
return [await action.tx(feeRate)];
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Creates transaction for depositing funds into the LP vault, wraps SOL to WSOL if required
|
|
173
|
-
*
|
|
174
|
-
* @param signer
|
|
175
|
-
* @param token
|
|
176
|
-
* @param amount
|
|
177
|
-
* @param feeRate
|
|
178
|
-
*/
|
|
179
|
-
public async txsDeposit(signer: PublicKey, token: PublicKey, amount: bigint, feeRate?: string): Promise<SolanaTx[]> {
|
|
180
|
-
const ata = getAssociatedTokenAddressSync(token, signer);
|
|
181
|
-
|
|
182
|
-
feeRate = feeRate || await this.getFeeRate(signer, token);
|
|
183
|
-
|
|
184
|
-
const action = new SolanaAction(signer, this.root);
|
|
185
|
-
|
|
186
|
-
let wrapping: boolean = false;
|
|
187
|
-
if(token.equals(SolanaTokens.WSOL_ADDRESS)) {
|
|
188
|
-
const account = await tryWithRetries<Account>(
|
|
189
|
-
() => this.root.Tokens.getATAOrNull(ata),
|
|
190
|
-
this.retryPolicy
|
|
191
|
-
);
|
|
192
|
-
let balance: bigint = account==null ? 0n : account.amount;
|
|
193
|
-
if(balance < amount) {
|
|
194
|
-
action.add(this.root.Tokens.Wrap(signer, amount - balance, account==null));
|
|
195
|
-
wrapping = true;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
action.addAction(await this.Deposit(signer, token, amount));
|
|
199
|
-
|
|
200
|
-
this.logger.debug("txsDeposit(): deposit TX created, token: "+token.toString()+
|
|
201
|
-
" amount: "+amount.toString(10)+" wrapping: "+wrapping);
|
|
202
|
-
|
|
203
|
-
return [await action.tx(feeRate)];
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
public getFeeRate(signer: PublicKey, token: PublicKey) {
|
|
207
|
-
const ata = getAssociatedTokenAddressSync(token, signer);
|
|
208
|
-
return this.root.Fees.getFeeRate([
|
|
209
|
-
signer,
|
|
210
|
-
ata,
|
|
211
|
-
this.program.SwapUserVault(signer, token),
|
|
212
|
-
this.program.SwapVault(token)
|
|
213
|
-
])
|
|
214
|
-
}
|
|
215
|
-
|
|
1
|
+
import {SolanaSwapModule} from "../SolanaSwapModule";
|
|
2
|
+
import {SolanaAction} from "../../chain/SolanaAction";
|
|
3
|
+
import {PublicKey, SystemProgram} from "@solana/web3.js";
|
|
4
|
+
import {
|
|
5
|
+
Account,
|
|
6
|
+
getAssociatedTokenAddress,
|
|
7
|
+
getAssociatedTokenAddressSync,
|
|
8
|
+
TOKEN_PROGRAM_ID
|
|
9
|
+
} from "@solana/spl-token";
|
|
10
|
+
import {SolanaTx} from "../../chain/modules/SolanaTransactions";
|
|
11
|
+
import {toBigInt, toBN, tryWithRetries} from "../../../utils/Utils";
|
|
12
|
+
import {SwapProgram} from "../programTypes";
|
|
13
|
+
import { IntermediaryReputationType } from "@atomiqlabs/base";
|
|
14
|
+
import { IdlAccounts } from "@coral-xyz/anchor";
|
|
15
|
+
import {SolanaTokens} from "../../chain/modules/SolanaTokens";
|
|
16
|
+
|
|
17
|
+
export class SolanaLpVault extends SolanaSwapModule {
|
|
18
|
+
|
|
19
|
+
private static readonly CUCosts = {
|
|
20
|
+
WITHDRAW: 50000,
|
|
21
|
+
DEPOSIT: 50000
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Action for withdrawing funds from the LP vault
|
|
26
|
+
*
|
|
27
|
+
* @param signer
|
|
28
|
+
* @param token
|
|
29
|
+
* @param amount
|
|
30
|
+
* @constructor
|
|
31
|
+
* @private
|
|
32
|
+
*/
|
|
33
|
+
private async Withdraw(signer: PublicKey, token: PublicKey, amount: bigint): Promise<SolanaAction> {
|
|
34
|
+
const ata = getAssociatedTokenAddressSync(token, signer);
|
|
35
|
+
return new SolanaAction(signer, this.root,
|
|
36
|
+
await this.swapProgram.methods
|
|
37
|
+
.withdraw(toBN(amount))
|
|
38
|
+
.accounts({
|
|
39
|
+
signer,
|
|
40
|
+
signerAta: ata,
|
|
41
|
+
userData: this.program.SwapUserVault(signer, token),
|
|
42
|
+
vault: this.program.SwapVault(token),
|
|
43
|
+
vaultAuthority: this.program.SwapVaultAuthority,
|
|
44
|
+
mint: token,
|
|
45
|
+
tokenProgram: TOKEN_PROGRAM_ID
|
|
46
|
+
})
|
|
47
|
+
.instruction(),
|
|
48
|
+
SolanaLpVault.CUCosts.WITHDRAW
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Action for depositing funds to the LP vault
|
|
54
|
+
*
|
|
55
|
+
* @param signer
|
|
56
|
+
* @param token
|
|
57
|
+
* @param amount
|
|
58
|
+
* @constructor
|
|
59
|
+
* @private
|
|
60
|
+
*/
|
|
61
|
+
private async Deposit(signer: PublicKey, token: PublicKey, amount: bigint): Promise<SolanaAction> {
|
|
62
|
+
const ata = getAssociatedTokenAddressSync(token, signer);
|
|
63
|
+
return new SolanaAction(signer, this.root,
|
|
64
|
+
await this.swapProgram.methods
|
|
65
|
+
.deposit(toBN(amount))
|
|
66
|
+
.accounts({
|
|
67
|
+
signer,
|
|
68
|
+
signerAta: ata,
|
|
69
|
+
userData: this.program.SwapUserVault(signer, token),
|
|
70
|
+
vault: this.program.SwapVault(token),
|
|
71
|
+
vaultAuthority: this.program.SwapVaultAuthority,
|
|
72
|
+
mint: token,
|
|
73
|
+
systemProgram: SystemProgram.programId,
|
|
74
|
+
tokenProgram: TOKEN_PROGRAM_ID
|
|
75
|
+
})
|
|
76
|
+
.instruction(),
|
|
77
|
+
SolanaLpVault.CUCosts.DEPOSIT
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns intermediary's reputation & vault balance for a specific token
|
|
83
|
+
*
|
|
84
|
+
* @param address
|
|
85
|
+
* @param token
|
|
86
|
+
*/
|
|
87
|
+
public async getIntermediaryData(address: PublicKey, token: PublicKey): Promise<{
|
|
88
|
+
balance: bigint,
|
|
89
|
+
reputation: IntermediaryReputationType
|
|
90
|
+
}> {
|
|
91
|
+
const data: IdlAccounts<SwapProgram>["userAccount"] = await this.swapProgram.account.userAccount.fetchNullable(
|
|
92
|
+
this.program.SwapUserVault(address, token)
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
if(data==null) return null;
|
|
96
|
+
|
|
97
|
+
const response: any = [];
|
|
98
|
+
|
|
99
|
+
for(let i=0;i<data.successVolume.length;i++) {
|
|
100
|
+
response[i] = {
|
|
101
|
+
successVolume: data.successVolume[i],
|
|
102
|
+
successCount: data.successCount[i],
|
|
103
|
+
failVolume: data.failVolume[i],
|
|
104
|
+
failCount: data.failCount[i],
|
|
105
|
+
coopCloseVolume: data.coopCloseVolume[i],
|
|
106
|
+
coopCloseCount: data.coopCloseCount[i]
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
balance: toBigInt(data.amount),
|
|
112
|
+
reputation: response
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Returns intermediary's reputation for a specific token
|
|
118
|
+
*
|
|
119
|
+
* @param address
|
|
120
|
+
* @param token
|
|
121
|
+
*/
|
|
122
|
+
public async getIntermediaryReputation(address: PublicKey, token: PublicKey): Promise<IntermediaryReputationType> {
|
|
123
|
+
const intermediaryData = await this.getIntermediaryData(address, token);
|
|
124
|
+
return intermediaryData?.reputation;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Returns the balance of the token an intermediary has in his LP vault
|
|
129
|
+
*
|
|
130
|
+
* @param address
|
|
131
|
+
* @param token
|
|
132
|
+
*/
|
|
133
|
+
public async getIntermediaryBalance(address: PublicKey, token: PublicKey): Promise<bigint> {
|
|
134
|
+
const intermediaryData = await this.getIntermediaryData(address, token);
|
|
135
|
+
const balance: bigint = intermediaryData?.balance;
|
|
136
|
+
|
|
137
|
+
this.logger.debug("getIntermediaryBalance(): token LP balance fetched, token: "+token.toString()+
|
|
138
|
+
" address: "+address+" amount: "+(balance==null ? "null" : balance.toString()));
|
|
139
|
+
|
|
140
|
+
return balance;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Creates transactions for withdrawing funds from the LP vault, creates ATA if it doesn't exist and unwraps
|
|
145
|
+
* WSOL to SOL if required
|
|
146
|
+
*
|
|
147
|
+
* @param signer
|
|
148
|
+
* @param token
|
|
149
|
+
* @param amount
|
|
150
|
+
* @param feeRate
|
|
151
|
+
*/
|
|
152
|
+
public async txsWithdraw(signer: PublicKey, token: PublicKey, amount: bigint, feeRate?: string): Promise<SolanaTx[]> {
|
|
153
|
+
const ata = await getAssociatedTokenAddress(token, signer);
|
|
154
|
+
|
|
155
|
+
feeRate = feeRate || await this.getFeeRate(signer, token);
|
|
156
|
+
|
|
157
|
+
const action = new SolanaAction(signer, this.root);
|
|
158
|
+
if(!await this.root.Tokens.ataExists(ata)) {
|
|
159
|
+
action.add(this.root.Tokens.InitAta(signer, signer, token));
|
|
160
|
+
}
|
|
161
|
+
action.add(await this.Withdraw(signer, token, amount));
|
|
162
|
+
const shouldUnwrap = token.equals(SolanaTokens.WSOL_ADDRESS);
|
|
163
|
+
if(shouldUnwrap) action.add(this.root.Tokens.Unwrap(signer));
|
|
164
|
+
|
|
165
|
+
this.logger.debug("txsWithdraw(): withdraw TX created, token: "+token.toString()+
|
|
166
|
+
" amount: "+amount.toString(10)+" unwrapping: "+shouldUnwrap);
|
|
167
|
+
|
|
168
|
+
return [await action.tx(feeRate)];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Creates transaction for depositing funds into the LP vault, wraps SOL to WSOL if required
|
|
173
|
+
*
|
|
174
|
+
* @param signer
|
|
175
|
+
* @param token
|
|
176
|
+
* @param amount
|
|
177
|
+
* @param feeRate
|
|
178
|
+
*/
|
|
179
|
+
public async txsDeposit(signer: PublicKey, token: PublicKey, amount: bigint, feeRate?: string): Promise<SolanaTx[]> {
|
|
180
|
+
const ata = getAssociatedTokenAddressSync(token, signer);
|
|
181
|
+
|
|
182
|
+
feeRate = feeRate || await this.getFeeRate(signer, token);
|
|
183
|
+
|
|
184
|
+
const action = new SolanaAction(signer, this.root);
|
|
185
|
+
|
|
186
|
+
let wrapping: boolean = false;
|
|
187
|
+
if(token.equals(SolanaTokens.WSOL_ADDRESS)) {
|
|
188
|
+
const account = await tryWithRetries<Account>(
|
|
189
|
+
() => this.root.Tokens.getATAOrNull(ata),
|
|
190
|
+
this.retryPolicy
|
|
191
|
+
);
|
|
192
|
+
let balance: bigint = account==null ? 0n : account.amount;
|
|
193
|
+
if(balance < amount) {
|
|
194
|
+
action.add(this.root.Tokens.Wrap(signer, amount - balance, account==null));
|
|
195
|
+
wrapping = true;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
action.addAction(await this.Deposit(signer, token, amount));
|
|
199
|
+
|
|
200
|
+
this.logger.debug("txsDeposit(): deposit TX created, token: "+token.toString()+
|
|
201
|
+
" amount: "+amount.toString(10)+" wrapping: "+wrapping);
|
|
202
|
+
|
|
203
|
+
return [await action.tx(feeRate)];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public getFeeRate(signer: PublicKey, token: PublicKey) {
|
|
207
|
+
const ata = getAssociatedTokenAddressSync(token, signer);
|
|
208
|
+
return this.root.Fees.getFeeRate([
|
|
209
|
+
signer,
|
|
210
|
+
ata,
|
|
211
|
+
this.program.SwapUserVault(signer, token),
|
|
212
|
+
this.program.SwapVault(token)
|
|
213
|
+
])
|
|
214
|
+
}
|
|
215
|
+
|
|
216
216
|
}
|