@huma-finance/sdk 0.0.10 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +660 -660
- package/README.md +30 -30
- package/dist/cjs/helpers/BaseCreditPoolReceivableContractHelper.d.ts +10 -0
- package/dist/cjs/helpers/BaseCreditPoolReceivableContractHelper.js +22 -0
- package/dist/cjs/helpers/BaseCreditPoolReceivableContractHelper.js.map +1 -0
- package/dist/cjs/helpers/ERC20TransferableReceivableContractHelper.d.ts +10 -0
- package/dist/cjs/helpers/ERC20TransferableReceivableContractHelper.js +22 -0
- package/dist/cjs/helpers/ERC20TransferableReceivableContractHelper.js.map +1 -0
- package/dist/cjs/helpers/PoolContractHelper.d.ts +12 -0
- package/dist/cjs/helpers/PoolContractHelper.js +21 -0
- package/dist/cjs/helpers/PoolContractHelper.js.map +1 -0
- package/dist/cjs/helpers/RealWorldReceivableContractHelper.d.ts +10 -0
- package/dist/cjs/helpers/RealWorldReceivableContractHelper.js +22 -0
- package/dist/cjs/helpers/RealWorldReceivableContractHelper.js.map +1 -0
- package/dist/cjs/helpers/index.d.ts +3 -0
- package/dist/cjs/helpers/index.js +7 -0
- package/dist/cjs/helpers/index.js.map +1 -0
- package/dist/cjs/hooks/index.d.ts +4 -0
- package/dist/cjs/hooks/index.js +8 -0
- package/dist/cjs/hooks/index.js.map +1 -0
- package/dist/cjs/hooks/useBaseCreditPoolReceivableContract.d.ts +10 -0
- package/dist/cjs/hooks/useBaseCreditPoolReceivableContract.js +20 -0
- package/dist/cjs/hooks/useBaseCreditPoolReceivableContract.js.map +1 -0
- package/dist/cjs/hooks/useContract.d.ts +13 -0
- package/dist/cjs/hooks/useContract.js +31 -0
- package/dist/cjs/hooks/useContract.js.map +1 -0
- package/dist/cjs/hooks/useERC20TransferableReceivableContract.d.ts +12 -0
- package/dist/cjs/hooks/useERC20TransferableReceivableContract.js +21 -0
- package/dist/cjs/hooks/useERC20TransferableReceivableContract.js.map +1 -0
- package/dist/cjs/hooks/usePoolContract.d.ts +60 -0
- package/dist/cjs/hooks/usePoolContract.js +54 -0
- package/dist/cjs/hooks/usePoolContract.js.map +1 -0
- package/dist/cjs/hooks/useRealWorldReceivableContract.d.ts +10 -0
- package/dist/cjs/hooks/useRealWorldReceivableContract.js +20 -0
- package/dist/cjs/hooks/useRealWorldReceivableContract.js.map +1 -0
- package/dist/cjs/hooks/useReceivableFactoringPoolContract.d.ts +10 -0
- package/dist/cjs/hooks/useReceivableFactoringPoolContract.js +17 -0
- package/dist/cjs/hooks/useReceivableFactoringPoolContract.js.map +1 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +8 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/services/ARWeaveService.d.ts +98 -0
- package/dist/cjs/services/ARWeaveService.js +175 -0
- package/dist/cjs/services/ARWeaveService.js.map +1 -0
- package/dist/cjs/services/EAService.d.ts +18 -0
- package/dist/cjs/services/EAService.js +13 -0
- package/dist/cjs/services/EAService.js.map +1 -0
- package/dist/cjs/services/ReceivableService.d.ts +76 -0
- package/dist/cjs/services/ReceivableService.js +138 -0
- package/dist/cjs/services/ReceivableService.js.map +1 -0
- package/dist/cjs/services/SubgraphService.d.ts +53 -0
- package/dist/cjs/services/SubgraphService.js +101 -0
- package/dist/cjs/services/SubgraphService.js.map +1 -0
- package/dist/cjs/services/index.d.ts +4 -0
- package/dist/cjs/services/index.js +8 -0
- package/dist/cjs/services/index.js.map +1 -0
- package/dist/cjs/utils/chain.d.ts +14 -0
- package/dist/cjs/utils/chain.js +34 -0
- package/dist/cjs/utils/chain.js.map +1 -0
- package/dist/cjs/utils/index.d.ts +3 -0
- package/dist/cjs/utils/index.js +7 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/cjs/utils/poolInfo.d.ts +9 -0
- package/dist/cjs/utils/poolInfo.js +14 -0
- package/dist/cjs/utils/poolInfo.js.map +1 -0
- package/dist/cjs/utils/web3.d.ts +4 -0
- package/dist/cjs/utils/web3.js +29 -0
- package/dist/cjs/utils/web3.js.map +1 -0
- package/dist/helpers/BaseCreditPoolReceivableContractHelper.d.ts +10 -0
- package/dist/helpers/BaseCreditPoolReceivableContractHelper.js +18 -0
- package/dist/helpers/BaseCreditPoolReceivableContractHelper.js.map +1 -0
- package/dist/helpers/ERC20TransferableReceivableContractHelper.d.ts +10 -0
- package/dist/helpers/ERC20TransferableReceivableContractHelper.js +18 -0
- package/dist/helpers/ERC20TransferableReceivableContractHelper.js.map +1 -0
- package/dist/helpers/PoolContractHelper.d.ts +12 -0
- package/dist/helpers/PoolContractHelper.js +17 -0
- package/dist/helpers/PoolContractHelper.js.map +1 -0
- package/dist/helpers/RealWorldReceivableContractHelper.d.ts +10 -0
- package/dist/helpers/RealWorldReceivableContractHelper.js +18 -0
- package/dist/helpers/RealWorldReceivableContractHelper.js.map +1 -0
- package/dist/helpers/index.d.ts +3 -0
- package/dist/helpers/index.js +4 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useBaseCreditPoolReceivableContract.d.ts +10 -0
- package/dist/hooks/useBaseCreditPoolReceivableContract.js +16 -0
- package/dist/hooks/useBaseCreditPoolReceivableContract.js.map +1 -0
- package/dist/hooks/useContract.d.ts +13 -0
- package/dist/hooks/useContract.js +27 -0
- package/dist/hooks/useContract.js.map +1 -0
- package/dist/hooks/useERC20TransferableReceivableContract.d.ts +12 -0
- package/dist/hooks/useERC20TransferableReceivableContract.js +17 -0
- package/dist/hooks/useERC20TransferableReceivableContract.js.map +1 -0
- package/dist/hooks/usePool.d.ts +0 -0
- package/dist/hooks/usePool.js +2 -0
- package/dist/hooks/usePool.js.map +1 -0
- package/dist/hooks/usePoolContract.d.ts +60 -0
- package/dist/hooks/usePoolContract.js +49 -0
- package/dist/hooks/usePoolContract.js.map +1 -0
- package/dist/hooks/useRealWorldReceivableContract.d.ts +10 -0
- package/dist/hooks/useRealWorldReceivableContract.js +16 -0
- package/dist/hooks/useRealWorldReceivableContract.js.map +1 -0
- package/dist/hooks/useReceivableFactoringPoolContract.d.ts +10 -0
- package/dist/hooks/useReceivableFactoringPoolContract.js +13 -0
- package/dist/hooks/useReceivableFactoringPoolContract.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/services/ARWeaveService.d.ts +84 -0
- package/dist/services/ARWeaveService.js +147 -0
- package/dist/services/ARWeaveService.js.map +1 -0
- package/dist/services/EAService.d.ts +18 -0
- package/dist/services/EAService.js +10 -0
- package/dist/services/EAService.js.map +1 -0
- package/dist/services/ReceivableService.d.ts +76 -0
- package/dist/services/ReceivableService.js +135 -0
- package/dist/services/ReceivableService.js.map +1 -0
- package/dist/services/SubgraphService.d.ts +53 -0
- package/dist/services/SubgraphService.js +98 -0
- package/dist/services/SubgraphService.js.map +1 -0
- package/dist/services/index.d.ts +4 -0
- package/dist/services/index.js +5 -0
- package/dist/services/index.js.map +1 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -0
- package/dist/utils/chain.d.ts +14 -0
- package/dist/utils/chain.js +29 -0
- package/dist/utils/chain.js.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/poolInfo.d.ts +9 -0
- package/dist/utils/poolInfo.js +10 -0
- package/dist/utils/poolInfo.js.map +1 -0
- package/dist/utils/web3.d.ts +4 -0
- package/dist/utils/web3.js +24 -0
- package/dist/utils/web3.js.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { Web3Provider } from '@ethersproject/providers';
|
|
2
|
+
import { REAL_WORLD_RECEIVABLE_ABI, PoolContractMap, SupplementaryContracts, SupplementaryContractsMap, } from '@huma-finance/shared';
|
|
3
|
+
import { getContract } from '../utils';
|
|
4
|
+
import { ARWeaveService } from './ARWeaveService';
|
|
5
|
+
import { getChainIdFromSignerOrProvider } from '../utils/chain';
|
|
6
|
+
async function declareReceivablePaymentByReferenceId(
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
8
|
+
signer,
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
10
|
+
referenceId,
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
12
|
+
paymentAmount) {
|
|
13
|
+
throw new Error('Not implemented');
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Declares a payment on a RealWorldReceivable given a tokenID of the receivable.
|
|
17
|
+
*
|
|
18
|
+
* @async
|
|
19
|
+
* @function
|
|
20
|
+
* @memberof ReceivableService
|
|
21
|
+
* @param {ethers.Signer} signer - The signer used to send the transaction. Note only the receivable owner can pay the receivable.
|
|
22
|
+
* @param {BigNumberish} receivableTokenId - The ID of the receivable token to pay.
|
|
23
|
+
* @param {number} paymentAmount - The amount to pay the receivable.
|
|
24
|
+
* @returns {Promise<TransactionResponse>} - A Promise of the transaction receipt.
|
|
25
|
+
* @throws {Error} - Throws an error if the RealWorldReceivable contract is not available on the network.
|
|
26
|
+
*/
|
|
27
|
+
async function declareReceivablePaymentByTokenId(signer, receivableTokenId, paymentAmount) {
|
|
28
|
+
var _a;
|
|
29
|
+
const chainId = await getChainIdFromSignerOrProvider(signer);
|
|
30
|
+
const realWorldReceivable = chainId
|
|
31
|
+
? (_a = SupplementaryContractsMap[chainId]) === null || _a === void 0 ? void 0 : _a[SupplementaryContracts.RealWorldReceivable]
|
|
32
|
+
: null;
|
|
33
|
+
if (!realWorldReceivable) {
|
|
34
|
+
throw new Error('RealWorldReceivable is not available on this network');
|
|
35
|
+
}
|
|
36
|
+
const contract = getContract(realWorldReceivable, REAL_WORLD_RECEIVABLE_ABI, signer);
|
|
37
|
+
if (!contract) {
|
|
38
|
+
throw new Error('Could not find RealWorldReceivable contract');
|
|
39
|
+
}
|
|
40
|
+
return contract.declarePayment(receivableTokenId, paymentAmount);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Creates a new RealWorldReceivable token on the given chain of the signer
|
|
44
|
+
*
|
|
45
|
+
* @async
|
|
46
|
+
* @function
|
|
47
|
+
* @memberof ReceivableService
|
|
48
|
+
* @param {ethers.Signer} signer - The signer used to send the transaction.
|
|
49
|
+
* @param {POOL_NAME} poolName - The name of the credit pool to mint the receivable token from. Used to lookup the pool address.
|
|
50
|
+
* @param {POOL_TYPE} poolType - The type of the credit pool to mint the receivable token from. Used to lookup the pool address.
|
|
51
|
+
* @param {number} currencyCode - The ISO 4217 currency code that the receivable is denominated in
|
|
52
|
+
* @param {number} receivableAmount - The amount of the receivable token to mint.
|
|
53
|
+
* @param {number} maturityDate - The maturity date of the receivable token, in UNIX timestamp format.
|
|
54
|
+
* @param {string} uri - The URI of the receivable token metadata.
|
|
55
|
+
* @param {Overrides} gasOpts - The gas options to use for the transaction.
|
|
56
|
+
* @returns {Promise<TransactionResponse>} - A Promise of the transaction response.
|
|
57
|
+
* @throws {Error} - Throws an error if the RealWorldReceivable contract is not available on the network.
|
|
58
|
+
*/
|
|
59
|
+
async function createReceivable(signer, poolName, poolType, currencyCode, receivableAmount, maturityDate, uri, gasOpts = {}) {
|
|
60
|
+
var _a, _b, _c;
|
|
61
|
+
const chainId = await getChainIdFromSignerOrProvider(signer);
|
|
62
|
+
const realWorldReceivable = chainId
|
|
63
|
+
? (_a = SupplementaryContractsMap[chainId]) === null || _a === void 0 ? void 0 : _a[SupplementaryContracts.RealWorldReceivable]
|
|
64
|
+
: null;
|
|
65
|
+
const poolInfo = chainId
|
|
66
|
+
? (_c = (_b = PoolContractMap[chainId]) === null || _b === void 0 ? void 0 : _b[poolType]) === null || _c === void 0 ? void 0 : _c[poolName]
|
|
67
|
+
: undefined;
|
|
68
|
+
if (!realWorldReceivable || !poolInfo) {
|
|
69
|
+
throw new Error('RealWorldReceivable is not available on this network');
|
|
70
|
+
}
|
|
71
|
+
const contract = getContract(realWorldReceivable, REAL_WORLD_RECEIVABLE_ABI, signer);
|
|
72
|
+
if (!contract) {
|
|
73
|
+
throw new Error('Could not find RealWorldReceivable contract');
|
|
74
|
+
}
|
|
75
|
+
return contract.createRealWorldReceivable(poolInfo.pool, currencyCode, receivableAmount, maturityDate, uri, gasOpts);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Creates a RealWorldReceivable token with metadata uploaded onto ARWeave
|
|
79
|
+
*
|
|
80
|
+
* @async
|
|
81
|
+
* @function
|
|
82
|
+
* @memberof ReceivableService
|
|
83
|
+
* @param {Web3Provider | ethers.Signer} signerOrProvider - If calling this function from a browser, this function expects a Web3Provider.
|
|
84
|
+
* If calling this function from a server, this function expects an ethers Signer. Note that privateKey only needs to be included
|
|
85
|
+
* from server calls.
|
|
86
|
+
* @param {string | null} privateKey - Private key of the wallet used to upload metadata to ARWeave. Only required if calling this function from a server.
|
|
87
|
+
* @param {number} chainId - The chain ID to mint the receivable token on and pay ARWeave funds from.
|
|
88
|
+
* @param {string} recipient - The receivable token recipient.
|
|
89
|
+
* @param {POOL_NAME} poolName - The pool name. Used to lookup the pool address to pay to.
|
|
90
|
+
* @param {POOL_TYPE} poolType - The pool type. Used to lookup the pool address to pay to.
|
|
91
|
+
* @param {number} currencyCode - The ISO 4217 currency code that the receivable is denominated in
|
|
92
|
+
* @param {number} receivableAmount - The receivable amount.
|
|
93
|
+
* @param {number} maturityDate - The maturity date of the receivable, in UNIX timestamp format.
|
|
94
|
+
* @param {JSON} metadata - The metadata in JSON format. This will be uploaded onto ARWeave
|
|
95
|
+
* @param {number} referenceId - An internal identifier value added as a tag on ARWeave, for easily querying the metadata later.
|
|
96
|
+
* @param {Array<{ name: string, value: string }>} extraTags - Any extraTags you'd like to tag your metadata with. Note that metadata on
|
|
97
|
+
* ARWeave is indexed by these tags, so make sure to include any tags that you'd like to be able to query by.
|
|
98
|
+
* @param {boolean} [lazyFund=true] - Whether to lazy fund the ARWeave uploads. If true, the ARWeave uploads will be paid for by the
|
|
99
|
+
* metadata uploader immediately before uploading. If false, the ARWeave node must be pre-funded before calling this function.
|
|
100
|
+
* @param {Overrides} [gasOpts] - Optional gas overrides for the transaction.
|
|
101
|
+
* @returns {Promise<TransactionResponse>} - The transaction receipt.
|
|
102
|
+
*/
|
|
103
|
+
async function createReceivableWithMetadata(signerOrProvider, privateKey, chainId, poolName, poolType, currencyCode, receivableAmount, maturityDate, metadata, referenceId, extraTags, lazyFund = true, gasOpts) {
|
|
104
|
+
const config = ARWeaveService.getBundlrNetworkConfig(chainId);
|
|
105
|
+
try {
|
|
106
|
+
const tags = [
|
|
107
|
+
{ name: 'Content-Type', value: 'application/json' },
|
|
108
|
+
{ name: 'appName', value: 'HumaFinance' },
|
|
109
|
+
{ name: 'poolName', value: poolName },
|
|
110
|
+
{ name: 'poolType', value: poolType },
|
|
111
|
+
{ name: 'referenceId', value: referenceId },
|
|
112
|
+
...extraTags,
|
|
113
|
+
];
|
|
114
|
+
const response = await ARWeaveService.storeData(config, signerOrProvider instanceof Web3Provider ? signerOrProvider : privateKey, metadata, tags, lazyFund);
|
|
115
|
+
console.log(`Data uploaded ==> https://arweave.net/${response.id}`);
|
|
116
|
+
return await createReceivable(signerOrProvider instanceof Web3Provider
|
|
117
|
+
? signerOrProvider.getSigner()
|
|
118
|
+
: signerOrProvider, poolName, poolType, currencyCode, receivableAmount, maturityDate, `https://arweave.net/${response.id}`, gasOpts);
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
console.error(e);
|
|
122
|
+
throw e;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* An object that contains functions to interact with Huma's receivables.
|
|
127
|
+
* @namespace ReceivableService
|
|
128
|
+
*/
|
|
129
|
+
export const ReceivableService = {
|
|
130
|
+
createReceivableWithMetadata,
|
|
131
|
+
createReceivable,
|
|
132
|
+
declareReceivablePaymentByTokenId,
|
|
133
|
+
declareReceivablePaymentByReferenceId,
|
|
134
|
+
};
|
|
135
|
+
//# sourceMappingURL=ReceivableService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReceivableService.js","sourceRoot":"","sources":["../../src/services/ReceivableService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAE5E,OAAO,EACL,yBAAyB,EAGzB,eAAe,EACf,sBAAsB,EACtB,yBAAyB,GAC1B,MAAM,sBAAsB,CAAA;AAE7B,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AACtC,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,8BAA8B,EAAE,MAAM,gBAAgB,CAAA;AAE/D,KAAK,UAAU,qCAAqC;AAClD,6DAA6D;AAC7D,MAAqB;AACrB,6DAA6D;AAC7D,WAAmB;AACnB,6DAA6D;AAC7D,aAAqB;IAErB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAA;AACpC,CAAC;AAED;;;;;;;;;;;GAWG;AACH,KAAK,UAAU,iCAAiC,CAC9C,MAAqB,EACrB,iBAA+B,EAC/B,aAAqB;;IAErB,MAAM,OAAO,GAAG,MAAM,8BAA8B,CAAC,MAAM,CAAC,CAAA;IAE5D,MAAM,mBAAmB,GAAG,OAAO;QACjC,CAAC,CAAC,MAAA,yBAAyB,CAAC,OAAO,CAAC,0CAChC,sBAAsB,CAAC,mBAAmB,CAC3C;QACH,CAAC,CAAC,IAAI,CAAA;IAER,IAAI,CAAC,mBAAmB,EAAE;QACxB,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAA;KACxE;IAED,MAAM,QAAQ,GAAG,WAAW,CAC1B,mBAAmB,EACnB,yBAAyB,EACzB,MAAM,CACP,CAAA;IAED,IAAI,CAAC,QAAQ,EAAE;QACb,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAA;KAC/D;I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CAAA"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { POOL_NAME, POOL_TYPE } from '@huma-finance/shared';
|
|
2
|
+
/**
|
|
3
|
+
* Returns the subgraph URL for a given chain ID.
|
|
4
|
+
*
|
|
5
|
+
* @memberof SubgraphService
|
|
6
|
+
* @param {number} chainId - The ID of the chain.
|
|
7
|
+
* @returns {string} The subgraph URL for the given chain ID.
|
|
8
|
+
*/
|
|
9
|
+
declare function getSubgraphUrlForChainId(chainId: number): string;
|
|
10
|
+
/**
|
|
11
|
+
* Represents the payload of a credit event.
|
|
12
|
+
* @typedef {Object} CreditEventPayload
|
|
13
|
+
*/
|
|
14
|
+
export type CreditEventPayload = {
|
|
15
|
+
amount?: string;
|
|
16
|
+
timestamp?: string;
|
|
17
|
+
owner?: string;
|
|
18
|
+
pool?: string;
|
|
19
|
+
event?: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Returns the credit events for a given user.
|
|
23
|
+
*
|
|
24
|
+
* @memberof SubgraphService
|
|
25
|
+
* @param {string} userAddress - The address of the user.
|
|
26
|
+
* @param {number} chainId - The ID of the chain.
|
|
27
|
+
* @param {POOL_NAME} poolName - The name of the pool.
|
|
28
|
+
* @param {POOL_TYPE} poolType - The type of the pool.
|
|
29
|
+
* @param {string[]} event - The event types to filter by.
|
|
30
|
+
* @returns {Promise<CreditEventPayload[]>} The credit events for the given user.
|
|
31
|
+
*/
|
|
32
|
+
declare function getCreditEventsForUser(userAddress: string, chainId: number, poolName: POOL_NAME, poolType: POOL_TYPE, event: string[]): Promise<CreditEventPayload[]>;
|
|
33
|
+
/**
|
|
34
|
+
* Returns the last factorized amount for a given user and pool.
|
|
35
|
+
*
|
|
36
|
+
* @memberof SubgraphService
|
|
37
|
+
* @param {string} userAddress - The address of the user.
|
|
38
|
+
* @param {number} chainId - The ID of the chain.
|
|
39
|
+
* @param {POOL_NAME} poolName - The name of the pool.
|
|
40
|
+
* @param {POOL_TYPE} poolType - The type of the pool.
|
|
41
|
+
* @returns {Promise<number>} The last factorized amount for the given user and pool.
|
|
42
|
+
*/
|
|
43
|
+
declare function getLastFactorizedAmountFromPool(userAddress: string, chainId: number, poolName: POOL_NAME, poolType: POOL_TYPE): Promise<number>;
|
|
44
|
+
/**
|
|
45
|
+
* An object that contains functions to interact with Huma's Subgraph storage.
|
|
46
|
+
* @namespace SubgraphService
|
|
47
|
+
*/
|
|
48
|
+
export declare const SubgraphService: {
|
|
49
|
+
getSubgraphUrlForChainId: typeof getSubgraphUrlForChainId;
|
|
50
|
+
getCreditEventsForUser: typeof getCreditEventsForUser;
|
|
51
|
+
getLastFactorizedAmountFromPool: typeof getLastFactorizedAmountFromPool;
|
|
52
|
+
};
|
|
53
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { CreditEvent, PoolContractMap, } from '@huma-finance/shared';
|
|
2
|
+
/**
|
|
3
|
+
* Returns the subgraph URL for a given chain ID.
|
|
4
|
+
*
|
|
5
|
+
* @memberof SubgraphService
|
|
6
|
+
* @param {number} chainId - The ID of the chain.
|
|
7
|
+
* @returns {string} The subgraph URL for the given chain ID.
|
|
8
|
+
*/
|
|
9
|
+
function getSubgraphUrlForChainId(chainId) {
|
|
10
|
+
switch (chainId) {
|
|
11
|
+
case 5:
|
|
12
|
+
return 'https://api.thegraph.com/subgraphs/name/00labs/huma-goerli';
|
|
13
|
+
case 137:
|
|
14
|
+
return 'https://api.thegraph.com/subgraphs/name/00labs/huma-polygon';
|
|
15
|
+
case 80001:
|
|
16
|
+
return 'https://api.thegraph.com/subgraphs/name/00labs/huma-mumbai';
|
|
17
|
+
default:
|
|
18
|
+
return '';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Returns the credit events for a given user.
|
|
23
|
+
*
|
|
24
|
+
* @memberof SubgraphService
|
|
25
|
+
* @param {string} userAddress - The address of the user.
|
|
26
|
+
* @param {number} chainId - The ID of the chain.
|
|
27
|
+
* @param {POOL_NAME} poolName - The name of the pool.
|
|
28
|
+
* @param {POOL_TYPE} poolType - The type of the pool.
|
|
29
|
+
* @param {string[]} event - The event types to filter by.
|
|
30
|
+
* @returns {Promise<CreditEventPayload[]>} The credit events for the given user.
|
|
31
|
+
*/
|
|
32
|
+
function getCreditEventsForUser(userAddress, chainId, poolName, poolType, event) {
|
|
33
|
+
var _a, _b, _c;
|
|
34
|
+
const url = getSubgraphUrlForChainId(chainId);
|
|
35
|
+
if (!url) {
|
|
36
|
+
return Promise.resolve([]);
|
|
37
|
+
}
|
|
38
|
+
const poolAddress = (_c = (_b = (_a = PoolContractMap[chainId]) === null || _a === void 0 ? void 0 : _a[poolType]) === null || _b === void 0 ? void 0 : _b[poolName]) === null || _c === void 0 ? void 0 : _c.pool;
|
|
39
|
+
const query = `
|
|
40
|
+
query {
|
|
41
|
+
creditEvents(
|
|
42
|
+
where: {
|
|
43
|
+
owner: "${userAddress}",
|
|
44
|
+
pool: "${poolAddress}",
|
|
45
|
+
event_in: "[${event.join('", "')}]"
|
|
46
|
+
}
|
|
47
|
+
orderBy: timestamp
|
|
48
|
+
orderDirection: desc
|
|
49
|
+
) {
|
|
50
|
+
amount
|
|
51
|
+
timestamp
|
|
52
|
+
owner
|
|
53
|
+
pool
|
|
54
|
+
event
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
`;
|
|
58
|
+
return fetch(url, {
|
|
59
|
+
method: 'POST',
|
|
60
|
+
headers: {
|
|
61
|
+
'Content-Type': 'application/json',
|
|
62
|
+
},
|
|
63
|
+
body: JSON.stringify({ query }),
|
|
64
|
+
})
|
|
65
|
+
.then((res) => res.json())
|
|
66
|
+
.then((res) => {
|
|
67
|
+
if (res.errors) {
|
|
68
|
+
console.error(res.errors);
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
return res.data.creditEvents;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Returns the last factorized amount for a given user and pool.
|
|
76
|
+
*
|
|
77
|
+
* @memberof SubgraphService
|
|
78
|
+
* @param {string} userAddress - The address of the user.
|
|
79
|
+
* @param {number} chainId - The ID of the chain.
|
|
80
|
+
* @param {POOL_NAME} poolName - The name of the pool.
|
|
81
|
+
* @param {POOL_TYPE} poolType - The type of the pool.
|
|
82
|
+
* @returns {Promise<number>} The last factorized amount for the given user and pool.
|
|
83
|
+
*/
|
|
84
|
+
function getLastFactorizedAmountFromPool(userAddress, chainId, poolName, poolType) {
|
|
85
|
+
return getCreditEventsForUser(userAddress, chainId, poolName, poolType, [
|
|
86
|
+
CreditEvent.DrawdownMadeWithReceivable.toString(),
|
|
87
|
+
]).then((events) => Number(events[0].amount));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* An object that contains functions to interact with Huma's Subgraph storage.
|
|
91
|
+
* @namespace SubgraphService
|
|
92
|
+
*/
|
|
93
|
+
export const SubgraphService = {
|
|
94
|
+
getSubgraphUrlForChainId,
|
|
95
|
+
getCreditEventsForUser,
|
|
96
|
+
getLastFactorizedAmountFromPool,
|
|
97
|
+
};
|
|
98
|
+
//# sourceMappingURL=SubgraphService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SubgraphService.js","sourceRoot":"","sources":["../../src/services/SubgraphService.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,eAAe,GAGhB,MAAM,sBAAsB,CAAA;AAE7B;;;;;;GAMG;AACH,SAAS,wBAAwB,CAAC,OAAe;IAC/C,QAAQ,OAAO,EAAE;QACf,KAAK,CAAC;YACJ,OAAO,4DAA4D,CAAA;QACrE,KAAK,GAAG;YACN,OAAO,6DAA6D,CAAA;QACtE,KAAK,KAAK;YACR,OAAO,4DAA4D,CAAA;QACrE;YACE,OAAO,EAAE,CAAA;KACZ;AACH,CAAC;AAcD;;;;;;;;;;GAUG;AACH,SAAS,sBAAsB,CAC7B,WAAmB,EACnB,OAAe,EACf,QAAmB,EACnB,QAAmB,EACnB,KA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