Skip to content

Commit

Permalink
Merge pull request #3 from mathwallet/tohuman
Browse files Browse the repository at this point in the history
Tohuman
  • Loading branch information
xueyuejie authored Nov 15, 2021
2 parents 714312d + 66a00b9 commit 90a1b2c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 23 deletions.
21 changes: 9 additions & 12 deletions Sources/SolanaSwift/Provider/SolanaRPCProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,15 @@ public struct SolanaRPCProvider {
}
}

extension SolanaRPCProvider {
public enum SolanaRpcProviderError: Error {
case unknown
case server(message: String)

var errorDescription: String? {
switch self {
case .server(let message):
return message
default:
return "Unknown error"
}
public enum SolanaRpcProviderError: Error {
case unknown
case server(message: String)
public var errorDescription: String? {
switch self {
case .server(let message):
return message
default:
return "Unknown error"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public struct SolanaInstructionAssetOwner: SolanaInstructionBase {
extension SolanaInstructionAssetOwner: SolanaHumanReadable {
public func toHuman() -> Dictionary<String, Any> {
return [
"type": "Asset Owner"
"type": "Asset Owner",
"data": [
"destination":self.signers.first?.publicKey.address,
"data":self.data.toHexString()
]
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,24 @@ public struct SolanaInstructionAssociatedAccount: SolanaInstructionBase {

extension SolanaInstructionAssociatedAccount: SolanaHumanReadable {
public func toHuman() -> Dictionary<String, Any> {
var dataDic:[String:String] = [String:String]()
for i in 0..<self.signers.count {
let signer = self.signers[i]
if signer.isWritable {
if signer.isSigner {
dataDic["from"] = signer.publicKey.address
continue
} else {
dataDic["associatedToken"] = signer.publicKey.address
continue
}
} else {
dataDic["key\(i)"] = signer.publicKey.address
}
}
return [
"type": "Associated Account"
"type": "Associated Account",
"data": dataDic
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,25 @@ extension SolanaInstructionRaw: SolanaHumanReadable {
let i = SolanaInstructionToken(promgramId: promgramId, signers: signers, data: data) {
return i.toHuman()
}
} else if promgramId == SolanaPublicKey.ASSOCIATEDTOKENPROGRAMID {
// SolanaInstructionAssociatedAccount
if let i = SolanaInstructionAssociatedAccount(promgramId: promgramId, signers: signers, data: data) {
return i.toHuman()
}
} else if promgramId == SolanaPublicKey.OWNERVALIDATIONPROGRAMID {
// SolanaInstructionAssetOwner
if let i = SolanaInstructionAssetOwner(promgramId: promgramId, signers: signers, data: data){
return i.toHuman()
}
}
var dataDic:[String:String] = [String:String]()
for i in 0..<self.signers.count {
dataDic["pubkey\(i)"] = self.signers[i].publicKey.address
}

dataDic["data"] = self.data.toHexString()
return [
"type": "Unknown Type",
"data": data.toHexString()
"data": dataDic
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ public struct SolanaInstructionToken: SolanaInstructionBase {

extension SolanaInstructionToken: SolanaHumanReadable {
public func toHuman() -> Dictionary<String, Any> {
// let type = self.data.readUInt8(at: 0)
var dataDic:[String:String] = [String:String]()
for i in 0..<self.signers.count {
let signer = self.signers[i]
if signer.isSigner {
dataDic["owner"] = signer.publicKey.address
continue
}
dataDic["pubkey\(i)"] = signer.publicKey.address
}
let lamports = self.data.readUInt64(at: 1)
dataDic["lamports"] = "\(lamports)"
return [
"type": "Transfer Token",
"data": [
"lamports": lamports
]
"data": dataDic
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,23 @@ public struct SolanaInstructionTransfer: SolanaInstructionBase {

extension SolanaInstructionTransfer: SolanaHumanReadable {
public func toHuman() -> Dictionary<String, Any> {
// let type = self.data.readUInt32(at: 0)

var from = ""
var to = ""
self.signers.forEach({ signer in
if signer.isSigner {
from = signer.publicKey.address
} else {
to = signer.publicKey.address
}
})
let lamports = self.data.readUInt64(at: 4)
return [
"type": "Transfer Token",
"type": "Transfer",
"data": [
"lamports": lamports
"from":from,
"to":to,
"lamports":"\(lamports)"
]
]
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/SolanaSwiftTests/SolanaSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,15 @@ final class SolanaSwiftTests: XCTestCase {



}
func testToHuman() throws {
let array:[SolanaSigner] = [SolanaSigner(publicKey: SolanaPublicKey(base58String: "D37m1SKWnyY4fmhEntD84uZpjejUZkbHQUBEP3X74LuH")!,isSigner: true,isWritable: true),SolanaSigner(publicKey: SolanaPublicKey(base58String: "EY2kNS5hKfxxLSkbaBMQtQuHYYbjyYk6Ai2phcGMNgpC")!,isSigner: false,isWritable: true),SolanaSigner(publicKey: SolanaPublicKey(base58String: "DcaWQQGErxtzTTph7r5xWMxEvWEywGahtjnRvoJPN9Vz")!)]
let instruction = SolanaInstructionTransfer(promgramId: SolanaPublicKey(base58String: "TokenwAJbNbGKPFXCWuBvf9Ss623VQ5DA")!, signers: array, data: Data(hex: "0x02000000a08601000000000002000000a086010000000000"))

let dataDic:Dictionary = instruction!.toHuman()
let dataarray = dataDic["data"] as! Dictionary<String, Any>
let data = try? JSONSerialization.data(withJSONObject: dataarray, options: [])
let str = String(data: data!, encoding: String.Encoding.utf8)
debugPrint(str!)
}
}

0 comments on commit 90a1b2c

Please sign in to comment.