@rubysdk/secret-client
Version:
RubySDK Secret Network client utilities for data, token and escrow operations
77 lines • 2.8 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.receivedSecretAllowances = receivedSecretAllowances;
exports.grantedSecretAllowances = grantedSecretAllowances;
const config_1 = require("@/config");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
bignumber_js_1.default.config({
DECIMAL_PLACES: 18,
ROUNDING_MODE: bignumber_js_1.default.ROUND_DOWN,
EXPONENTIAL_AT: [-18, 36],
});
function extractSpendLimit(allowance) {
if (allowance['@type'] === '/cosmos.feegrant.v1beta1.BasicAllowance') {
return allowance.spend_limit;
}
else if (allowance['@type'] === '/cosmos.feegrant.v1beta1.AllowedMsgAllowance') {
return allowance.allowance.spend_limit;
}
return null;
}
function processAllowance(allowance) {
const spendLimit = extractSpendLimit(allowance.allowance);
const relevantSpendLimit = spendLimit?.find((coin) => coin.denom === config_1.SecretChainInfo.feeCurrencies[0].coinMinimalDenom);
if (!relevantSpendLimit) {
return {
granter: allowance.granter,
grantee: allowance.grantee,
amount: null,
type: allowance.allowance['@type'],
};
}
const exp = config_1.SecretChainInfo.feeCurrencies[0].coinDecimals;
const rawAmount = new bignumber_js_1.default(relevantSpendLimit.amount);
const amount = rawAmount.shiftedBy(-exp);
return {
granter: allowance.granter,
grantee: allowance.grantee,
amount: amount,
type: allowance.allowance['@type'],
};
}
async function receivedSecretAllowances(client, granteeAddress) {
try {
const response = await client.query.feegrant.allowances({
grantee: granteeAddress,
});
const allowances = response.allowances;
if (!allowances || allowances.length === 0) {
return []; // No allowances found
}
return allowances.map(processAllowance);
}
catch (error) {
console.error('Error fetching allowances:', error);
throw error;
}
}
async function grantedSecretAllowances(client, granterAddress) {
try {
const response = await client.query.feegrant.allowancesByGranter({
granter: granterAddress,
});
const allowances = response.allowances;
if (!allowances || allowances.length === 0) {
return []; // No allowances found
}
return allowances.map(processAllowance);
}
catch (error) {
console.error('Error fetching granted allowances:', error);
throw error;
}
}
//# sourceMappingURL=get-allowances.js.map