@lunch-money/ethereum-to-lunch-money 1.3.0 → 1.4.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.
- package/README.md +34 -10
- package/dist/cjs/src/client.js +2 -2
- package/dist/cjs/src/client.js.map +1 -1
- package/dist/cjs/src/main.js +44 -5
- package/dist/cjs/src/main.js.map +1 -1
- package/dist/cjs/src/types.d.ts +3 -3
- package/dist/cjs/src/types.js.map +1 -1
- package/dist/mjs/src/client.js +2 -2
- package/dist/mjs/src/client.js.map +1 -1
- package/dist/mjs/src/main.js +44 -5
- package/dist/mjs/src/main.js.map +1 -1
- package/dist/mjs/src/types.d.ts +3 -3
- package/dist/mjs/src/types.js.map +1 -1
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -15,20 +15,44 @@ yarn
|
|
|
15
15
|
yarn test
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
client against a real wallet. It is invoked in the following way:
|
|
18
|
+
## Debugging
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
export LM_ETHERSCAN_API_KEY="***********************"
|
|
23
|
-
export LM_ETHEREUM_WALLET_ADDRESS="0xA9D1e08C7793af67e9d92fe308d5697FB81d3E43"
|
|
20
|
+
The following environment variables can be run with the test script or when using the plugin with Lunch Money
|
|
24
21
|
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
- DEBUG_ETHEREUM - set to enable detailed debug messages and enable API Key validation
|
|
23
|
+
- ETHEREUM_BALANCE_TIMEOUT_MSECS - the number of msecs to wait for a response before timing out
|
|
27
24
|
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
The timeout was added to avoid a false CORS message when the API never returns.
|
|
26
|
+
|
|
27
|
+
Tweaking this to about 1000-2000, and watching the debug messages can be useful for validating that failover is working as expected.
|
|
28
|
+
|
|
29
|
+
## Live Testing
|
|
30
|
+
|
|
31
|
+
There is a script, `get-balances.ts`, that can be invoked to invoke the client against a real or test wallet. This mimics the behavior of the Lunch Money server when a user attempts to connect an Ethereum wallet.
|
|
32
|
+
|
|
33
|
+
Copy [env.example](./env.example) to .env and set some or all of the environment variables that the script and connector use.
|
|
34
|
+
|
|
35
|
+
### Ethereum Service Provider API Keys
|
|
36
|
+
|
|
37
|
+
If no keys are set, the script will use the public APIs which may or may not work. One or both of the following are recommended:
|
|
30
38
|
|
|
31
|
-
|
|
39
|
+
- ALCHEMY_API_KEY - [set up a key here](https://dashboard.alchemy.com/). This is the recommended service to use
|
|
40
|
+
- INFURA_API_KEY - [set up a key here](https://developer.metamask.io/). If set with Alchemy will be used as a secondary
|
|
41
|
+
|
|
42
|
+
While the connector can run with other service node keys, such as Etherscan, the test script will not support it.
|
|
43
|
+
|
|
44
|
+
### Wallet Configuration
|
|
45
|
+
- LM_ETHEREUM_WALLET_ADDRESS
|
|
46
|
+
|
|
47
|
+
If no wallet is set, the test script will use a well know test wallet
|
|
48
|
+
|
|
49
|
+
The Blockchain network is no longer configurable in the test script and defaults to "mainnet".
|
|
50
|
+
|
|
51
|
+
To run the script
|
|
52
|
+
```
|
|
53
|
+
yarn test-live
|
|
54
|
+
```
|
|
55
|
+
A .vscode/launch.json configuration is include to facilitate running the script in the debugger with VSCode.
|
|
32
56
|
|
|
33
57
|
## Refreshing the Token List
|
|
34
58
|
|
package/dist/cjs/src/client.js
CHANGED
|
@@ -46,7 +46,7 @@ const createEthereumWalletClient = (provider) => {
|
|
|
46
46
|
// Type pulled from: https://github.com/MyCryptoHQ/eth-scan/blob/master/src/providers/provider.ts#L32
|
|
47
47
|
const typedParams = params;
|
|
48
48
|
return provider.call({ to: typedParams[0].to, data: typedParams[0].data });
|
|
49
|
-
}
|
|
49
|
+
},
|
|
50
50
|
};
|
|
51
51
|
return {
|
|
52
52
|
getChainId() {
|
|
@@ -56,7 +56,7 @@ const createEthereumWalletClient = (provider) => {
|
|
|
56
56
|
},
|
|
57
57
|
getWeiBalance(walletAddress) {
|
|
58
58
|
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
-
return
|
|
59
|
+
return yield provider.getBalance(walletAddress);
|
|
60
60
|
});
|
|
61
61
|
},
|
|
62
62
|
getTokensBalance(walletAddress, tokenContractAddresses) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4DAA8C;AAG9C,yEAAoD;AAS7C,MAAM,0BAA0B,GAAG,CAAC,QAAiC,EAAwB,EAAE;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/client.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4DAA8C;AAG9C,yEAAoD;AAS7C,MAAM,0BAA0B,GAAG,CAAC,QAAiC,EAAwB,EAAE;IACpG,6HAA6H;IAC7H,0FAA0F;IAC1F,MAAM,cAAc,GAAuB;QACzC,IAAI,CAAS,MAAc,EAAE,MAA2B;YACtD,qGAAqG;YACrG,MAAM,WAAW,GAAG,MAAgD,CAAC;YAErE,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAoB,CAAC;QAChG,CAAC;KACF,CAAC;IAEF,OAAO;QACC,UAAU;;gBACd,OAAO,CAAC,MAAM,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC,OAAO,CAAC;YAC/C,CAAC;SAAA;QACK,aAAa,CAAC,aAAa;;gBAC/B,OAAO,MAAM,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;YAClD,CAAC;SAAA;QACK,gBAAgB,CAAC,aAAa,EAAE,sBAAsB;;gBAC1D,OAAO,OAAO,CAAC,gBAAgB,CAAC,cAAc,EAAE,aAAa,EAAE,sBAAsB,CAAC,CAAC;YACzF,CAAC;SAAA;KACF,CAAC;AACJ,CAAC,CAAC;AAvBW,QAAA,0BAA0B,8BAuBrC;AAWK,MAAM,aAAa,GAAG,GAA2B,EAAE;IACxD,OAAO,qBAAc,CAAC,MAAM,CAAC;AAC/B,CAAC,CAAA,CAAC;AAFW,QAAA,aAAa,iBAExB"}
|
package/dist/cjs/src/main.js
CHANGED
|
@@ -40,6 +40,15 @@ Object.defineProperty(exports, "createEthereumWalletClient", { enumerable: true,
|
|
|
40
40
|
/** The minimum balance (in wei) that a token should have in order to be
|
|
41
41
|
* considered for returning as a balance. */
|
|
42
42
|
const NEGLIGIBLE_BALANCE_THRESHOLD = 1000;
|
|
43
|
+
/**
|
|
44
|
+
* Debug function that logs to console.log if DEBUG_ETHEREUM environment variable is set
|
|
45
|
+
*/
|
|
46
|
+
const debug = (...args) => {
|
|
47
|
+
if (process.env.DEBUG_ETHEREUM) {
|
|
48
|
+
const timestamp = new Date().toISOString();
|
|
49
|
+
console.log(`[DEBUG_ETHEREUM] [${timestamp}]`, ...args);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
43
52
|
exports.LunchMoneyEthereumWalletConnection = {
|
|
44
53
|
initiate(config, context) {
|
|
45
54
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -48,11 +57,40 @@ exports.LunchMoneyEthereumWalletConnection = {
|
|
|
48
57
|
},
|
|
49
58
|
getBalances({ walletAddress, negligibleBalanceThreshold = NEGLIGIBLE_BALANCE_THRESHOLD }, { client }) {
|
|
50
59
|
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const
|
|
60
|
+
const obscuredWalletAddress = `0x..${walletAddress.slice(-6)}`;
|
|
61
|
+
debug('getBalances called for wallet address:', obscuredWalletAddress);
|
|
62
|
+
// Create a timeout so we fail instead of generating a CORS errors
|
|
63
|
+
let timeoutId;
|
|
64
|
+
const timeoutDuration = process.env.ETHEREUM_BALANCE_TIMEOUT_MSECS
|
|
65
|
+
? parseInt(process.env.ETHEREUM_BALANCE_TIMEOUT_MSECS)
|
|
66
|
+
: 60000;
|
|
67
|
+
const timeout = new Promise((_, reject) => {
|
|
68
|
+
timeoutId = setTimeout(() => {
|
|
69
|
+
reject(new Error(`Ethereum connector getBalances timed out after ${timeoutDuration} milliseconds.`));
|
|
70
|
+
}, timeoutDuration);
|
|
71
|
+
});
|
|
72
|
+
// Wrap all ethers calls in a single timeout
|
|
73
|
+
const result = yield Promise.race([
|
|
74
|
+
(() => __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
try {
|
|
76
|
+
const weiBalance = yield client.getWeiBalance(walletAddress);
|
|
77
|
+
// Filter out tokens that are not on mainnet
|
|
78
|
+
const chainId = yield client.getChainId();
|
|
79
|
+
const chainFilteredTokensList = (yield (0, client_js_1.loadTokenList)()).filter((t) => BigInt(t.chainId) === BigInt(chainId));
|
|
80
|
+
const map = yield client.getTokensBalance(walletAddress, chainFilteredTokensList.map((t) => t.address));
|
|
81
|
+
return { weiBalance, chainId, map, chainFilteredTokensList };
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
// Clear the timeout when the operation completes (success or failure)
|
|
85
|
+
if (timeoutId) {
|
|
86
|
+
clearTimeout(timeoutId);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}))(),
|
|
90
|
+
timeout,
|
|
91
|
+
]);
|
|
92
|
+
const { weiBalance, chainId, map, chainFilteredTokensList } = result;
|
|
93
|
+
debug('ethers.getTokensBalance returned for wallet address:', obscuredWalletAddress);
|
|
56
94
|
const balances = Object.entries(map)
|
|
57
95
|
.map(([address, balance]) => {
|
|
58
96
|
const token = chainFilteredTokensList.find((t) => t.address === address);
|
|
@@ -71,6 +109,7 @@ exports.LunchMoneyEthereumWalletConnection = {
|
|
|
71
109
|
.filter((b) => ethers.parseUnits(b.amount, 18) > negligibleBalanceThreshold)
|
|
72
110
|
.map((b) => ({ asset: b.asset, amount: String(b.amount) }))
|
|
73
111
|
.sort((a, b) => a.asset.localeCompare(b.asset));
|
|
112
|
+
debug(`Returning from getBalances for ${obscuredWalletAddress}:`, balances);
|
|
74
113
|
return {
|
|
75
114
|
providerName: 'wallet_ethereum',
|
|
76
115
|
balances,
|
package/dist/cjs/src/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.js","sourceRoot":"","sources":["../../../src/main.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,2CAAkE;AAClE,+CAAiC;AAEjC,yCAAyD;AAAhD,uHAAA,0BAA0B,OAAA;AAEnC;4CAC4C;AAC5C,MAAM,4BAA4B,GAAG,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../../../src/main.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,2CAAkE;AAClE,+CAAiC;AAEjC,yCAAyD;AAAhD,uHAAA,0BAA0B,OAAA;AAEnC;4CAC4C;AAC5C,MAAM,4BAA4B,GAAG,IAAI,CAAC;AAE1C;;GAEG;AACH,MAAM,KAAK,GAAG,CAAC,GAAG,IAAe,EAAQ,EAAE;IACzC,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE;QAC9B,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QAC3C,OAAO,CAAC,GAAG,CAAC,qBAAqB,SAAS,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC;KACzD;AACH,CAAC,CAAC;AAYW,QAAA,kCAAkC,GAG3C;IACI,QAAQ,CAAC,MAAM,EAAE,OAAO;;YAC5B,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAC3C,CAAC;KAAA;IAEK,WAAW,CAAC,EAAE,aAAa,EAAE,0BAA0B,GAAG,4BAA4B,EAAE,EAAE,EAAE,MAAM,EAAE;;YACxG,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C,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,GAAG,0BAA0B,CAAC;iBAC3E,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;iBAC1D,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YAElD,KAAK,CAAC,kCAAkC,qBAAqB,GAAG,EAAE,QAAQ,CAAC,CAAC;YAE5E,OAAO;gBACL,YAAY,EAAE,iBAAiB;gBAC/B,QAAQ;aACT,CAAC;QACJ,CAAC;KAAA;CACF,CAAC"}
|
package/dist/cjs/src/types.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Serializable configuration for the connection
|
|
3
3
|
*/
|
|
4
|
-
export type LunchMoneyCryptoConnectionConfig =
|
|
4
|
+
export type LunchMoneyCryptoConnectionConfig = Record<string, unknown>;
|
|
5
5
|
/**
|
|
6
6
|
* Non-serializable injected dependencies for the connection
|
|
7
7
|
*/
|
|
8
|
-
export type LunchMoneyCryptoConnectionContext =
|
|
8
|
+
export type LunchMoneyCryptoConnectionContext = Record<string, unknown>;
|
|
9
9
|
export interface CryptoBalance {
|
|
10
10
|
asset: string;
|
|
11
11
|
amount: string;
|
|
12
12
|
}
|
|
13
13
|
export declare const providerNames: readonly ["coinbase", "coinbase_pro", "kraken", "binance", "wallet_ethereum"];
|
|
14
|
-
export type ProviderName = typeof providerNames[number];
|
|
14
|
+
export type ProviderName = (typeof providerNames)[number];
|
|
15
15
|
export interface LunchMoneyCryptoConnectionBalances {
|
|
16
16
|
providerName: ProviderName;
|
|
17
17
|
balances: CryptoBalance[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":";;;AAmBa,QAAA,aAAa,GAAG,CAAC,UAAU,EAAE,cAAc,EAAE,QAAQ,EAAE,SAAS,EAAE,iBAAiB,CAAU,CAAC"}
|
package/dist/mjs/src/client.js
CHANGED
|
@@ -8,14 +8,14 @@ export const createEthereumWalletClient = (provider) => {
|
|
|
8
8
|
// Type pulled from: https://github.com/MyCryptoHQ/eth-scan/blob/master/src/providers/provider.ts#L32
|
|
9
9
|
const typedParams = params;
|
|
10
10
|
return provider.call({ to: typedParams[0].to, data: typedParams[0].data });
|
|
11
|
-
}
|
|
11
|
+
},
|
|
12
12
|
};
|
|
13
13
|
return {
|
|
14
14
|
async getChainId() {
|
|
15
15
|
return (await provider.getNetwork()).chainId;
|
|
16
16
|
},
|
|
17
17
|
async getWeiBalance(walletAddress) {
|
|
18
|
-
return
|
|
18
|
+
return await provider.getBalance(walletAddress);
|
|
19
19
|
},
|
|
20
20
|
async getTokensBalance(walletAddress, tokenContractAddresses) {
|
|
21
21
|
return ethscan.getTokensBalance(customProvider, walletAddress, tokenContractAddresses);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAC;AAG9C,OAAO,cAAc,MAAM,wBAAwB,CAAC;AASpD,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,QAAiC,EAAwB,EAAE;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAC;AAG9C,OAAO,cAAc,MAAM,wBAAwB,CAAC;AASpD,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,QAAiC,EAAwB,EAAE;IACpG,6HAA6H;IAC7H,0FAA0F;IAC1F,MAAM,cAAc,GAAuB;QACzC,IAAI,CAAS,MAAc,EAAE,MAA2B;YACtD,qGAAqG;YACrG,MAAM,WAAW,GAAG,MAAgD,CAAC;YAErE,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAoB,CAAC;QAChG,CAAC;KACF,CAAC;IAEF,OAAO;QACL,KAAK,CAAC,UAAU;YACd,OAAO,CAAC,MAAM,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC,OAAO,CAAC;QAC/C,CAAC;QACD,KAAK,CAAC,aAAa,CAAC,aAAa;YAC/B,OAAO,MAAM,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;QAClD,CAAC;QACD,KAAK,CAAC,gBAAgB,CAAC,aAAa,EAAE,sBAAsB;YAC1D,OAAO,OAAO,CAAC,gBAAgB,CAAC,cAAc,EAAE,aAAa,EAAE,sBAAsB,CAAC,CAAC;QACzF,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AAWF,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,IAAsB,EAAE;IACxD,OAAO,cAAc,CAAC,MAAM,CAAC;AAC/B,CAAC,CAAC"}
|
package/dist/mjs/src/main.js
CHANGED
|
@@ -4,16 +4,54 @@ export { createEthereumWalletClient } from './client.js';
|
|
|
4
4
|
/** The minimum balance (in wei) that a token should have in order to be
|
|
5
5
|
* considered for returning as a balance. */
|
|
6
6
|
const NEGLIGIBLE_BALANCE_THRESHOLD = 1000;
|
|
7
|
+
/**
|
|
8
|
+
* Debug function that logs to console.log if DEBUG_ETHEREUM environment variable is set
|
|
9
|
+
*/
|
|
10
|
+
const debug = (...args) => {
|
|
11
|
+
if (process.env.DEBUG_ETHEREUM) {
|
|
12
|
+
const timestamp = new Date().toISOString();
|
|
13
|
+
console.log(`[DEBUG_ETHEREUM] [${timestamp}]`, ...args);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
7
16
|
export const LunchMoneyEthereumWalletConnection = {
|
|
8
17
|
async initiate(config, context) {
|
|
9
18
|
return this.getBalances(config, context);
|
|
10
19
|
},
|
|
11
20
|
async getBalances({ walletAddress, negligibleBalanceThreshold = NEGLIGIBLE_BALANCE_THRESHOLD }, { client }) {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
21
|
+
const obscuredWalletAddress = `0x..${walletAddress.slice(-6)}`;
|
|
22
|
+
debug('getBalances called for wallet address:', obscuredWalletAddress);
|
|
23
|
+
// Create a timeout so we fail instead of generating a CORS errors
|
|
24
|
+
let timeoutId;
|
|
25
|
+
const timeoutDuration = process.env.ETHEREUM_BALANCE_TIMEOUT_MSECS
|
|
26
|
+
? parseInt(process.env.ETHEREUM_BALANCE_TIMEOUT_MSECS)
|
|
27
|
+
: 60000;
|
|
28
|
+
const timeout = new Promise((_, reject) => {
|
|
29
|
+
timeoutId = setTimeout(() => {
|
|
30
|
+
reject(new Error(`Ethereum connector getBalances timed out after ${timeoutDuration} milliseconds.`));
|
|
31
|
+
}, timeoutDuration);
|
|
32
|
+
});
|
|
33
|
+
// Wrap all ethers calls in a single timeout
|
|
34
|
+
const result = await Promise.race([
|
|
35
|
+
(async () => {
|
|
36
|
+
try {
|
|
37
|
+
const weiBalance = await client.getWeiBalance(walletAddress);
|
|
38
|
+
// Filter out tokens that are not on mainnet
|
|
39
|
+
const chainId = await client.getChainId();
|
|
40
|
+
const chainFilteredTokensList = (await loadTokenList()).filter((t) => BigInt(t.chainId) === BigInt(chainId));
|
|
41
|
+
const map = await client.getTokensBalance(walletAddress, chainFilteredTokensList.map((t) => t.address));
|
|
42
|
+
return { weiBalance, chainId, map, chainFilteredTokensList };
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
// Clear the timeout when the operation completes (success or failure)
|
|
46
|
+
if (timeoutId) {
|
|
47
|
+
clearTimeout(timeoutId);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
})(),
|
|
51
|
+
timeout,
|
|
52
|
+
]);
|
|
53
|
+
const { weiBalance, chainId, map, chainFilteredTokensList } = result;
|
|
54
|
+
debug('ethers.getTokensBalance returned for wallet address:', obscuredWalletAddress);
|
|
17
55
|
const balances = Object.entries(map)
|
|
18
56
|
.map(([address, balance]) => {
|
|
19
57
|
const token = chainFilteredTokensList.find((t) => t.address === address);
|
|
@@ -32,6 +70,7 @@ export const LunchMoneyEthereumWalletConnection = {
|
|
|
32
70
|
.filter((b) => ethers.parseUnits(b.amount, 18) > negligibleBalanceThreshold)
|
|
33
71
|
.map((b) => ({ asset: b.asset, amount: String(b.amount) }))
|
|
34
72
|
.sort((a, b) => a.asset.localeCompare(b.asset));
|
|
73
|
+
debug(`Returning from getBalances for ${obscuredWalletAddress}:`, balances);
|
|
35
74
|
return {
|
|
36
75
|
providerName: 'wallet_ethereum',
|
|
37
76
|
balances,
|
package/dist/mjs/src/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.js","sourceRoot":"","sources":["../../../src/main.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,aAAa,EAAwB,MAAM,aAAa,CAAC;AAClE,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAEzD;4CAC4C;AAC5C,MAAM,4BAA4B,GAAG,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../../../src/main.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,aAAa,EAAwB,MAAM,aAAa,CAAC;AAClE,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAEzD;4CAC4C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
|
-
export type LunchMoneyCryptoConnectionConfig =
|
|
4
|
+
export type LunchMoneyCryptoConnectionConfig = Record<string, unknown>;
|
|
5
5
|
/**
|
|
6
6
|
* Non-serializable injected dependencies for the connection
|
|
7
7
|
*/
|
|
8
|
-
export type LunchMoneyCryptoConnectionContext =
|
|
8
|
+
export type LunchMoneyCryptoConnectionContext = Record<string, unknown>;
|
|
9
9
|
export interface CryptoBalance {
|
|
10
10
|
asset: string;
|
|
11
11
|
amount: string;
|
|
12
12
|
}
|
|
13
13
|
export declare const providerNames: readonly ["coinbase", "coinbase_pro", "kraken", "binance", "wallet_ethereum"];
|
|
14
|
-
export type ProviderName = typeof providerNames[number];
|
|
14
|
+
export type ProviderName = (typeof providerNames)[number];
|
|
15
15
|
export interface LunchMoneyCryptoConnectionBalances {
|
|
16
16
|
providerName: ProviderName;
|
|
17
17
|
balances: CryptoBalance[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/types.ts"],"names":[],"mappings":"AAmBA,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,UAAU,EAAE,cAAc,EAAE,QAAQ,EAAE,SAAS,EAAE,iBAAiB,CAAU,CAAC"}
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"url": "https://github.com/lunch-money/ethereum-to-lunch-money/issues"
|
|
7
7
|
},
|
|
8
8
|
"homepage": "https://github.com/lunch-money/ethereum-to-lunch-money#README",
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.4.0",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"keywords": [
|
|
12
12
|
"lunch money",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"build": "yarn clean && npx tsc -p tsconfig.build-mjs.json && npx tsc -p tsconfig.build-cjs.json && ./postbuild",
|
|
43
43
|
"lint": "eslint '{src,test}/**/*.{js,ts,tsx}' --fix",
|
|
44
44
|
"test": "tsx node_modules/mocha/lib/cli/cli --recursive --extension ts",
|
|
45
|
+
"test-live": "npx ts-node --project tsconfig.build-cjs.json bin/get-balances.ts",
|
|
45
46
|
"prepare": "husky install"
|
|
46
47
|
},
|
|
47
48
|
"devDependencies": {
|
|
@@ -52,7 +53,8 @@
|
|
|
52
53
|
"@typescript-eslint/eslint-plugin": "^8.22.0",
|
|
53
54
|
"@typescript-eslint/parser": "^8.22.0",
|
|
54
55
|
"chai": "^5.1.2",
|
|
55
|
-
"
|
|
56
|
+
"dotenv": "^16.5.0",
|
|
57
|
+
"eslint": "^8.57.0",
|
|
56
58
|
"eslint-config-prettier": "^10.0.1",
|
|
57
59
|
"eslint-plugin-prettier": "^5.2.3",
|
|
58
60
|
"husky": "^9.1.7",
|