@yoroi/claim
Version:
The Claim (proof-of-onboarding) package of Yoroi SDK
72 lines (71 loc) • 2.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.asClaimToken = exports.asClaimApiError = void 0;
var _common = require("@yoroi/common");
var _portfolio = require("@yoroi/portfolio");
var _errors = require("./errors");
// if the error is a known claim api error, throw it with a more specific error message otherwise throw the api error
const asClaimApiError = error => {
const ClaimApiError = _errors.claimApiErrors.find(_ref => {
let {
statusCode
} = _ref;
return statusCode === error.status;
});
if (ClaimApiError) throw new ClaimApiError();
throw (0, _common.getApiError)(error);
};
exports.asClaimApiError = asClaimApiError;
const asClaimToken = async (claimItemResponse, primaryTokenInfo, tokenManager) => {
const {
lovelaces,
tokens,
status
} = claimItemResponse;
const ptQuantity = (0, _common.toBigInt)(lovelaces, 0, true);
// NOTE: filter out wrong token ids and pt if wrongly included
const ids = new Set(Object.keys(tokens).filter(_portfolio.isTokenId).filter(id => !(0, _portfolio.isPrimaryToken)(id)));
const infos = await tokenManager.sync({
secondaryTokenIds: Array.from(ids),
sourceId: 'claim-module'
});
const amounts = [];
if (ptQuantity > 0n) {
amounts.push({
info: primaryTokenInfo,
quantity: ptQuantity
});
}
for (const [tokenId, cachedInfo] of infos.entries()) {
if (!cachedInfo?.record || !ids.has(tokenId)) continue;
const quantity = tokens[tokenId];
if (quantity) amounts.push({
info: cachedInfo.record,
quantity: (0, _common.toBigInt)(quantity, 0, true)
});
}
if (status === 'claimed') {
const claimed = {
status: 'done',
amounts,
txHash: claimItemResponse.tx_hash
};
return claimed;
} else if (status === 'queued') {
const queued = {
status: 'processing',
amounts
};
return queued;
} else {
const accepted = {
status: 'accepted',
amounts
};
return accepted;
}
};
exports.asClaimToken = asClaimToken;
//# sourceMappingURL=transformers.js.map