Skip to content

Commit

Permalink
Merge branch 'release/2.11.0' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bigearsenal committed Jan 17, 2024
2 parents a761fd6 + 43f3e9a commit 8f20a8d
Show file tree
Hide file tree
Showing 96 changed files with 1,068 additions and 686 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1520"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "KeyAppNetworkingTests"
BuildableName = "KeyAppNetworkingTests"
BlueprintName = "KeyAppNetworkingTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
2 changes: 1 addition & 1 deletion Packages/KeyAppKit/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/p2p-org/solana-swift", branch: "main"),
.package(url: "https://github.com/p2p-org/solana-swift", branch: "feature/token-2022"),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMajor(from: "1.6.0")),
.package(url: "https://github.com/Boilertalk/Web3.swift.git", from: "0.6.0"),
// .package(url: "https://github.com/trustwallet/wallet-core", branch: "master"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ public enum KeyAppAnalyticsEvent: AnalyticsEvent {
case settingsSupportClick
case settingsRecoveryClick
case settingsPinClick
case settingsNetworkClick
case settingsFaceidClick
case settingsLogOut

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ extension SolanaAPIClient {

// The account doesn't exists
if account == nil {
return try PublicKey.associatedTokenAddress(walletAddress: address, tokenMintAddress: mint)
return try PublicKey.associatedTokenAddress(
walletAddress: address,
tokenMintAddress: mint,
tokenProgramId: TokenProgram.id
)
}

// The account is already token account
Expand All @@ -28,7 +32,11 @@ extension SolanaAPIClient {
guard account?.owner != SystemProgram.id.base58EncodedString else {
throw FeeRelayerError.wrongAddress
}
return try PublicKey.associatedTokenAddress(walletAddress: address, tokenMintAddress: mint)
return try PublicKey.associatedTokenAddress(
walletAddress: address,
tokenMintAddress: mint,
tokenProgramId: TokenProgram.id
)
}

func isAccountExists(_ address: PublicKey) async throws -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ import SolanaSwift

/// A basic class that represents SPL TokenMetadata.
public struct TokenAccount: Equatable, Codable {
public init(address: PublicKey, mint: PublicKey) {
public init(
address: PublicKey,
mint: PublicKey,
minimumTokenAccountBalance: UInt64
) {
self.address = address
self.mint = mint
self.minimumTokenAccountBalance = minimumTokenAccountBalance
}

/// A address of spl token.
public let address: PublicKey

/// A mint address for spl token.
public let mint: PublicKey

/// Mint rent for token
public let minimumTokenAccountBalance: UInt64
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ import Foundation
import SolanaSwift

public struct RelayContext: Hashable, Codable {
public let minimumTokenAccountBalance: UInt64
public let minimumRelayAccountBalance: UInt64
public let feePayerAddress: PublicKey
public let lamportsPerSignature: UInt64
public let relayAccountStatus: RelayAccountStatus
public var usageStatus: UsageStatus

public init(
minimumTokenAccountBalance: UInt64,
minimumRelayAccountBalance: UInt64,
feePayerAddress: PublicKey,
lamportsPerSignature: UInt64,
relayAccountStatus: RelayAccountStatus,
usageStatus: UsageStatus
) {
self.minimumTokenAccountBalance = minimumTokenAccountBalance
self.minimumRelayAccountBalance = minimumRelayAccountBalance
self.feePayerAddress = feePayerAddress
self.lamportsPerSignature = lamportsPerSignature
Expand All @@ -26,7 +23,6 @@ public struct RelayContext: Hashable, Codable {
}

public func hash(into hasher: inout Hasher) {
hasher.combine(minimumTokenAccountBalance)
hasher.combine(minimumRelayAccountBalance)
hasher.combine(feePayerAddress)
hasher.combine(lamportsPerSignature)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,29 @@ public class RelayContextManagerImpl: RelayContextManager {
else { throw RelayContextManagerError.invalidContext }

// retrieve RelayContext
let (
minimumTokenAccountBalance,
minimumRelayAccountBalance,
lamportsPerSignature,
feePayerAddress,
relayAccountStatus,
usageStatus
) = try await(
solanaAPIClient.getMinimumBalanceForRentExemption(span: 165),
solanaAPIClient.getMinimumBalanceForRentExemption(span: 0),
solanaAPIClient.getFees(commitment: nil).feeCalculator?.lamportsPerSignature ?? 0,
feeRelayerAPIClient.getFeePayerPubkey(),
solanaAPIClient.getRelayAccountStatus(
RelayProgram.getUserRelayAddress(user: account.publicKey, network: solanaAPIClient.endpoint.network)
async let minimumRelayAccountBalance = solanaAPIClient
.getMinimumBalanceForRentExemption(span: 0)
async let lamportsPerSignature = solanaAPIClient
.getFees(commitment: nil).feeCalculator?
.lamportsPerSignature
async let feePayerAddress = feeRelayerAPIClient.getFeePayerPubkey()
async let relayAccountStatus = solanaAPIClient
.getRelayAccountStatus(
RelayProgram
.getUserRelayAddress(
user: account.publicKey,
network: solanaAPIClient.endpoint.network
)
.base58EncodedString
),
feeRelayerAPIClient.getFreeFeeLimits(for: account.publicKey.base58EncodedString)
.asUsageStatus()
)
)
async let usageStatus = feeRelayerAPIClient
.getFreeFeeLimits(for: account.publicKey.base58EncodedString)
.asUsageStatus()

return try RelayContext(
minimumTokenAccountBalance: minimumTokenAccountBalance,
return try await RelayContext(
minimumRelayAccountBalance: minimumRelayAccountBalance,
feePayerAddress: PublicKey(string: feePayerAddress),
lamportsPerSignature: lamportsPerSignature,
lamportsPerSignature: lamportsPerSignature ?? 5000,
relayAccountStatus: relayAccountStatus,
usageStatus: usageStatus
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class DestinationAnalysatorImpl: DestinationAnalysator {
// Check destination address is exist.
let info: BufferInfo<SPLTokenAccountState>? = try? await solanaAPIClient
.getAccountInfo(account: address.base58EncodedString)
let needsCreateDestinationTokenAccount = info?.owner != TokenProgram.id.base58EncodedString
let needsCreateDestinationTokenAccount = !PublicKey.isSPLTokenOrToken2022ProgramId(info?.owner)

return .splAccount(needsCreation: needsCreateDestinationTokenAccount)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class TransitTokenAccountManagerImpl: TransitTokenAccountManager {

return TokenAccount(
address: transitTokenAccountAddress,
mint: transitTokenMintPubkey
mint: transitTokenMintPubkey,
minimumTokenAccountBalance: getTransitTokenMintRentExemption(pools: pools) ?? 2_039_280
)
}

Expand All @@ -39,6 +40,11 @@ public class TransitTokenAccountManagerImpl: TransitTokenAccountManager {
return try PublicKey(string: orcaSwap.getMint(tokenName: interTokenName))
}

func getTransitTokenMintRentExemption(pools: PoolsPair) -> UInt64? {
guard pools.count == 2 else { return nil }
return pools[0].tokenBMinimumBalanceForRentExemption
}

public func checkIfNeedsCreateTransitTokenAccount(transitToken: TokenAccount?) async throws -> Bool? {
guard let transitToken = transitToken else { return nil }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ extension SwapTransactionBuilderImpl {
// For other token, get associated token address
let associatedAddress = try PublicKey.associatedTokenAddress(
walletAddress: owner.publicKey,
tokenMintAddress: destinationMint
tokenMintAddress: destinationMint,
tokenProgramId: TokenProgram.id
)

if needsCreation {
let instruction = try AssociatedTokenProgram.createAssociatedTokenAccountInstruction(
mint: destinationMint,
owner: owner.publicKey,
payer: feePayerAddress
payer: feePayerAddress,
tokenProgramId: TokenProgram.id
)

// SPECIAL CASE WHEN WE SWAP FROM SOL TO NON-CREATED SPL TOKEN, THEN WE NEEDS ADDITIONAL TRANSACTION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public class SwapTransactionBuilderImpl: SwapTransactionBuilder {
// assert userSource
let associatedToken = try PublicKey.associatedTokenAddress(
walletAddress: feePayerAddress,
tokenMintAddress: sourceTokenAccount.mint
tokenMintAddress: sourceTokenAccount.mint,
tokenProgramId: TokenProgram.id
)
guard output.userSource != associatedToken else { throw FeeRelayerError.wrongAddress }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class TopUpTransactionBuilderImpl: TopUpTransactionBuilder {
let feePayerAddress = context.feePayerAddress
let associatedTokenAddress = try PublicKey.associatedTokenAddress(
walletAddress: feePayerAddress,
tokenMintAddress: sourceTokenMintAddress
tokenMintAddress: sourceTokenMintAddress,
tokenProgramId: TokenProgram.id
) ?! FeeRelayerError.unknown
let network = solanaApiClient.endpoint.network

Expand Down Expand Up @@ -102,7 +103,7 @@ class TopUpTransactionBuilderImpl: TopUpTransactionBuilder {

switch swap.swapData {
case let swap as DirectSwapData:
expectedFee.accountBalances += context.minimumTokenAccountBalance
expectedFee.accountBalances += sourceToken.minimumTokenAccountBalance
// approve
if let userTransferAuthority = userTransferAuthority {
instructions.append(
Expand Down Expand Up @@ -154,7 +155,7 @@ class TopUpTransactionBuilderImpl: TopUpTransactionBuilder {
}

// Destination WSOL account funding
expectedFee.accountBalances += context.minimumTokenAccountBalance
expectedFee.accountBalances += sourceToken.minimumTokenAccountBalance

// top up
try instructions.append(
Expand Down
30 changes: 15 additions & 15 deletions Packages/KeyAppKit/Sources/KeyAppBusiness/Price/PriceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ protocol PriceRule {

// Make price rate equals 1:1 when `ruleOfProcessingTokenPrice` equals `byCountOfTokensValue`.
class OneToOnePriceRule: PriceRule {
func adjustValue(token: SomeToken, price: TokenPrice, fiat _: String) -> PriceRuleHandler {
if token.keyAppExtension.ruleOfProcessingTokenPriceWS == .byCountOfTokensValue {
return .continue(TokenPrice(currencyCode: price.currencyCode, value: 1.0, token: token))
}
func adjustValue(token _: SomeToken, price: TokenPrice, fiat _: String) -> PriceRuleHandler {
// if token.keyAppExtension.ruleOfProcessingTokenPriceWS == .byCountOfTokensValue {
// return .continue(TokenPrice(currencyCode: price.currencyCode, value: 1.0, token: token))
// }

return .continue(price)
.continue(price)
}
}

// Depegging price by measure percentage difference.
class DepeggingPriceRule: PriceRule {
func adjustValue(token: SomeToken, price: TokenPrice, fiat _: String) -> PriceRuleHandler {
if let allowPercentageDifferenceValue = token.keyAppExtension.percentDifferenceToShowByPriceOnWS {
let percentageDifferenceValue = 100 - (1 / price.value) * 100
if abs(percentageDifferenceValue) <= BigDecimal(floatLiteral: allowPercentageDifferenceValue) {
return .break(TokenPrice(currencyCode: price.currencyCode, value: 1.0, token: token))
} else {
return .break(price)
}
}
func adjustValue(token _: SomeToken, price: TokenPrice, fiat _: String) -> PriceRuleHandler {
// if let allowPercentageDifferenceValue = token.keyAppExtension.percentDifferenceToShowByPriceOnWS {
// let percentageDifferenceValue = 100 - (1 / price.value) * 100
// if abs(percentageDifferenceValue) <= BigDecimal(floatLiteral: allowPercentageDifferenceValue) {
// return .break(TokenPrice(currencyCode: price.currencyCode, value: 1.0, token: token))
// } else {
// return .break(price)
// }
// }

return .continue(price)
.continue(price)
}
}
Loading

0 comments on commit 8f20a8d

Please sign in to comment.