Skip to content

Commit

Permalink
Create tangle responses
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
Boldizsar Mezei committed Dec 10, 2023
1 parent a0453d5 commit 8f0c622
Show file tree
Hide file tree
Showing 38 changed files with 196 additions and 174 deletions.
18 changes: 12 additions & 6 deletions packages/client/src/otr/otr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Dataset, Network, Transaction, TransactionType } from '@build-5/interfaces';
import {
Dataset,
Network,
TangleResponse,
Transaction,
TransactionType,
} from '@build-5/interfaces';
import { utf8ToHex } from '@iota/sdk';
import { Observable as RxjsObservable, Subscriber, Subscription } from 'rxjs';
import { API_KEY, Build5 } from '..';
Expand Down Expand Up @@ -50,8 +56,8 @@ export class OtrWrapper {
};
}

class Observable extends RxjsObservable<string> {
private observer: Subscriber<string> | undefined;
class Observable extends RxjsObservable<TangleResponse> {
private observer: Subscriber<TangleResponse> | undefined;
private paymentsSubs: Subscription;
private payments: string[] = [];
private dataset: TransactionDataset<Dataset.TRANSACTION>;
Expand All @@ -63,7 +69,7 @@ class Observable extends RxjsObservable<string> {
});
this.dataset = https(origin).project(API_KEY[origin]).dataset(Dataset.TRANSACTION);

this.observer?.next('Waiting for payment');
this.observer?.next({ status: 'waiting for payment' });

this.paymentsSubs = this.dataset
.getPaymentByTagLive(tag.startsWith('0x') ? tag : utf8ToHex(tag))
Expand All @@ -85,12 +91,12 @@ class Observable extends RxjsObservable<string> {
const result = await this.dataset.getBySourceTransaction(payment.uid);
const credit = result.find((t) => t.type === TransactionType.CREDIT_TANGLE_REQUEST);
if (credit) {
this.observer?.next(JSON.stringify(credit.payload.response));
this.observer?.next(credit.payload.response);
return;
}
const transfer = result.find((t) => t.type === TransactionType.UNLOCK);
if (transfer) {
this.observer?.next('Success');
this.observer?.next({ status: 'success' });
return;
}
await new Promise((resolve) => setTimeout(resolve, 1000));
Expand Down
4 changes: 4 additions & 0 deletions packages/functions/src/services/payment/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ export abstract class BaseService {

abstract handleRequest(params: HandlerParams): Promise<unknown>;
}

export abstract class BaseTangleService<T> extends BaseService {
abstract handleRequest(params: HandlerParams): Promise<T>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import {
Network,
NetworkAddress,
TangleRequestType,
TangleResponse,
Transaction,
WenError,
} from '@build-5/interfaces';
import { get } from 'lodash';
import { get, isEmpty } from 'lodash';
import { getOutputMetadata } from '../../../utils/basic-output.utils';
import { invalidArgument } from '../../../utils/error.utils';
import { getRandomNonce } from '../../../utils/wallet.utils';
import { BaseService, HandlerParams } from '../base';
import { BaseTangleService, HandlerParams } from '../base';
import { TangleAddressValidationService } from './address/address-validation.service';
import { TangleAuctionBidService, TangleNftAuctionBidService } from './auction/auction.bid.service';
import { TangleAuctionCreateService } from './auction/auction.create.service';
Expand All @@ -38,7 +39,7 @@ import { TangleStakeService } from './token/stake.service';
import { TangleTokenClaimService } from './token/token-claim.service';
import { TangleTokenTradeService } from './token/token-trade.service';

export class TangleRequestService extends BaseService {
export class TangleRequestService extends BaseTangleService<TangleResponse> {
public handleRequest = async (params: HandlerParams) => {
const { match, order, tranEntry } = params;
let owner = match.from;
Expand All @@ -53,7 +54,7 @@ export class TangleRequestService extends BaseService {
const service = this.getService(serviceParams);
const response = await service.handleRequest(serviceParams);

if (response) {
if (!isEmpty(response)) {
this.transactionService.createTangleCredit(
payment,
match,
Expand All @@ -77,9 +78,11 @@ export class TangleRequestService extends BaseService {
tranEntry.outputId!,
);
}

return {};
};

private getService = (params: HandlerParams) => {
private getService = (params: HandlerParams): BaseTangleService<TangleResponse> => {
if (params.tranEntry.nftOutput) {
return new NftDepositService(this.transactionService);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { build5Db } from '@build-5/database';
import {
BaseTangleResponse,
COL,
Network,
Space,
TRANSACTION_AUTO_EXPIRY_MS,
TangleResponse,
Transaction,
TransactionPayloadType,
TransactionType,
Expand All @@ -20,10 +20,10 @@ import { assertValidationAsync } from '../../../../utils/schema.utils';
import { assertIsGuardian } from '../../../../utils/token.utils';
import { getRandomEthAddress } from '../../../../utils/wallet.utils';
import { WalletService } from '../../../wallet/wallet.service';
import { BaseService, HandlerParams } from '../../base';
import { BaseTangleService, HandlerParams } from '../../base';
import { validateAddressSchemaObject } from './AddressValidationTangleRequestSchema';

export class TangleAddressValidationService extends BaseService {
export class TangleAddressValidationService extends BaseTangleService<TangleResponse> {
public handleRequest = async ({
project,
tran,
Expand All @@ -32,7 +32,7 @@ export class TangleAddressValidationService extends BaseService {
owner,
request,
payment,
}: HandlerParams): Promise<BaseTangleResponse | undefined> => {
}: HandlerParams) => {
const params = await assertValidationAsync(validateAddressSchemaObject, request);
const order = await createAddressValidationOrder(
project,
Expand All @@ -56,7 +56,8 @@ export class TangleAddressValidationService extends BaseService {
TransactionPayloadType.TANGLE_TRANSFER,
tranEntry.outputId,
);
return;

return {};
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { build5Db } from '@build-5/database';
import { COL, Nft, TransactionPayloadType, WenError } from '@build-5/interfaces';
import { COL, Nft, TangleResponse, TransactionPayloadType, WenError } from '@build-5/interfaces';
import { invalidArgument } from '../../../../utils/error.utils';
import { assertValidationAsync } from '../../../../utils/schema.utils';
import { HandlerParams } from '../../base';
import { TransactionService } from '../../transaction-service';
import { BaseTangleService, HandlerParams } from '../../base';
import { auctionBidTangleSchema } from './AuctionBidTangleRequestSchema';
import { nftBidSchema } from './NftBidTangleRequestSchema';
import { createBidOrder } from './auction.bid.order';

export class TangleNftAuctionBidService {
constructor(readonly transactionService: TransactionService) {}

export class TangleNftAuctionBidService extends BaseTangleService<TangleResponse> {
public handleRequest = async ({
request,
project,
Expand Down Expand Up @@ -48,13 +45,12 @@ export class TangleNftAuctionBidService {
TransactionPayloadType.TANGLE_TRANSFER,
tranEntry.outputId,
);
return;

return {};
};
}

export class TangleAuctionBidService {
constructor(readonly transactionService: TransactionService) {}

export class TangleAuctionBidService extends BaseTangleService<TangleResponse> {
public handleRequest = async ({
request,
project,
Expand Down Expand Up @@ -87,6 +83,6 @@ export class TangleAuctionBidService {
TransactionPayloadType.TANGLE_TRANSFER,
tranEntry.outputId,
);
return;
return {};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Auction,
AuctionCreateRequest,
AuctionCreateTangleRequest,
AuctionCreateTangleResponse,
AuctionType,
COL,
Member,
Expand All @@ -13,14 +14,15 @@ import { assertMemberHasValidAddress, getAddress } from '../../../../utils/addre
import { dateToTimestamp } from '../../../../utils/dateTime.utils';
import { assertValidationAsync } from '../../../../utils/schema.utils';
import { getRandomEthAddress } from '../../../../utils/wallet.utils';
import { HandlerParams } from '../../base';
import { TransactionService } from '../../transaction-service';
import { BaseTangleService, HandlerParams } from '../../base';
import { auctionCreateTangleSchema } from './AuctionCreateTangleRequestSchema';

export class TangleAuctionCreateService {
constructor(readonly transactionService: TransactionService) {}

public handleRequest = async ({ request, project, owner }: HandlerParams) => {
export class TangleAuctionCreateService extends BaseTangleService<AuctionCreateTangleResponse> {
handleRequest = async ({
request,
owner,
project,
}: HandlerParams): Promise<AuctionCreateTangleResponse> => {
const params = await assertValidationAsync(auctionCreateTangleSchema, request);

const memberDocRef = build5Db().doc(`${COL.MEMBER}/${owner}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import { invalidArgument } from '../../../../utils/error.utils';
import { assertValidationAsync } from '../../../../utils/schema.utils';
import { assertIsGuardian } from '../../../../utils/token.utils';
import { getRandomEthAddress } from '../../../../utils/wallet.utils';
import { BaseService, HandlerParams } from '../../base';
import { BaseTangleService, HandlerParams } from '../../base';
import { approveAwardParticipantSchemaObject } from './AwardAppParticipantTangleRequestSchema';

export class AwardApproveParticipantService extends BaseService {
export class AwardApproveParticipantService extends BaseTangleService<AwardApproveParticipantTangleResponse> {
public handleRequest = async ({
project,
owner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import { getRandomEthAddress } from '../../../../utils/wallet.utils';
import { isStorageUrl } from '../../../joi/common';
import { WalletService } from '../../../wallet/wallet.service';
import { getAwardgStorageDeposits as getAwardStorageDeposits } from '../../award/award-service';
import { BaseService, HandlerParams } from '../../base';
import { BaseTangleService, HandlerParams } from '../../base';
import { awardCreateSchema } from './AwardCreateTangleRequestSchema';
import { createAwardFundOrder } from './award.fund.service';

export class AwardCreateService extends BaseService {
export class AwardCreateService extends BaseTangleService<AwardCreateTangleResponse> {
public handleRequest = async ({
project,
owner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { build5Db } from '@build-5/database';
import {
Award,
AwardBadgeType,
BaseTangleResponse,
COL,
TRANSACTION_AUTO_EXPIRY_MS,
TangleResponse,
Transaction,
TransactionPayloadType,
TransactionType,
Expand All @@ -19,15 +19,11 @@ import { assertValidationAsync } from '../../../../utils/schema.utils';
import { assertIsGuardian } from '../../../../utils/token.utils';
import { getRandomEthAddress } from '../../../../utils/wallet.utils';
import { WalletService } from '../../../wallet/wallet.service';
import { BaseService, HandlerParams } from '../../base';
import { BaseTangleService, HandlerParams } from '../../base';
import { awardFundSchema } from './AwardFundTangleRequestSchema';

export class AwardFundService extends BaseService {
public handleRequest = async ({
project,
owner,
request,
}: HandlerParams): Promise<BaseTangleResponse> => {
export class AwardFundService extends BaseTangleService<TangleResponse> {
public handleRequest = async ({ project, owner, request }: HandlerParams) => {
const params = await assertValidationAsync(awardFundSchema, request);

const award = await getAwardForFunding(owner, params.uid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Space,
SpaceGuardian,
TRANSACTION_AUTO_EXPIRY_MS,
TangleResponse,
Transaction,
TransactionPayloadType,
TransactionType,
Expand Down Expand Up @@ -35,10 +36,10 @@ import {
import { getRandomEthAddress } from '../../../../utils/wallet.utils';
import { Wallet } from '../../../wallet/wallet';
import { WalletService } from '../../../wallet/wallet.service';
import { BaseService, HandlerParams } from '../../base';
import { BaseTangleService, HandlerParams } from '../../base';
import { metadataNftSchema } from './MetadataNftTangleRequestSchema';

export class MintMetadataNftService extends BaseService {
export class MintMetadataNftService extends BaseTangleService<TangleResponse> {
public handleRequest = async ({
project,
owner,
Expand Down Expand Up @@ -126,7 +127,8 @@ export class MintMetadataNftService extends BaseService {
TransactionPayloadType.TANGLE_TRANSFER,
tranEntry.outputId,
);
return;

return {};
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { build5Db } from '@build-5/database';
import {
COL,
TRANSACTION_AUTO_EXPIRY_MS,
TangleResponse,
Transaction,
TransactionPayloadType,
TransactionType,
Expand All @@ -12,9 +13,9 @@ import { getProject } from '../../../../utils/common.utils';
import { dateToTimestamp } from '../../../../utils/dateTime.utils';
import { getRandomEthAddress } from '../../../../utils/wallet.utils';
import { WalletService } from '../../../wallet/wallet.service';
import { BaseService, HandlerParams } from '../../base';
import { BaseTangleService, HandlerParams } from '../../base';

export class NftDepositService extends BaseService {
export class NftDepositService extends BaseTangleService<TangleResponse> {
public handleRequest = async ({ owner, tran, tranEntry, ...params }: HandlerParams) => {
const wallet = await WalletService.newWallet(params.order.network);
const targetAddress = await wallet.getNewIotaAddressDetails();
Expand Down Expand Up @@ -46,6 +47,7 @@ export class NftDepositService extends BaseService {
TransactionPayloadType.UNLOCK_NFT,
tranEntry.outputId,
);
return;

return {};
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { build5Db } from '@build-5/database';
import {
BaseTangleResponse,
COL,
Collection,
CollectionStatus,
Expand All @@ -17,6 +16,7 @@ import {
Space,
StakeType,
TRANSACTION_AUTO_EXPIRY_MS,
TangleResponse,
Transaction,
TransactionPayloadType,
TransactionType,
Expand All @@ -37,18 +37,18 @@ import { getSpace } from '../../../../utils/space.utils';
import { getRandomEthAddress } from '../../../../utils/wallet.utils';
import { assertHasAccess } from '../../../validators/access';
import { WalletService } from '../../../wallet/wallet.service';
import { BaseService, HandlerParams } from '../../base';
import { BaseTangleService, HandlerParams } from '../../base';
import { nftPurchaseSchema } from './NftPurchaseTangleRequestSchema';

export class TangleNftPurchaseService extends BaseService {
export class TangleNftPurchaseService extends BaseTangleService<TangleResponse> {
public handleRequest = async ({
order: tangleOrder,
request,
owner,
tran,
tranEntry,
payment,
}: HandlerParams): Promise<BaseTangleResponse | undefined> => {
}: HandlerParams) => {
const params = await assertValidationAsync(nftPurchaseSchema, request);

const order = await createNftPuchaseOrder(
Expand Down Expand Up @@ -86,7 +86,8 @@ export class TangleNftPurchaseService extends BaseService {
TransactionPayloadType.TANGLE_TRANSFER,
tranEntry.outputId,
);
return;

return {};
};
}

Expand Down
Loading

0 comments on commit 8f0c622

Please sign in to comment.