Skip to content

Commit

Permalink
fix: add/del currency
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulgalimov committed May 28, 2024
1 parent 3698877 commit 3487453
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "unique-marketplace-backend",
"version": "3.0.261",
"version": "3.0.262",
"description": "Backend project for unique marketplace",
"author": "Unique Network",
"private": true,
Expand Down
20 changes: 20 additions & 0 deletions packages/common/modules/database/services/settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ export class SettingsService {
return this.setValue('contract_currencies', JSON.stringify(currencies));
}

public async addContractCurrency(currency: CurrencyDto): Promise<void> {
const currencies = await this.getContractCurrencies();
const found = currencies.find((c) => c.collectionId === currency.collectionId);
if (found) {
found.iconUrl = currency.iconUrl;
found.decimals = currency.decimals;
found.fee = currency.fee;
} else {
currencies.push(currency);
}
return this.setContractCurrencies(currencies);
}

public async removeContractCurrency(collectionId: number): Promise<void> {
const currencies = await this.getContractCurrencies();
const newCurrencies = currencies.filter((c) => c.collectionId !== collectionId);

return this.setContractCurrencies(newCurrencies);
}

public async getContractCurrencies(): Promise<CurrencyDto[]> {
const value = await this.getValue('contract_currencies');
return value ? JSON.parse(value) : [];
Expand Down
19 changes: 14 additions & 5 deletions packages/market/src/contracts/contract.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { readApiDocs } from '../utils/utils';
import { ContractsService } from './contracts.service';
import { CheckApprovedDto } from './dto/check-approved.dto';
import { ApiBearerAuthMetamaskAndSubstrate } from '../admin/decorators/login.decorator';
import { SetCurrenciesDto } from './dto/set-currencies.dto';
import { RemoveCurrencyDto, SetCurrenciesDto } from './dto/set-currencies.dto';

@ApiTags('Contracts')
@Controller('contracts')
Expand Down Expand Up @@ -34,12 +34,21 @@ export class ContractsController extends BaseController<ContractsService> {
return this.contractsService.getAllAbi();
}

@Post('/currencies')
@Post('/currencies/add')
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary: 'Set available currencies',
summary: 'Add available currency',
})
setCurrencies(@Body() dto: SetCurrenciesDto) {
return this.contractsService.setCurrencies(dto);
addCurrency(@Body() dto: SetCurrenciesDto) {
return this.contractsService.addCurrency(dto);
}

@Post('/currencies/del')
@HttpCode(HttpStatus.OK)
@ApiOperation({
summary: 'Add available currency',
})
delCurrency(@Body() dto: RemoveCurrencyDto) {
return this.contractsService.delCurrency(dto);
}
}
28 changes: 20 additions & 8 deletions packages/market/src/contracts/contracts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CheckApprovedDto } from './dto/check-approved.dto';
import { getContractAbi } from '@app/contracts/scripts';
import { Market } from '@app/contracts/assemblies/3/market';
import { OfferStatus } from '@app/common/modules/types';
import { SetCurrenciesDto } from './dto/set-currencies.dto';
import { RemoveCurrencyDto, SetCurrenciesDto } from './dto/set-currencies.dto';

interface ContractEventValue {
item: Market.OrderStructOutput;
Expand Down Expand Up @@ -115,11 +115,24 @@ export class ContractsService {
}, {});
}

public async setCurrencies(dto: SetCurrenciesDto): Promise<any> {
const { contractAddress, currencies } = dto;
public async delCurrency(dto: RemoveCurrencyDto): Promise<void> {
const { collectionId, contractAddress } = dto;
await this.settingsService.removeContractCurrency(collectionId);
await this.currencyCall(contractAddress, 'removeCurrency', { collectionId });
}
public async addCurrency(dto: SetCurrenciesDto): Promise<void> {
const { currency, contractAddress } = dto;
await this.settingsService.addContractCurrency(currency);

await this.settingsService.setContractCurrencies(currencies);
const { collectionId, fee, decimals } = currency;
await this.currencyCall(contractAddress, 'addCurrency', {
collectionId,
decimals,
fee,
});
}

private async currencyCall(contractAddress: string, method: string, args: any): Promise<any> {
const contractEntity = await this.contractService.findOne({
where: {
address: contractAddress,
Expand All @@ -138,11 +151,10 @@ export class ContractsService {

const callArgs = {
address: this.sdk.options.signer.address,
funcName: 'setCurrencies',
args: {
currencies,
},
funcName: method,
args: args,
};

try {
await contract.call(callArgs);
} catch (err) {
Expand Down
24 changes: 19 additions & 5 deletions packages/market/src/contracts/dto/set-currencies.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsNumber, IsString, ValidateNested } from 'class-validator';
import { IsNumber, IsObject, IsString, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

export class RemoveCurrencyDto {
@ApiProperty()
@IsNumber()
collectionId: number;

@ApiProperty()
@IsString()
contractAddress: string;
}

export class CurrencyDto {
@ApiProperty()
@IsNumber()
Expand All @@ -14,14 +24,18 @@ export class CurrencyDto {
@ApiProperty()
@IsString()
iconUrl: string;

@ApiProperty()
@IsNumber()
fee: number;
}

export class SetCurrenciesDto {
@ApiProperty({ type: CurrencyDto, isArray: true })
@ApiProperty({ type: CurrencyDto })
@Type(() => CurrencyDto)
@IsArray()
@ValidateNested({ each: true })
currencies: CurrencyDto[];
@IsObject()
@ValidateNested()
currency: CurrencyDto;

@ApiProperty()
@IsString()
Expand Down

0 comments on commit 3487453

Please sign in to comment.