@raac/rpc 1.0.0

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.
Files changed (72) hide show
  1. package/README.md +1 -0
  2. package/RPCLibrary.ts +358 -0
  3. package/assets/getAsset.ts +26 -0
  4. package/assets/getAssets.ts +30 -0
  5. package/assets/getSupply.ts +23 -0
  6. package/configs/chains/8453.json +116 -0
  7. package/configs/chains/chain.d.ts +87 -0
  8. package/configs/chains/index.ts +13 -0
  9. package/configs/chains/list.json +3 -0
  10. package/configs/createConfig.ts +91 -0
  11. package/configs/index.ts +51 -0
  12. package/contracts/crvUSDToken/getBalance.ts +23 -0
  13. package/contracts/crvUSDToken/getTotalSupply.ts +36 -0
  14. package/contracts/getContract.ts +21 -0
  15. package/contracts/getContractAddress.ts +61 -0
  16. package/contracts/housePrices/getLatestPrice.ts +38 -0
  17. package/contracts/housePrices/setOracle.ts +40 -0
  18. package/contracts/rcrvUSDToken/getBalance.ts +23 -0
  19. package/contracts/rcrvUSDToken/getTotalSupply.ts +39 -0
  20. package/contracts/zeno/createAuction.ts +57 -0
  21. package/contracts/zeno/createZeno.ts +45 -0
  22. package/contracts/zeno/getAuctions.ts +38 -0
  23. package/contracts/zeno/getZenos.ts +38 -0
  24. package/custom-types.d.ts +20 -0
  25. package/index.ts +3 -0
  26. package/methods/approveToken.ts +47 -0
  27. package/methods/commons/checkAllowance.ts +36 -0
  28. package/methods/commons/estimateGasPrice.ts +21 -0
  29. package/methods/debug/mintToken.js +83 -0
  30. package/methods/getAssetBalance.ts +47 -0
  31. package/methods/getAssetsBalance.ts +9 -0
  32. package/minter/get.ts +150 -0
  33. package/minter/getApy.ts +37 -0
  34. package/minter/tick.ts +40 -0
  35. package/nfts/addNewBatch.ts +37 -0
  36. package/nfts/getCurrentBatchSize.ts +26 -0
  37. package/nfts/getDepositedNFTs.ts +31 -0
  38. package/nfts/getHousePrice.ts +30 -0
  39. package/nfts/getOwnedNFTs.ts +46 -0
  40. package/nfts/mint.ts +43 -0
  41. package/nfts/setBaseUri.ts +33 -0
  42. package/package.json +27 -0
  43. package/pools/lendingPool/borrowFromLendingPool.ts +39 -0
  44. package/pools/lendingPool/depositNFTToLendingPool.ts +70 -0
  45. package/pools/lendingPool/depositToLendingPool.ts +52 -0
  46. package/pools/lendingPool/getHousePrice.ts +46 -0
  47. package/pools/lendingPool/getLendingPoolInfo.ts +153 -0
  48. package/pools/lendingPool/getLendingPoolTotalLiquidity.ts +32 -0
  49. package/pools/lendingPool/repayToLendingPool.ts +52 -0
  50. package/pools/lendingPool/withdrawFromLendingPool.ts +68 -0
  51. package/pools/lendingPool/withdrawNFTFromLendingPool.ts +39 -0
  52. package/pools/liquidityPool/getLiquidityPoolInfo.ts +90 -0
  53. package/pools/stabilityPool/calculateRAACRewards.ts +37 -0
  54. package/pools/stabilityPool/depositToStabilityPool.ts +47 -0
  55. package/pools/stabilityPool/getStabilityPoolInfo.ts +121 -0
  56. package/pools/stabilityPool/getStabilityPoolTotalDeposits.ts +27 -0
  57. package/pools/stabilityPool/withdrawFromStabilityPool.ts +47 -0
  58. package/sync-contracts.sh +63 -0
  59. package/tests/test.ts +40 -0
  60. package/tsconfig.json +113 -0
  61. package/utils/artifacts.ts +66 -0
  62. package/utils/chain.ts +94 -0
  63. package/utils/config.ts +49 -0
  64. package/utils/contracts.ts +94 -0
  65. package/wallet/assets/approveAsset.ts +44 -0
  66. package/wallet/assets/burnAsset.ts +55 -0
  67. package/wallet/assets/getAllowance.ts +38 -0
  68. package/wallet/assets/getAsset.ts +33 -0
  69. package/wallet/assets/getAssets.ts +35 -0
  70. package/wallet/assets/getBalance.ts +34 -0
  71. package/wallet/assets/mintAsset.ts +57 -0
  72. package/wallet/assets/transferAsset.ts +78 -0
@@ -0,0 +1,49 @@
1
+ // let fs;
2
+ // let path;
3
+ // let fileURLToPath;
4
+
5
+ // import { ChainId } from "../configs/chains";
6
+ // // if (typeof window === 'undefined') {
7
+ // // // Server-side
8
+ // // fs = await import('fs');
9
+ // // path = await import('path');
10
+ // // fileURLToPath = await import('url').fileURLToPath;
11
+ // // }
12
+
13
+ // // @ts-ignore
14
+ // import chain8453 from "../configs/chains/8453.json"; // assert { type: "json" };
15
+
16
+ // export const getConfig = (chainId: ChainId) => {
17
+ // if (typeof window === "undefined") {
18
+ // // Server-side logic
19
+ // console.log("Server-side logic", fs, path, fileURLToPath);
20
+ // const __filename = fileURLToPath(import.meta.url);
21
+ // const __dirname = path.dirname(__filename);
22
+ // const configPath = path.join(
23
+ // __dirname,
24
+ // "..",
25
+ // "configs",
26
+ // "chains",
27
+ // `${chainId}.json`
28
+ // );
29
+
30
+ // if (fs.existsSync(configPath)) {
31
+ // const configData = fs.readFileSync(configPath, "utf8");
32
+ // return JSON.parse(configData);
33
+ // } else {
34
+ // throw new Error(`Configuration not found for chainId: ${chainId}`);
35
+ // }
36
+ // } else {
37
+ // const configs = {
38
+ // 8453: chain8453,
39
+ // };
40
+
41
+ // if (configs[chainId]) {
42
+ // return configs[chainId];
43
+ // } else {
44
+ // throw new Error(`Configuration not found for chainId: ${chainId}`);
45
+ // }
46
+ // }
47
+ // };
48
+
49
+ // export default { getConfig };
@@ -0,0 +1,94 @@
1
+ import { getChainsConfig, getChainConfig } from "../configs";
2
+ import { ChainId, configs } from "../configs/chains";
3
+ import { Chain } from "../configs/chains/chain";
4
+ import { ABIS, ContractType, getABI } from "./artifacts";
5
+ import { ethers, Provider } from "ethers";
6
+
7
+ let _config;
8
+ let _configs: ReturnType<typeof getChainsConfig>;
9
+
10
+ export const getConfigs = () => {
11
+ if (!_configs) {
12
+ _configs = getChainsConfig();
13
+ }
14
+ return _configs;
15
+ };
16
+
17
+ export const setOverrideConfig = (chainId: ChainId, newConfig: any) => {
18
+ if (!_configs) {
19
+ _configs = getConfigs();
20
+ }
21
+ // FIXME: configs is not defined here.
22
+ if (!configs[chainId]) {
23
+ // @ts-ignore
24
+ configs[chainId] = {};
25
+ }
26
+ configs[chainId] = {
27
+ ...configs[chainId],
28
+ ...newConfig,
29
+ contracts: {
30
+ ...configs[chainId].contracts,
31
+ ...newConfig.contracts,
32
+ },
33
+ };
34
+ };
35
+
36
+ export const getConfig = (chainId: ChainId) => {
37
+ const configs = getConfigs();
38
+ if (!configs[chainId]) {
39
+ throw new Error(
40
+ `Unsupported chain ID: ${chainId}. Has ${JSON.stringify(
41
+ Object.keys(configs)
42
+ )}`
43
+ );
44
+ }
45
+ return configs[chainId];
46
+ };
47
+
48
+ export const getContract = (
49
+ chainId: ChainId,
50
+ contractName: ContractType,
51
+ provider: Provider
52
+ ) => {
53
+ const normalizedContractName =
54
+ contractName.toLowerCase() as keyof typeof ABIS;
55
+ const address = getContractAddress(chainId, normalizedContractName);
56
+ const abi = getABI(normalizedContractName);
57
+ return new ethers.Contract(address, abi, provider);
58
+ };
59
+
60
+ export const getContractAddress = (chainId: ChainId, contractName: string) => {
61
+ const normalizedContractName = contractName.toLowerCase();
62
+ const config = getConfig(chainId);
63
+ if (!config) {
64
+ throw new Error(`Unsupported chain ID: ${chainId}`);
65
+ }
66
+ const chainKeys = ["contracts", "assets", "pools", "nfts"] as (keyof Chain)[];
67
+ const keys = (Object.keys(config) as (keyof Chain)[]).filter((key) =>
68
+ chainKeys.includes(key)
69
+ );
70
+ let address = null;
71
+ for (const key of keys) {
72
+ if (config[key] && (config[key] as any)[normalizedContractName]) {
73
+ address = (config[key] as any)[normalizedContractName]?.contract;
74
+ break;
75
+ }
76
+ }
77
+ if (!address) {
78
+ const hasIds = keys
79
+ .map((key) => `${key}: ${Object.keys(config[key]).join(", ")}`)
80
+ .join("\n");
81
+ throw new Error(
82
+ `Contract ${normalizedContractName} not found for chain ID: ${chainId}.\nHas:\n${hasIds}`
83
+ );
84
+ }
85
+ return address;
86
+ };
87
+
88
+ export default {
89
+ getContract,
90
+ getContractAddress,
91
+ setOverrideConfig,
92
+ getConfig,
93
+ getConfigs,
94
+ };
@@ -0,0 +1,44 @@
1
+ import { ethers, Signer } from "ethers";
2
+ import { getContractAddress } from "../../utils/contracts";
3
+ import { ABIS, AssetType, getABI } from "../../utils/artifacts";
4
+ import estimateGasPrice from "../../methods/commons/estimateGasPrice";
5
+ import { ChainId } from "../../configs/chains/index";
6
+
7
+ async function approveAsset(
8
+ chainId: ChainId,
9
+ assetId: AssetType,
10
+ spender: string,
11
+ amount: string,
12
+ signer: Signer
13
+ ) {
14
+ if (!signer) {
15
+ throw new Error("Wallet not connected");
16
+ }
17
+
18
+ try {
19
+ const tokenAddress = getContractAddress(chainId, assetId);
20
+ const abi = getABI(assetId);
21
+ const tokenContract = new ethers.Contract(tokenAddress, abi, signer);
22
+
23
+ const amountInWei = ethers.parseEther(amount);
24
+ const { maxFeePerGas } = await estimateGasPrice(signer);
25
+
26
+ const tx = await tokenContract.approve(spender, amountInWei, {
27
+ maxFeePerGas,
28
+ nonce: await signer.getNonce(),
29
+ });
30
+
31
+ console.log(`Approval transaction sent for ${assetId}:`, tx.hash);
32
+ const receipt = await tx.wait();
33
+ console.log(
34
+ `Approved ${amount} ${assetId} for ${spender} successfully:`,
35
+ receipt.transactionHash
36
+ );
37
+ return receipt;
38
+ } catch (error: any) {
39
+ console.error(`Approval error for ${assetId}:`, error);
40
+ throw new Error(`Token approval failed for ${assetId}: ${error.message}`);
41
+ }
42
+ }
43
+
44
+ export default approveAsset;
@@ -0,0 +1,55 @@
1
+ import { ethers, Signer } from "ethers";
2
+ import { getConfig } from "../../utils/contracts";
3
+ import estimateGasPrice from "../../methods/commons/estimateGasPrice";
4
+ import { ABIS, AssetType, getABI } from "../../utils/artifacts";
5
+ import { ChainId } from "../../configs/chains/index";
6
+
7
+ async function burnAsset(
8
+ chainId: ChainId,
9
+ assetId: AssetType,
10
+ amount: bigint,
11
+ signer: Signer
12
+ ) {
13
+ try {
14
+ if (!signer) {
15
+ throw new Error("Wallet not connected");
16
+ }
17
+
18
+ const { assets } = getConfig(chainId);
19
+ const asset = Object.values(assets).find(
20
+ (asset) => asset.id.toLowerCase() === (assetId as string).toLowerCase()
21
+ );
22
+ if (!asset) {
23
+ throw new Error(`Asset ${assetId} not found on chain ${chainId}`);
24
+ }
25
+
26
+ const contractAddress = asset.contract;
27
+ const abi = getABI(assetId);
28
+ const contract = new ethers.Contract(contractAddress, abi, signer);
29
+
30
+ const { maxFeePerGas } = await estimateGasPrice(signer);
31
+ const amountToBurn = ethers.parseEther(amount.toString());
32
+
33
+ if (!contract.burn) {
34
+ throw new Error(`Burn function not found on contract ${contractAddress}`);
35
+ }
36
+
37
+ const tx = await contract.burn(amountToBurn, {
38
+ maxFeePerGas,
39
+ nonce: +(await signer.getNonce()),
40
+ });
41
+
42
+ console.log(`Burning transaction sent for ${assetId}:`, tx.hash);
43
+ const receipt = await tx.wait();
44
+ console.log(
45
+ `Burned ${amount} ${assetId} successfully:`,
46
+ receipt.transactionHash
47
+ );
48
+ return receipt;
49
+ } catch (error: any) {
50
+ console.error(`Error burning asset ${assetId}:`, error);
51
+ throw new Error(`Burning failed for ${assetId}: ${error.message}`);
52
+ }
53
+ }
54
+
55
+ export default burnAsset;
@@ -0,0 +1,38 @@
1
+ import { ethers, Provider } from "ethers";
2
+ import { getContractAddress } from "../../utils/contracts";
3
+ import { ABIS, AssetType } from "../../utils/artifacts";
4
+ import { ChainId } from "../../configs/chains/index";
5
+
6
+ async function getAllowance(
7
+ chainId: ChainId,
8
+ assetId: AssetType,
9
+ owner: string,
10
+ spender: string,
11
+ provider: Provider
12
+ ) {
13
+ try {
14
+ if (assetId === "eth") {
15
+ // ETH doesn't have an allowance concept, return max uint256 value ?
16
+ return ethers.formatEther(ethers.MaxUint256);
17
+ }
18
+
19
+ const ALLOWANCE_ABI = [
20
+ "function allowance(address,address) view returns (uint256)",
21
+ ];
22
+ const contractAddress = getContractAddress(chainId, assetId);
23
+ const contract = new ethers.Contract(
24
+ contractAddress,
25
+ ALLOWANCE_ABI,
26
+ provider
27
+ );
28
+ const allowance = await contract.allowance(owner, spender);
29
+ return ethers.formatEther(allowance);
30
+ } catch (error: any) {
31
+ console.error(`Error getting allowance for asset ${assetId}:`, error);
32
+ console.log({ assetId, owner, spender });
33
+ console.log(error);
34
+ return 0;
35
+ }
36
+ }
37
+
38
+ export default getAllowance;
@@ -0,0 +1,33 @@
1
+ import { Provider, Signer } from "ethers";
2
+ import { AssetType } from "../../utils/artifacts";
3
+ import { getConfig } from "../../utils/contracts";
4
+ import { ChainId } from "../../configs/chains/index";
5
+
6
+ async function getAsset(
7
+ chainId: ChainId,
8
+ assetId: string,
9
+ address: string,
10
+ provider: Provider
11
+ ) {
12
+ const { assets } = getConfig(chainId);
13
+ // @ts-ignore
14
+ const asset = assets[assetId];
15
+ let response = {
16
+ ...asset,
17
+ balance: 0,
18
+ };
19
+
20
+ const owner = address;
21
+
22
+ try {
23
+ // @ts-expect-error The method is used in the RPCLibrary class and therefore, it has access to this.
24
+ const balance = await this.getBalance(chainId, assetId, owner, provider);
25
+ response.balance = parseFloat(balance).toString();
26
+ } catch (error: any) {
27
+ console.log(`Error getting balance: ${error}`);
28
+ }
29
+
30
+ return response;
31
+ }
32
+
33
+ export default getAsset;
@@ -0,0 +1,35 @@
1
+ import { Provider, Signer } from "ethers";
2
+ import { getConfig } from "../../utils/contracts";
3
+ import { ChainId } from "../../configs/chains/index";
4
+ import RPCLibrary from "../../RPCLibrary";
5
+
6
+ async function getAssets(
7
+ chainId: ChainId,
8
+ address: string,
9
+ provider: Provider
10
+ ) {
11
+ const { assets } = getConfig(chainId);
12
+ const responses: any = {};
13
+
14
+ const assetIds = Object.keys(assets);
15
+ const assetPromises = assetIds.map(async (assetId) => {
16
+ // @ts-ignore
17
+ const asset = await this.getAsset(chainId, assetId, address, signer);
18
+ responses[assetId] = {
19
+ // @ts-ignore
20
+ ...assets[assetId],
21
+ ...asset,
22
+ };
23
+ return responses[assetId];
24
+ });
25
+ const assetResponses = await Promise.all(assetPromises);
26
+ const response = {};
27
+ assetResponses.forEach((asset) => {
28
+ // @ts-ignore
29
+ response[asset.id] = asset;
30
+ });
31
+
32
+ return response;
33
+ }
34
+
35
+ export default getAssets;
@@ -0,0 +1,34 @@
1
+ import { ethers, Provider } from "ethers";
2
+ import { getContractAddress } from "../../utils/contracts";
3
+ import { AssetType, getABI } from "../../utils/artifacts";
4
+ import { ChainId } from "../../configs/chains/index";
5
+
6
+ async function getBalance(
7
+ chainId: ChainId,
8
+ assetId: AssetType,
9
+ address: string,
10
+ provider: Provider
11
+ ) {
12
+ const BALANCE_OF_ABI = ["function balanceOf(address) view returns (uint256)"];
13
+ try {
14
+ if (assetId === "eth") {
15
+ const balance = await provider.getBalance(address);
16
+ const parsedBalance = parseFloat(ethers.formatEther(balance));
17
+ return parsedBalance;
18
+ }
19
+ const contractAddress = getContractAddress(chainId, assetId);
20
+ const contract = new ethers.Contract(
21
+ contractAddress,
22
+ BALANCE_OF_ABI,
23
+ provider
24
+ );
25
+ const balance = await contract.balanceOf(address);
26
+ const parsedBalance = parseFloat(ethers.formatEther(balance));
27
+ return parsedBalance.toString();
28
+ } catch (error: any) {
29
+ console.error(`Error getting balance for asset ${assetId}:`, error);
30
+ return 0;
31
+ }
32
+ }
33
+
34
+ export default getBalance;
@@ -0,0 +1,57 @@
1
+ import { ethers, Signer } from "ethers";
2
+ import { getConfig } from "../../utils/contracts";
3
+ import estimateGasPrice from "../../methods/commons/estimateGasPrice";
4
+ import { AssetType, getABI } from "../../utils/artifacts";
5
+ import { ChainId } from "../../configs/chains/index";
6
+
7
+ async function mintAsset(
8
+ chainId: ChainId,
9
+ assetId: AssetType,
10
+ amount: bigint,
11
+ address: string,
12
+ signer: Signer
13
+ ) {
14
+ try {
15
+ if (!signer) {
16
+ throw new Error("Wallet not connected");
17
+ }
18
+
19
+ const { assets } = getConfig(chainId);
20
+ console.log("assets", assets);
21
+ const asset = Object.values(assets).find(
22
+ (asset) => asset.id.toLowerCase() === (assetId as string).toLowerCase()
23
+ );
24
+ if (!asset) {
25
+ throw new Error(`Asset ${assetId} not found on chain ${chainId}`);
26
+ }
27
+
28
+ const contractAddress = asset.contract;
29
+ let contract;
30
+
31
+ const abi = getABI(assetId);
32
+ contract = new ethers.Contract(contractAddress, abi, signer);
33
+
34
+ const { maxFeePerGas } = await estimateGasPrice(signer);
35
+ if (!contract.mint) {
36
+ throw new Error(`Mint function not found on contract ${contractAddress}`);
37
+ }
38
+ const tx = await contract.mint(
39
+ address,
40
+ ethers.parseEther(amount.toString()),
41
+ {
42
+ maxFeePerGas,
43
+ nonce: +(await signer.getNonce()),
44
+ }
45
+ );
46
+
47
+ console.log(`Minting transaction sent for ${assetId}:`, tx.hash);
48
+ const receipt = await tx.wait();
49
+ console.log(`Minted ${amount} ${assetId} successfully:`, receipt.hash);
50
+ return receipt;
51
+ } catch (error: any) {
52
+ console.error(`Error minting asset ${assetId}:`, error);
53
+ throw new Error(`Minting failed for ${assetId}: ${error.message}`);
54
+ }
55
+ }
56
+
57
+ export default mintAsset;
@@ -0,0 +1,78 @@
1
+ import { ethers, Signer } from "ethers";
2
+ import { getConfig } from "../../utils/contracts";
3
+ import estimateGasPrice from "../../methods/commons/estimateGasPrice";
4
+ import { AssetType, getABI } from "../../utils/artifacts";
5
+ import { ChainId } from "../../configs/chains/index";
6
+
7
+ async function transferAsset(
8
+ chainId: ChainId,
9
+ assetId: AssetType,
10
+ amount: bigint,
11
+ fromAddress: string,
12
+ toAddress: string,
13
+ signer: Signer
14
+ ) {
15
+ try {
16
+ if (!signer) {
17
+ throw new Error("Wallet not connected");
18
+ }
19
+
20
+ const { maxFeePerGas } = await estimateGasPrice(signer);
21
+ const amountToTransfer = ethers.parseEther(amount.toString());
22
+
23
+ if ((assetId as string).toLowerCase() === "eth") {
24
+ // Handle ETH transfer
25
+ const tx = await signer.sendTransaction({
26
+ to: toAddress,
27
+ value: amountToTransfer,
28
+ maxFeePerGas,
29
+ nonce: await signer.getNonce(),
30
+ });
31
+
32
+ console.log(`ETH transfer transaction sent:`, tx.hash);
33
+ const receipt = await tx.wait();
34
+ console.log(
35
+ `Transferred ${amount} ETH from ${fromAddress} to ${toAddress} successfully:`,
36
+ (await receipt?.getTransaction())?.hash
37
+ );
38
+ return receipt;
39
+ } else {
40
+ // Handle ERC20 token transfer
41
+ const { assets } = getConfig(chainId);
42
+ const asset = Object.values(assets).find(
43
+ (asset) => asset.id.toLowerCase() === (assetId as string).toLowerCase()
44
+ );
45
+ if (!asset) {
46
+ throw new Error(`Asset ${assetId} not found on chain ${chainId}`);
47
+ }
48
+
49
+ const contractAddress = asset.contract;
50
+ const abi = getABI(assetId);
51
+ const contract = new ethers.Contract(contractAddress, abi, signer);
52
+
53
+ if (!contract.transfer) {
54
+ throw new Error(
55
+ `Transfer function not found on contract ${contractAddress}`
56
+ );
57
+ }
58
+
59
+ const tx = await contract.transfer(toAddress, amountToTransfer, {
60
+ maxFeePerGas,
61
+ nonce: await signer.getNonce(),
62
+ });
63
+
64
+ console.log(`Transfer transaction sent for ${assetId}:`, tx.hash);
65
+ const receipt = await tx.wait();
66
+ console.log(
67
+ `Transferred ${amount} ${assetId} from ${fromAddress} to ${toAddress} successfully:`,
68
+ receipt.transactionHash
69
+ );
70
+ return receipt;
71
+ }
72
+ } catch (error: any) {
73
+ console.error(`Error transferring asset ${assetId}:`, error);
74
+ throw new Error(`Transfer failed for ${assetId}: ${error.message}`);
75
+ }
76
+ }
77
+
78
+ export default transferAsset;