From 8babbb7253af8cd6916f9a7861b98176e7fbb7f0 Mon Sep 17 00:00:00 2001 From: Chung Tran Date: Tue, 9 Jan 2024 15:44:36 +0700 Subject: [PATCH] feat: some fixes --- .../APIClient/APIClient+Extension.swift | 34 ++++++++----------- .../Networking/JSONRPCAPIClient.swift | 8 ++--- .../APIClient/SolanaAPIClient.swift | 6 ++-- .../BlockchainClient+Extensions.swift | 28 +++++---------- .../BlockchainClient/BlockchainClient.swift | 20 +++++------ .../PublicKey/PublicKey+Validation.swift | 8 +++++ Sources/SolanaSwift/Models/PublicKey.swift | 16 ++------- .../Programs/AssociatedTokenProgram.swift | 12 ++++--- .../Token2022AccountState.swift | 2 +- .../Token2022Program/Token2022MintState.swift | 2 +- ...untState.swift => TokenAccountState.swift} | 4 +-- ...enMintState.swift => TokenMintState.swift} | 4 +-- .../TokenProgram/SPLTokenAccountState.swift | 2 +- .../TokenProgram/SPLTokenMintState.swift | 2 +- .../SolanaAPIClient+getAccountBalances.swift | 14 ++++---- .../APIClient/APIClientExtensionsTests.swift | 9 +++-- .../BlockchainClientWithNativeSOLTests.swift | 6 ++-- ...lockchainClientWithTokenProgramTests.swift | 10 +++--- .../BlockchainClient/FeeCalculatorTests.swift | 19 ++++++----- .../BufferLayout/BufferLayoutTests.swift | 2 +- .../AssociatedTokenProgramTests.swift | 6 ++-- 21 files changed, 101 insertions(+), 113 deletions(-) create mode 100644 Sources/SolanaSwift/Extensions/PublicKey/PublicKey+Validation.swift rename Sources/SolanaSwift/Programs/TokenPrograms/{SolanaSPLTokenAccountState.swift => TokenAccountState.swift} (93%) rename Sources/SolanaSwift/Programs/TokenPrograms/{SolanaSPLTokenMintState.swift => TokenMintState.swift} (90%) diff --git a/Sources/SolanaSwift/APIClient/APIClient+Extension.swift b/Sources/SolanaSwift/APIClient/APIClient+Extension.swift index 2c1ae698d..c2789bfcf 100644 --- a/Sources/SolanaSwift/APIClient/APIClient+Extension.swift +++ b/Sources/SolanaSwift/APIClient/APIClient+Extension.swift @@ -5,19 +5,6 @@ import Foundation public extension SolanaAPIClient { // MARK: - Convenience methods - func getTokenAccountsByOwner( - pubkey: String, - params: OwnerInfoParams?, - configs: RequestConfiguration? - ) async throws -> [TokenAccount] { - try await getTokenAccountsByOwner( - pubkey: pubkey, - params: params, - configs: configs, - decodingTo: SPLTokenAccountState.self - ) - } - func getMinimumBalanceForRentExemption(span: UInt64) async throws -> UInt64 { try await getMinimumBalanceForRentExemption(dataLength: span, commitment: "recent") } @@ -40,7 +27,7 @@ public extension SolanaAPIClient { try await request(method: method, params: []) } - func getMultipleMintDatas( + func getMultipleMintDatas( mintAddresses: [String], commitment: Commitment, mintType _: M.Type @@ -88,13 +75,15 @@ public extension SolanaAPIClient { func checkIfAssociatedTokenAccountExists( owner: PublicKey, - mint: String + mint: String, + tokenProgramId: PublicKey ) async throws -> Bool { let mintAddress = try mint.toPublicKey() let associatedTokenAccount = try PublicKey.associatedTokenAddress( walletAddress: owner, - tokenMintAddress: mintAddress + tokenMintAddress: mintAddress, + tokenProgramId: tokenProgramId ) let bufferInfo: BufferInfo? = try await getAccountInfo(account: associatedTokenAccount @@ -114,7 +103,8 @@ public extension SolanaAPIClient { func findSPLTokenDestinationAddress( mintAddress: String, - destinationAddress: String + destinationAddress: String, + tokenProgramId: PublicKey ) async throws -> SPLTokenDestinationAddress { var address: String var accountInfo: BufferInfo? @@ -131,7 +121,8 @@ public extension SolanaAPIClient { // create associated token address address = try PublicKey.associatedTokenAddress( walletAddress: owner, - tokenMintAddress: tokenMint + tokenMintAddress: tokenMint, + tokenProgramId: tokenProgramId ).base58EncodedString } else { throw PublicKeyError.invalidAddress(destinationAddress) @@ -142,7 +133,8 @@ public extension SolanaAPIClient { // create associated token address address = try PublicKey.associatedTokenAddress( walletAddress: owner, - tokenMintAddress: tokenMint + tokenMintAddress: tokenMint, + tokenProgramId: tokenProgramId ).base58EncodedString } catch { throw error @@ -163,7 +155,9 @@ public extension SolanaAPIClient { isUnregisteredAsocciatedToken = true // if associated token account has been registered - if info?.owner == TokenProgram.id.base58EncodedString, info?.data != nil { + if PublicKey.isSPLTokenOrToken2022ProgramId(info?.owner), + info?.data != nil + { isUnregisteredAsocciatedToken = false } } diff --git a/Sources/SolanaSwift/APIClient/Networking/JSONRPCAPIClient.swift b/Sources/SolanaSwift/APIClient/Networking/JSONRPCAPIClient.swift index ff888ed18..525a7c7a9 100644 --- a/Sources/SolanaSwift/APIClient/Networking/JSONRPCAPIClient.swift +++ b/Sources/SolanaSwift/APIClient/Networking/JSONRPCAPIClient.swift @@ -143,13 +143,13 @@ public class JSONRPCAPIClient: SolanaAPIClient { return result.value } - public func getTokenAccountsByDelegate( + public func getTokenAccountsByDelegate( pubkey: String, mint: String? = nil, programId: String? = nil, configs: RequestConfiguration? = nil - ) async throws -> [TokenAccount] { - let result: Rpc<[TokenAccount]> = try await get( + ) async throws -> [TokenAccount] { + let result: Rpc<[TokenAccount]> = try await get( method: "getTokenAccountsByDelegate", params: [ pubkey, @@ -161,7 +161,7 @@ public class JSONRPCAPIClient: SolanaAPIClient { return result.value } - public func getTokenAccountsByOwner( + public func getTokenAccountsByOwner( pubkey: String, params: OwnerInfoParams?, configs: RequestConfiguration?, diff --git a/Sources/SolanaSwift/APIClient/SolanaAPIClient.swift b/Sources/SolanaSwift/APIClient/SolanaAPIClient.swift index f38303a9e..9229b0519 100644 --- a/Sources/SolanaSwift/APIClient/SolanaAPIClient.swift +++ b/Sources/SolanaSwift/APIClient/SolanaAPIClient.swift @@ -166,12 +166,12 @@ public protocol SolanaAPIClient { /// - Returns The result will be an array of TokenAccount /// - SeeAlso https://docs.solana.com/developing/clients/jsonrpc-api#gettokenaccountsbydelegate /// - func getTokenAccountsByDelegate( + func getTokenAccountsByDelegate( pubkey: String, mint: String?, programId: String?, configs: RequestConfiguration? - ) async throws -> [TokenAccount] + ) async throws -> [TokenAccount] /// Returns all SPL Token accounts by token owner /// - Parameters: @@ -183,7 +183,7 @@ public protocol SolanaAPIClient { /// - Returns The result will be an array of TokenAccount /// - SeeAlso https://docs.solana.com/developing/clients/jsonrpc-api#gettokenaccountsbyowner /// - func getTokenAccountsByOwner( + func getTokenAccountsByOwner( pubkey: String, params: OwnerInfoParams?, configs: RequestConfiguration?, diff --git a/Sources/SolanaSwift/BlockchainClient/BlockchainClient+Extensions.swift b/Sources/SolanaSwift/BlockchainClient/BlockchainClient+Extensions.swift index 2fa01d481..e3e349aa9 100644 --- a/Sources/SolanaSwift/BlockchainClient/BlockchainClient+Extensions.swift +++ b/Sources/SolanaSwift/BlockchainClient/BlockchainClient+Extensions.swift @@ -13,24 +13,9 @@ public extension SolanaBlockchainClient { from owner: PublicKey, amount: Lamports, payer: PublicKey, - minRentExemption mre: Lamports? + minRentExemption: Lamports ) async throws -> AccountInstructions { - let newAccount: KeyPair - let minRentExemption: Lamports - async let requestNewAccount = KeyPair(network: apiClient.endpoint.network) - - if let mre = mre { - minRentExemption = mre - newAccount = try await requestNewAccount - } else { - (minRentExemption, newAccount) = try await ( - apiClient.getMinimumBalanceForRentExemption( - dataLength: UInt64(SPLTokenAccountState.BUFFER_LENGTH), - commitment: "recent" - ), - requestNewAccount - ) - } + let newAccount = try await KeyPair(network: apiClient.endpoint.network) return .init( account: newAccount.publicKey, @@ -72,19 +57,21 @@ public extension SolanaBlockchainClient { func prepareForCreatingAssociatedTokenAccount( owner: PublicKey, mint: PublicKey, + tokenProgramId: PublicKey, feePayer: PublicKey, closeAfterward: Bool ) async throws -> AccountInstructions { let associatedAddress = try PublicKey.associatedTokenAddress( walletAddress: owner, - tokenMintAddress: mint + tokenMintAddress: mint, + tokenProgramId: tokenProgramId ) let isAssociatedTokenAddressRegistered: Bool do { let info: BufferInfo? = try await apiClient .getAccountInfo(account: associatedAddress.base58EncodedString) - if info?.owner == TokenProgram.id.base58EncodedString, + if PublicKey.isSPLTokenOrToken2022ProgramId(info?.owner), info?.data.owner == owner { isAssociatedTokenAddressRegistered = true @@ -127,7 +114,8 @@ public extension SolanaBlockchainClient { .createAssociatedTokenAccountInstruction( mint: mint, owner: owner, - payer: feePayer + payer: feePayer, + tokenProgramId: tokenProgramId ), ], cleanupInstructions: cleanupInstructions, diff --git a/Sources/SolanaSwift/BlockchainClient/BlockchainClient.swift b/Sources/SolanaSwift/BlockchainClient/BlockchainClient.swift index f1bb0d110..209bc09a8 100644 --- a/Sources/SolanaSwift/BlockchainClient/BlockchainClient.swift +++ b/Sources/SolanaSwift/BlockchainClient/BlockchainClient.swift @@ -34,7 +34,7 @@ public class BlockchainClient: SolanaBlockchainClient { if let fc = fc { feeCalculator = fc } else { - let (lps, minRentExemption) = try await ( + let (lps, minRentExemption) = try await( apiClient.getFees(commitment: nil).feeCalculator?.lamportsPerSignature, apiClient.getMinimumBalanceForRentExemption(span: 165) ) @@ -117,26 +117,21 @@ public class BlockchainClient: SolanaBlockchainClient { public func prepareSendingSPLTokens( account: KeyPair, mintAddress: String, + tokenProgramId: PublicKey, decimals: Decimals, from fromPublicKey: String, to destinationAddress: String, amount: UInt64, feePayer: PublicKey? = nil, transferChecked: Bool = false, - minRentExemption mre: Lamports? = nil + minRentExemption: Lamports ) async throws -> (preparedTransaction: PreparedTransaction, realDestination: String) { let feePayer = feePayer ?? account.publicKey - let minRenExemption: Lamports - if let mre = mre { - minRenExemption = mre - } else { - minRenExemption = try await apiClient - .getMinimumBalanceForRentExemption(span: SPLTokenAccountState.BUFFER_LENGTH) - } let splDestination = try await apiClient.findSPLTokenDestinationAddress( mintAddress: mintAddress, - destinationAddress: destinationAddress + destinationAddress: destinationAddress, + tokenProgramId: tokenProgramId ) // get address @@ -160,10 +155,11 @@ public class BlockchainClient: SolanaBlockchainClient { let createATokenInstruction = try AssociatedTokenProgram.createAssociatedTokenAccountInstruction( mint: mint, owner: owner, - payer: feePayer + payer: feePayer, + tokenProgramId: tokenProgramId ) instructions.append(createATokenInstruction) - accountsCreationFee += minRenExemption + accountsCreationFee += minRentExemption } // send instruction diff --git a/Sources/SolanaSwift/Extensions/PublicKey/PublicKey+Validation.swift b/Sources/SolanaSwift/Extensions/PublicKey/PublicKey+Validation.swift new file mode 100644 index 000000000..eb3e5625d --- /dev/null +++ b/Sources/SolanaSwift/Extensions/PublicKey/PublicKey+Validation.swift @@ -0,0 +1,8 @@ +import Foundation + +public extension PublicKey { + static func isSPLTokenOrToken2022ProgramId(_ programId: String?) -> Bool { + programId == TokenProgram.id.base58EncodedString || + programId == Token2022Program.id.base58EncodedString + } +} diff --git a/Sources/SolanaSwift/Models/PublicKey.swift b/Sources/SolanaSwift/Models/PublicKey.swift index 34bd9754c..e69fdb61f 100644 --- a/Sources/SolanaSwift/Models/PublicKey.swift +++ b/Sources/SolanaSwift/Models/PublicKey.swift @@ -90,12 +90,13 @@ public enum PublicKeyError: Error, Equatable { public extension PublicKey { static func associatedTokenAddress( walletAddress: PublicKey, - tokenMintAddress: PublicKey + tokenMintAddress: PublicKey, + tokenProgramId: PublicKey ) throws -> PublicKey { try findProgramAddress( seeds: [ walletAddress.data, - TokenProgram.id.data, + tokenProgramId.data, tokenMintAddress.data, ], programId: AssociatedTokenProgram.id @@ -217,25 +218,14 @@ public extension PublicKey { } public extension PublicKey { - @available(*, deprecated, renamed: "TokenProgram.id") - static var tokenProgramId: PublicKey { TokenProgram.id } - static var sysvarRent: PublicKey { "SysvarRent111111111111111111111111111111111" } - @available(*, deprecated, renamed: "SystemProgram.id") - static var programId: PublicKey { SystemProgram.id } - static var wrappedSOLMint: PublicKey { "So11111111111111111111111111111111111111112" } static var solMint: PublicKey { "Ejmc1UB4EsES5oAaRN63SpoxMJidt3ZGBrqrZk49vjTZ" } // Arbitrary mint to represent SOL (not wrapped SOL). -// @available(*, deprecated, renamed: "OwnerValidationProgram.id") -// static var ownerValidationProgramId: PublicKey { OwnerValidationProgram.id } static var swapHostFeeAddress: PublicKey { "AHLwq66Cg3CuDJTFtwjPfwjJhifiv6rFwApQNKgX57Yg" } - @available(*, deprecated, renamed: "AssociatedTokenProgram.id") - static var splAssociatedTokenAccountProgramId: PublicKey { AssociatedTokenProgram.id } - static var renBTCMint: PublicKey { "CDJWUqTcYTVAKXAVXoQZFes5JUFc7owSeq7eMQcDSbo5" } static var renBTCMintDevnet: PublicKey { "FsaLodPu4VmSwXGr3gWfwANe4vKf8XSZcCh1CEeJ3jpD" } static var fake: PublicKey { "BGcmLttQoYIw4Yfzc7RkZJCKR53IlAybgq8HK0vmovP0\n" } diff --git a/Sources/SolanaSwift/Programs/AssociatedTokenProgram.swift b/Sources/SolanaSwift/Programs/AssociatedTokenProgram.swift index fab7cea5f..3f39aeb41 100644 --- a/Sources/SolanaSwift/Programs/AssociatedTokenProgram.swift +++ b/Sources/SolanaSwift/Programs/AssociatedTokenProgram.swift @@ -12,15 +12,17 @@ public enum AssociatedTokenProgram: SolanaBasicProgram { public static func createAssociatedTokenAccountInstruction( mint: PublicKey, owner: PublicKey, - payer: PublicKey + payer: PublicKey, + tokenProgramId: PublicKey ) throws -> TransactionInstruction { - TransactionInstruction( + try TransactionInstruction( keys: [ .init(publicKey: payer, isSigner: true, isWritable: true), - try .init( + .init( publicKey: PublicKey.associatedTokenAddress( walletAddress: owner, - tokenMintAddress: mint + tokenMintAddress: mint, + tokenProgramId: tokenProgramId ), isSigner: false, isWritable: true @@ -28,7 +30,7 @@ public enum AssociatedTokenProgram: SolanaBasicProgram { .init(publicKey: owner, isSigner: false, isWritable: false), .init(publicKey: mint, isSigner: false, isWritable: false), .init(publicKey: SystemProgram.id, isSigner: false, isWritable: false), - .init(publicKey: TokenProgram.id, isSigner: false, isWritable: false), + .init(publicKey: tokenProgramId, isSigner: false, isWritable: false), .init(publicKey: .sysvarRent, isSigner: false, isWritable: false), ], programId: AssociatedTokenProgram.id, diff --git a/Sources/SolanaSwift/Programs/TokenPrograms/Token2022Program/Token2022AccountState.swift b/Sources/SolanaSwift/Programs/TokenPrograms/Token2022Program/Token2022AccountState.swift index 3c25cd464..e582555a8 100644 --- a/Sources/SolanaSwift/Programs/TokenPrograms/Token2022Program/Token2022AccountState.swift +++ b/Sources/SolanaSwift/Programs/TokenPrograms/Token2022Program/Token2022AccountState.swift @@ -1,6 +1,6 @@ import Foundation -public struct Token2022AccountState: SolanaSPLTokenAccountState { +public struct Token2022AccountState: TokenAccountState { public let mint: PublicKey public let owner: PublicKey public let lamports: UInt64 diff --git a/Sources/SolanaSwift/Programs/TokenPrograms/Token2022Program/Token2022MintState.swift b/Sources/SolanaSwift/Programs/TokenPrograms/Token2022Program/Token2022MintState.swift index 2b37ac2ec..d5b95c6f3 100644 --- a/Sources/SolanaSwift/Programs/TokenPrograms/Token2022Program/Token2022MintState.swift +++ b/Sources/SolanaSwift/Programs/TokenPrograms/Token2022Program/Token2022MintState.swift @@ -1,6 +1,6 @@ import Foundation -public struct Token2022MintState: SolanaSPLTokenMintState { +public struct Token2022MintState: TokenMintState { public let mintAuthorityOption: UInt32 public let mintAuthority: PublicKey? public let supply: UInt64 diff --git a/Sources/SolanaSwift/Programs/TokenPrograms/SolanaSPLTokenAccountState.swift b/Sources/SolanaSwift/Programs/TokenPrograms/TokenAccountState.swift similarity index 93% rename from Sources/SolanaSwift/Programs/TokenPrograms/SolanaSPLTokenAccountState.swift rename to Sources/SolanaSwift/Programs/TokenPrograms/TokenAccountState.swift index 1499d6c49..d07b54759 100644 --- a/Sources/SolanaSwift/Programs/TokenPrograms/SolanaSPLTokenAccountState.swift +++ b/Sources/SolanaSwift/Programs/TokenPrograms/TokenAccountState.swift @@ -1,6 +1,6 @@ import Foundation -public protocol SolanaSPLTokenAccountState: BufferLayout { +public protocol TokenAccountState: BufferLayout { var mint: PublicKey { get } var owner: PublicKey { get } var lamports: UInt64 { get } @@ -18,7 +18,7 @@ public protocol SolanaSPLTokenAccountState: BufferLayout { var closeAuthority: PublicKey? { get set } } -extension SolanaSPLTokenAccountState { +extension TokenAccountState { func serializeCommonProperties(to writer: inout Data) throws { try mint.serialize(to: &writer) try owner.serialize(to: &writer) diff --git a/Sources/SolanaSwift/Programs/TokenPrograms/SolanaSPLTokenMintState.swift b/Sources/SolanaSwift/Programs/TokenPrograms/TokenMintState.swift similarity index 90% rename from Sources/SolanaSwift/Programs/TokenPrograms/SolanaSPLTokenMintState.swift rename to Sources/SolanaSwift/Programs/TokenPrograms/TokenMintState.swift index f9d34466c..7242f8ad2 100644 --- a/Sources/SolanaSwift/Programs/TokenPrograms/SolanaSPLTokenMintState.swift +++ b/Sources/SolanaSwift/Programs/TokenPrograms/TokenMintState.swift @@ -1,6 +1,6 @@ import Foundation -public protocol SolanaSPLTokenMintState: BufferLayout, Equatable, Hashable, Encodable { +public protocol TokenMintState: BufferLayout, Equatable, Hashable, Encodable { var mintAuthorityOption: UInt32 { get } var mintAuthority: PublicKey? { get } var supply: UInt64 { get } @@ -10,7 +10,7 @@ public protocol SolanaSPLTokenMintState: BufferLayout, Equatable, Hashable, Enco var freezeAuthority: PublicKey? { get } } -extension SolanaSPLTokenMintState { +extension TokenMintState { func serializeCommonProperties(to writer: inout Data) throws { try mintAuthorityOption.serialize(to: &writer) if let mintAuthority = mintAuthority { diff --git a/Sources/SolanaSwift/Programs/TokenPrograms/TokenProgram/SPLTokenAccountState.swift b/Sources/SolanaSwift/Programs/TokenPrograms/TokenProgram/SPLTokenAccountState.swift index a974ecfbc..31981ca52 100644 --- a/Sources/SolanaSwift/Programs/TokenPrograms/TokenProgram/SPLTokenAccountState.swift +++ b/Sources/SolanaSwift/Programs/TokenPrograms/TokenProgram/SPLTokenAccountState.swift @@ -1,6 +1,6 @@ import Foundation -public struct SPLTokenAccountState: SolanaSPLTokenAccountState { +public struct SPLTokenAccountState: TokenAccountState { public static let BUFFER_LENGTH: UInt64 = 165 public let mint: PublicKey diff --git a/Sources/SolanaSwift/Programs/TokenPrograms/TokenProgram/SPLTokenMintState.swift b/Sources/SolanaSwift/Programs/TokenPrograms/TokenProgram/SPLTokenMintState.swift index 33990f6d2..3083d45e3 100644 --- a/Sources/SolanaSwift/Programs/TokenPrograms/TokenProgram/SPLTokenMintState.swift +++ b/Sources/SolanaSwift/Programs/TokenPrograms/TokenProgram/SPLTokenMintState.swift @@ -1,6 +1,6 @@ import Foundation -public struct SPLTokenMintState: SolanaSPLTokenMintState { +public struct SPLTokenMintState: TokenMintState { public static var BUFFER_LENGTH: UInt64 = 82 public let mintAuthorityOption: UInt32 diff --git a/Sources/SolanaSwift/SolanaTokenMetadata/SolanaAPIClient+getAccountBalances.swift b/Sources/SolanaSwift/SolanaTokenMetadata/SolanaAPIClient+getAccountBalances.swift index e6517dfa5..a1a0e7eb1 100644 --- a/Sources/SolanaSwift/SolanaTokenMetadata/SolanaAPIClient+getAccountBalances.swift +++ b/Sources/SolanaSwift/SolanaTokenMetadata/SolanaAPIClient+getAccountBalances.swift @@ -1,14 +1,14 @@ public extension SolanaAPIClient { func getAccountBalances< - T: SolanaSPLTokenAccountState, - M: SolanaSPLTokenMintState + T: TokenAccountState, + M: TokenMintState >( for address: String, tokensRepository: TokenRepository, commitment: String = "confirmed", - programId: String = TokenProgram.id.base58EncodedString, - accountStateType _: T.Type = SPLTokenAccountState.self, - mintType _: M.Type = SPLTokenMintState.self + programId: String, + accountStateType _: T.Type, + mintType _: M.Type ) async throws -> ( resolved: [AccountBalance], unresolved: [UnknownAccountBalance] @@ -73,8 +73,8 @@ public extension SolanaAPIClient { // MARK: - Helpers private func convertResult< - T: SolanaSPLTokenAccountState, - M: SolanaSPLTokenMintState + T: TokenAccountState, + M: TokenMintState >( tokenAccounts: [TokenAccount], tokensRepository: TokenRepository, diff --git a/Tests/SolanaSwiftUnitTests/APIClient/APIClientExtensionsTests.swift b/Tests/SolanaSwiftUnitTests/APIClient/APIClientExtensionsTests.swift index d337126f0..7ce4ee5a3 100644 --- a/Tests/SolanaSwiftUnitTests/APIClient/APIClientExtensionsTests.swift +++ b/Tests/SolanaSwiftUnitTests/APIClient/APIClientExtensionsTests.swift @@ -40,7 +40,8 @@ class APIClientExtensionsTests: XCTestCase { let apiClient = BaseAPIClientMock(endpoint: endpoint) let result = try await apiClient.findSPLTokenDestinationAddress( mintAddress: mintAddress, - destinationAddress: destination + destinationAddress: destination, + tokenProgramId: TokenProgram.id ) XCTAssertEqual(result.destination, "3uetDDizgTtadDHZzyy9BqxrjQcozMEkxzbKhfZF4tG3") XCTAssertEqual(result.isUnregisteredAsocciatedToken, false) @@ -50,13 +51,15 @@ class APIClientExtensionsTests: XCTestCase { let apiClient = BaseAPIClientMock(endpoint: endpoint) let exist = try await apiClient.checkIfAssociatedTokenAccountExists( owner: "9sdwzJWooFrjNGVX6GkkWUG9GyeBnhgJYqh27AsPqwbM", - mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" + mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", + tokenProgramId: TokenProgram.id ) XCTAssertTrue(exist) let exist2 = try await apiClient.checkIfAssociatedTokenAccountExists( owner: "9sdwzJWooFrjNGVX6GkkWUG9GyeBnhgJYqh27AsPqwbM", - mint: "2FPyTwcZLUg1MDrwsyoP4D6s1tM7hAkHYRjkNb5w6Pxk" + mint: "2FPyTwcZLUg1MDrwsyoP4D6s1tM7hAkHYRjkNb5w6Pxk", + tokenProgramId: TokenProgram.id ) XCTAssertFalse(exist2) } diff --git a/Tests/SolanaSwiftUnitTests/BlockchainClient/BlockchainClientWithNativeSOLTests.swift b/Tests/SolanaSwiftUnitTests/BlockchainClient/BlockchainClientWithNativeSOLTests.swift index f1907ccd8..59df41eb5 100644 --- a/Tests/SolanaSwiftUnitTests/BlockchainClient/BlockchainClientWithNativeSOLTests.swift +++ b/Tests/SolanaSwiftUnitTests/BlockchainClient/BlockchainClientWithNativeSOLTests.swift @@ -261,16 +261,16 @@ private class MockAPIClient: SolanaAPIClient { fatalError() } - func getTokenAccountsByDelegate( + func getTokenAccountsByDelegate( pubkey _: String, mint _: String?, programId _: String?, configs _: RequestConfiguration? - ) async throws -> [TokenAccount] { + ) async throws -> [TokenAccount] { fatalError() } - func getTokenAccountsByOwner( + func getTokenAccountsByOwner( pubkey _: String, params _: OwnerInfoParams?, configs _: RequestConfiguration?, diff --git a/Tests/SolanaSwiftUnitTests/BlockchainClient/BlockchainClientWithTokenProgramTests.swift b/Tests/SolanaSwiftUnitTests/BlockchainClient/BlockchainClientWithTokenProgramTests.swift index c61fd0357..572c31d76 100644 --- a/Tests/SolanaSwiftUnitTests/BlockchainClient/BlockchainClientWithTokenProgramTests.swift +++ b/Tests/SolanaSwiftUnitTests/BlockchainClient/BlockchainClientWithTokenProgramTests.swift @@ -87,10 +87,12 @@ final class BlockchainClientWithTokenProgramTests: XCTestCase { let tx = try await blockchainClient.prepareSendingSPLTokens( account: account, mintAddress: mintAddress, + tokenProgramId: TokenProgram.id, decimals: 6, from: source, to: destination, - amount: amount.toLamport(decimals: 6) + amount: amount.toLamport(decimals: 6), + minRentExemption: 2_039_280 ) .preparedTransaction @@ -295,16 +297,16 @@ private class MockAPIClient: SolanaAPIClient { fatalError() } - func getTokenAccountsByDelegate( + func getTokenAccountsByDelegate( pubkey _: String, mint _: String?, programId _: String?, configs _: RequestConfiguration? - ) async throws -> [TokenAccount] { + ) async throws -> [TokenAccount] { fatalError() } - func getTokenAccountsByOwner( + func getTokenAccountsByOwner( pubkey _: String, params _: OwnerInfoParams?, configs _: RequestConfiguration?, diff --git a/Tests/SolanaSwiftUnitTests/BlockchainClient/FeeCalculatorTests.swift b/Tests/SolanaSwiftUnitTests/BlockchainClient/FeeCalculatorTests.swift index 3dc470c7c..e36d3ec0d 100644 --- a/Tests/SolanaSwiftUnitTests/BlockchainClient/FeeCalculatorTests.swift +++ b/Tests/SolanaSwiftUnitTests/BlockchainClient/FeeCalculatorTests.swift @@ -102,12 +102,13 @@ class FeeCalculatorTests: XCTestCase { XCTAssertEqual(fee2.deposit, minRentExemption) // create associated token - let transaction3 = createTransaction( + let transaction3 = try createTransaction( instructions: [ - try AssociatedTokenProgram.createAssociatedTokenAccountInstruction( + AssociatedTokenProgram.createAssociatedTokenAccountInstruction( mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", owner: "3h1zGmCwsRJnVk5BuRNMLsPaQu1y2aqXqXDWYCgrp5UG", - payer: "GrDMoeqMLFjeXQ24H56S1RLgT4R76jsuWCd6SvXyGPQ5" + payer: "GrDMoeqMLFjeXQ24H56S1RLgT4R76jsuWCd6SvXyGPQ5", + tokenProgramId: TokenProgram.id ), ] ) @@ -118,17 +119,19 @@ class FeeCalculatorTests: XCTestCase { XCTAssertEqual(fee3.deposit, 0) // create associated token and close - let transaction4 = createTransaction( + let transaction4 = try createTransaction( instructions: [ - try AssociatedTokenProgram.createAssociatedTokenAccountInstruction( + AssociatedTokenProgram.createAssociatedTokenAccountInstruction( mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", owner: "3h1zGmCwsRJnVk5BuRNMLsPaQu1y2aqXqXDWYCgrp5UG", - payer: "GrDMoeqMLFjeXQ24H56S1RLgT4R76jsuWCd6SvXyGPQ5" + payer: "GrDMoeqMLFjeXQ24H56S1RLgT4R76jsuWCd6SvXyGPQ5", + tokenProgramId: TokenProgram.id ), - try TokenProgram.closeAccountInstruction( + TokenProgram.closeAccountInstruction( account: PublicKey.associatedTokenAddress( walletAddress: "3h1zGmCwsRJnVk5BuRNMLsPaQu1y2aqXqXDWYCgrp5UG", - tokenMintAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" + tokenMintAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", + tokenProgramId: TokenProgram.id ), destination: "3h1zGmCwsRJnVk5BuRNMLsPaQu1y2aqXqXDWYCgrp5UG", owner: "3h1zGmCwsRJnVk5BuRNMLsPaQu1y2aqXqXDWYCgrp5UG" diff --git a/Tests/SolanaSwiftUnitTests/Other/BufferLayout/BufferLayoutTests.swift b/Tests/SolanaSwiftUnitTests/Other/BufferLayout/BufferLayoutTests.swift index a8eb864ed..82b94a8d8 100644 --- a/Tests/SolanaSwiftUnitTests/Other/BufferLayout/BufferLayoutTests.swift +++ b/Tests/SolanaSwiftUnitTests/Other/BufferLayout/BufferLayoutTests.swift @@ -30,7 +30,7 @@ class BufferLayoutTests: XCTestCase { XCTAssertEqual(SPLTokenAccountState.BUFFER_LENGTH, 165) let string = - "BhrZ0FOHFUhTft4+JhhJo9+3/QL6vHWyI8jkatuFPQwCqmOzhzy1ve5l2AqL0ottCChJZ1XSIW3k3C7TaBQn7aCGAQAAAAAAAQAAAOt6vNDYdevCbaGxgaMzmz7yoxaVu3q9vGeCc7ytzeWqAQAAAAAAAAAAAAAAAGQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "Ki8B8ueli4aki+iJfBsIY86NqGNtSFNBWByygEBeZ9QljVXOg8TM+cg/bJJqIcR4NCTqhLKqJTeTag0dQ5U+1gBAehDzWgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgcAAAA=" let data = Data(base64Encoded: string)! diff --git a/Tests/SolanaSwiftUnitTests/Programs/AssociatedTokenProgramTests.swift b/Tests/SolanaSwiftUnitTests/Programs/AssociatedTokenProgramTests.swift index 0a73bc385..b5cf7b5f4 100644 --- a/Tests/SolanaSwiftUnitTests/Programs/AssociatedTokenProgramTests.swift +++ b/Tests/SolanaSwiftUnitTests/Programs/AssociatedTokenProgramTests.swift @@ -5,7 +5,8 @@ class AssociatedTokenProgramTests: XCTestCase { func testAssociatedTokenAddress() throws { let associatedTokenAddress = try PublicKey.associatedTokenAddress( walletAddress: "3h1zGmCwsRJnVk5BuRNMLsPaQu1y2aqXqXDWYCgrp5UG", - tokenMintAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" + tokenMintAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", + tokenProgramId: TokenProgram.id ) XCTAssertEqual(associatedTokenAddress.base58EncodedString, "3uetDDizgTtadDHZzyy9BqxrjQcozMEkxzbKhfZF4tG3") @@ -19,7 +20,8 @@ class AssociatedTokenProgramTests: XCTestCase { .createAssociatedTokenAccountInstruction( mint: mintAddress, owner: owner, - payer: owner + payer: owner, + tokenProgramId: TokenProgram.id ) XCTAssertEqual(instruction.keys.count, 7)