@lidofinance/lido-ethereum-sdk
Version:
<div style="display: flex;" align="center"> <h1 align="center">Lido Ethereum SDK</h1> </div>
334 lines • 20.7 kB
JavaScript
"use strict";
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
var useValue = arguments.length > 2;
for (var i = 0; i < initializers.length; i++) {
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
}
return useValue ? value : void 0;
};
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
var _, done = false;
for (var i = decorators.length - 1; i >= 0; i--) {
var context = {};
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
if (kind === "accessor") {
if (result === void 0) continue;
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
if (target) Object.defineProperty(target, contextIn.name, descriptor);
done = true;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractLidoSDKErc20 = void 0;
const index_js_1 = require("../core/index.js");
const index_js_2 = require("../common/decorators/index.js");
const erc20abi_js_1 = require("./abi/erc20abi.js");
const viem_1 = require("viem");
const constants_js_1 = require("../common/constants.js");
const parse_value_js_1 = require("../common/utils/parse-value.js");
const sdk_module_js_1 = require("../common/class-primitives/sdk-module.js");
const bytes_1 = require("@ethersproject/bytes");
let AbstractLidoSDKErc20 = (() => {
var _a;
let _classSuper = sdk_module_js_1.LidoSDKModule;
let _instanceExtraInitializers = [];
let _getContract_decorators;
let _balance_decorators;
let _transfer_decorators;
let _populateTransfer_decorators;
let _simulateTransfer_decorators;
let _estimateTransfer_decorators;
let _signPermit_decorators;
let _populatePermit_decorators;
let _approve_decorators;
let _populateApprove_decorators;
let _simulateApprove_decorators;
let _estimateApprove_decorators;
let _allowance_decorators;
let _erc20Metadata_decorators;
let _totalSupply_decorators;
let _nonces_decorators;
let _erc721Domain_decorators;
return _a = class AbstractLidoSDKErc20 extends _classSuper {
async getContract() {
const address = await this.contractAddress();
return (0, viem_1.getContract)({
address,
abi: erc20abi_js_1.erc20abi,
client: {
public: this.core.rpcProvider,
wallet: this.core.web3Provider,
},
});
}
async balance(address) {
const { address: parsedAddress } = await this.core.useAccount(address);
const contract = await this.getContract();
return contract.read.balanceOf([parsedAddress]);
}
async transfer(props) {
this.core.useWeb3Provider();
const parsedProps = await this.parseProps(props);
const { account, amount, to, from = account.address } = parsedProps;
const isTransferFrom = from !== account.address;
const contract = await this.getContract();
const getGasLimit = async (overrides) => isTransferFrom
? contract.estimateGas.transferFrom([from, to, amount], overrides)
: contract.estimateGas.transfer([to, amount], overrides);
const sendTransaction = async (overrides) => isTransferFrom
? contract.write.transferFrom([from, to, amount], overrides)
: contract.write.transfer([to, amount], overrides);
return this.core.performTransaction({
...parsedProps,
getGasLimit,
sendTransaction,
});
}
async populateTransfer(props) {
const parsedProps = await this.parseProps(props);
const { account, amount, to, from = account.address } = parsedProps;
const isTransferFrom = from !== account.address;
const contractAddress = await this.contractAddress();
return {
to: contractAddress,
from: account,
data: isTransferFrom
? (0, viem_1.encodeFunctionData)({
abi: erc20abi_js_1.erc20abi,
functionName: 'transferFrom',
args: [from, to, amount],
})
: (0, viem_1.encodeFunctionData)({
abi: erc20abi_js_1.erc20abi,
functionName: 'transfer',
args: [to, amount],
}),
};
}
async simulateTransfer(props) {
const parsedProps = await this.parseProps(props);
const { account, amount, to, from = account.address } = parsedProps;
const isTransferFrom = from !== account.address;
const contract = await this.getContract();
return isTransferFrom
? contract.simulate.transferFrom([from, to, amount], {
account,
})
: contract.simulate.transfer([to, amount], { account });
}
async estimateTransfer(props) {
const parsedProps = await this.parseProps(props);
const { account, amount, to, from = account.address } = parsedProps;
const isTransferFrom = from !== account.address;
const contract = await this.getContract();
return isTransferFrom
? contract.estimateGas.transferFrom([from, to, amount], {
account,
})
: contract.estimateGas.transfer([to, amount], { account });
}
async signPermit(props) {
const web3Provider = this.core.useWeb3Provider();
const payload = await this.populatePermit(props);
const signature = await web3Provider.signTypedData(payload);
const { s, r, v } = (0, bytes_1.splitSignature)(signature);
return {
v,
r: r,
s: s,
chainId: BigInt(this.core.chain.id),
...payload.message,
};
}
async populatePermit(props) {
const { amount, account: accountProp, spender, deadline = index_js_1.LidoSDKCore.INFINITY_DEADLINE_VALUE, } = props;
const contract = await this.getContract();
const domain = await this.erc721Domain();
const account = await this.core.useAccount(accountProp);
const nonce = await contract.read.nonces([account.address]);
const message = {
owner: account.address,
spender,
value: amount,
nonce,
deadline,
};
return {
account,
domain,
types: constants_js_1.PERMIT_MESSAGE_TYPES,
primaryType: 'Permit',
message,
};
}
async approve(props) {
this.core.useWeb3Provider();
const parsedProps = await this.parseProps(props);
const contract = await this.getContract();
const txArguments = [parsedProps.to, parsedProps.amount];
return this.core.performTransaction({
...parsedProps,
getGasLimit: (options) => contract.estimateGas.approve(txArguments, options),
sendTransaction: (options) => contract.write.approve(txArguments, options),
});
}
async populateApprove(props) {
const { account, amount, to } = await this.parseProps(props);
const address = await this.contractAddress();
return {
to: address,
from: account.address,
data: (0, viem_1.encodeFunctionData)({
abi: erc20abi_js_1.erc20abi,
functionName: 'approve',
args: [to, amount],
}),
};
}
async simulateApprove(props) {
const { account, amount, to } = await this.parseProps(props);
const contract = await this.getContract();
return contract.simulate.approve([to, amount], {
account,
});
}
async estimateApprove(props, options) {
const { account, amount, to } = await this.parseProps(props);
const contract = await this.getContract();
return contract.estimateGas.approve([to, amount], {
account,
...options,
});
}
async allowance({ account: accountProp, to, }) {
const account = await this.core.useAccount(accountProp);
return (await this.getContract()).read.allowance([account.address, to]);
}
async erc20Metadata() {
if (this.core.rpcProvider.multicall) {
const contract = { address: await this.contractAddress(), abi: erc20abi_js_1.erc20abi };
const [decimals, name, symbol, domainSeparator] = await this.core.rpcProvider.multicall({
allowFailure: false,
contracts: [
{
...contract,
functionName: 'decimals',
},
{
...contract,
functionName: 'name',
},
{
...contract,
functionName: 'symbol',
},
{
...contract,
functionName: 'DOMAIN_SEPARATOR',
},
],
});
return {
decimals,
name,
symbol,
domainSeparator,
};
}
else {
const contract = await this.getContract();
const [decimals, name, symbol, domainSeparator] = await Promise.all([
contract.read.decimals(),
contract.read.name(),
contract.read.symbol(),
contract.read.DOMAIN_SEPARATOR(),
]);
return {
decimals,
name,
symbol,
domainSeparator,
};
}
}
async totalSupply() {
return (await this.getContract()).read.totalSupply();
}
async nonces(address) {
return (await this.getContract()).read.nonces([address]);
}
async erc721Domain() {
const contract = await this.getContract();
const [name, version, chainId, verifyingContract] = await contract.read.eip712Domain();
return { name, version, chainId, verifyingContract };
}
async parseProps(props) {
return {
...props,
account: await this.core.useAccount(props.account),
amount: (0, parse_value_js_1.parseValue)(props.amount),
callback: props.callback ?? constants_js_1.NOOP,
};
}
constructor() {
super(...arguments);
__runInitializers(this, _instanceExtraInitializers);
}
},
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
_getContract_decorators = [(0, index_js_2.Logger)('Contracts:'), (0, index_js_2.Cache)(30 * 60 * 1000, ['core.chain.id'])];
_balance_decorators = [(0, index_js_2.Logger)('Balances:'), (0, index_js_2.ErrorHandler)()];
_transfer_decorators = [(0, index_js_2.Logger)('Call:'), (0, index_js_2.ErrorHandler)()];
_populateTransfer_decorators = [(0, index_js_2.Logger)('Utils:'), (0, index_js_2.ErrorHandler)()];
_simulateTransfer_decorators = [(0, index_js_2.Logger)('Utils:'), (0, index_js_2.ErrorHandler)()];
_estimateTransfer_decorators = [(0, index_js_2.Logger)('Utils:'), (0, index_js_2.ErrorHandler)()];
_signPermit_decorators = [(0, index_js_2.Logger)('Permit:'), (0, index_js_2.ErrorHandler)()];
_populatePermit_decorators = [(0, index_js_2.Logger)('Utils:'), (0, index_js_2.ErrorHandler)()];
_approve_decorators = [(0, index_js_2.Logger)('Call:'), (0, index_js_2.ErrorHandler)()];
_populateApprove_decorators = [(0, index_js_2.Logger)('Utils:'), (0, index_js_2.ErrorHandler)()];
_simulateApprove_decorators = [(0, index_js_2.Logger)('Utils:'), (0, index_js_2.ErrorHandler)()];
_estimateApprove_decorators = [(0, index_js_2.Logger)('Utils:'), (0, index_js_2.ErrorHandler)()];
_allowance_decorators = [(0, index_js_2.Logger)('Views:')];
_erc20Metadata_decorators = [(0, index_js_2.Logger)('Views:'), (0, index_js_2.ErrorHandler)(), (0, index_js_2.Cache)(30 * 60 * 1000, ['core.chain.id', 'contractAddress'])];
_totalSupply_decorators = [(0, index_js_2.Logger)('Views:'), (0, index_js_2.ErrorHandler)()];
_nonces_decorators = [(0, index_js_2.Logger)('Views:'), (0, index_js_2.ErrorHandler)()];
_erc721Domain_decorators = [(0, index_js_2.Logger)('Views:'), (0, index_js_2.ErrorHandler)(), (0, index_js_2.Cache)(30 * 60 * 1000, ['core.chain.id', 'contractAddress'])];
__esDecorate(_a, null, _getContract_decorators, { kind: "method", name: "getContract", static: false, private: false, access: { has: obj => "getContract" in obj, get: obj => obj.getContract }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _balance_decorators, { kind: "method", name: "balance", static: false, private: false, access: { has: obj => "balance" in obj, get: obj => obj.balance }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _transfer_decorators, { kind: "method", name: "transfer", static: false, private: false, access: { has: obj => "transfer" in obj, get: obj => obj.transfer }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _populateTransfer_decorators, { kind: "method", name: "populateTransfer", static: false, private: false, access: { has: obj => "populateTransfer" in obj, get: obj => obj.populateTransfer }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _simulateTransfer_decorators, { kind: "method", name: "simulateTransfer", static: false, private: false, access: { has: obj => "simulateTransfer" in obj, get: obj => obj.simulateTransfer }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _estimateTransfer_decorators, { kind: "method", name: "estimateTransfer", static: false, private: false, access: { has: obj => "estimateTransfer" in obj, get: obj => obj.estimateTransfer }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _signPermit_decorators, { kind: "method", name: "signPermit", static: false, private: false, access: { has: obj => "signPermit" in obj, get: obj => obj.signPermit }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _populatePermit_decorators, { kind: "method", name: "populatePermit", static: false, private: false, access: { has: obj => "populatePermit" in obj, get: obj => obj.populatePermit }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _approve_decorators, { kind: "method", name: "approve", static: false, private: false, access: { has: obj => "approve" in obj, get: obj => obj.approve }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _populateApprove_decorators, { kind: "method", name: "populateApprove", static: false, private: false, access: { has: obj => "populateApprove" in obj, get: obj => obj.populateApprove }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _simulateApprove_decorators, { kind: "method", name: "simulateApprove", static: false, private: false, access: { has: obj => "simulateApprove" in obj, get: obj => obj.simulateApprove }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _estimateApprove_decorators, { kind: "method", name: "estimateApprove", static: false, private: false, access: { has: obj => "estimateApprove" in obj, get: obj => obj.estimateApprove }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _allowance_decorators, { kind: "method", name: "allowance", static: false, private: false, access: { has: obj => "allowance" in obj, get: obj => obj.allowance }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _erc20Metadata_decorators, { kind: "method", name: "erc20Metadata", static: false, private: false, access: { has: obj => "erc20Metadata" in obj, get: obj => obj.erc20Metadata }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _totalSupply_decorators, { kind: "method", name: "totalSupply", static: false, private: false, access: { has: obj => "totalSupply" in obj, get: obj => obj.totalSupply }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _nonces_decorators, { kind: "method", name: "nonces", static: false, private: false, access: { has: obj => "nonces" in obj, get: obj => obj.nonces }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _erc721Domain_decorators, { kind: "method", name: "erc721Domain", static: false, private: false, access: { has: obj => "erc721Domain" in obj, get: obj => obj.erc721Domain }, metadata: _metadata }, null, _instanceExtraInitializers);
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
})(),
_a;
})();
exports.AbstractLidoSDKErc20 = AbstractLidoSDKErc20;
//# sourceMappingURL=erc20.js.map