otomato-sdk 2.0.10 → 2.0.12
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/dist/src/index.js
CHANGED
|
@@ -22,4 +22,4 @@ export * from './services/ApiService.js';
|
|
|
22
22
|
export * from './services/RpcServices.js';
|
|
23
23
|
export * from './utils/helpers.js';
|
|
24
24
|
export * from './utils/typeValidator.js';
|
|
25
|
-
export * from './utils/
|
|
25
|
+
export * from './utils/addressBalance.js';
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
// getUserProtocolBalances.ts
|
|
11
|
+
import { ethers } from 'ethers';
|
|
12
|
+
import { rpcServices } from '../index.js';
|
|
13
|
+
// -------------- PROTOCOLS ENUM --------------
|
|
14
|
+
export const PROTOCOLS = {
|
|
15
|
+
AAVE: 'AAVE',
|
|
16
|
+
COMPOUND: 'COMPOUND',
|
|
17
|
+
IONIC: 'IONIC',
|
|
18
|
+
MOONWELL: 'MOONWELL',
|
|
19
|
+
WALLET: 'WALLET',
|
|
20
|
+
};
|
|
21
|
+
// Minimal ABIs
|
|
22
|
+
const ERC20_ABI = [
|
|
23
|
+
'function balanceOf(address) view returns (uint256)',
|
|
24
|
+
'function decimals() view returns (uint8)',
|
|
25
|
+
'function symbol() view returns (string)',
|
|
26
|
+
];
|
|
27
|
+
const COMPOUNDV2_EXCHANGERATE_ABI = [
|
|
28
|
+
'function exchangeRateCurrent() view returns (uint)',
|
|
29
|
+
];
|
|
30
|
+
// -------------- ACTUAL MAP --------------
|
|
31
|
+
const chainTokenProtocolMap = {
|
|
32
|
+
8453: {
|
|
33
|
+
// USDC on Base
|
|
34
|
+
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913': [
|
|
35
|
+
{ protocol: PROTOCOLS.AAVE, token: '0x4e65fE4DbA92790696d040ac24Aa414708F5c0AB' },
|
|
36
|
+
{ protocol: PROTOCOLS.COMPOUND, token: '0xb125E6687d4313864e53df431d5425969c15Eb2F' },
|
|
37
|
+
{ protocol: PROTOCOLS.IONIC, token: '0xa900A17a49Bc4D442bA7F72c39FA2108865671f0' },
|
|
38
|
+
{ protocol: PROTOCOLS.MOONWELL, token: '0xEdc817A28E8B93B03976FBd4a3dDBc9f7D176c22' },
|
|
39
|
+
{ protocol: PROTOCOLS.WALLET, token: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913' },
|
|
40
|
+
],
|
|
41
|
+
// WETH on Base
|
|
42
|
+
'0x4200000000000000000000000000000000000006': [
|
|
43
|
+
{ protocol: PROTOCOLS.AAVE, token: '0xD4a0e0b9149BCee3C920d2E00b5dE09138fd8bb7' },
|
|
44
|
+
{ protocol: PROTOCOLS.COMPOUND, token: '0x46e6b214b524310239732D51387075E0e70970bf' },
|
|
45
|
+
{ protocol: PROTOCOLS.IONIC, token: '0x49420311B518f3d0c94e897592014de53831cfA3' },
|
|
46
|
+
{ protocol: PROTOCOLS.MOONWELL, token: '0x628ff693426583D9a7FB391E54366292F509D457' },
|
|
47
|
+
{ protocol: PROTOCOLS.WALLET, token: '0x4200000000000000000000000000000000000006' },
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
34443: {
|
|
51
|
+
// USDC on Mode
|
|
52
|
+
'0xd988097fb8612cc24eec14542bc03424c656005f': [
|
|
53
|
+
{ protocol: PROTOCOLS.IONIC, token: '0x2BE717340023C9e14C1Bb12cb3ecBcfd3c3fB038' },
|
|
54
|
+
{ protocol: PROTOCOLS.WALLET, token: '0xd988097fb8612cc24eeC14542bC03424c656005f' },
|
|
55
|
+
],
|
|
56
|
+
// WETH on Mode
|
|
57
|
+
'0x4200000000000000000000000000000000000006': [
|
|
58
|
+
{ protocol: PROTOCOLS.IONIC, token: '0x71ef7EDa2Be775E5A7aa8afD02C45F059833e9d2' },
|
|
59
|
+
{ protocol: PROTOCOLS.WALLET, token: '0x4200000000000000000000000000000000000006' },
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
// -------------- EXACT getBalanceInUnderlying LOGIC --------------
|
|
64
|
+
function executeReadContract(address, abi, method, params, provider) {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
const contract = new ethers.Contract(address, abi, provider);
|
|
67
|
+
return contract[method](...params);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The function that does your protocol-specific "wrapper => underlying" logic
|
|
72
|
+
* using BigInt(10) instead of 10n for older TS targets.
|
|
73
|
+
*/
|
|
74
|
+
function getBalanceInUnderlying(protocol, chainId, smartAccountAddress, contractAddress, balanceObj, decimals, provider) {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
switch (protocol) {
|
|
77
|
+
case PROTOCOLS.WALLET:
|
|
78
|
+
return balanceObj.displayValue;
|
|
79
|
+
case PROTOCOLS.AAVE:
|
|
80
|
+
return balanceObj.displayValue;
|
|
81
|
+
case PROTOCOLS.COMPOUND: {
|
|
82
|
+
return balanceObj.displayValue;
|
|
83
|
+
}
|
|
84
|
+
case PROTOCOLS.IONIC:
|
|
85
|
+
case PROTOCOLS.MOONWELL: {
|
|
86
|
+
// read exchangeRateCurrent
|
|
87
|
+
const rawExRate = yield executeReadContract(contractAddress, COMPOUNDV2_EXCHANGERATE_ABI, 'exchangeRateCurrent', [], provider);
|
|
88
|
+
const exRateBN = BigInt(rawExRate.toString());
|
|
89
|
+
// For IONIC => scale = 18 - decimals + 6
|
|
90
|
+
// For MOONWELL => you used scale=18. Let's handle them with logic or keep it simple
|
|
91
|
+
let scale = 18;
|
|
92
|
+
// Fix: BigInt(10) instead of 10n
|
|
93
|
+
const TEN = BigInt(10);
|
|
94
|
+
const divisor = TEN ** BigInt(scale);
|
|
95
|
+
const rawBalanceInAssetBN = (balanceObj.value * exRateBN) / divisor;
|
|
96
|
+
const balanceInAssetString = ethers.formatUnits(rawBalanceInAssetBN, decimals);
|
|
97
|
+
return balanceInAssetString;
|
|
98
|
+
}
|
|
99
|
+
default:
|
|
100
|
+
return balanceObj.displayValue;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
// -------------- MAIN FUNCTION --------------
|
|
105
|
+
export function getUserProtocolBalances(params) {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
const { chainId, address, contractAddress } = params;
|
|
108
|
+
const chainMap = chainTokenProtocolMap[chainId];
|
|
109
|
+
if (!chainMap || chainId === undefined) {
|
|
110
|
+
throw new Error(`No token map for chainId=${chainId}`);
|
|
111
|
+
}
|
|
112
|
+
// If recognized => read all wrappers
|
|
113
|
+
const protocolWrappers = chainMap[contractAddress.toLowerCase()];
|
|
114
|
+
let addressesToCheck;
|
|
115
|
+
if (protocolWrappers) {
|
|
116
|
+
addressesToCheck = protocolWrappers;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
// fallback => just WALLET
|
|
120
|
+
addressesToCheck = [
|
|
121
|
+
{
|
|
122
|
+
protocol: PROTOCOLS.WALLET,
|
|
123
|
+
token: contractAddress,
|
|
124
|
+
},
|
|
125
|
+
];
|
|
126
|
+
}
|
|
127
|
+
// Setup provider from rpcServices
|
|
128
|
+
const rpcUrl = rpcServices.getRPC(chainId);
|
|
129
|
+
const provider = new ethers.JsonRpcProvider(rpcUrl);
|
|
130
|
+
// Minimal read ABI
|
|
131
|
+
const readABI = [
|
|
132
|
+
'function balanceOf(address) view returns (uint256)',
|
|
133
|
+
'function decimals() view returns (uint8)',
|
|
134
|
+
'function symbol() view returns (string)',
|
|
135
|
+
];
|
|
136
|
+
const results = [];
|
|
137
|
+
for (const { protocol, token } of addressesToCheck) {
|
|
138
|
+
const contract = new ethers.Contract(token, readABI, provider);
|
|
139
|
+
const [rawBalanceBN, decimals, symbol] = yield Promise.all([
|
|
140
|
+
contract.balanceOf(address),
|
|
141
|
+
contract.decimals(),
|
|
142
|
+
contract.symbol(),
|
|
143
|
+
]);
|
|
144
|
+
const rawBalance = BigInt(rawBalanceBN.toString());
|
|
145
|
+
const displayValue = ethers.formatUnits(rawBalance, decimals);
|
|
146
|
+
// We'll guess the underlying decimals from the symbol or fallback
|
|
147
|
+
// In your snippet, you explicitly used the WALLET decimals for USDC => 6
|
|
148
|
+
let underlyingDecimals = decimals;
|
|
149
|
+
if (symbol.toLowerCase().includes('usdc')) {
|
|
150
|
+
underlyingDecimals = 6;
|
|
151
|
+
}
|
|
152
|
+
else if (symbol.toLowerCase().includes('weth') || symbol.toLowerCase().includes('eth')) {
|
|
153
|
+
underlyingDecimals = 18;
|
|
154
|
+
}
|
|
155
|
+
// Build the "balanceObj"
|
|
156
|
+
const balanceObj = {
|
|
157
|
+
value: rawBalance,
|
|
158
|
+
decimals,
|
|
159
|
+
symbol,
|
|
160
|
+
displayValue,
|
|
161
|
+
};
|
|
162
|
+
// Now do your conversion
|
|
163
|
+
const underlyingBalance = yield getBalanceInUnderlying(protocol, chainId, address, token, balanceObj, underlyingDecimals, provider);
|
|
164
|
+
results.push({
|
|
165
|
+
protocol,
|
|
166
|
+
wrapperTokenAddress: token,
|
|
167
|
+
wrapperBalance: displayValue, // raw wrapper token balance
|
|
168
|
+
underlyingBalance, // computed by getBalanceInUnderlying
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
return results;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "2.0.
|
|
1
|
+
export declare const SDK_VERSION = "2.0.12";
|
|
2
2
|
export declare function compareVersions(v1: string, v2: string): number;
|
|
@@ -19,4 +19,4 @@ export * from './services/ApiService.js';
|
|
|
19
19
|
export * from './services/RpcServices.js';
|
|
20
20
|
export * from './utils/helpers.js';
|
|
21
21
|
export * from './utils/typeValidator.js';
|
|
22
|
-
export * from './utils/
|
|
22
|
+
export * from './utils/addressBalance.js';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare const PROTOCOLS: {
|
|
2
|
+
readonly AAVE: "AAVE";
|
|
3
|
+
readonly COMPOUND: "COMPOUND";
|
|
4
|
+
readonly IONIC: "IONIC";
|
|
5
|
+
readonly MOONWELL: "MOONWELL";
|
|
6
|
+
readonly WALLET: "WALLET";
|
|
7
|
+
};
|
|
8
|
+
export type Protocol = typeof PROTOCOLS[keyof typeof PROTOCOLS];
|
|
9
|
+
export interface GetUserProtocolBalancesParams {
|
|
10
|
+
chainId: number;
|
|
11
|
+
address: string;
|
|
12
|
+
contractAddress: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ProtocolBalanceResult {
|
|
15
|
+
protocol: Protocol;
|
|
16
|
+
wrapperTokenAddress: string;
|
|
17
|
+
wrapperBalance: string;
|
|
18
|
+
underlyingBalance: string;
|
|
19
|
+
}
|
|
20
|
+
export declare function getUserProtocolBalances(params: GetUserProtocolBalancesParams): Promise<ProtocolBalanceResult[]>;
|