@lunch-money/ethereum-to-lunch-money
Version:
A wrapper around Ethereum APIs for enabling Lunch Money to gather information about a user's wallet.
81 lines • 4.43 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LunchMoneyEthereumWalletConnection = exports.createEthereumWalletClient = void 0;
const client_js_1 = require("./client.js");
const ethers = __importStar(require("ethers"));
var client_js_2 = require("./client.js");
Object.defineProperty(exports, "createEthereumWalletClient", { enumerable: true, get: function () { return client_js_2.createEthereumWalletClient; } });
/** The minimum balance (in wei) that a token should have in order to be
* considered for returning as a balance. */
const NEGLIGIBLE_BALANCE_THRESHOLD = 1000;
exports.LunchMoneyEthereumWalletConnection = {
initiate(config, context) {
return __awaiter(this, void 0, void 0, function* () {
return this.getBalances(config, context);
});
},
getBalances({ walletAddress, negligibleBalanceThreshold = NEGLIGIBLE_BALANCE_THRESHOLD }, { client }) {
return __awaiter(this, void 0, void 0, function* () {
const weiBalance = yield client.getWeiBalance(walletAddress);
// Filter out tokens that are not on mainnet
const chainId = yield client.getChainId();
const chainFilteredTokensList = (yield (0, client_js_1.loadTokenList)()).filter((t) => BigInt(t.chainId) === BigInt(chainId));
const map = yield client.getTokensBalance(walletAddress, chainFilteredTokensList.map((t) => t.address));
const balances = Object.entries(map)
.map(([address, balance]) => {
const token = chainFilteredTokensList.find((t) => t.address === address);
if (!token) {
throw new Error(`Token ${address} not found in filtered token list for chainId ${chainId}`);
}
return {
asset: token.symbol,
undivisedAmount: balance,
decimals: token.decimals,
};
})
.concat({ asset: 'ETH', undivisedAmount: weiBalance, decimals: 18 })
.map(({ asset, undivisedAmount, decimals }) => ({ asset, amount: ethers.formatUnits(undivisedAmount, decimals) }))
// Normalize the amount to 18 decimal places (for the wei per eth standard) for filtering out negligible balances
.filter((b) => ethers.parseUnits(b.amount, 18) > negligibleBalanceThreshold)
.map((b) => ({ asset: b.asset, amount: String(b.amount) }))
.sort((a, b) => a.asset.localeCompare(b.asset));
return {
providerName: 'wallet_ethereum',
balances,
};
});
},
};
//# sourceMappingURL=main.js.map