Files
prime-sdk-go/contract/contract.go
Andrew Miller e15b205f9c Add remote smart contract and gRPC connection support
Add Remote field to SmartContractCreateRequest and GrpcConnectionInfo
struct to SmartContract model for remote smart contract execution
via gRPC.
2026-02-11 11:20:21 -05:00

82 lines
2.3 KiB
Go
Executable File

package contract
import (
"context"
"fmt"
"os"
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/client"
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/models"
)
type ContractClient struct {
client *client.Client
}
func NewContractClient(c *client.Client) *ContractClient {
return &ContractClient{client: c}
}
func (cc *ContractClient) Create(ctx context.Context, req *models.SmartContractCreateRequest) (*models.SmartContract, error) {
var resp models.SmartContract
err := cc.client.Post(ctx, "/api/v1/contract", models.ContentTypeJSON, req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) Get(ctx context.Context, contractID string) (*models.SmartContract, error) {
var resp models.SmartContract
path := fmt.Sprintf("/api/v1/contract/%s", contractID)
err := cc.client.Get(ctx, path, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) List(ctx context.Context) (*models.ListResponse, error) {
var resp models.ListResponse
err := cc.client.Get(ctx, "/api/v1/contract", &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) Update(ctx context.Context, contractID string, req *models.SmartContractUpdateRequest) (*models.SuccessResponse, error) {
var resp models.SuccessResponse
path := fmt.Sprintf("/api/v1/contract/%s", contractID)
err := cc.client.Put(ctx, path, models.ContentTypeJSON, req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) Upload(ctx context.Context, contractID string, filepath string) (*models.SuccessResponse, error) {
fileContent, err := os.ReadFile(filepath)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
var resp models.SuccessResponse
path := fmt.Sprintf("/api/v1/contract/%s/upload", contractID)
err = cc.client.Put(ctx, path, "application/octet-stream", fileContent, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (cc *ContractClient) Delete(ctx context.Context, contractID string) (*models.SuccessResponse, error) {
var resp models.SuccessResponse
path := fmt.Sprintf("/api/v1/contract/%s", contractID)
err := cc.client.Delete(ctx, path, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}