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

Add support for VeChain RPC #101

Open
vikmeup opened this issue Aug 30, 2019 · 0 comments
Open

Add support for VeChain RPC #101

vikmeup opened this issue Aug 30, 2019 · 0 comments

Comments

@vikmeup
Copy link
Contributor

vikmeup commented Aug 30, 2019

Example Cosmos RPC: https://github.com/trustwallet/web-core/tree/master/packages/rpc/src/cosmos

Endpoint: for testing: https://vethor-node.vechain.com

Replicate Swift implementation in TypeScript, as implemented in Vechain RPC

Swift implementation:

enum VechainRPC {
    case getBalance(address: String)
    case sendTransaction(VechainSendTransaction)
    case getTransactionReceipt(id: String)
    case getBlock
    case callContract(contract: String, VechainCallContract)
}

extension VechainRPC: TargetType {

    var baseURL: URL {
        return RPCServer(.vechain).rpcURL
    }

    var path: String {
        switch self {
        case .getBalance(let address):
            return "/accounts/\(address)"
        case .sendTransaction:
            return "/transactions"
        case .getTransactionReceipt(let id):
            return  "/transactions/" + id + "/receipt"
        case .getBlock:
            return "/blocks/best"
        case .callContract(let contract, _):
            return "/accounts/\(contract)"
        }
    }

    var method: Moya.Method {
        switch self {
        case .getBalance: return .get
        case .sendTransaction: return .post
        case .getTransactionReceipt: return .get
        case .getBlock: return .get
        case .callContract: return .post
        }
    }

    var task: Task {
        switch self {
        case .getBalance, .getTransactionReceipt, .getBlock:
            return .requestPlain
        case .sendTransaction(let transaction):
            return .requestJSONEncodable(transaction)
        case .callContract(_, let data):
            return .requestJSONEncodable(data)
        }
    }

    var sampleData: Data {
        return Data()
    }

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

Models:

struct VechainBalance: Codable {
    let balance: String
    let energy: String
}

struct VechainSendTransaction: Codable {
    let raw: String
}

struct VechainSentTransaction: Codable {
    let id: String
}

struct VechainTransactionReceipt: Codable {
    let gasUsed: Int64
    let reverted: Bool
}

struct VechainBlock: Codable {
    let number: UInt64
    let id: String
}

struct VechainCallContract: Codable {
    let value: String
    let data: String
}

struct VechainCallContractResponse: Codable {
    let data: String
    let gasUsed: UInt64
}
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