Skip to content

Commit

Permalink
lock assest with meshJs & json datum
Browse files Browse the repository at this point in the history
  • Loading branch information
saigonbitmaster committed Mar 13, 2023
1 parent 3fe1b71 commit bf55526
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 60 deletions.
18 changes: 18 additions & 0 deletions api/src/adminwallet/controller.spec.ts
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AdminWalletController } from './controller';

describe('AdminWalletController', () => {
let controller: AdminWalletController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AdminWalletController],
}).compile();

controller = module.get<AdminWalletController>(AdminWalletController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
58 changes: 58 additions & 0 deletions api/src/adminwallet/controller.ts
@@ -0,0 +1,58 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
Response,
Query,
Request,
} from '@nestjs/common';
import { CreateAdminWalletDto } from './dto/create.dto';
import { UpdateAdminWalletDto } from './dto/update.dto';
import { AdminWalletService } from './service';
import { queryTransform, formatRaList } from '../flatworks/utils/getlist';
import getToken from '../flatworks/utils/token';

import { JwtService } from '@nestjs/jwt';
import { userJwtPayload } from '../flatworks/types/types';

@Controller('adminwallets')
export class AdminWalletController {
constructor(
private readonly service: AdminWalletService,
private readonly jwtService: JwtService,
) {}

@Get()
async index(@Response() res: any, @Query() query) {
const mongooseQuery = queryTransform(query);
const result = await this.service.findAll(mongooseQuery);
return formatRaList(res, result);
}

@Get(':id')
async find(@Param('id') id: string) {
return await this.service.findOne(id);
}

@Post()
async create(@Body() createAdminWalletDto: CreateAdminWalletDto) {
return await this.service.create(createAdminWalletDto);
}

@Put(':id')
async update(
@Param('id') id: string,
@Body() updateAdminWalletDto: UpdateAdminWalletDto,
) {
return await this.service.update(id, updateAdminWalletDto);
}

@Delete(':id')
async delete(@Param('id') id: string) {
return await this.service.delete(id);
}
}
6 changes: 6 additions & 0 deletions api/src/adminwallet/dto/base.dto.ts
@@ -0,0 +1,6 @@
export class BaseAdminWalletDto {
address: string;
pKeyHash: string;
pKeyHashBech32: string;
description: string;
}
4 changes: 4 additions & 0 deletions api/src/adminwallet/dto/create.dto.ts
@@ -0,0 +1,4 @@
// Wallet/dto/create-Wallet.dto.ts
import { BaseAdminWalletDto } from './base.dto';

export class CreateAdminWalletDto extends BaseAdminWalletDto {}
6 changes: 6 additions & 0 deletions api/src/adminwallet/dto/update.dto.ts
@@ -0,0 +1,6 @@
// todo/dto/update-todo.dto.ts
import { BaseAdminWalletDto } from './base.dto';

export class UpdateAdminWalletDto extends BaseAdminWalletDto {
completedAt: Date;
}
17 changes: 17 additions & 0 deletions api/src/adminwallet/module.ts
@@ -0,0 +1,17 @@
import { Module } from '@nestjs/common';
import { AdminWalletService } from './service';
import { AdminWalletController } from './controller';
import { MongooseModule } from '@nestjs/mongoose';
import { AdminWallet, AdminWalletSchema } from './schemas/schema';
import { JwtService } from '@nestjs/jwt';

@Module({
providers: [AdminWalletService, JwtService],
controllers: [AdminWalletController],
imports: [
MongooseModule.forFeature([
{ name: AdminWallet.name, schema: AdminWalletSchema },
]),
],
})
export class AdminWalletModule {}
28 changes: 28 additions & 0 deletions api/src/adminwallet/schemas/schema.ts
@@ -0,0 +1,28 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import * as uniqueValidator from 'mongoose-unique-validator';

export type AdminWalletDocument = AdminWallet & Document;

@Schema()
export class AdminWallet {
@Prop({ required: true, unique: true })
address: string;

@Prop()
pKeyHash?: string;

@Prop()
pKeyHashBech32?: string;

@Prop()
completedAt?: Date;

@Prop()
createdAt?: Date;
}

const AdminWalletSchema = SchemaFactory.createForClass(AdminWallet);
AdminWalletSchema.plugin(uniqueValidator);

export { AdminWalletSchema };
18 changes: 18 additions & 0 deletions api/src/adminwallet/service.spec.ts
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AdminWalletService } from './service';

describe('AdminWalletService', () => {
let service: AdminWalletService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AdminWalletService],
}).compile();

service = module.get<AdminWalletService>(AdminWalletService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
62 changes: 62 additions & 0 deletions api/src/adminwallet/service.ts
@@ -0,0 +1,62 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CreateAdminWalletDto } from './dto/create.dto';
import { UpdateAdminWalletDto } from './dto/update.dto';
import { AdminWallet, AdminWalletDocument } from './schemas/schema';
import { RaList, MongooseQuery } from '../flatworks/types/types';
import { inspectAddress } from 'cardano-addresses';

@Injectable()
export class AdminWalletService {
constructor(
@InjectModel(AdminWallet.name)
private readonly model: Model<AdminWalletDocument>,
) {}

async findAll(query: MongooseQuery): Promise<RaList> {
const count = await this.model.find(query.filter).count().exec();
const data = await this.model
.find(query.filter)
.sort(query.sort)
.skip(query.skip)
.limit(query.limit)
.exec();

return { count: count, data: data };
}

async findOne(id: string): Promise<AdminWallet> {
return await this.model.findById(id).exec();
}

async findByUser(userId: string): Promise<AdminWallet> {
return await this.model.findOne({ userId }).exec();
}

async create(
createAdminWalletDto: CreateAdminWalletDto,
): Promise<AdminWallet> {
const address = createAdminWalletDto.address;
console.log(address);

const info = (await inspectAddress(address)) as any;
return await new this.model({
...createAdminWalletDto,
createdAt: new Date(),
pKeyHash: info.spending_key_hash,
pKeyHashBech32: info.spending_key_hash_bech32,
}).save();
}

async update(
id: string,
updateAdminWalletDto: UpdateAdminWalletDto,
): Promise<AdminWallet> {
return await this.model.findByIdAndUpdate(id, updateAdminWalletDto).exec();
}

async delete(id: string): Promise<AdminWallet> {
return await this.model.findByIdAndDelete(id).exec();
}
}
2 changes: 2 additions & 0 deletions api/src/app.module.ts
Expand Up @@ -13,6 +13,7 @@ import { ContractModule } from './contract/module';
import { QueueModule } from './queue/queue.module';
import { BullModule } from '@nestjs/bull';
import { JobTaskModule } from './jobtask/module';
import { AdminWalletModule } from './adminwallet/module';

@Module({
imports: [
Expand Down Expand Up @@ -43,6 +44,7 @@ import { JobTaskModule } from './jobtask/module';
QueueModule,
ContractModule,
JobTaskModule,
AdminWalletModule,
],
})
export class AppModule {}
33 changes: 26 additions & 7 deletions emp/src/components/smartContractJobMeshJs.tsx
Expand Up @@ -38,9 +38,11 @@ export default function SmartContract(props) {
const handleChangeLockAda = props.handleChangeLockAda || null;
const handleChangRedeemAda = props.handleChangRedeemAda || null;
const redeemAdaFromPlutus = props.redeemAdaFromPlutus || null;
const lockAdaValues = props.lockAdaValues || {};
const amountToLock = props.amountToLock || {};
const datum = props.datum || {};

const handleChangeUnlockPartner = props.handleChangeUnlockPartner || null;
const unlockPartner = props.unlockPartner || "bworks";
const handleChangePublicKeyHash = props.handleChangePublicKeyHash || null;
if (!contracts || contracts.length === 0) {
return (
<Typography variant="subtitle1" gutterBottom>
Expand Down Expand Up @@ -72,7 +74,6 @@ export default function SmartContract(props) {
sx={{
paddingTop: 0,
paddingLeft: 0,

display: "flex",
flexDirection: "column",
justifyContent: "space-between",
Expand Down Expand Up @@ -140,21 +141,39 @@ export default function SmartContract(props) {
label="Value (ADA)"
variant="standard"
type="number"
value={lockAdaValues.amountToLock}
onChange={handleChangeLockAda("amountToLock")}
value={amountToLock}
onChange={handleChangeLockAda}
/>
<FormControl variant="standard" sx={{ minWidth: 120 }}>
<InputLabel id="unlockPartner">Select unlock partner</InputLabel>
<Select
labelId="unlockPartner"
id="unlockPartner"
value={unlockPartner}
onChange={handleChangeUnlockPartner}
label="Select unlock partner"
sx={{ width: 240 }}
>
<MenuItem value={"bworks"}>By bWorks</MenuItem>
<MenuItem value={"employer"}>By employer</MenuItem>
<MenuItem value={"other"}>By other</MenuItem>
</Select>
</FormControl>
<TextField
sx={{ width: 500 }}
id="standard-basic"
label="Datum PublicKeyHash"
variant="standard"
value={datum.publicKeyHash}
onChange={handleChangeLockAda("datumToLock")}
onChange={handleChangePublicKeyHash}
disabled={
unlockPartner === "bworks" || unlockPartner === "employer"
}
/>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DateTimePicker
label="Datum Deadline"
value={props.dateValue}
value={datum.deadline}
onChange={props.handleChangeDate}
renderInput={(params) => (
<TextField {...params} sx={{ width: 500, p: 0 }} />
Expand Down

0 comments on commit bf55526

Please sign in to comment.