Skip to content
This repository has been archived by the owner on Jul 10, 2020. It is now read-only.

Add Support for Nebulas RPC #91

Open
leoneparise opened this issue Aug 29, 2019 · 0 comments
Open

Add Support for Nebulas RPC #91

leoneparise opened this issue Aug 29, 2019 · 0 comments

Comments

@leoneparise
Copy link
Contributor

leoneparise commented Aug 29, 2019

RPC methods:

// Copyright DApps Platform Inc. All rights reserved.

import Foundation
import Moya

enum NebulasRPC {
    case account(address: String)
    case getGasPrice
    case sendRawTransaction(data: Data)
    case getNebState
    case getTransactionReceipt(hash: String)
}

extension NebulasRPC: TargetType {
    var baseURL: URL {
        return URL(string: "/")!
    }

    var path: String {
        switch self {
        case .account:
            return "/v1/user/accountstate"
        case .getGasPrice:
            return "/v1/user/getGasPrice"
        case .sendRawTransaction:
            return "/v1/user/rawtransaction"
        case .getNebState:
            return "/v1/user/nebstate"
        case .getTransactionReceipt:
            return "/v1/user/getTransactionReceipt"
        }
    }

    var method: Moya.Method {
        switch self {
        case .account, .sendRawTransaction, .getTransactionReceipt:
            return .post
        case .getGasPrice, .getNebState:
            return .get
        }
    }

    var sampleData: Data {
        return Data()
    }

    var task: Task {
        switch self {
        case .account(let address):
            return .requestParameters(parameters: ["address": address], encoding: JSONEncoding())
        case .getGasPrice, .getNebState:
            return .requestPlain
        case .sendRawTransaction(let data):
            return .requestParameters(parameters: ["data": String(decoding: data, as: UTF8.self)], encoding: JSONEncoding())
        case .getTransactionReceipt(let hash):
            return .requestParameters(parameters: ["hash": hash], encoding: JSONEncoding())
        }
    }

    var headers: [String: String]? {
        return [
            "content-type": "application/json",
        ]
    }
}

Models:

struct NebulasResult<T: Codable>: Decodable {
    let result: T

    private enum CodingKeys: String, CodingKey {
        case error
        case result
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        if let error = try container.decodeIfPresent(String.self, forKey: .error) {
            throw NebulasRPCError.general(reason: error)
        }
        self.result = try container.decode(T.self, forKey: .result)
    }
}

struct NebulasAccountState: Codable {
    let balance: BigIntCodable
    let nonce: BigIntCodable
}

struct NebulasGasPrice: Codable {
    let gas_price: BigIntCodable
}

struct NebulasSentTransaction: Codable {
    let txhash: String
}

struct NebulasState: Codable {
    let chain_id: Int
}

struct NebulasTransactionReceipt: Codable {
    let status: Int
}

enum NebulasRPCError: LocalizedError {
    case general(reason: String)

    public var errorDescription: String? {
        switch self {
        case .general(let reason):
            return reason
        }
    }
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant