Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/token 2022 renaming #89

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
- Remove deprecated PublicKey.programId, use SystemProgram.id instead.
- Remove deprecated PublicKey.ownerValidationProgramId, use OwnerValidationProgram.id instead.
- Remove deprecated PublicKey.splAssociatedTokenAccountProgramId, use AssociatedTokenProgram.id instead.
- Remove deprecated typealias AccountInfo, use SPLTokenAccountState or Token2022AccountState instead.
- Remove deprecated typealias Mint, use SPLTokenMintState or Token2022MintState instead.
- Remove deprecated typealias AccountInfo, use TokenAccountState or Token2022AccountState instead.
- Remove deprecated typealias Mint, use TokenMintState or Token2022MintState instead.
- Remove deprecated typealias Wallet, use AccountBalance instead.
- Support token 2022 via method getAccountBalances (See GetAccountBalancesTests).
- Support token 2022 and Token2022Program.
Expand Down
10 changes: 5 additions & 5 deletions Sources/SolanaSwift/APIClient/APIClient+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public extension SolanaAPIClient {
pubkey: String,
params: OwnerInfoParams?,
configs: RequestConfiguration?
) async throws -> [TokenAccount<SPLTokenAccountState>] {
) async throws -> [TokenAccount<TokenAccountState>] {
try await getTokenAccountsByOwner(
pubkey: pubkey,
params: params,
configs: configs,
decodingTo: SPLTokenAccountState.self
decodingTo: TokenAccountState.self
)
}

Expand Down Expand Up @@ -99,7 +99,7 @@ public extension SolanaAPIClient {
tokenProgramId: tokenProgramId
)

let bufferInfo: BufferInfo<SPLTokenAccountState>? = try await getAccountInfo(account: associatedTokenAccount
let bufferInfo: BufferInfo<TokenAccountState>? = try await getAccountInfo(account: associatedTokenAccount
.base58EncodedString)
return bufferInfo?.data.mint == mintAddress
}
Expand All @@ -120,7 +120,7 @@ public extension SolanaAPIClient {
tokenProgramId: PublicKey
) async throws -> SPLTokenDestinationAddress {
var address: String
var accountInfo: BufferInfo<SPLTokenAccountState>?
var accountInfo: BufferInfo<TokenAccountState>?
do {
accountInfo = try await getAccountInfoThrowable(account: destinationAddress)
let toTokenMint = accountInfo?.data.mint.base58EncodedString
Expand Down Expand Up @@ -159,7 +159,7 @@ public extension SolanaAPIClient {
var isUnregisteredAsocciatedToken = false
if destinationAddress != toPublicKey.base58EncodedString {
// check if associated address is already registered
let info: BufferInfo<SPLTokenAccountState>?
let info: BufferInfo<TokenAccountState>?
do {
info = try await getAccountInfoThrowable(account: toPublicKey.base58EncodedString)
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public class JSONRPCAPIClient: SolanaAPIClient {
return result.value
}

public func getTokenAccountsByDelegate<T: TokenAccountState>(
public func getTokenAccountsByDelegate<T: TokenAccountLayoutState>(
pubkey: String,
mint: String? = nil,
programId: String? = nil,
Expand All @@ -161,7 +161,7 @@ public class JSONRPCAPIClient: SolanaAPIClient {
return result.value
}

public func getTokenAccountsByOwner<T: TokenAccountState>(
public func getTokenAccountsByOwner<T: TokenAccountLayoutState>(
pubkey: String,
params: OwnerInfoParams?,
configs: RequestConfiguration?,
Expand Down
4 changes: 2 additions & 2 deletions Sources/SolanaSwift/APIClient/SolanaAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public protocol SolanaAPIClient {
/// - Returns The result will be an array of TokenAccount<AccountInfo>
/// - SeeAlso https://docs.solana.com/developing/clients/jsonrpc-api#gettokenaccountsbydelegate
///
func getTokenAccountsByDelegate<T: TokenAccountState>(
func getTokenAccountsByDelegate<T: TokenAccountLayoutState>(
pubkey: String,
mint: String?,
programId: String?,
Expand All @@ -183,7 +183,7 @@ public protocol SolanaAPIClient {
/// - Returns The result will be an array of TokenAccount<AccountInfo>
/// - SeeAlso https://docs.solana.com/developing/clients/jsonrpc-api#gettokenaccountsbyowner
///
func getTokenAccountsByOwner<T: TokenAccountState>(
func getTokenAccountsByOwner<T: TokenAccountLayoutState>(
pubkey: String,
params: OwnerInfoParams?,
configs: RequestConfiguration?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public extension SolanaBlockchainClient {
from: owner,
toNewPubkey: newAccount.publicKey,
lamports: amount + minRentExemption,
space: SPLTokenAccountState.BUFFER_LENGTH,
space: TokenAccountState.BUFFER_LENGTH,
programId: TokenProgram.id
),
TokenProgram.initializeAccountInstruction(
Expand Down Expand Up @@ -69,7 +69,7 @@ public extension SolanaBlockchainClient {

let isAssociatedTokenAddressRegistered: Bool
do {
let info: BufferInfo<SPLTokenAccountState>? = try await apiClient
let info: BufferInfo<TokenAccountState>? = try await apiClient
.getAccountInfo(account: associatedAddress.base58EncodedString)
if PublicKey.isSPLTokenProgram(info?.owner),
info?.data.owner == owner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct Token2022AccountState: TokenAccountState {
public struct Token2022AccountState: TokenAccountLayoutState {
static var ACCOUNT_TYPE: UInt8 { 2 }

public let mint: PublicKey
Expand Down Expand Up @@ -68,7 +68,7 @@ extension Token2022AccountState: BorshCodable {
}

public init(from reader: inout BinaryReader) throws {
let oldTokenProgramData = try SPLTokenAccountState(from: &reader)
let oldTokenProgramData = try TokenAccountState(from: &reader)
mint = oldTokenProgramData.mint
owner = oldTokenProgramData.owner
lamports = oldTokenProgramData.lamports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct Token2022MintState: MintLayoutState {

extension Token2022MintState: BorshCodable {
public init(from reader: inout BinaryReader) throws {
let oldTokenMintState = try SPLTokenMintState(from: &reader)
let oldTokenMintState = try TokenMintState(from: &reader)
mintAuthorityOption = oldTokenMintState.mintAuthorityOption
mintAuthority = oldTokenMintState.mintAuthority
supply = oldTokenMintState.supply
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public protocol TokenAccountState: BufferLayout {
public protocol TokenAccountLayoutState: BufferLayout {
var mint: PublicKey { get }
var owner: PublicKey { get }
var lamports: UInt64 { get }
Expand All @@ -18,7 +18,7 @@ public protocol TokenAccountState: BufferLayout {
var closeAuthority: PublicKey? { get set }
}

extension TokenAccountState {
extension TokenAccountLayoutState {
func serializeCommonProperties(to writer: inout Data) throws {
try mint.serialize(to: &writer)
try owner.serialize(to: &writer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public struct SPLTokenAccountState: TokenAccountState {
public struct TokenAccountState: TokenAccountLayoutState {
public static let BUFFER_LENGTH: UInt64 = 165

public let mint: PublicKey
Expand Down Expand Up @@ -54,7 +54,7 @@ public struct SPLTokenAccountState: TokenAccountState {
}
}

extension SPLTokenAccountState: BorshCodable {
extension TokenAccountState: BorshCodable {
public func serialize(to writer: inout Data) throws {
try serializeCommonProperties(to: &writer)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public extension SolanaAPIClient {
tokensRepository: tokensRepository,
commitment: commitment,
programId: TokenProgram.id.base58EncodedString,
accountStateType: SPLTokenAccountState.self,
mintType: SPLTokenMintState.self
accountStateType: TokenAccountState.self,
mintType: TokenMintState.self
)

// token 2022
Expand Down Expand Up @@ -45,8 +45,8 @@ public extension SolanaAPIClient {
// MARK: - Helpers

private func getAccountBalances<
T: TokenAccountState,
M: TokenMintState
T: TokenAccountLayoutState,
M: MintLayoutState
>(
for address: String,
tokensRepository: TokenRepository,
Expand Down Expand Up @@ -80,8 +80,8 @@ public extension SolanaAPIClient {
}

private func convertResult<
T: TokenAccountState,
M: TokenMintState
T: TokenAccountLayoutState,
M: MintLayoutState
>(
tokenAccounts: [TokenAccount<T>],
tokensRepository: TokenRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class APIClientExtensionsTests: XCTestCase {
let apiClient = BaseAPIClientMock(endpoint: endpoint, networkManager: mock)

do {
let _: BufferInfo<SPLTokenAccountState> = try await apiClient
let _: BufferInfo<TokenAccountState> = try await apiClient
.getAccountInfoThrowable(account: "djfijijasdf")
} catch {
XCTAssertTrue(error.isEqualTo(.couldNotRetrieveAccountInfo))
Expand Down Expand Up @@ -105,11 +105,11 @@ class BaseAPIClientMock: JSONRPCAPIClient {
pubkey _: String,
params _: OwnerInfoParams? = nil,
configs _: RequestConfiguration? = nil
) async throws -> [TokenAccount<SPLTokenAccountState>] {
) async throws -> [TokenAccount<TokenAccountState>] {
let json =
"[{\"account\":{\"data\":[\"ppdSk884LShYnHoHm7XiDlZ28iJVm9BHPgrAEfxU44AJ7HiGa7fztefqNjU2MSBOZ3HPlRmb0eAXj0bEanmyfAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"base64\"],\"executable\":false,\"lamports\":2039280,\"owner\":\"So11111111111111111111111111111111111111112\",\"rentEpoch\":309},\"pubkey\":\"9bNJ7AF8w1Ms4BsqpqbUPZ16vCSePYJpgSBUTRqd8ph4\"}]"
let decoder = try JSONDecoder().decode(
[TokenAccount<SPLTokenAccountState>].self,
[TokenAccount<TokenAccountState>].self,
from: json.data(using: .utf8)!
)
return decoder
Expand All @@ -122,7 +122,7 @@ class BaseAPIClientMock: JSONRPCAPIClient {
let json =
"{\"context\":{\"slot\":132420615},\"value\":[{\"data\":[\"APoAh5MDAAAAAAKLjuya35R64GfrOPbupmMcxJ1pmaH2fciYq9DxSQ88FioLlNul6FnDNF06/RKhMFBVI8fFQKRYcqukjYZitosKxZBjjg9hLR2AsDm2e/itloPtlrPeVDPIVdnO4+dmM2JiSZHdhsj7+Fn94OTNte9elt1ek0p487C2fLrFA9CvUPerjZvfP97EqlF9OXbPSzaGJzdmfWhk4jRnThsg5scAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAObFpMVhxY3CRrzEcywhYTa4a4SsovPp4wKPRTbTJVtzAfQBZAAAAABDU47UFrGnHMTsb0EaE1TBoVQGvCIHKJ4/EvpK3zvIfwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACsWQY44PYS0dgLA5tnv4rZaD7Zaz3lQzyFXZzuPnZjMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\",\"base64\"],\"executable\":false,\"lamports\":1345194,\"owner\":\"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA\",\"rentEpoch\":306}]}"
let decoder = try JSONDecoder()
.decode(Rpc<[BufferInfo<SPLTokenMintState>]>.self, from: json.data(using: .utf8)!) as! Rpc<[BufferInfo<T>]>
.decode(Rpc<[BufferInfo<TokenMintState>]>.self, from: json.data(using: .utf8)!) as! Rpc<[BufferInfo<T>]>
return decoder.value
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/SolanaSwiftUnitTests/APIClient/APIClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class APIClientTests: XCTestCase {
func testGetAccountInfo() async throws {
let mock = NetworkManagerMock(NetworkManagerMockJSON["getAccountInfo"]!)
let apiClient = JSONRPCAPIClient(endpoint: endpoint, networkManager: mock)
let result: BufferInfo<SPLTokenAccountState>? = try! await apiClient
let result: BufferInfo<TokenAccountState>? = try! await apiClient
.getAccountInfo(account: "HWbsF542VSCxdGKcHrXuvJJnpwCEewmzdsG6KTxXMRRk")
XCTAssert(result?.owner == "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
XCTAssert(result?.lamports == 2_039_280)
Expand All @@ -28,7 +28,7 @@ class APIClientTests: XCTestCase {
let mock = NetworkManagerMock(NetworkManagerMockJSON["getAccountInfo_2"]!)
let apiClient = JSONRPCAPIClient(endpoint: endpoint, networkManager: mock)
do {
let _: BufferInfo<SPLTokenAccountState>? = try await apiClient
let _: BufferInfo<TokenAccountState>? = try await apiClient
.getAccountInfo(account: "HWbsF542VSCxdGKcHrXuvJJnpwCEewmzdsG6KTxXMRRk")
} catch let error as APIClientError {
XCTAssertTrue(error == .couldNotRetrieveAccountInfo)
Expand Down Expand Up @@ -221,7 +221,7 @@ class APIClientTests: XCTestCase {
func testGetMultipleAccounts() async throws {
let mock = NetworkManagerMock(NetworkManagerMockJSON["getMultipleAccounts"]!)
let apiClient = JSONRPCAPIClient(endpoint: endpoint, networkManager: mock)
let result: [BufferInfo<SPLTokenMintState>?] = try await apiClient
let result: [BufferInfo<TokenMintState>?] = try await apiClient
.getMultipleAccounts(pubkeys: ["DkZzno16JLXYda4eHZzM9J8Vxet9StJJ5mimrtjbK5V3"], commitment: "confirm")
XCTAssertNotNil(result)
}
Expand All @@ -236,7 +236,7 @@ class APIClientTests: XCTestCase {
"Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB",
],
commitment: "confirm",
mintType: SPLTokenMintState.self
mintType: TokenMintState.self
)
XCTAssertNotNil(result)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private class MockAPIClient: SolanaAPIClient {
switch testCase {
case "testPrepareSendingSPLTokens()#2", "testPrepareSendingSPLTokens()#4",
"testPrepareSendingSPLTokens()#5":
data = SPLTokenAccountState(
data = TokenAccountState(
mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
owner: "6QuXb6mB6WmRASP2y8AavXh6aabBXEH5ZzrSH5xRrgSm",
lamports: 100,
Expand Down Expand Up @@ -261,7 +261,7 @@ private class MockAPIClient: SolanaAPIClient {
fatalError()
}

func getTokenAccountsByDelegate<T: TokenAccountState>(
func getTokenAccountsByDelegate<T: TokenAccountLayoutState>(
pubkey _: String,
mint _: String?,
programId _: String?,
Expand All @@ -270,7 +270,7 @@ private class MockAPIClient: SolanaAPIClient {
fatalError()
}

func getTokenAccountsByOwner<T: TokenAccountState>(
func getTokenAccountsByOwner<T: TokenAccountLayoutState>(
pubkey _: String,
params _: OwnerInfoParams?,
configs _: RequestConfiguration?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private class MockAPIClient: SolanaAPIClient {
switch testCase {
case "testPrepareSendingSPLTokens()#2", "testPrepareSendingSPLTokens()#4",
"testPrepareSendingSPLTokens()#5":
data = SPLTokenAccountState(
data = TokenAccountState(
mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
owner: "6QuXb6mB6WmRASP2y8AavXh6aabBXEH5ZzrSH5xRrgSm",
lamports: 100,
Expand Down Expand Up @@ -298,7 +298,7 @@ private class MockAPIClient: SolanaAPIClient {
fatalError()
}

func getTokenAccountsByDelegate<T: TokenAccountState>(
func getTokenAccountsByDelegate<T: TokenAccountLayoutState>(
pubkey _: String,
mint _: String?,
programId _: String?,
Expand All @@ -307,7 +307,7 @@ private class MockAPIClient: SolanaAPIClient {
fatalError()
}

func getTokenAccountsByOwner<T: TokenAccountState>(
func getTokenAccountsByOwner<T: TokenAccountLayoutState>(
pubkey _: String,
params _: OwnerInfoParams?,
configs _: RequestConfiguration?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ class BufferLayoutDecodingTests: XCTestCase {
// MARK: - Mint

func testDecodingMint() throws {
XCTAssertEqual(SPLTokenMintState.BUFFER_LENGTH, 82)
XCTAssertEqual(TokenMintState.BUFFER_LENGTH, 82)

let string =
"AQAAAAYa2dBThxVIU37ePiYYSaPft/0C+rx1siPI5GrbhT0MABCl1OgAAAAGAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="

let data = Data(base64Encoded: string)!

var binaryReader = BinaryReader(bytes: data.bytes)
let mintLayout = try SPLTokenMintState(from: &binaryReader)
let mintLayout = try TokenMintState(from: &binaryReader)

XCTAssertEqual(mintLayout.mintAuthorityOption, 1)
XCTAssertEqual(mintLayout.mintAuthority?.base58EncodedString, "QqCCvshxtqMAL2CVALqiJB7uEeE5mjSPsseQdDzsRUo")
Expand All @@ -66,15 +66,15 @@ class BufferLayoutDecodingTests: XCTestCase {
// MARK: - Account info

func testDecodingAccountInfo() throws {
XCTAssertEqual(SPLTokenAccountState.BUFFER_LENGTH, 165)
XCTAssertEqual(TokenAccountState.BUFFER_LENGTH, 165)

let string =
"BhrZ0FOHFUhTft4+JhhJo9+3/QL6vHWyI8jkatuFPQwCqmOzhzy1ve5l2AqL0ottCChJZ1XSIW3k3C7TaBQn7aCGAQAAAAAAAQAAAOt6vNDYdevCbaGxgaMzmz7yoxaVu3q9vGeCc7ytzeWqAQAAAAAAAAAAAAAAAGQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

let data = Data(base64Encoded: string)!

var binaryReader = BinaryReader(bytes: data.bytes)
let accountInfo = try SPLTokenAccountState(from: &binaryReader)
let accountInfo = try TokenAccountState(from: &binaryReader)

XCTAssertEqual("QqCCvshxtqMAL2CVALqiJB7uEeE5mjSPsseQdDzsRUo", accountInfo.mint.base58EncodedString)
XCTAssertEqual("BQWWFhzBdw2vKKBUX17NHeFbCoFQHfRARpdztPE2tDJ", accountInfo.owner.base58EncodedString)
Expand All @@ -100,7 +100,7 @@ class BufferLayoutDecodingTests: XCTestCase {
let data = Data(base64Encoded: string)!

var binaryReader = BinaryReader(bytes: data.bytes)
let accountInfo = try SPLTokenAccountState(from: &binaryReader)
let accountInfo = try TokenAccountState(from: &binaryReader)

XCTAssertEqual("11111111111111111111111111111111", accountInfo.mint.base58EncodedString)
XCTAssertEqual("11111111111111111111111111111111", accountInfo.owner.base58EncodedString)
Expand All @@ -123,7 +123,7 @@ class BufferLayoutDecodingTests: XCTestCase {
let data2 = Data(base64Encoded: string2)!

var binaryReader2 = BinaryReader(bytes: data2.bytes)
let accountInfo2 = try SPLTokenAccountState(from: &binaryReader2)
let accountInfo2 = try TokenAccountState(from: &binaryReader2)

XCTAssertEqual("11111111111111111111111111111111", accountInfo2.mint.base58EncodedString)
XCTAssertEqual("11111111111111111111111111111111", accountInfo2.owner.base58EncodedString)
Expand Down
Loading