Skip to content

Commit

Permalink
added state to orders
Browse files Browse the repository at this point in the history
  • Loading branch information
santiagoziel committed Dec 11, 2023
1 parent 9df98c1 commit d7b80ec
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 8 deletions.
5 changes: 3 additions & 2 deletions backend/src/service-account/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AssetManagementService } from "../service-asset-management"
import { SuportedWallet } from "../service-asset-store/models"
import { AssetStoreService } from "../service-asset-store/service"
import { BlockchainService } from "../service-blockchain/service-spec"
import { CollectionFilter, CollectionService } from "../service-collection"
Expand Down Expand Up @@ -323,8 +324,8 @@ export class AccountServiceDsl implements AccountService {
return {status: "ok"}
}

async initAOTSellContract(userId: string, buyerAddress: string, quantity: number):Promise<InitAotSellResult>{
const buyerAdaDepositTX = await this.aotStoreService.initAOTContract(userId,buyerAddress, quantity)
async initAOTSellContract(userId: string, browserWallet: SuportedWallet, buyerAddress: string, quantity: number):Promise<InitAotSellResult>{
const buyerAdaDepositTX = await this.aotStoreService.initAOTContract(userId, browserWallet, buyerAddress, quantity)
if (buyerAdaDepositTX.ctype !== "success") return {status: "invalid", reason: buyerAdaDepositTX.error}
return {status: "ok", contractId: buyerAdaDepositTX.contractId, depositTx: buyerAdaDepositTX.depositTx, orderId: buyerAdaDepositTX.orderId}
}
Expand Down
5 changes: 5 additions & 0 deletions backend/src/service-asset-store/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ export type CompensatingAction =
{command: "release assets", assets: Token[]} |
{command: "retrive assets", assets: Token[], contractId: string}

//CHECKME: this type really should not live here hahaha
export type SuportedWallet = "Nami" | "Eternl"

export type TextEnvelope = {type: string, description: string, cborHex: string }

export type OrderState = 'created' | 'transaction_confirmed' | 'order_completed'

export const ADA: Token = {
"currency_symbol": "",
"token_name": ""
Expand Down
21 changes: 20 additions & 1 deletion backend/src/service-asset-store/orders/aot-order-db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataTypes, Model, Sequelize } from "sequelize"
import { Token } from "../models"
import { OrderState, SuportedWallet, Token } from "../models"

export const ordersTableName = "asset_store_aot_orders"

Expand All @@ -9,6 +9,7 @@ export type CreateAOTOrder = {
adaDepositTxId: string
assets: Token[]
contractId: string
browserWallet: string
}

export class AOTStoreOrder extends Model implements CreateAOTOrder{
Expand All @@ -18,8 +19,13 @@ export class AOTStoreOrder extends Model implements CreateAOTOrder{
declare adaDepositTxId: string
declare assets: Token[]
declare contractId: string
declare browserWallet: SuportedWallet
declare orderState: OrderState
}

type OrderStateArray = Array<OrderState>
const orderStates: OrderStateArray = ["created", "transaction_confirmed","order_completed"]

export const aotStoreOrderTableAttributes = {
orderId: {
type: DataTypes.UUID,
Expand All @@ -46,6 +52,19 @@ export const aotStoreOrderTableAttributes = {
type: DataTypes.STRING,
allowNull: false
}
,
browserWallet: {
type: DataTypes.STRING,
allowNull: false
},
orderState: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: "created",
validate: {
isIn: [orderStates]
}
}
}

export const configureSequelizeModel = (sequelize: Sequelize): void => {
Expand Down
25 changes: 24 additions & 1 deletion backend/src/service-asset-store/orders/aot-order-dsl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import { OrderState } from "../models";
import { AOTStoreOrder, CreateAOTOrder } from "./aot-order-db";


export class AotOrdersDSL {
static async create (info: CreateAOTOrder){ return (await AOTStoreOrder.create(info)).orderId}

static async get(orderId: string){return AOTStoreOrder.findByPk(orderId)}
//this migth be dumb
static async get(orderId: string){
const order = await AOTStoreOrder.findByPk(orderId)
if(!order) return null
return {
orderId: order.orderId,
userId: order.userId,
buyerAddress: order.buyerAddress,
adaDepositTxId: order.adaDepositTxId,
assets: order.assets,
contractId: order.contractId,
browserWallet: order.browserWallet,
orderState: order.orderState,
}
}

static async updateOrder(orderId: string, state: OrderState){
const order = await AOTStoreOrder.findByPk(orderId)
if(!order) return null
order.orderState = state
await order.save()
}
}
4 changes: 3 additions & 1 deletion backend/src/service-asset-store/service-spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { LoggingContext } from "../tools-tracing"
import { SResult, Unit } from "../tools-utils"
import { SuportedWallet } from "./models"

export interface AssetStoreDSL {

loadDatabaseModels(): Promise<void>

unloadDatabaseModels(): Promise<void>

initAOTContract(userId: string, buyerAddres: string, quantity: number, logger?: LoggingContext): Promise<SResult<{contractId: string}>>
initAOTContract(userId: string, browserWallet: SuportedWallet, buyerAddres: string, quantity: number, logger?: LoggingContext): Promise<SResult<{contractId: string}>>


}
8 changes: 5 additions & 3 deletions backend/src/service-asset-store/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AotOrdersDSL } from "./orders/aot-order-dsl";
import { AOTInventory } from "./inventory/aot-invenotry-dsl";
import { MarloweDSl } from "./marlowe/marlowe-dsl";
import { CreateContractResponse } from "./marlowe/models";
import { ADA, CompensatingAction, Token } from "./models";
import { ADA, CompensatingAction, SuportedWallet, Token } from "./models";
import { AssetStoreDSL } from "./service-spec";
import dotenv from "dotenv"
import { config } from "../tools-utils"
Expand Down Expand Up @@ -45,7 +45,7 @@ export class AssetStoreService implements AssetStoreDSL {
}

//FIXME: I should propably split this into a funciton to initializes the contract and another one to generate the buyer TX
async initAOTContract(userId: string, buyerAddress: string, quantity: number, logger?: LoggingContext): Promise<SResult<{ contractId: string, depositTx: string, orderId: string }>> {
async initAOTContract(userId: string, browserWallet: SuportedWallet, buyerAddress: string, quantity: number, logger?: LoggingContext): Promise<SResult<{ contractId: string, depositTx: string, orderId: string }>> {
const compensatingActions: CompensatingAction[] = []
try {
const reservedItems = await this.aotInventory.reserveAssets(quantity)
Expand All @@ -61,7 +61,7 @@ export class AssetStoreService implements AssetStoreDSL {
const adaQuantity = (quantity * this.AOTPrice).toString()
const buyerAdaDepositTX = await this.marloweDSL.genDepositIntoContractTX(contractId, buyerAddress, [{asset: ADA, quantity: adaQuantity}])
if (buyerAdaDepositTX.ctype !== "success") throw new Error("Failed to geenrate ADA deposit transaction")
const orderId = await AotOrdersDSL.create({buyerAddress, userId, adaDepositTxId: buyerAdaDepositTX.transactionId, assets: reservedItems, contractId})
const orderId = await AotOrdersDSL.create({buyerAddress, userId, browserWallet, adaDepositTxId: buyerAdaDepositTX.transactionId, assets: reservedItems, contractId})
return success({ contractId: contractId, depositTx: buyerAdaDepositTX.textEnvelope.cborHex, orderId })
} catch (error: any) {
await this.rollbackSaga(compensatingActions)
Expand All @@ -76,6 +76,7 @@ export class AssetStoreService implements AssetStoreDSL {
if(!order) throw new Error("Could not find order by id")
//TODO: check the client types
const submitResult = await this.marloweDSL.submitContractInteraciton(signedTx, contractId, order.adaDepositTxId)
await AotOrdersDSL.updateOrder(order.orderId, "transaction_confirmed")
} catch (error: any){
console.error(`error on init AOT contract ${error.message}`)
return sfailure(error.message)
Expand All @@ -92,6 +93,7 @@ export class AssetStoreService implements AssetStoreDSL {
const signedTxResult = await this.marloweDSL.signContractInteraction(sellerAOTDepositTX.textEnvelope.cborHex)
if (signedTxResult.ctype !== "success") throw new Error("Failed to sign AOT deposit transaction")
const submitResult = await this.marloweDSL.submitContractInteraciton(signedTxResult.signedTx, contractId, order.adaDepositTxId)
await AotOrdersDSL.updateOrder(order.orderId, "order_completed")
}
catch(error: any){
console.error(`error on init AOT contract ${error.message}`)
Expand Down

0 comments on commit d7b80ec

Please sign in to comment.