@@ -14,7 +14,6 @@ enum Solana {
1414
1515 static let lamportsPerSOL = Decimal ( SOLANA_LAMPORTS_PER_SOL)
1616 static let microLamportsPerLamport : Decimal = 1_000_000
17- static let accountCreationCost : Decimal = 0.002_039_28
1817 static let keyPairCount = 64
1918
2019 static func publicKey( seed: Data ) throws -> String {
@@ -154,8 +153,7 @@ extension Solana {
154153 priorityFee: PriorityFee ? ,
155154 token: Web3Token
156155 ) throws {
157- let isSendingSOL = token. chainID == ChainID . solana
158- && ( token. assetKey == Web3Token . AssetKey. sol || token. assetKey == Web3Token . AssetKey. wrappedSOL)
156+ let isSendingSOL = token. chainID == ChainID . solana && token. assetKey == Web3Token . AssetKey. sol
159157 let solanaPriorityFee : SolanaPriorityFee ? = if let fee = priorityFee {
160158 SolanaPriorityFee ( price: fee. unitPrice, limit: fee. unitLimit)
161159 } else {
@@ -243,3 +241,77 @@ extension Solana {
243241 }
244242
245243}
244+
245+ extension Solana {
246+
247+ enum RentExemptionFailedReason {
248+
249+ case reserveSOLForRent( Decimal )
250+ case sendSOLForRent( Decimal )
251+ case insufficientSOL( requiredAmount: Decimal )
252+
253+ var localizedDescription : String {
254+ switch self {
255+ case . reserveSOLForRent( let amount) :
256+ R . string. localizable. reserve_sol_for_rent (
257+ CurrencyFormatter . localizedString (
258+ from: amount,
259+ format: . precision,
260+ sign: . never
261+ )
262+ )
263+ case . sendSOLForRent( let amount) :
264+ R . string. localizable. send_sol_for_rent (
265+ CurrencyFormatter . localizedString (
266+ from: amount,
267+ format: . precision,
268+ sign: . never
269+ )
270+ )
271+ case . insufficientSOL( let requiredAmount) :
272+ R . string. localizable. insufficient_sol_for_sending_spl_token (
273+ CurrencyFormatter . localizedString (
274+ from: requiredAmount,
275+ format: . precision,
276+ sign: . never
277+ )
278+ )
279+ }
280+ }
281+
282+ }
283+
284+ enum RentExemptionValue {
285+ static let systemAccount : Decimal = 0.00089088
286+ static let tokenAccount : Decimal = 0.00203928
287+ }
288+
289+ static func checkRentExemptionForSOLTransfer(
290+ sendingAmount: Decimal ,
291+ feeAmount: Decimal ,
292+ senderSOLBalance: Decimal ,
293+ receiverAccountExists: Bool
294+ ) -> RentExemptionFailedReason ? {
295+ if senderSOLBalance - sendingAmount - feeAmount < RentExemptionValue . systemAccount {
296+ . reserveSOLForRent( RentExemptionValue . systemAccount)
297+ } else if !receiverAccountExists && sendingAmount < RentExemptionValue . tokenAccount {
298+ . sendSOLForRent( RentExemptionValue . tokenAccount)
299+ } else {
300+ nil
301+ }
302+ }
303+
304+ static func checkRentExemptionForSPLTokenTransfer(
305+ senderSOLBalance: Decimal ,
306+ feeAmount: Decimal ,
307+ receiverAccountExists: Bool
308+ ) -> RentExemptionFailedReason ? {
309+ let minBalance = RentExemptionValue . systemAccount + RentExemptionValue. tokenAccount + feeAmount
310+ if receiverAccountExists || senderSOLBalance >= minBalance {
311+ return nil
312+ } else {
313+ return . insufficientSOL( requiredAmount: minBalance)
314+ }
315+ }
316+
317+ }
0 commit comments