solpay
Version:
Solana Payment API.
29 lines (28 loc) • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseTxBalanceChange = parseTxBalanceChange;
var coin_1 = require("./coin");
function parseTxBalanceChange(tx, owner, coin_type) {
var _a, _b, _c, _d;
switch (coin_type) {
case "SOL": {
var preBalances = (_a = tx.meta) === null || _a === void 0 ? void 0 : _a.preBalances;
var postBalances = (_b = tx.meta) === null || _b === void 0 ? void 0 : _b.postBalances;
var index = tx.transaction.message.accountKeys.findIndex(function (it) { return it.pubkey.equals(owner); });
if (index === -1)
return 0;
var change = ((postBalances === null || postBalances === void 0 ? void 0 : postBalances[index]) || 0) - ((preBalances === null || preBalances === void 0 ? void 0 : preBalances[index]) || 0);
return change / Math.pow(10, coin_1.Coin[coin_type].decimals);
}
default: {
var preTokenBalances = (_c = tx.meta) === null || _c === void 0 ? void 0 : _c.preTokenBalances;
var postTokenBalances = (_d = tx.meta) === null || _d === void 0 ? void 0 : _d.postTokenBalances;
var _e = [preTokenBalances, postTokenBalances].map(function (it) {
var _a;
return (_a = it === null || it === void 0 ? void 0 : it.find(function (it) { return it.owner == owner.toBase58(); })) === null || _a === void 0 ? void 0 : _a.uiTokenAmount.uiAmountString;
}), pre = _e[0], post = _e[1];
var change = Number(post || 0) - Number(pre || 0);
return change;
}
}
}