@swipewallet/venus-js
Version:
A JavaScript SDK for Ethereum and the Venus Protocol.
396 lines • 19.5 kB
JavaScript
"use strict";
/**
* @file cToken
* @desc These methods facilitate interactions with the cToken smart
* contracts.
*/
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
exports.repayBorrow = exports.borrow = exports.redeem = exports.supply = void 0;
var ethers_1 = require("ethers");
var eth = require("./eth");
var helpers_1 = require("./helpers");
var constants_1 = require("./constants");
/**
* Supplies the user's Binance Smart Chain asset to the Venus Protocol.
*
* @param {string} asset A string of the asset to supply.
* @param {number | string | BigNumber} amount A string, number, or BigNumber
* object of the amount of an asset to supply. Use the `mantissa` boolean in
* the `options` parameter to indicate if this value is scaled up (so there
* are no decimals) or in its natural scale.
* @param {boolean} noApprove Explicitly prevent this method from attempting an
* BEP-20 `approve` transaction prior to sending the `mint` transaction.
* @param {CallOptions} [options] Call options and Ethers.js overrides for the
* transaction. A passed `gasLimit` will be used in both the `approve` (if
* not supressed) and `mint` transactions.
*
* @returns {object} Returns an Ethers.js transaction object of the supply
* transaction.
*
* @example
*
* ```
* const venus = new Venus(window.ethereum);
*
* // Ethers.js overrides are an optional 3rd parameter for `supply`
* // const trxOptions = { gasLimit: 250000, mantissa: false };
*
* (async function() {
*
* console.log('Supplying SXP to the Venus Protocol...');
* const trx = await venus.supply(Venus.SXP, 1);
* console.log('Ethers.js transaction object', trx);
*
* })().catch(console.error);
* ```
*/
function supply(asset, amount, noApprove, options) {
if (noApprove === void 0) { noApprove = false; }
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var errorPrefix, vTokenName, vTokenAddress, underlyingAddress, userAddress, allowance, notEnough, parameters;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, helpers_1.netId(this)];
case 1:
_a.sent();
errorPrefix = 'Venus [supply] | ';
vTokenName = 'v' + asset;
vTokenAddress = constants_1.address[this._network.name][vTokenName];
if (!vTokenAddress || !constants_1.underlyings.includes(asset)) {
throw Error(errorPrefix + 'Argument `asset` cannot be supplied.');
}
if (typeof amount !== 'number' &&
typeof amount !== 'string' &&
!ethers_1.ethers.BigNumber.isBigNumber(amount)) {
throw Error(errorPrefix + 'Argument `amount` must be a string, number, or BigNumber.');
}
if (!options.mantissa) {
amount = +amount;
amount = amount * Math.pow(10, constants_1.decimals[asset]);
}
amount = ethers_1.ethers.BigNumber.from(amount.toString());
// if (vTokenName === constants.cETH) {
// options.abi = abi.cEther;
// } else {
// options.abi = abi.cErc20;
// }
if (vTokenName === constants_1.constants.vBNB) {
options.abi = constants_1.abi.vBNB;
}
else {
options.abi = constants_1.abi.vBep20;
}
options._compoundProvider = this._provider;
if (!(vTokenName !== constants_1.constants.vBNB && noApprove !== true)) return [3 /*break*/, 4];
underlyingAddress = constants_1.address[this._network.name][asset];
userAddress = this._provider.address;
return [4 /*yield*/, eth.read(underlyingAddress, 'allowance', [userAddress, vTokenAddress], options)];
case 2:
allowance = _a.sent();
notEnough = allowance.lt(amount);
if (!notEnough) return [3 /*break*/, 4];
// ERC-20 approve transaction
return [4 /*yield*/, eth.trx(underlyingAddress, 'approve', [vTokenAddress, amount], options)];
case 3:
// ERC-20 approve transaction
_a.sent();
_a.label = 4;
case 4:
parameters = [];
if (vTokenName === constants_1.constants.vBNB) {
options.value = amount;
}
else {
parameters.push(amount);
}
return [2 /*return*/, eth.trx(vTokenAddress, 'mint', parameters, options)];
}
});
});
}
exports.supply = supply;
/**
* Redeems the user's Binance Smart Chain asset from the Venus Protocol.
*
* @param {string} asset A string of the asset to redeem, or its vToken name.
* @param {number | string | BigNumber} amount A string, number, or BigNumber
* object of the amount of an asset to redeem. Use the `mantissa` boolean in
* the `options` parameter to indicate if this value is scaled up (so there
* are no decimals) or in its natural scale. This can be an amount of
* vTokens or underlying asset (use the `asset` parameter to specify).
* @param {CallOptions} [options] Call options and Ethers.js overrides for the
* transaction.
*
* @returns {object} Returns an Ethers.js transaction object of the redeem
* transaction.
*
* @example
*
* ```
* const venus = new Venus(window.ethereum);
*
* (async function() {
*
* console.log('Redeeming SXP...');
* const trx = await venus.redeem(Venus.SXP, 1); // also accepts vToken args
* console.log('Ethers.js transaction object', trx);
*
* })().catch(console.error);
* ```
*/
function redeem(asset, amount, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var errorPrefix, assetIsVToken, vTokenName, vTokenAddress, underlyingName, trxOptions, parameters, method;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, helpers_1.netId(this)];
case 1:
_a.sent();
errorPrefix = 'Venus [redeem] | ';
if (typeof asset !== 'string' || asset.length < 1) {
throw Error(errorPrefix + 'Argument `asset` must be a non-empty string.');
}
assetIsVToken = asset[0] === 'v';
vTokenName = assetIsVToken ? asset : 'v' + asset;
vTokenAddress = constants_1.address[this._network.name][vTokenName];
underlyingName = assetIsVToken ? asset.slice(1, asset.length) : asset;
if (!constants_1.cTokens.includes(vTokenName) || !constants_1.underlyings.includes(underlyingName)) {
throw Error(errorPrefix + 'Argument `asset` is not supported.');
}
if (typeof amount !== 'number' &&
typeof amount !== 'string' &&
!ethers_1.ethers.BigNumber.isBigNumber(amount)) {
throw Error(errorPrefix + 'Argument `amount` must be a string, number, or BigNumber.');
}
if (!options.mantissa) {
amount = +amount;
amount = amount * Math.pow(10, constants_1.decimals[asset]);
}
amount = ethers_1.ethers.BigNumber.from(amount.toString());
trxOptions = __assign(__assign({}, options), { _compoundProvider: this._provider, abi: vTokenName === constants_1.constants.vBNB ? constants_1.abi.vBNB : constants_1.abi.vBep20 });
parameters = [amount];
method = assetIsVToken ? 'redeem' : 'redeemUnderlying';
return [2 /*return*/, eth.trx(vTokenAddress, method, parameters, trxOptions)];
}
});
});
}
exports.redeem = redeem;
/**
* Borrows an Binance Smart Chain asset from the Venus Protocol for the user.
* The user's address must first have supplied collateral and entered a
* corresponding market.
*
* @param {string} asset A string of the asset to borrow (must be a supported
* underlying asset).
* @param {number | string | BigNumber} amount A string, number, or BigNumber
* object of the amount of an asset to borrow. Use the `mantissa` boolean in
* the `options` parameter to indicate if this value is scaled up (so there
* are no decimals) or in its natural scale.
* @param {CallOptions} [options] Call options and Ethers.js overrides for the
* transaction.
*
* @returns {object} Returns an Ethers.js transaction object of the borrow
* transaction.
*
* @example
*
* ```
* const venus = new Venus(window.ethereum);
*
* (async function() {
*
* const sxpScaledUp = '32000000000000000000';
* const trxOptions = { mantissa: true };
*
* console.log('Borrowing 32 SXP...');
* const trx = await venus.borrow(Venus.SXP, sxpScaledUp, trxOptions);
*
* console.log('Ethers.js transaction object', trx);
*
* })().catch(console.error);
* ```
*/
function borrow(asset, amount, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var errorPrefix, vTokenName, vTokenAddress, trxOptions, parameters;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, helpers_1.netId(this)];
case 1:
_a.sent();
errorPrefix = 'Venus [borrow] | ';
vTokenName = 'v' + asset;
vTokenAddress = constants_1.address[this._network.name][vTokenName];
if (!vTokenAddress || !constants_1.underlyings.includes(asset)) {
throw Error(errorPrefix + 'Argument `asset` cannot be borrowed.');
}
if (typeof amount !== 'number' &&
typeof amount !== 'string' &&
!ethers_1.ethers.BigNumber.isBigNumber(amount)) {
throw Error(errorPrefix + 'Argument `amount` must be a string, number, or BigNumber.');
}
if (!options.mantissa) {
amount = +amount;
amount = amount * Math.pow(10, constants_1.decimals[asset]);
}
amount = ethers_1.ethers.BigNumber.from(amount.toString());
trxOptions = __assign(__assign({}, options), { _compoundProvider: this._provider });
parameters = [amount];
trxOptions.abi = vTokenName === constants_1.constants.vBNB ? constants_1.abi.vBNB : constants_1.abi.vBep20;
return [2 /*return*/, eth.trx(vTokenAddress, 'borrow', parameters, trxOptions)];
}
});
});
}
exports.borrow = borrow;
/**
* Repays a borrowed Binance Smart Chain asset for the user or on behalf of
* another Binance Smart Chain address.
*
* @param {string} asset A string of the asset that was borrowed (must be a
* supported underlying asset).
* @param {number | string | BigNumber} amount A string, number, or BigNumber
* object of the amount of an asset to borrow. Use the `mantissa` boolean in
* the `options` parameter to indicate if this value is scaled up (so there
* are no decimals) or in its natural scale.
* @param {string | null} [borrower] The Binance Smart Chain address of the borrower
* to repay an open borrow for. Set this to `null` if the user is repaying
* their own borrow.
* @param {boolean} noApprove Explicitly prevent this method from attempting an
* ERC-20 `approve` transaction prior to sending the subsequent repayment
* transaction.
* @param {CallOptions} [options] Call options and Ethers.js overrides for the
* transaction. A passed `gasLimit` will be used in both the `approve` (if
* not supressed) and `repayBorrow` or `repayBorrowBehalf` transactions.
*
* @returns {object} Returns an Ethers.js transaction object of the repayBorrow
* or repayBorrowBehalf transaction.
*
* @example
*
* ```
* const venus = new Venus(window.ethereum);
*
* (async function() {
*
* console.log('Repaying SXP borrow...');
* const address = null; // set this to any address to repayBorrowBehalf
* const trx = await venus.repayBorrow(Venus.SXP, 32, address);
*
* console.log('Ethers.js transaction object', trx);
*
* })().catch(console.error);
* ```
*/
function repayBorrow(asset, amount, borrower, noApprove, options) {
if (noApprove === void 0) { noApprove = false; }
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var errorPrefix, vTokenName, vTokenAddress, method, trxOptions, parameters, underlyingAddress, userAddress, allowance, notEnough;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, helpers_1.netId(this)];
case 1:
_a.sent();
errorPrefix = 'Venus [repayBorrow] | ';
vTokenName = 'v' + asset;
vTokenAddress = constants_1.address[this._network.name][vTokenName];
if (!vTokenAddress || !constants_1.underlyings.includes(asset)) {
throw Error(errorPrefix + 'Argument `asset` is not supported.');
}
if (typeof amount !== 'number' &&
typeof amount !== 'string' &&
!ethers_1.ethers.BigNumber.isBigNumber(amount)) {
throw Error(errorPrefix + 'Argument `amount` must be a string, number, or BigNumber.');
}
method = ethers_1.ethers.utils.isAddress(borrower) ? 'repayBorrowBehalf' : 'repayBorrow';
if (borrower && method === 'repayBorrow') {
throw Error(errorPrefix + 'Invalid `borrower` address.');
}
if (!options.mantissa) {
amount = +amount;
amount = amount * Math.pow(10, constants_1.decimals[asset]);
}
amount = ethers_1.ethers.BigNumber.from(amount.toString());
trxOptions = __assign(__assign({}, options), { _compoundProvider: this._provider });
parameters = method === 'repayBorrowBehalf' ? [borrower] : [];
if (vTokenName === constants_1.constants.vBNB) {
trxOptions.value = amount;
trxOptions.abi = constants_1.abi.vBNB;
}
else {
parameters.push(amount);
trxOptions.abi = constants_1.abi.vBep20;
}
if (!(vTokenName !== constants_1.constants.vBNB && noApprove !== true)) return [3 /*break*/, 4];
underlyingAddress = constants_1.address[this._network.name][asset];
userAddress = this._provider.address;
return [4 /*yield*/, eth.read(underlyingAddress, 'allowance', [userAddress, vTokenAddress], trxOptions)];
case 2:
allowance = _a.sent();
notEnough = allowance.lt(amount);
if (!notEnough) return [3 /*break*/, 4];
// ERC-20 approve transaction
return [4 /*yield*/, eth.trx(underlyingAddress, 'approve', [vTokenAddress, amount], trxOptions)];
case 3:
// ERC-20 approve transaction
_a.sent();
_a.label = 4;
case 4: return [2 /*return*/, eth.trx(vTokenAddress, method, parameters, trxOptions)];
}
});
});
}
exports.repayBorrow = repayBorrow;
//# sourceMappingURL=cToken.js.map