@lidofinance/lido-ethereum-sdk
Version:
<div style="display: flex;" align="center"> <h1 align="center">Lido Ethereum SDK</h1> </div>
333 lines • 19.9 kB
JavaScript
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;
};
import { LidoSDKCore } from '../core/index.js';
import { Logger, Cache, ErrorHandler } from '../common/decorators/index.js';
import { erc20abi } from './abi/erc20abi.js';
import { encodeFunctionData, getContract, parseSignature, } from 'viem';
import { NOOP, PERMIT_MESSAGE_TYPES } from '../common/constants.js';
import { parseValue } from '../common/utils/parse-value.js';
import { LidoSDKModule } from '../common/class-primitives/sdk-module.js';
import { getEncodableContract } from '../common/index.js';
let AbstractLidoSDKErc20 = (() => {
var _a;
let _classSuper = 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 getEncodableContract(getContract({
address,
abi: erc20abi,
client: this.core.keyedClient,
}));
}
// Balance
async balance(address) {
const { address: parsedAddress } = await this.core.useAccount(address);
const contract = await this.getContract();
return contract.read.balanceOf([parsedAddress]);
}
// Transfer
async transfer(props) {
this.core.useWalletClient();
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
? encodeFunctionData({
abi: erc20abi,
functionName: 'transferFrom',
args: [from, to, amount],
})
: encodeFunctionData({
abi: 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 });
}
// PERMIT
async signPermit(props) {
const walletClient = this.core.useWalletClient();
const payload = await this.populatePermit(props);
const signature = await walletClient.signTypedData(payload);
const { s, r, v } = parseSignature(signature);
return {
v: Number(v),
r,
s,
chainId: BigInt(this.core.chain.id),
...payload.message,
};
}
async populatePermit(props) {
const { amount, account: accountProp, spender, deadline = 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: PERMIT_MESSAGE_TYPES,
primaryType: 'Permit',
message,
};
}
// Allowance
async approve(props) {
this.core.useWalletClient();
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: encodeFunctionData({
abi: 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]);
}
// Views
async erc20Metadata() {
if (this.core.publicClient.multicall) {
const contract = { address: await this.contractAddress(), abi: erc20abi };
const [decimals, name, symbol, domainSeparator] = await this.core.publicClient.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: parseValue(props.amount),
callback: props.callback ?? NOOP,
};
}
constructor() {
super(...arguments);
__runInitializers(this, _instanceExtraInitializers);
}
},
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
_getContract_decorators = [Logger('Contracts:'), Cache(30 * 60 * 1000, ['core.chain.id'])];
_balance_decorators = [Logger('Balances:'), ErrorHandler()];
_transfer_decorators = [Logger('Call:'), ErrorHandler()];
_populateTransfer_decorators = [Logger('Utils:'), ErrorHandler()];
_simulateTransfer_decorators = [Logger('Utils:'), ErrorHandler()];
_estimateTransfer_decorators = [Logger('Utils:'), ErrorHandler()];
_signPermit_decorators = [Logger('Permit:'), ErrorHandler()];
_populatePermit_decorators = [Logger('Utils:'), ErrorHandler()];
_approve_decorators = [Logger('Call:'), ErrorHandler()];
_populateApprove_decorators = [Logger('Utils:'), ErrorHandler()];
_simulateApprove_decorators = [Logger('Utils:'), ErrorHandler()];
_estimateApprove_decorators = [Logger('Utils:'), ErrorHandler()];
_allowance_decorators = [Logger('Views:')];
_erc20Metadata_decorators = [Logger('Views:'), ErrorHandler(), Cache(30 * 60 * 1000, ['core.chain.id', 'contractAddress'])];
_totalSupply_decorators = [Logger('Views:'), ErrorHandler()];
_nonces_decorators = [Logger('Views:'), ErrorHandler()];
_erc721Domain_decorators = [Logger('Views:'), ErrorHandler(), 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;
})();
export { AbstractLidoSDKErc20 };
//# sourceMappingURL=erc20.js.map