Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support calldata #14

Merged
merged 12 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 49 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,10 @@ console.log(`FlatDirectory address is ${contracAddress}.`);

### upload

Upload data to the FlatDirectory.
Upload `buffer | file` to the FlatDirectory.

```js
const key = "test.txt";
const filePath = "/users/dist/test.txt";
const data = fs.readFileSync(filePath);

await flatDirectory.upload(key, data, {
const callback = {
onProgress: function (progress, count, isChange) {
console.log(`Uploaded ${progress} of ${count} chunks`);
},
Expand All @@ -144,45 +140,45 @@ await flatDirectory.upload(key, data, {
onFinish: function (totalUploadChunks, totalUploadSize, totalStorageCost) {
console.log(`Total upload chunk count is ${totalUploadChunks}, size is ${totalUploadSize}, storage cost is ${totalStorageCost}`);
}
});
```
};

### uploadFile
const request = {
key: "test.txt",
content: Buffer.from("big data"),
type: 2, // blob
callback: callback
}
await flatDirectory.upload(request);
```

Upload a file to the FlatDirectory.
If you want to use `file`, it can be divided into browser and Node.js.

Browser
```javascript
// <input id='fileToUpload' />
const file = document.getElementById('fileToUpload').files[0];
await flatDirectory.uploadFile(key, file, {
onProgress: function (progress, count, isChange) {
console.log(`Uploaded ${progress} of ${count} chunks`);
},
onFail: function (err) {
console.log(err);
},
onFinish: function (totalUploadChunks, totalUploadSize, totalStorageCost) {
console.log(`Total upload chunk count is ${totalUploadChunks}, size is ${totalUploadSize}, storage cost is ${totalStorageCost}`);
}
});

const request = {
key: "test.txt",
content: file,
type: 1, // calldata
callback: callback
}
await flatDirectory.upload(request);
```

Node.js
```javascript
const {NodeFile} = require("ethstorage-sdk/file");
const file = new NodeFile("/usr/download/test.jpg");
await flatDirectory.uploadFile(key, file, {
onProgress: function (progress, count, isChange) {
console.log(`Uploaded ${progress} of ${count} chunks`);
},
onFail: function (err) {
console.log(err);
},
onFinish: function (totalUploadChunks, totalUploadSize, totalStorageCost) {
console.log(`Total upload chunk count is ${totalUploadChunks}, size is ${totalUploadSize}, storage cost is ${totalStorageCost}`);
}
});

const request = {
key: "test.txt",
content: file,
type: 2, // blob
callback: callback
}
await flatDirectory.upload(request);
```

### download
Expand All @@ -209,29 +205,41 @@ await flatDirectory.download(key, {
Estimate gas costs before uploading.

```js
const key = "example1.txt";
const data = Buffer.from("large data to upload");
const request = {
key: "example1.txt",
content: Buffer.from("large data to upload"),
type: 2
}

const cost = await flatDirectory.estimateCost(key, data);
const cost = await flatDirectory.estimateCost(request);
console.log(`Gas Cost: ${cost.gasCost}, Storage Cost: ${cost.storageCost}`);
```

### estimateFileCost

Estimate gas costs before uploading.
Use `file`.

Browser
```javascript
// <input id='fileToUpload' />
const file = document.getElementById('fileToUpload').files[0];
const cost = await flatDirectory.estimateFileCost("example1.txt", file);

const request = {
key: "example1.txt",
content: file,
type: 2 // calldata
}
const cost = await flatDirectory.estimateCost(request);
console.log(`Gas Cost: ${cost.gasCost}, Storage Cost: ${cost.storageCost}`);
```

Node.js
```javascript
const {NodeFile} = require("ethstorage-sdk/file");
const file = new NodeFile("/usr/download/test.jpg");
const cost = await flatDirectory.estimateFileCost("example1.txt", file);
console.log(`Gas Cost: ${cost.gasCost}, Storage Cost: ${cost.storageCost}`);

const request = {
key: "example1.txt",
content: file,
type: 1 // blob
iteyelmp marked this conversation as resolved.
Show resolved Hide resolved
}
const cost = await flatDirectory.estimateCost(request);
```
31 changes: 26 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ declare module 'ethstorage-sdk' {
export const MAX_BLOB_COUNT: number;
export const PaddingPer31Bytes: number;
export const RawData: number;
export const BLOB_COUNT_LIMIT: number;
export const UPLOAD_TYPE_CALLDATA: number;
export const UPLOAD_TYPE_BLOB: number;

export const ETHSTORAGE_MAPPING: {
[chainId: number]: string;
Expand All @@ -17,14 +20,30 @@ declare module 'ethstorage-sdk' {
export const FlatDirectoryAbi: string[];
export const FlatDirectoryBytecode: string;

// Types
export type BufferLike = Buffer | Uint8Array;
export type FileLike = File | NodeFile;
export type ContentLike = BufferLike | FileLike;

// Interfaces
export interface SDKConfig {
rpc: string;
ethStorageRpc: string;
ethStorageRpc?: string;
privateKey: string;
address?: string;
}

export interface EstimateGasRequest {
key: string,
content: ContentLike,
type: number,
gasIncPct?: number,
}

export interface UploadRequest extends EstimateGasRequest {
callback: Partial<UploadCallback>,
}

export interface CostEstimate {
storageCost: bigint;
gasCost: bigint;
Expand Down Expand Up @@ -61,10 +80,8 @@ declare module 'ethstorage-sdk' {
setDefault(filename: string): Promise<boolean>;
remove(key: string): Promise<boolean>;
download(key: string, cb: Partial<DownloadCallback>): void;
estimateCost(key: string, data: Buffer | Uint8Array, gasIncPct?: number): Promise<CostEstimate>;
estimateFileCost(key: string, file: File | NodeFile, gasIncPct?: number): Promise<CostEstimate>;
upload(key: string, data: Buffer | Uint8Array, cb?: Partial<UploadCallback>, gasIncPct?: number): Promise<void>;
uploadFile(key: string, file: File | NodeFile, cb?: Partial<UploadCallback>, gasIncPct?: number): Promise<void>;
estimateCost(request: EstimateGasRequest): Promise<CostEstimate>;
upload(request: UploadRequest): Promise<void>;
}

// Utils
Expand All @@ -87,6 +104,10 @@ declare module 'ethstorage-sdk' {
export function encodeBlobs(data: Buffer): Uint8Array[];
export function decodeBlob(blob: string | Uint8Array): Uint8Array;
export function decodeBlobs(blobs: string | Uint8Array): Buffer;

export function getFileChunk(file: FileLike, fileSize: number, start: number, end: number);
export function isBuffer(content: BufferLike);
export function isFile(content: FileLike);
}

// Default export
Expand Down
Loading