-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathindex.ts
More file actions
182 lines (180 loc) · 5.02 KB
/
index.ts
File metadata and controls
182 lines (180 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Program, Wallet, web3 } from "@project-serum/anchor";
import {
getStorageConfigPDA,
getUserInfo,
getAnchorEnvironmet,
} from "./utils/helpers";
import { ShadowDriveUserStaking } from "./utils/idl";
import {
createStorageAccount,
addStorage,
claimStake,
deleteFile,
deleteStorageAccount,
editFile,
getStorageAcc,
getStorageAccs,
makeStorageImmutable,
reduceStorage,
cancelDeleteFile,
cancelDeleteStorageAccount,
uploadFile,
uploadMultipleFiles,
listObjects,
listObjectsAndSizes,
redeemRent,
migrate,
} from "./methods";
import {
ShadowDriveVersion,
CreateStorageResponse,
ShadowBatchUploadResponse,
ShadowDriveResponse,
ShadowFile,
ShadowUploadResponse,
ShadowEditResponse,
StorageAccount,
StorageAccountResponse,
ListObjectsResponse,
ListObjectsAndSizesResponse,
StorageAccountInfo,
} from "./types";
interface ShadowDrive {
createStorageAccount(
name: string,
size: string,
version: ShadowDriveVersion,
owner2: web3.PublicKey
): Promise<CreateStorageResponse>;
addStorage(
key: web3.PublicKey,
size: string,
version: ShadowDriveVersion
): Promise<ShadowDriveResponse>;
claimStake(
key: web3.PublicKey,
version: ShadowDriveVersion
): Promise<ShadowDriveResponse>;
deleteFile(
key: web3.PublicKey,
url: string,
version: ShadowDriveVersion
): Promise<ShadowDriveResponse>;
editFile(
key: web3.PublicKey,
url: string,
data: File | ShadowFile,
version: ShadowDriveVersion
): Promise<ShadowEditResponse>;
getStorageAcc?(key: web3.PublicKey): Promise<StorageAccount>;
getStorageAccs?(): Promise<StorageAccount[]>;
listObjects(key: web3.PublicKey): Promise<ListObjectsResponse>;
listObjectsAndSizes(
key: web3.PublicKey
): Promise<ListObjectsAndSizesResponse>;
makeStorageImmutable(
key: web3.PublicKey,
version: ShadowDriveVersion
): Promise<ShadowDriveResponse>;
getStorageAccount(key: web3.PublicKey): Promise<StorageAccountInfo>;
getStorageAccounts(
version: ShadowDriveVersion
): Promise<StorageAccountResponse[]>;
reduceStorage(
key: web3.PublicKey,
size: string,
version: ShadowDriveVersion
): Promise<ShadowDriveResponse>;
cancelDeleteFile(
key: web3.PublicKey,
url: string
): Promise<ShadowDriveResponse>;
cancelDeleteStorageAccount(
key: web3.PublicKey,
version: ShadowDriveVersion
): Promise<ShadowDriveResponse>;
uploadFile(
key: web3.PublicKey,
data: File | ShadowFile,
version: ShadowDriveVersion
): Promise<ShadowUploadResponse>;
uploadMultipleFiles(
key: web3.PublicKey,
data: FileList | ShadowFile[],
concurrent?: number,
callback?: Function
): Promise<ShadowBatchUploadResponse[]>;
deleteStorageAccount(
key: web3.PublicKey,
version: ShadowDriveVersion
): Promise<ShadowDriveResponse>;
redeemRent(
key: web3.PublicKey,
fileAccount: web3.PublicKey
): Promise<ShadowDriveResponse>;
migrate(key: web3.PublicKey): Promise<ShadowDriveResponse>;
}
/**
*
* Todo - Typescript does not currently support splitting up class definition into multiple files. These methods
* are therefore added as properties to the ShdwDrive class. Can move all method definitions into this file to resolve.
*
*/
export class ShdwDrive implements ShadowDrive {
private program: Program<ShadowDriveUserStaking>;
public storageConfigPDA: web3.PublicKey;
public userInfo: web3.PublicKey;
createStorageAccount = createStorageAccount;
addStorage = addStorage;
claimStake = claimStake;
deleteFile = deleteFile;
deleteStorageAccount = deleteStorageAccount;
editFile = editFile;
getStorageAccount = getStorageAcc;
getStorageAccounts = getStorageAccs;
listObjects = listObjects;
listObjectsAndSizes = listObjectsAndSizes;
makeStorageImmutable = makeStorageImmutable;
reduceStorage = reduceStorage;
/**
* @deprecated The method should not be used as of Shadow Drive v1.5
*/
cancelDeleteFile = cancelDeleteFile;
cancelDeleteStorageAccount = cancelDeleteStorageAccount;
uploadFile = uploadFile;
uploadMultipleFiles = uploadMultipleFiles;
redeemRent = redeemRent;
migrate = migrate;
/**
*
* @param connection {web3.Connection} connection - initialized web3 connection object
* @param wallet - Web3 wallet
*/
constructor(private connection: web3.Connection, private wallet: any) {
this.wallet = wallet;
this.connection = connection;
const [program, provider] = getAnchorEnvironmet(wallet, connection);
this.program = program;
}
public async init(): Promise<ShdwDrive> {
if (!this.wallet && !this.wallet.publicKey) {
return;
}
this.storageConfigPDA = (await getStorageConfigPDA(this.program))[0];
this.userInfo = (await getUserInfo(this.program, this.wallet.publicKey))[0];
return this;
}
}
export {
ShadowDriveVersion,
CreateStorageResponse,
ShadowDriveResponse,
ShadowUploadResponse,
ShadowEditResponse,
ShadowFile,
StorageAccount,
StorageAccountResponse,
ShadowBatchUploadResponse,
ListObjectsResponse,
StorageAccountInfo,
};