2021-09-14 08:27:45 -04:00
|
|
|
import * as grpcWeb from 'grpc-web';
|
|
|
|
import {GetVersionClient, WalletsClient, OffersClient, PaymentAccountsClient} from './protobuf/GrpcServiceClientPb';
|
2021-09-14 08:30:22 -04:00
|
|
|
import {GetVersionRequest, GetVersionReply, GetBalancesRequest, GetBalancesReply, XmrBalanceInfo, GetOffersRequest, GetOffersReply, OfferInfo, GetPaymentAccountsRequest, GetPaymentAccountsReply, CreateCryptoCurrencyPaymentAccountRequest, CreateCryptoCurrencyPaymentAccountReply, CreateOfferRequest, CreateOfferReply, CancelOfferRequest} from './protobuf/grpc_pb';
|
2021-09-14 08:27:45 -04:00
|
|
|
import {PaymentAccount} from './protobuf/pb_pb';
|
2021-09-12 09:39:21 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Haveno daemon client using gRPC.
|
|
|
|
*/
|
|
|
|
class HavenoDaemon {
|
|
|
|
|
|
|
|
// instance variables
|
|
|
|
_url: string;
|
2021-09-14 08:27:45 -04:00
|
|
|
_password: string;
|
|
|
|
_getVersionClient: GetVersionClient;
|
|
|
|
_walletsClient: WalletsClient;
|
|
|
|
_offersClient: OffersClient;
|
|
|
|
_paymentAccountsClient: PaymentAccountsClient;
|
2021-09-12 09:39:21 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a client connected to a Haveno daemon.
|
|
|
|
*
|
|
|
|
* @param {string} url - Haveno daemon url
|
|
|
|
* @param {string} password - Haveno daemon password if applicable
|
|
|
|
*/
|
2021-09-14 08:27:45 -04:00
|
|
|
constructor(url: string, password: string) {
|
2021-09-12 09:39:21 -04:00
|
|
|
this._url = url;
|
|
|
|
this._password = password;
|
2021-09-14 08:27:45 -04:00
|
|
|
this._getVersionClient = new GetVersionClient(this._url);
|
|
|
|
this._walletsClient = new WalletsClient(this._url);
|
|
|
|
this._offersClient = new OffersClient(this._url);
|
|
|
|
this._paymentAccountsClient = new PaymentAccountsClient(this._url);
|
2021-09-12 09:39:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Haveno version.
|
|
|
|
*
|
|
|
|
* @return {string} the Haveno daemon version
|
|
|
|
*/
|
|
|
|
async getVersion(): Promise<string> {
|
|
|
|
let that = this;
|
2021-09-14 08:27:45 -04:00
|
|
|
let request = new GetVersionRequest();
|
2021-09-12 09:39:21 -04:00
|
|
|
return new Promise(function(resolve, reject) {
|
2021-09-14 08:27:45 -04:00
|
|
|
that._getVersionClient.getVersion(request, {password: that._password}, function(err: grpcWeb.Error, response: GetVersionReply) {
|
2021-09-12 09:39:21 -04:00
|
|
|
if (err) reject(err);
|
|
|
|
else resolve(response.getVersion());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user's balances.
|
|
|
|
*
|
2021-09-14 08:27:45 -04:00
|
|
|
* @return {XmrBalanceInfo} the user's balances
|
2021-09-12 09:39:21 -04:00
|
|
|
*/
|
2021-09-14 08:27:45 -04:00
|
|
|
async getBalances(): Promise<XmrBalanceInfo> {
|
2021-09-12 09:39:21 -04:00
|
|
|
let that = this;
|
2021-09-14 08:27:45 -04:00
|
|
|
let request = new GetBalancesRequest();
|
2021-09-12 09:39:21 -04:00
|
|
|
return new Promise(function(resolve, reject) {
|
2021-09-14 08:27:45 -04:00
|
|
|
that._walletsClient.getBalances(request, {password: that._password}, function(err: grpcWeb.Error, response: GetBalancesReply) {
|
2021-09-12 09:39:21 -04:00
|
|
|
if (err) reject(err);
|
2021-09-15 08:06:50 -04:00
|
|
|
else resolve(response.getBalances()!.getXmr()!);
|
2021-09-12 09:39:21 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-14 08:27:45 -04:00
|
|
|
* Get available offers to buy or sell XMR.
|
2021-09-12 09:39:21 -04:00
|
|
|
*
|
|
|
|
* @param {string} direction - one of "BUY" or "SELL"
|
|
|
|
*
|
2021-09-14 08:27:45 -04:00
|
|
|
* @return {OfferInfo[]} available offers
|
2021-09-12 09:39:21 -04:00
|
|
|
*/
|
2021-09-14 08:27:45 -04:00
|
|
|
async getOffers(direction: string): Promise<OfferInfo[]> {
|
2021-09-12 09:39:21 -04:00
|
|
|
let request = new GetOffersRequest()
|
|
|
|
.setDirection(direction)
|
2021-09-15 08:06:50 -04:00
|
|
|
.setCurrencyCode("XMR");
|
2021-09-12 09:39:21 -04:00
|
|
|
let that = this;
|
|
|
|
return new Promise(function(resolve, reject) {
|
2021-09-14 08:27:45 -04:00
|
|
|
that._offersClient.getOffers(request, {password: that._password}, function(err: grpcWeb.Error, response: GetOffersReply) {
|
2021-09-12 09:39:21 -04:00
|
|
|
if (err) reject(err);
|
2021-09-14 08:27:45 -04:00
|
|
|
else resolve(response.getOffersList());
|
2021-09-12 09:39:21 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-09-14 08:27:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get user's created offers to buy or sell XMR.
|
|
|
|
*
|
|
|
|
* @param {string} direction - one of "BUY" or "SELL"
|
|
|
|
*
|
|
|
|
* @return {OfferInfo[]} the user's created offers
|
|
|
|
*/
|
|
|
|
async getMyOffers(direction: string): Promise<OfferInfo[]> {
|
|
|
|
let that = this;
|
|
|
|
let request = new GetOffersRequest()
|
|
|
|
.setDirection(direction)
|
2021-09-15 08:06:50 -04:00
|
|
|
.setCurrencyCode("XMR");
|
2021-09-14 08:27:45 -04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
that._offersClient.getMyOffers(request, {password: that._password}, function(err: grpcWeb.Error, response: GetOffersReply) {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve(response.getOffersList());
|
|
|
|
});
|
|
|
|
});
|
2021-09-12 09:39:21 -04:00
|
|
|
}
|
2021-09-14 08:27:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get payment accounts.
|
|
|
|
*
|
|
|
|
* @return {PaymentAccount[]} the payment accounts
|
|
|
|
*/
|
|
|
|
async getPaymentAccounts(): Promise<PaymentAccount[]> {
|
|
|
|
let that = this;
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
that._paymentAccountsClient.getPaymentAccounts(new GetPaymentAccountsRequest(), {password: that._password}, function(err: grpcWeb.Error, response: GetPaymentAccountsReply) {
|
|
|
|
if (err) reject(err);
|
2021-09-15 08:06:50 -04:00
|
|
|
else resolve(response.getPaymentAccountsList());
|
2021-09-14 08:27:45 -04:00
|
|
|
});
|
|
|
|
});
|
2021-09-12 09:39:21 -04:00
|
|
|
}
|
2021-09-14 08:30:22 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a crypto payment account.
|
|
|
|
*
|
|
|
|
* @param {string} accountName - description of the account
|
|
|
|
* @param {string} currencyCode - currency code of the account
|
|
|
|
* @param {string} address - payment address of the account
|
|
|
|
* @param {boolean} tradeInstant - whether or not trade can be completed quickly
|
|
|
|
* @return {PaymentAccount} the created payment account
|
|
|
|
*/
|
|
|
|
async createCryptoPaymentAccount(accountName: string,
|
|
|
|
currencyCode: string,
|
|
|
|
address: string,
|
|
|
|
tradeInstant: boolean): Promise<PaymentAccount> {
|
|
|
|
let that = this;
|
|
|
|
let request = new CreateCryptoCurrencyPaymentAccountRequest()
|
2021-09-15 08:06:50 -04:00
|
|
|
.setAccountName(accountName)
|
|
|
|
.setCurrencyCode(currencyCode)
|
2021-09-14 08:30:22 -04:00
|
|
|
.setAddress(address)
|
2021-09-15 08:06:50 -04:00
|
|
|
.setTradeInstant(tradeInstant);
|
2021-09-14 08:30:22 -04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
that._paymentAccountsClient.createCryptoCurrencyPaymentAccount(request, {password: that._password}, function(err: grpcWeb.Error, response: CreateCryptoCurrencyPaymentAccountReply) {
|
|
|
|
if (err) reject(err);
|
2021-09-15 08:06:50 -04:00
|
|
|
else resolve(response.getPaymentAccount()!);
|
2021-09-14 08:30:22 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post an offer.
|
|
|
|
*
|
|
|
|
* @param {string} currencyCode - currency code of traded pair
|
|
|
|
* @param {string} direction - one of "BUY" or "SELL"
|
|
|
|
* @param {number} price - trade price
|
|
|
|
* @param {bool} useMarketBasedPrice - base trade on market price
|
|
|
|
* @param {number} marketPriceMargin - % from market price to tolerate
|
|
|
|
* @param {bigint} amount - amount to trade
|
|
|
|
* @param {bigint} minAmount - minimum amount to trade
|
|
|
|
* @param {number} buyerSecurityDeposit - buyer security deposit as % of trade amount
|
|
|
|
* @param {string} paymentAccountId - payment account id
|
|
|
|
* @param {number} triggerPrice - price to remove offer
|
|
|
|
* @return {HavenoOffer[]} created offers
|
|
|
|
*/
|
|
|
|
async postOffer(currencyCode: string,
|
|
|
|
direction: string,
|
|
|
|
price: number,
|
|
|
|
useMarketBasedPrice: boolean,
|
|
|
|
marketPriceMargin: number,
|
|
|
|
amount: bigint,
|
|
|
|
minAmount: bigint,
|
|
|
|
buyerSecurityDeposit: number,
|
|
|
|
paymentAccountId: string,
|
|
|
|
triggerPrice?: number): Promise<OfferInfo> {
|
|
|
|
let that = this;
|
|
|
|
let request = new CreateOfferRequest()
|
2021-09-15 08:06:50 -04:00
|
|
|
.setCurrencyCode(currencyCode)
|
2021-09-14 08:30:22 -04:00
|
|
|
.setDirection(direction)
|
|
|
|
.setPrice(price.toString())
|
2021-09-15 08:06:50 -04:00
|
|
|
.setUseMarketBasedPrice(useMarketBasedPrice)
|
|
|
|
.setMarketPriceMargin(marketPriceMargin)
|
2021-09-14 08:30:22 -04:00
|
|
|
.setAmount(amount.toString())
|
2021-09-15 08:06:50 -04:00
|
|
|
.setMinAmount(minAmount.toString())
|
|
|
|
.setBuyerSecurityDeposit(buyerSecurityDeposit)
|
|
|
|
.setPaymentAccountId(paymentAccountId)
|
|
|
|
.setMakerFeeCurrencyCode("XMR");
|
|
|
|
if (triggerPrice) request.setTriggerPrice(BigInt(triggerPrice.toString()).toString());
|
2021-09-14 08:30:22 -04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
that._offersClient.createOffer(request, {password: that._password}, function(err: grpcWeb.Error, response: CreateOfferReply) {
|
|
|
|
if (err) reject(err);
|
2021-09-15 08:06:50 -04:00
|
|
|
else resolve(response.getOffer()!);
|
2021-09-14 08:30:22 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a posted offer, unreserving its funds.
|
|
|
|
*
|
|
|
|
* @param {string} id - the offer id to cancel
|
|
|
|
*/
|
2021-09-15 08:06:50 -04:00
|
|
|
async cancelOffer(id: string): Promise<void> {
|
2021-09-14 08:30:22 -04:00
|
|
|
let that = this;
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
that._offersClient.cancelOffer(new CancelOfferRequest().setId(id), {password: that._password}, function(err: grpcWeb.Error) {
|
|
|
|
if (err) reject(err);
|
|
|
|
else resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-09-12 09:39:21 -04:00
|
|
|
}
|
|
|
|
|
2021-09-14 08:27:45 -04:00
|
|
|
export {HavenoDaemon};
|