UNPKG

@ajna-finance/sdk

Version:

A typescript SDK that can be used to create Dapps in Ajna ecosystem.

41 lines 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBlockTime = exports.getBlock = exports.getExpiry = void 0; const tslib_1 = require("tslib"); const constants_1 = require("../constants"); const types_1 = require("../types"); const ethers_1 = require("ethers"); // calculates expiration time based on current block timestamp const getExpiry = (signer_1, ...args_1) => tslib_1.__awaiter(void 0, [signer_1, ...args_1], void 0, function* (signer, ttlSeconds = constants_1.DEFAULT_TTL) { // TODO: Make the default timeout DEFAULT_TTL configurable in the SDK // TODO: Look into whether we can subscribe to block updates and // asynchronously maintain the current block height and timestamp. return (yield (0, exports.getBlockTime)(signer)) + ttlSeconds; }); exports.getExpiry = getExpiry; const getBlock = (signer) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const provider = getProvider(signer); let blockNumber = yield provider.getBlockNumber(); let block = yield provider.getBlock(blockNumber); // handle inconsistencies for tests using evm_revert and evm_increaseTime while (!block) { block = yield provider.getBlock(--blockNumber); } return block; }); exports.getBlock = getBlock; const getBlockTime = (signer) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const block = yield (0, exports.getBlock)(signer); return block.timestamp; }); exports.getBlockTime = getBlockTime; function getProvider(signer) { if (!(signer instanceof ethers_1.Signer)) { return signer; } if (signer.provider) { return signer.provider; } throw new types_1.SdkError('Provider cannot be empty'); } //# sourceMappingURL=time.js.map