@superfluid-finance/sdk-core
Version:
SDK Core for building with Superfluid Protocol
352 lines • 17.1 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ethers_1 = require("ethers");
const Host_1 = __importDefault(require("./Host"));
const SFError_1 = require("./SFError");
const SuperfluidAgreement_1 = __importDefault(require("./SuperfluidAgreement"));
const SuperfluidPool_1 = __importDefault(require("./SuperfluidPool"));
const typechain_types_1 = require("./typechain-types");
const utils_1 = require("./utils");
const gdaInterface = typechain_types_1.IGeneralDistributionAgreementV1__factory.createInterface();
/**
* General Distribution Agreement V1 Helper Class
* @description A helper class to interact with the GDAV1 contract.
*/
class GeneralDistributionAgreementV1 extends SuperfluidAgreement_1.default {
constructor(hostAddress, gdaV1Address, gdaV1ForwarderAddress) {
super();
/**
* Retrieves the net flow for a specific token and account.
*
* @param token The token address.
* @param account The account address.
* @param providerOrSigner A provider or signer object
* @returns The net flow of the account for the token.
*/
this.getNetFlow = async (params) => {
const normalizedToken = (0, utils_1.normalizeAddress)(params.token);
const normalizedAccount = (0, utils_1.normalizeAddress)(params.account);
try {
return (await this.contract
.connect(params.providerOrSigner)
.getNetFlow(normalizedToken, normalizedAccount)).toString();
}
catch (err) {
throw new SFError_1.SFError({
type: "GDAV1_READ",
message: "There was an error getting the GDA net flow.",
cause: err,
});
}
};
/**
* Retrieves the flow rate for a specific token, sender, and pool.
*
* @param token The token address.
* @param from The sender address.
* @param pool The pool address.
* @param providerOrSigner A provider or signer object
* @returns The flow rate from the sender to the pool for the token.
*/
this.getFlowRate = async (params) => {
const normalizedToken = (0, utils_1.normalizeAddress)(params.token);
const normalizedFrom = (0, utils_1.normalizeAddress)(params.from);
const normalizedPool = (0, utils_1.normalizeAddress)(params.pool);
try {
return (await this.contract
.connect(params.providerOrSigner)
.getFlowRate(normalizedToken, normalizedFrom, normalizedPool)).toString();
}
catch (err) {
throw new SFError_1.SFError({
type: "GDAV1_READ",
message: "There was an error getting the GDA flow rate.",
cause: err,
});
}
};
/**
* Estimates the flow distribution's actual flow rate for a specific token, sender, and pool.
*
* @param token The token address.
* @param from The sender address.
* @param pool The pool address.
* @param requestedFlowRate The requested flow rate.
* @param providerOrSigner A provider or signer object
* @returns The flow distribution's actual flow rate and the total distribution flow rate for the pool.
*/
this.estimateFlowDistributionActualFlowRate = async (params) => {
const normalizedToken = (0, utils_1.normalizeAddress)(params.token);
const normalizedFrom = (0, utils_1.normalizeAddress)(params.from);
const normalizedPool = (0, utils_1.normalizeAddress)(params.pool);
try {
const data = await this.contract
.connect(params.providerOrSigner)
.estimateFlowDistributionActualFlowRate(normalizedToken, normalizedFrom, normalizedPool, params.requestedFlowRate);
return {
actualFlowRate: data.actualFlowRate.toString(),
totalDistributionFlowRate: data.totalDistributionFlowRate.toString(),
};
}
catch (err) {
throw new SFError_1.SFError({
type: "GDAV1_READ",
message: "There was an error estimating the GDA flow distribution's actual flow rate.",
cause: err,
});
}
};
/**
* Estimates the distribution's actual amount for a specific token, sender, and pool.
*
* @param token The token address.
* @param from The sender address.
* @param pool The pool address.
* @param requestedAmount The requested amount.
* @param providerOrSigner A provider or signer object
* @returns The actual amount that will be distributed.
*/
this.estimateDistributionActualAmount = async (params) => {
const normalizedToken = (0, utils_1.normalizeAddress)(params.token);
const normalizedFrom = (0, utils_1.normalizeAddress)(params.from);
const normalizedPool = (0, utils_1.normalizeAddress)(params.pool);
try {
return (await this.contract
.connect(params.providerOrSigner)
.estimateDistributionActualAmount(normalizedToken, normalizedFrom, normalizedPool, params.requestedAmount)).toString();
}
catch (err) {
throw new SFError_1.SFError({
type: "GDAV1_READ",
message: "There was an error estimating the GDA distribution's actual amount.",
cause: err,
});
}
};
/**
* Retrieves the pool adjustment flow rate for a specific token and pool.
*
* @param token The token address.
* @param pool The pool address.
* @param providerOrSigner A provider or signer object
* @returns The pool adjustment flow rate for the token and pool.
*/
this.getPoolAdjustmentFlowRate = async (params) => {
const normalizedPool = (0, utils_1.normalizeAddress)(params.pool);
try {
return (await this.contract
.connect(params.providerOrSigner)
.getPoolAdjustmentFlowRate(normalizedPool)).toString();
}
catch (err) {
throw new SFError_1.SFError({
type: "GDAV1_READ",
message: "There was an error getting the GDA pool adjustment flow rate.",
cause: err,
});
}
};
/**
* Checks if a given token and account form a pool.
*
* @param token The token address.
* @param account The account address.
* @param providerOrSigner A provider or signer object
* @returns Whether the account is a pool for the token.
*/
this.isPool = async (params) => {
const normalizedToken = (0, utils_1.normalizeAddress)(params.token);
const normalizedAccount = (0, utils_1.normalizeAddress)(params.account);
try {
return await this.contract
.connect(params.providerOrSigner)
.isPool(normalizedToken, normalizedAccount);
}
catch (err) {
throw new SFError_1.SFError({
type: "GDAV1_READ",
message: "There was an error checking if the account is a pool.",
cause: err,
});
}
};
/**
* Checks if a member is connected to a specific pool.
*
* @param pool The pool address.
* @param member The member address.
* @param providerOrSigner A provider or signer object
* @returns Whether the member is connected to the pool.
*/
this.isMemberConnected = async (params) => {
const normalizedPool = (0, utils_1.normalizeAddress)(params.pool);
const normalizedMember = (0, utils_1.normalizeAddress)(params.member);
try {
return await this.contract
.connect(params.providerOrSigner)
.isMemberConnected(normalizedPool, normalizedMember);
}
catch (err) {
throw new SFError_1.SFError({
type: "GDAV1_READ",
message: "There was an error checking if the member is connected to the pool.",
cause: err,
});
}
};
/**
* Retrieves the pool adjustment flow information for a specific pool.
*
* @param pool The address of the pool.
* @param providerOrSigner A provider or signer object
* @returns The recipient of the pool adjustment flow, the flow hash and the rate of the adjustment flow.
*/
this.getPoolAdjustmentFlowInfo = async (params) => {
const normalizedPool = (0, utils_1.normalizeAddress)(params.pool);
try {
const data = await this.contract
.connect(params.providerOrSigner)
.getPoolAdjustmentFlowInfo(normalizedPool);
return {
recipient: data[0],
flowHash: data[1],
flowRate: data[2].toString(),
};
}
catch (err) {
throw new SFError_1.SFError({
type: "GDAV1_READ",
message: "There was an error getting the GDA pool adjustment flow information.",
cause: err,
});
}
};
/**
* Creates a new pool with the given token and admin.
*
* @param token The token address.
* @param admin The admin address.
* @returns CreatePoolTxn and SuperfluidPool instance
*/
this.createPool = async (params) => {
var _a, _b;
const normalizedToken = (0, utils_1.normalizeAddress)(params.token);
const normalizedAdmin = (0, utils_1.normalizeAddress)(params.admin);
try {
const createPoolTxn = await this.contract
.connect(params.signer)
.createPool(normalizedToken, normalizedAdmin, params.config);
const txnReceipt = await createPoolTxn.wait();
const poolCreatedEvent = (_a = txnReceipt.events) === null || _a === void 0 ? void 0 : _a.find((x) => x.event === "PoolCreated");
const poolAddress = ((_b = poolCreatedEvent === null || poolCreatedEvent === void 0 ? void 0 : poolCreatedEvent.args) === null || _b === void 0 ? void 0 : _b.pool) || ethers_1.ethers.constants.AddressZero;
return {
createPoolTxn,
pool: new SuperfluidPool_1.default(poolAddress),
};
}
catch (err) {
throw new SFError_1.SFError({
type: "GDAV1_WRITE",
message: "There was an error creating the GDA pool.",
cause: err,
});
}
};
/**
* Connects a pool to the contract.
*
* @param pool The pool address.
* @param userData The user data.
* @param overrides The transaction overrides.
* @returns The call agreement operation result.
*/
this.connectPool = (params) => {
const normalizedPool = (0, utils_1.normalizeAddress)(params.pool);
const callData = gdaInterface.encodeFunctionData("connectPool", [
normalizedPool,
"0x",
]);
const callAgreementOperation = this.host.callAgreement(this.contract.address, callData, params.userData || "0x", params.overrides);
const forwarderPopulatedTxnPromise = this.forwarder.populateTransaction.connectPool(normalizedPool, params.userData || "0x", params.overrides || {});
return this._getCallAgreementOperation(callAgreementOperation, forwarderPopulatedTxnPromise, params.shouldUseCallAgreement);
};
/**
* Disconnects a pool from the contract.
*
* @param pool The pool address.
* @param userData The user data.
* @param overrides The transaction overrides.
* @returns The call agreement operation result.
*/
this.disconnectPool = (params) => {
const normalizedPool = (0, utils_1.normalizeAddress)(params.pool);
const callData = gdaInterface.encodeFunctionData("disconnectPool", [
normalizedPool,
"0x",
]);
const callAgreementOperation = this.host.callAgreement(this.contract.address, callData, params.userData || "0x", params.overrides);
const forwarderPopulatedTxnPromise = this.forwarder.populateTransaction.disconnectPool(normalizedPool, params.userData || "0x", params.overrides || {});
return this._getCallAgreementOperation(callAgreementOperation, forwarderPopulatedTxnPromise, params.shouldUseCallAgreement);
};
/**
* Distributes funds from the sender's account to the specified pool.
*
* @param token The token address.
* @param from The sender's address.
* @param pool The pool address.
* @param requestedAmount The requested amount to distribute.
* @param userData The user data.
* @param overrides The transaction overrides.
* @returns The call agreement operation result.
*/
this.distribute = (params) => {
const normalizedToken = (0, utils_1.normalizeAddress)(params.token);
const normalizedFrom = (0, utils_1.normalizeAddress)(params.from);
const normalizedPool = (0, utils_1.normalizeAddress)(params.pool);
const callData = gdaInterface.encodeFunctionData("distribute", [
normalizedToken,
normalizedFrom,
normalizedPool,
params.requestedAmount,
"0x",
]);
const callAgreementOperation = this.host.callAgreement(this.contract.address, callData, params.userData || "0x", params.overrides);
const forwarderPopulatedTxnPromise = this.forwarder.populateTransaction.distribute(normalizedToken, normalizedFrom, normalizedPool, params.requestedAmount, params.userData || "0x", params.overrides || {});
return this._getCallAgreementOperation(callAgreementOperation, forwarderPopulatedTxnPromise, params.shouldUseCallAgreement);
};
/**
* Distributes the flow from the sender's account to the specified pool.
*
* @param token The token address.
* @param from The sender's address.
* @param pool The pool address.
* @param requestedFlowRate The requested flow rate.
* @param userData The user data.
* @param overrides The transaction overrides.
* @returns The call agreement operation result.
*/
this.distributeFlow = (params) => {
const normalizedToken = (0, utils_1.normalizeAddress)(params.token);
const normalizedFrom = (0, utils_1.normalizeAddress)(params.from);
const normalizedPool = (0, utils_1.normalizeAddress)(params.pool);
const callData = gdaInterface.encodeFunctionData("distributeFlow", [
normalizedToken,
normalizedFrom,
normalizedPool,
params.requestedFlowRate,
"0x",
]);
const callAgreementOperation = this.host.callAgreement(this.contract.address, callData, params.userData || "0x", params.overrides);
const forwarderPopulatedTxnPromise = this.forwarder.populateTransaction.distributeFlow(normalizedToken, normalizedFrom, normalizedPool, params.requestedFlowRate, params.userData || "0x", params.overrides || {});
return this._getCallAgreementOperation(callAgreementOperation, forwarderPopulatedTxnPromise, params.shouldUseCallAgreement);
};
this.host = new Host_1.default(hostAddress);
this.contract = new ethers_1.ethers.Contract(gdaV1Address, typechain_types_1.IGeneralDistributionAgreementV1__factory.abi);
this.forwarder = new ethers_1.ethers.Contract(gdaV1ForwarderAddress, typechain_types_1.GDAv1Forwarder__factory.abi);
}
}
exports.default = GeneralDistributionAgreementV1;
//# sourceMappingURL=GeneralDistributionAgreementV1.js.map