Skip to content

Commit

Permalink
feat: deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
bigearsenal committed Jul 19, 2023
1 parent 0228adc commit 69909f4
Show file tree
Hide file tree
Showing 77 changed files with 214 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Foundation
import SolanaSwift

/// A basic class that represents SPL Token.
/// A basic class that represents SPL TokenMetadata.
public struct TokenAccount: Equatable, Codable {
public init(address: PublicKey, mint: PublicKey) {
self.address = address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class TransitTokenAccountManagerImpl: TransitTokenAccountManager {
public func checkIfNeedsCreateTransitTokenAccount(transitToken: TokenAccount?) async throws -> Bool? {
guard let transitToken = transitToken else { return nil }

guard let account: BufferInfo<AccountInfo> = try await solanaAPIClient.getAccountInfo(account: transitToken.address.base58EncodedString) else {
guard let account: BufferInfo<SPLTokenAccountState> = try await solanaAPIClient.getAccountInfo(account: transitToken.address.base58EncodedString) else {
return true
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation
import SolanaSwift
import OrcaSwapSwift
import SolanaSwift

extension SwapTransactionBuilderImpl {
func checkDestination(
Expand All @@ -15,14 +15,14 @@ extension SwapTransactionBuilderImpl {
// return the address
output.userDestinationTokenAccountAddress = destinationAddress
}

// else, find real destination
else {
let result = try await destinationAnalysator.analyseDestination(
owner: owner.publicKey,
mint: destinationMint
)

switch result {
case .wsolAccount:
// For native solana, create and initialize WSOL
Expand All @@ -32,7 +32,7 @@ extension SwapTransactionBuilderImpl {
from: feePayerAddress,
toNewPubkey: destinationNewAccount.publicKey,
lamports: minimumTokenAccountBalance,
space: AccountInfo.BUFFER_LENGTH,
space: SPLTokenAccountState.BUFFER_LENGTH,
programId: TokenProgram.id
),
TokenProgram.initializeAccountInstruction(
Expand All @@ -41,26 +41,27 @@ extension SwapTransactionBuilderImpl {
owner: owner.publicKey
),
])

// return the address
output.userDestinationTokenAccountAddress = destinationNewAccount.publicKey
output.accountCreationFee += minimumTokenAccountBalance
output.destinationNewAccount = destinationNewAccount
case .splAccount(let needsCreation):
case let .splAccount(needsCreation):
// For other token, get associated token address
let associatedAddress = try PublicKey.associatedTokenAddress(
walletAddress: owner.publicKey,
tokenMintAddress: destinationMint
)

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

// SPECIAL CASE WHEN WE SWAP FROM SOL TO NON-CREATED SPL TOKEN, THEN WE NEEDS ADDITIONAL TRANSACTION BECAUSE TRANSACTION IS TOO LARGE
// SPECIAL CASE WHEN WE SWAP FROM SOL TO NON-CREATED SPL TOKEN, THEN WE NEEDS ADDITIONAL TRANSACTION
// BECAUSE TRANSACTION IS TOO LARGE
if output.sourceWSOLNewAccount != nil {
output.additionalTransaction = try makeTransaction(
instructions: [instruction],
Expand All @@ -73,7 +74,7 @@ extension SwapTransactionBuilderImpl {
output.accountCreationFee += minimumTokenAccountBalance
}
}

// return the address
output.userDestinationTokenAccountAddress = associatedAddress
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension SwapTransactionBuilderImpl {
from: feePayerAddress,
toNewPubkey: sourceWSOLNewAccount!.publicKey,
lamports: minimumTokenAccountBalance + inputAmount,
space: AccountInfo.BUFFER_LENGTH,
space: SPLTokenAccountState.BUFFER_LENGTH,
programId: TokenProgram.id
),
TokenProgram.initializeAccountInstruction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,31 @@ public struct HistoryTransaction: Identifiable, Codable {
do {
switch type {
case .send:
info = .send(try container.decode(Transfer.self, forKey: .info))
info = try .send(container.decode(Transfer.self, forKey: .info))
case .receive:
info = .receive(try container.decode(Transfer.self, forKey: .info))
info = try .receive(container.decode(Transfer.self, forKey: .info))
case .swap:
info = .swap(try container.decode(Swap.self, forKey: .info))
info = try .swap(container.decode(Swap.self, forKey: .info))
case .stake:
info = .stake(try container.decode(Transfer.self, forKey: .info))
info = try .stake(container.decode(Transfer.self, forKey: .info))
case .unstake:
info = .unstake(try container.decode(Transfer.self, forKey: .info))
info = try .unstake(container.decode(Transfer.self, forKey: .info))
case .createAccount:
info = .createAccount(try container.decode(TokenAmount.self, forKey: .info))
info = try .createAccount(container.decode(TokenAmount.self, forKey: .info))
case .closeAccount:
info = .closeAccount(try container.decode(TokenAmount.self, forKey: .info))
info = try .closeAccount(container.decode(TokenAmount.self, forKey: .info))
case .mint:
info = .mint(try container.decode(TokenAmount.self, forKey: .info))
info = try .mint(container.decode(TokenAmount.self, forKey: .info))
case .burn:
info = .burn(try container.decode(TokenAmount.self, forKey: .info))
info = try .burn(container.decode(TokenAmount.self, forKey: .info))
case .wormholeSend:
info = .wormholeSend(try container.decode(WormholeSend.self, forKey: .info))
info = try .wormholeSend(container.decode(WormholeSend.self, forKey: .info))
case .wormholeReceive:
info = .wormholeReceive(try container.decode(WormholeReceive.self, forKey: .info))
info = try .wormholeReceive(container.decode(WormholeReceive.self, forKey: .info))
case .tryCreateAccount:
info = .tryCreateAccount
case .unknown:
info = .unknown(try container.decode(TokenAmount.self, forKey: .info))
info = try .unknown(container.decode(TokenAmount.self, forKey: .info))
}
} catch {
info = nil
Expand Down Expand Up @@ -238,7 +238,7 @@ public extension HistoryTransaction {
name = try container.decode(String.self, forKey: .name)
mint = try container.decode(String.self, forKey: .mint)
logoUrl = try? container.decodeIfPresent(URL.self, forKey: .logoUrl)
usdRate = Double(try container.decodeIfPresent(String.self, forKey: .usdRate) ?? "") ?? 0.0
usdRate = try Double(container.decodeIfPresent(String.self, forKey: .usdRate) ?? "") ?? 0.0
coingeckoId = try container.decodeIfPresent(String.self, forKey: .coingeckoId)
decimals = try container.decode(UInt8.self, forKey: .decimals)
}
Expand Down
2 changes: 1 addition & 1 deletion Packages/KeyAppKit/Sources/Jupiter/JupiterAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum SwapMode: String {
}

public protocol JupiterAPI {
func getTokens() async throws -> [Token]
func getTokens() async throws -> [TokenMetadata]

func quote(
inputMint: String,
Expand Down
4 changes: 2 additions & 2 deletions Packages/KeyAppKit/Sources/Jupiter/JupiterRestClientAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class JupiterRestClientAPI: JupiterAPI {
self.tokensHost = tokensHost
}

public func getTokens() async throws -> [Token] {
public func getTokens() async throws -> [TokenMetadata] {
let (data, _) = try await URLSession.shared.data(from: URL(string: "\(tokensHost ?? host)/tokens")!)
return try JSONDecoder().decode([Token].self, from: data)
return try JSONDecoder().decode([TokenMetadata].self, from: data)
}

public func quote(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public struct SolanaAccount: Identifiable, Equatable, Hashable {
}

@available(*, deprecated)
public init(pubkey: String? = nil, lamports: Lamports? = nil, token: Token) {
public init(pubkey: String? = nil, lamports: Lamports? = nil, token: TokenMetadata) {
address = pubkey ?? ""
self.lamports = lamports ?? 0
self.token = token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct ICloudAccount: Codable, Hashable {
self.phrase = phrase
self.derivablePath = derivablePath

let account = try await Account(
let account = try await KeyPair(
phrase: phrase.components(separatedBy: " "),
network: .mainnetBeta,
derivablePath: derivablePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public enum RestoreICloudState: Codable, State, Equatable {
let rawAccounts = try await provider.icloudAccountProvider.getAll()
var accounts: [ICloudAccount] = []
for rawAccount in rawAccounts {
accounts
.append(try await .init(
try accounts
.append(await .init(
name: rawAccount.name,
phrase: rawAccount.phrase,
derivablePath: rawAccount.derivablePath
Expand Down Expand Up @@ -76,8 +76,8 @@ public enum RestoreICloudState: Codable, State, Equatable {
}
}

private func account(from icloudAccount: ICloudAccount) async throws -> Account {
let account = try await Account(
private func account(from icloudAccount: ICloudAccount) async throws -> KeyPair {
let account = try await KeyPair(
phrase: icloudAccount.phrase.components(separatedBy: " "),
network: .mainnetBeta,
derivablePath: icloudAccount.derivablePath
Expand Down
15 changes: 7 additions & 8 deletions Packages/KeyAppKit/Sources/Send/Action/SendAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class SendActionServiceImpl: SendActionService {
operationType: StatsInfo.OperationType
) async throws -> String {
let amount = amount.toLamport(decimals: wallet.token.decimals)
guard let sender = wallet.pubkey else { throw SendError.invalidSourceWallet }
let sender = wallet.address

// assert payingFeeWallet
if !ignoreTopUp && feeWallet == nil {
Expand Down Expand Up @@ -157,7 +157,7 @@ public class SendActionServiceImpl: SendActionService {
memo: String?
) async throws -> (preparedTransaction: PreparedTransaction, useFeeRelayer: Bool) {
let amount = amount.toLamport(decimals: wallet.token.decimals)
guard let sender = wallet.pubkey else { throw SendError.invalidSourceWallet }
let sender = wallet.address
guard let account = account else { throw SolanaError.unauthorized }
guard let context = contextManager.currentContext else { throw RelayContextManagerError.invalidContext }
// prepare fee payer
Expand Down Expand Up @@ -201,8 +201,8 @@ public class SendActionServiceImpl: SendActionService {

// add memo
if let memo {
preparedTransaction.transaction.instructions.append(
try MemoProgram.createMemoInstruction(memo: memo)
try preparedTransaction.transaction.instructions.append(
MemoProgram.createMemoInstruction(memo: memo)
)
}

Expand All @@ -222,10 +222,9 @@ public class SendActionServiceImpl: SendActionService {

private func getPayingFeeToken(feeWallet: SolanaAccount?) throws -> FeeRelayerSwift.TokenAccount? {
if let feeWallet = feeWallet {
guard
let addressString = feeWallet.pubkey,
let address = try? PublicKey(string: addressString),
let mintAddress = try? PublicKey(string: feeWallet.token.address)
let addressString = feeWallet.address
guard let address = try? PublicKey(string: addressString),
let mintAddress = try? PublicKey(string: feeWallet.token.address)
else {
throw SendError.invalidPayingFeeWallet
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SolanaSwift
extension SendInputBusinessLogic {
static func changeToken(
state: SendInputState,
token: Token,
token: TokenMetadata,
services: SendInputServices
) async -> SendInputState {
guard let feeRelayerContext = state.feeRelayerContext else {
Expand Down Expand Up @@ -85,9 +85,9 @@ extension SendInputBusinessLogic {
static func autoSelectTokenFee(
userWallets: [SolanaAccount],
feeInSol: FeeAmount,
token: Token,
token: TokenMetadata,
services: SendInputServices
) async -> (token: Token, fee: FeeAmount?) {
) async -> (token: TokenMetadata, fee: FeeAmount?) {
var preferOrder = ["SOL": 2]
if !preferOrder.keys.contains(token.symbol) {
preferOrder[token.symbol] = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SolanaSwift
extension SendInputBusinessLogic {
static func changeFeeToken(
state: SendInputState,
feeToken: Token,
feeToken: TokenMetadata,
services: SendInputServices
) async -> SendInputState {
guard let feeRelayerContext = state.feeRelayerContext else {
Expand Down
24 changes: 12 additions & 12 deletions Packages/KeyAppKit/Sources/Send/Input/SendInputState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public enum SendInputAction: Equatable {

case changeAmountInFiat(Double)
case changeAmountInToken(Double)
case changeUserToken(Token)
case changeFeeToken(Token)
case changeUserToken(TokenMetadata)
case changeFeeToken(TokenMetadata)
}

public struct SendInputServices {
Expand Down Expand Up @@ -81,11 +81,11 @@ public struct SendInputState: Equatable {
public let walletAccount: BufferInfo<SolanaAddressInfo>?

/// Usable when recipient category is ``Recipient.Category.solanaAddress``
public let splAccounts: [SolanaSwift.TokenAccount<AccountInfo>]
public let splAccounts: [SolanaSwift.TokenAccount<SPLTokenAccountState>]

public init(
walletAccount: BufferInfo<SolanaAddressInfo>?,
splAccounts: [SolanaSwift.TokenAccount<AccountInfo>]
splAccounts: [SolanaSwift.TokenAccount<SPLTokenAccountState>]
) {
self.walletAccount = walletAccount
self.splAccounts = splAccounts
Expand All @@ -101,7 +101,7 @@ public struct SendInputState: Equatable {

public let recipient: Recipient
public let recipientAdditionalInfo: RecipientAdditionalInfo
public let token: Token
public let token: TokenMetadata
public let userWalletEnvironments: UserWalletEnvironments

public let amountInFiat: Double
Expand All @@ -111,7 +111,7 @@ public struct SendInputState: Equatable {
public let fee: FeeAmount

/// Selected fee token
public let tokenFee: Token
public let tokenFee: TokenMetadata

/// Amount fee in Token (Converted from amount fee in SOL)
public let feeInToken: FeeAmount
Expand All @@ -131,12 +131,12 @@ public struct SendInputState: Equatable {
status: Status,
recipient: Recipient,
recipientAdditionalInfo: RecipientAdditionalInfo,
token: Token,
token: TokenMetadata,
userWalletEnvironments: UserWalletEnvironments,
amountInFiat: Double,
amountInToken: Double,
fee: FeeAmount,
tokenFee: Token,
tokenFee: TokenMetadata,
feeInToken: FeeAmount,
feeRelayerContext: RelayContext?,
sendViaLinkSeed: String?
Expand All @@ -159,8 +159,8 @@ public struct SendInputState: Equatable {
status: Status = .requiredInitialize,
recipient: Recipient,
recipientAdditionalInfo: RecipientAdditionalInfo = .zero,
token: Token,
feeToken: Token,
token: TokenMetadata,
feeToken: TokenMetadata,
userWalletState: UserWalletEnvironments,
feeRelayerContext: RelayContext? = nil,
sendViaLinkSeed: String?
Expand All @@ -185,12 +185,12 @@ public struct SendInputState: Equatable {
status: Status? = nil,
recipient: Recipient? = nil,
recipientAdditionalInfo: RecipientAdditionalInfo? = nil,
token: Token? = nil,
token: TokenMetadata? = nil,
userWalletEnvironments: UserWalletEnvironments? = nil,
amountInFiat: Double? = nil,
amountInToken: Double? = nil,
fee: FeeAmount? = nil,
tokenFee: Token? = nil,
tokenFee: TokenMetadata? = nil,
feeInToken: FeeAmount? = nil,
feeRelayerContext: RelayContext? = nil,
sendViaLinkSeed: String?? = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SolanaSwift

public protocol SendFeeCalculator: AnyObject {
func getFees(
from token: Token,
from token: TokenMetadata,
recipient: Recipient,
recipientAdditionalInfo: SendInputState.RecipientAdditionalInfo,
payingTokenMint: String?,
Expand All @@ -20,7 +20,7 @@ public class SendFeeCalculatorImpl: SendFeeCalculator {
// MARK: - Fees calculator

public func getFees(
from token: Token,
from token: TokenMetadata,
recipient: Recipient,
recipientAdditionalInfo: SendInputState.RecipientAdditionalInfo,
payingTokenMint: String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct Recipient: Hashable, Codable {
case username(name: String, domain: String)

case solanaAddress
case solanaTokenAddress(walletAddress: PublicKey, token: Token)
case solanaTokenAddress(walletAddress: PublicKey, token: TokenMetadata)

case bitcoinAddress
case ethereumAddress
Expand Down
Loading

0 comments on commit 69909f4

Please sign in to comment.