@venly/connect
Version:
Venly Connect SDK
210 lines • 9.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Api = void 0;
var tslib_1 = require("tslib");
var Utils_1 = tslib_1.__importDefault(require("../utils/Utils"));
var SecretType_1 = require("../models/SecretType");
var Api = /** @class */ (function () {
function Api(baseURL, tokenProvider) {
var _this = this;
////////////
// Chains //
////////////
this.getAvailableSecretTypes = function () {
var response = _this.fetchGet("chains");
return _this.processResponse(response);
};
////////////
// Wallet //
////////////
this.getWallets = function (params) {
params = (params && Utils_1.default.removeNulls(params)) || {};
var response = _this.fetchGet('wallets', params);
return _this.processResponse(response);
};
this.getWallet = function (walletId) {
var response = _this.fetchGet("wallets/" + walletId);
return _this.processResponse(response);
};
this.getBalance = function (walletId) {
var response = _this.fetchGet("wallets/" + walletId + "/balance");
return _this.processResponse(response);
};
this.getBalanceByAddress = function (secretType, walletAddress) {
var response = _this.fetchGet("wallets/" + secretType + "/" + walletAddress + "/balance");
return _this.processResponse(response);
};
this.getTokenBalances = function (walletId) {
var response = _this.fetchGet("wallets/" + walletId + "/balance/tokens");
return _this.processResponse(response);
};
this.getTokenBalancesByAddress = function (secretType, walletAddress) {
var response = _this.fetchGet("wallets/" + secretType + "/" + walletAddress + "/balance/tokens");
return _this.processResponse(response);
};
this.getTokenBalance = function (walletId, tokenAddress) {
var response = _this.fetchGet("wallets/" + walletId + "/balance/tokens/" + tokenAddress);
return _this.processResponse(response);
};
this.getTokenBalanceByAddress = function (secretType, walletAddress, tokenAddress) {
var response = _this.fetchGet("wallets/" + secretType + "/" + walletAddress + "/balance/tokens/" + tokenAddress);
return _this.processResponse(response);
};
this.getNonfungibles = function (walletId) {
var response = _this.fetchGet("wallets/" + walletId + "/nonfungibles");
return _this.processResponse(response);
};
this.getNonfungiblesByAddress = function (secretType, walletAddress) {
var response = _this.fetchGet("wallets/" + secretType + "/" + walletAddress + "/nonfungibles");
return _this.processResponse(response);
};
this.getAllNonfungibles = function (secretTypes) {
var queryParams = secretTypes && secretTypes.length > 0
? "?" + secretTypes.map(function (st) { return "secretType=" + st; }).join("&")
: "";
var response = _this.fetchGet("wallets/nonfungibles", queryParams);
return _this.processResponse(response);
};
this.unlink = function (walletId) {
var response = _this.fetchGet("wallets/" + walletId + "/link");
return _this.processResponse(response);
};
/////////////
// Profile //
/////////////
this.getProfile = function () {
var response = _this.fetchGet('profile');
return _this.processResponse(response);
};
//////////////////
// Transactions //
//////////////////
this.getPendingTransactions = function () {
var response = _this.fetchGet('transactions');
return _this.processResponse(response);
};
this.deleteTransaction = function (transactionId) {
var response = _this.fetchDelete("transactions/" + transactionId);
return _this.processResponse(response);
};
this.getTransactionStatus = function (transactionHash, secretType) {
var response = _this.fetchGet("transactions/" + secretType + "/" + transactionHash + "/status");
return _this.mapTransactionData(secretType, response);
};
this.mapTransactionData = function (secretType, response) {
switch (secretType) {
case SecretType_1.SecretType.AVAC || SecretType_1.SecretType.BSC || SecretType_1.SecretType.ETHEREUM || SecretType_1.SecretType.GOCHAIN || SecretType_1.SecretType.MATIC || SecretType_1.SecretType.ARBITRUM:
return _this.processResponse(response);
case SecretType_1.SecretType.HEDERA:
return _this.processResponse(response);
case SecretType_1.SecretType.TRON:
return _this.processResponse(response);
case SecretType_1.SecretType.VECHAIN:
return _this.processResponse(response);
default:
return _this.processResponse(response);
}
};
///////////////
// Contracts //
///////////////
this.readContract = function (contractReadRequest) {
var response = _this.fetchPost('contracts/read', contractReadRequest);
return _this.processResponse(response);
};
this._baseUrl = baseURL.endsWith('/') ? baseURL.substring(0, baseURL.length - 1) : baseURL;
this._tokenProvider = tokenProvider;
}
Api.prototype.fetchGet = function (url, queryParams) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var bearerToken;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
bearerToken = this._tokenProvider();
if (!bearerToken) {
throw new Error('Not authenticated');
}
return [4 /*yield*/, fetch(this._baseUrl + "/" + url + "?" + new URLSearchParams(queryParams).toString(), {
headers: { Authorization: "Bearer " + bearerToken }
}).then(function (response) { return response.json(); }).then(function (data) { return data; })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
Api.prototype.fetchPost = function (url, body) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var bearerToken;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
bearerToken = this._tokenProvider();
if (!bearerToken) {
throw new Error('Not authenticated');
}
return [4 /*yield*/, fetch(this._baseUrl + "/" + url, {
method: 'POST',
body: JSON.stringify(body),
headers: {
Authorization: "Bearer " + bearerToken,
'Content-Type': 'application/json'
}
}).then(function (response) { return response.json(); }).then(function (data) { return data; })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
Api.prototype.fetchDelete = function (url) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var bearerToken;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
bearerToken = this._tokenProvider();
if (!bearerToken) {
throw new Error('Not authenticated');
}
return [4 /*yield*/, fetch(this._baseUrl + "/" + url, {
method: 'DELETE',
headers: { Authorization: "Bearer " + bearerToken }
}).then(function (response) { return response.json(); }).then(function (data) { return data; })];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
Api.prototype.processResponse = function (responsePromise) {
return new Promise(function (resolve, reject) {
responsePromise.then(function (response) {
if (response.success) {
if (response.result) {
resolve(response.result);
}
else {
resolve();
}
}
else {
reject(response.errors);
}
})
.catch(function (error) {
if (error.response && error.response.data) {
reject(error.response.data.errors);
}
else if (error.message) {
var code = error.message.indexOf('authenticat') >= 0 ? 'auth.error' : 'unknown.error';
reject([{ code: code, message: error.message }]);
}
else {
reject([{ code: 'unknown.error', message: 'An unknown error occured' }]);
}
});
});
};
return Api;
}());
exports.Api = Api;
//# sourceMappingURL=Api.js.map