Skip to content

Commit

Permalink
feat: VecU8
Browse files Browse the repository at this point in the history
  • Loading branch information
bigearsenal committed Jan 24, 2024
1 parent 5827e9f commit 4100ad8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct UnparsedExtensionState: Token2022ExtensionState {
struct VecU8: BorshCodable, Codable, Equatable, Hashable {
let length: UInt16
let data: Data
init(from reader: inout BinaryReader) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public struct AnyToken2022ExtensionState: BorshCodable, Codable, Equatable, Hash
case .transferFeeConfig:
state = try container.decode(TransferFeeConfigExtensionState.self, forKey: .state)
default:
state = try container.decode(UnparsedExtensionState.self, forKey: .state)
state = try container.decode(VecU8.self, forKey: .state)
}
}

Expand All @@ -58,7 +58,7 @@ public struct AnyToken2022ExtensionState: BorshCodable, Codable, Equatable, Hash
case .interestBearingConfig:
state = try InterestBearingConfigExtensionState(from: &reader)
default:
state = try UnparsedExtensionState(from: &reader)
state = try VecU8(from: &reader)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ import Foundation
public protocol Token2022ExtensionState: BorshCodable, Codable, Equatable, Hashable {
var length: UInt16 { get }
}

extension VecU8: Token2022ExtensionState {}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct Token2022MintState: TokenMintState {
public var extensions: [AnyToken2022ExtensionState]

public func getParsedExtension<T: Token2022ExtensionState>(ofType _: T.Type) -> T? {
assert(T.self != UnparsedExtensionState.self)
assert(T.self != VecU8.self)
return extensions.first(where: { $0.state is T })?.state as? T
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,21 @@ final class BufferLayoutEncodingTests: XCTestCase {
)
}

// func testEncodingToken2022MintState() throws {
// let extensionState = TransferFeeConfigExtensionState(
// func testEncodingToken2022MintState2() throws {
// // Mint FZYEgCWzzedxcmxYvGXSkMrj7TaA3bXoaEv6XMnwtLKh
// let state = Token2022MintState(
// mintAuthorityOption: 1,
// mintAuthority: "FZYEgCWzzedxcmxYvGXSkMrj7TaA3bXoaEv6XMnwtLKh",
// supply: 1_000_000_000_000,
// decimals: 10,
// isInitialized: true,
// freezeAuthorityOption: 10,
// freezeAuthority: nil,
// extensions: []
// )
//
// // Add TransferFeeConfigExtensionState
// let transferConfig = TransferFeeConfigExtensionState(
// length: 108,
// transferFeeConfigAuthority: "11111111111111111111111111111111",
// withdrawWithHeldAuthority: "11111111111111111111111111111111",
Expand All @@ -200,7 +213,9 @@ final class BufferLayoutEncodingTests: XCTestCase {
// transferFeeBasisPoints: 300
// )
// )
// state.extensions.append(TokenExtension(type: 1, state: transferConfig))
//
// // Add InterestBearingConfigExtensionState
// let interestBearingConfig = InterestBearingConfigExtensionState(
// length: 52,
// rateAuthority: "2a9H7uNfUxt7YdS5yH3ZEijdPqpeBtyq7JPtVyi6XKtk",
Expand All @@ -209,24 +224,16 @@ final class BufferLayoutEncodingTests: XCTestCase {
// lastUpdateTimestamp: 1_692_005_389,
// currentRate: 0
// )
// state.extensions.append(TokenExtension(type: 2, state: interestBearingConfig))
//
// let state = Token2022MintState(
// mintAuthorityOption: <#T##UInt32#>,
// mintAuthority: <#T##PublicKey?#>,
// supply: <#T##UInt64#>,
// decimals: <#T##UInt8#>,
// isInitialized: <#T##Bool#>,
// freezeAuthorityOption: <#T##UInt32#>,
// freezeAuthority: <#T##PublicKey?#>,
// extensions: <#T##[AnyToken2022ExtensionState]#>
// )
//
// // Serialize the state
// var data = Data()
// try state.serialize(to: &data)
//
// // Base64 encode the serialized data and compare with the expected value
// XCTAssertEqual(
// data.base64EncodedString(),
// "AAAAAAT3LznRbp1toHmr0Mjv1bBjc6oSrtihgQu/PG0Sunz6XUTVg3ktAAAFAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQEAbAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAT3LznRbp1toHmr0Mjv1bBjc6oSrtihgQu/PG0Sunz6N5bilgAAAAASAgAAAAAAAAAgPYh5LQAALAESAgAAAAAAAAAgPYh5LQAALAE="
// "AAAAABdZNqd8UPqRoeBHXdhoEwzZNLf6UnDQ1UDsr4oXimfhquOLA1BVIXECAQAAAAAXWTanfFD6kaHgR13YaBMM2TS3+lJw0NVA7K+KF4pn4QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQEAbAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALoDKJKHBCRLpAQAAAAAAAACQI15ZrVt7LAHpAQAAAAAAAACQI15ZrVt7LAEKADQAF1k2p3xQ+pGh4Edd2GgTDNk0t/pScNDVQOyviheKZ+EN9NlkAAAAAAAADfTZZAAAAAAAAAYAAQAB"
// )
// }

Expand Down

0 comments on commit 4100ad8

Please sign in to comment.