74 lines
1.8 KiB
TypeScript
74 lines
1.8 KiB
TypeScript
/**
|
|
* Contract module for managing Dragonchain smart contracts
|
|
*/
|
|
|
|
import * as fs from 'fs';
|
|
import { DragonchainClient } from './client';
|
|
import {
|
|
ContentType,
|
|
SmartContractCreateRequest,
|
|
SmartContractUpdateRequest,
|
|
SmartContract,
|
|
ListResponse,
|
|
SuccessResponse,
|
|
} from './types';
|
|
|
|
export class ContractClient {
|
|
private client: DragonchainClient;
|
|
|
|
constructor(client: DragonchainClient) {
|
|
this.client = client;
|
|
}
|
|
|
|
/**
|
|
* Creates a new smart contract
|
|
*/
|
|
async create(request: SmartContractCreateRequest): Promise<SmartContract> {
|
|
return this.client.post<SmartContract>('/api/v1/contract', ContentType.JSON, request);
|
|
}
|
|
|
|
/**
|
|
* Gets a smart contract by ID
|
|
*/
|
|
async get(contractId: string): Promise<SmartContract> {
|
|
return this.client.get<SmartContract>(`/api/v1/contract/${contractId}`);
|
|
}
|
|
|
|
/**
|
|
* Lists all smart contracts
|
|
*/
|
|
async list(): Promise<ListResponse> {
|
|
return this.client.get<ListResponse>('/api/v1/contract');
|
|
}
|
|
|
|
/**
|
|
* Updates a smart contract
|
|
*/
|
|
async update(contractId: string, request: SmartContractUpdateRequest): Promise<SuccessResponse> {
|
|
return this.client.put<SuccessResponse>(
|
|
`/api/v1/contract/${contractId}`,
|
|
ContentType.JSON,
|
|
request
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Uploads smart contract code
|
|
*/
|
|
async upload(contractId: string, filePath: string): Promise<SuccessResponse> {
|
|
const fileContent = fs.readFileSync(filePath);
|
|
return this.client.put<SuccessResponse>(
|
|
`/api/v1/contract/${contractId}/upload`,
|
|
ContentType.OCTET_STREAM,
|
|
fileContent
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Deletes a smart contract
|
|
*/
|
|
async delete(contractId: string): Promise<SuccessResponse> {
|
|
return this.client.delete<SuccessResponse>(`/api/v1/contract/${contractId}`);
|
|
}
|
|
}
|