ethstorage-sdk
Version:
eip-4844 blobs upload sdk
204 lines (191 loc) • 8.67 kB
text/typescript
import { ethers, Contract } from 'ethers';
declare const EthStorageAbi: readonly string[];
declare const FlatDirectoryAbi: readonly string[];
declare const ETHSTORAGE_MAPPING: Record<number, string>;
declare const BLOB_SIZE: number;
declare const OP_BLOB_DATA_SIZE: number;
declare enum DecodeType {
RawData = 0,
PaddingPer31Bytes = 1,
OptimismCompact = 2
}
declare const BLOB_COUNT_LIMIT: number;
declare const MAX_BLOB_COUNT: number;
declare enum UploadType {
Undefined = 0,
Calldata = 1,
Blob = 2
}
declare const MAX_RETRIES: number;
/**
* eth_call consumes gas, so we need to estimate the maximum number of chunks based on a 30 million gas limit.
* Additionally, we need to reserve a portion of the gas for the cost of the request parameters (which can vary dynamically).
*/
declare const MAX_CHUNKS: number;
declare const DUMMY_VERSIONED_COMMITMENT_HASH = "0x01f32ebe6ad26adca597cdb198f041f5d96fc197e3de72e299e86fbf1f5817c8";
declare const FlatDirectoryBytecode: string;
declare const FLAT_DIRECTORY_CONTRACT_VERSION_1_0_0: string;
interface NodeFile {
isNodeJs: boolean;
size: number;
start: number;
end: number;
slice(start: number, end: number): NodeFile;
arrayBuffer(): Promise<ArrayBuffer>;
text(): Promise<string>;
}
type BufferLike = Uint8Array;
type FileLike = File | NodeFile;
type ContentLike = BufferLike | FileLike;
interface SDKConfig {
rpc?: string;
privateKey?: string;
ethStorageRpc?: string;
address?: string;
}
interface UploadCallback {
onProgress: (currentChunk: number, totalChunks: number, isChange: boolean) => void;
onFail: (error: Error) => void;
onFinish: (totalUploadChunks: number, totalUploadSize: number, totalStorageCost: bigint) => void;
}
interface DownloadCallback {
onProgress: (currentChunk: number, totalChunks: number, chunkData: Uint8Array) => void;
onFail: (error: Error) => void;
onFinish: () => void;
}
interface EstimateGasRequest {
key: string;
content: ContentLike;
type: UploadType;
gasIncPct?: number;
chunkHashes?: string[];
}
interface UploadRequest extends EstimateGasRequest {
callback: Partial<UploadCallback>;
}
interface CostEstimate {
storageCost: bigint;
gasCost: bigint;
}
interface FileBatch {
name: string;
chunkIds: number[];
}
interface UploadDetails {
fileMode: number;
oldChunkCount: number;
cost: bigint;
gasFeeData?: ethers.FeeData;
maxFeePerBlobGas?: bigint;
}
interface ChunkCountResult {
key: string;
chunkCount: number;
}
interface ChunkHashResult {
name: string;
chunkId: number;
hash: string;
}
declare const stringToHex: (s: string) => string;
declare function getChainId(rpc: string): Promise<number>;
declare function getContentChunk(content: ContentLike, start: number, end: number): Promise<Uint8Array>;
declare function isBuffer(content: ContentLike): content is BufferLike;
declare function isFile(content: ContentLike): content is FileLike;
declare function isNodejs(): boolean;
declare function computeVersionedCommitmentHash(commitment: Uint8Array): Uint8Array;
declare function convertToEthStorageHash(commitment: Uint8Array): string;
declare function convertToEthStorageHashes(commitments: Uint8Array[]): string[];
declare function retry<T>(fn: (...args: any[]) => Promise<T>, retries: number, ...args: any[]): Promise<T>;
declare function copy(des: Uint8Array, desOff: number, src: Uint8Array, srcOff: number): number;
declare function encodeOpBlobs(data: Uint8Array): Uint8Array[];
declare function encodeOpBlob(data: Uint8Array): Uint8Array;
declare class BlobUploader {
private readonly provider;
private readonly wallet;
private readonly mutex;
private readonly kzg;
constructor(rpc: string, pk: string);
getBlobGasPrice(): Promise<bigint>;
getGasPrice(): Promise<ethers.FeeData>;
sendTx(tx: ethers.TransactionRequest, blobs?: Uint8Array[] | null, commitments?: Uint8Array[] | null): Promise<ethers.TransactionResponse>;
sendTxLock(tx: ethers.TransactionRequest, blobs?: Uint8Array[] | null, commitments?: Uint8Array[] | null): Promise<ethers.TransactionResponse>;
private send;
private lockSend;
computeCommitmentsForBlobs(blobs: Uint8Array[]): Promise<Uint8Array[]>;
computeEthStorageHashesForBlobs(blobs: Uint8Array[]): Promise<string[]>;
close(): Promise<void>;
}
declare function getUploadInfo(contract: Contract, hexName: string, retries: number): Promise<UploadDetails>;
declare function getChunkCounts(contract: Contract, batch: string[], retries: number): Promise<ChunkCountResult[]>;
declare function getChunkHashes(contract: Contract, batch: FileBatch[], retries: number): Promise<ChunkHashResult[]>;
type index_BlobUploader = BlobUploader;
declare const index_BlobUploader: typeof BlobUploader;
declare const index_computeVersionedCommitmentHash: typeof computeVersionedCommitmentHash;
declare const index_convertToEthStorageHash: typeof convertToEthStorageHash;
declare const index_convertToEthStorageHashes: typeof convertToEthStorageHashes;
declare const index_copy: typeof copy;
declare const index_encodeOpBlob: typeof encodeOpBlob;
declare const index_encodeOpBlobs: typeof encodeOpBlobs;
declare const index_getChainId: typeof getChainId;
declare const index_getChunkCounts: typeof getChunkCounts;
declare const index_getChunkHashes: typeof getChunkHashes;
declare const index_getContentChunk: typeof getContentChunk;
declare const index_getUploadInfo: typeof getUploadInfo;
declare const index_isBuffer: typeof isBuffer;
declare const index_isFile: typeof isFile;
declare const index_isNodejs: typeof isNodejs;
declare const index_retry: typeof retry;
declare const index_stringToHex: typeof stringToHex;
declare namespace index {
export { index_BlobUploader as BlobUploader, index_computeVersionedCommitmentHash as computeVersionedCommitmentHash, index_convertToEthStorageHash as convertToEthStorageHash, index_convertToEthStorageHashes as convertToEthStorageHashes, index_copy as copy, index_encodeOpBlob as encodeOpBlob, index_encodeOpBlobs as encodeOpBlobs, index_getChainId as getChainId, index_getChunkCounts as getChunkCounts, index_getChunkHashes as getChunkHashes, index_getContentChunk as getContentChunk, index_getUploadInfo as getUploadInfo, index_isBuffer as isBuffer, index_isFile as isFile, index_isNodejs as isNodejs, index_retry as retry, index_stringToHex as stringToHex };
}
declare class EthStorage {
private contractAddr;
private ethStorageRpc?;
private wallet?;
private blobUploader?;
static create(config: SDKConfig): Promise<EthStorage>;
private init;
estimateCost(key: string, data: Uint8Array): Promise<CostEstimate>;
write(key: string, data: Uint8Array): Promise<{
hash: string;
success: boolean;
}>;
read(key: string, decodeType?: DecodeType, address?: string): Promise<Uint8Array>;
writeBlobs(keys: string[], dataBlobs: Uint8Array[]): Promise<{
hash: string;
success: boolean;
}>;
close(): Promise<void>;
private get _wallet();
private get _blobUploader();
private get _ethStorageRpc();
private checkData;
}
declare class FlatDirectory {
#private;
private rpc?;
private ethStorageRpc?;
private contractAddr?;
private wallet?;
private blobUploader?;
private retries;
isSupportBlob: boolean;
static create(config: SDKConfig): Promise<FlatDirectory>;
init(config: SDKConfig): Promise<void>;
deploy(): Promise<string | null>;
setDefault(filename: string): Promise<boolean>;
remove(key: string): Promise<boolean>;
download(key: string, cb?: DownloadCallback): Promise<void>;
fetchHashes(keys: string[]): Promise<Record<string, string[]>>;
estimateCost(request: EstimateGasRequest): Promise<CostEstimate>;
upload(request: UploadRequest): Promise<void>;
close(): Promise<void>;
private get _contractAddr();
private get _ethStorageRpc();
private get _rpc();
private get _wallet();
private get _blobUploader();
}
export { BLOB_COUNT_LIMIT, BLOB_SIZE, type BufferLike, type ChunkCountResult, type ChunkHashResult, type ContentLike, type CostEstimate, DUMMY_VERSIONED_COMMITMENT_HASH, DecodeType, type DownloadCallback, ETHSTORAGE_MAPPING, type EstimateGasRequest, EthStorage, EthStorageAbi, FLAT_DIRECTORY_CONTRACT_VERSION_1_0_0, type FileBatch, type FileLike, FlatDirectory, FlatDirectoryAbi, FlatDirectoryBytecode, MAX_BLOB_COUNT, MAX_CHUNKS, MAX_RETRIES, type NodeFile, OP_BLOB_DATA_SIZE, type SDKConfig, type UploadCallback, type UploadDetails, type UploadRequest, UploadType, index as utils };