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

Add support for Binance Chain RPC #78

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

Add support for Binance Chain RPC #78

vikmeup opened this issue Aug 28, 2019 · 0 comments

Comments

@vikmeup
Copy link
Contributor

vikmeup commented Aug 28, 2019

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

Endpoint: for testing: https://dex-asiapacific.binance.org/api

Replicate Swift implementation in TypeScript, as implemented in Cosmos RPC

Swift implementation:

var path: String {
        switch self {
        case .getAccount(let address):
            return "/v1/account/\(address)"
        case .getTransaction(let hash):
            return "/v1/tx/\(hash)?format=json"
        case .order(let order):
            return "/v1/orders/\(order)"
        case .broadcast(_, let sync):
            return "/v1/broadcast?sync=\(sync)"
        case .markets:
            return "/v1/markets"
        case .nodeInfo:
            return "/v1/node-info"
        }
    }

    var method: Moya.Method {
        switch self {
        case .getAccount,
             .getTransaction,
             .order,
             .markets,
             .nodeInfo:
            return .get
        case .broadcast:
            return .post
        }
    }

    var task: Task {
        switch self {
        case .getAccount, .getTransaction, .order, .markets, .nodeInfo:
            return .requestPlain
        case .broadcast(let body, _):
            return Task.requestData(body.hexString.data(using: .utf8) ?? Data())
        }
    }

    var sampleData: Data {
        return Data()
    }

    var headers: [String: String]? {
        switch self {
        case .broadcast:
            return [
                "Content-type": "text/plain",
            ]
        case .getAccount, .getTransaction, .order, .markets, .nodeInfo:
            return [
                "Content-type": "application/json",
            ]
        }
    }

models:

struct BinanceTokenBalance: Codable {
    let symbol: String
    let free: String
    let locked: String
    let frozen: String
}

struct BinanceAccount: Codable {
    let address: String
    let account_number: Int64
    let sequence: Int64
    let balances: [BinanceTokenBalance]
}

struct BinanceNodeInfo: Codable {
    struct NodeInfo: Codable {
        let network: String
    }
    let node_info: NodeInfo

    var chainID: String {
        return node_info.network
    }
}

enum BinanceOrderType: String, Codable {
    case FullyFill
    case Ack
    case PartialFill
    case IocNoFill
    case Canceled
    case Expired
    case FailedBlocking
    case FailedMatching
}

struct BinanceOrder: Codable {
    let symbol: String
    let price: String
    let quantity: String
    let cumulateQuantity: String
    let lastExecutedPrice: String
    let lastExecutedQuantity: String
    let transactionHash: String
    let status: BinanceOrderType
}

struct BinanceRPCTransaction: Codable {
    let hash: String
}

public struct BinanceTraderOrder: Codable {
    public struct Tx: Codable {
        public struct Value: Codable {
            public struct Msg: Codable {
                public struct Value: Codable {
                    public let id: String // order ID
                }
                let value: Value
            }
            let msg: [Msg]
        }
        let value: Value
    }
    let tx: Tx
}
@vikmeup vikmeup assigned vikmeup and unassigned vikmeup Aug 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant