@@ -14,6 +14,8 @@ import type { LogRecord } from "@opentelemetry/api-logs";
1414import type { Exchange } from "@usherlabs/ccxt" ;
1515import { MAX_ARCHIVE_BODY_BYTES } from "../services/archive-forwarder/limits" ;
1616import type { ExecuteActionContext } from "../src/handlers/execute-action/context" ;
17+ import { handleOrders } from "../src/handlers/execute-action/orders" ;
18+ import { handleTreasuryCall } from "../src/handlers/execute-action/treasury-call" ;
1719import { handleWithdraw } from "../src/handlers/execute-action/withdraw" ;
1820import {
1921 redactSecretLiterals ,
@@ -44,9 +46,11 @@ import {
4446 resolveArchiveForwarderUrlFromEnv ,
4547 rethrowArchiveDurabilityError ,
4648} from "../src/helpers/broker-execution-archive/writer" ;
49+ import { Action } from "../src/helpers/constants" ;
4750import { log } from "../src/helpers/logger" ;
4851import { buildOrderExecutionTelemetry } from "../src/helpers/order-telemetry" ;
4952import type { OtelLogs } from "../src/helpers/otel" ;
53+ import type { PolicyConfig } from "../src/types" ;
5054import { startForwarderServer } from "./archive-forwarder-server" ;
5155
5256const archiveTestDirectory = mkdtempSync (
@@ -286,6 +290,7 @@ describe("broker execution archive rows", () => {
286290 cex : "binance" ,
287291 accountLabel : "primary" ,
288292 symbol : "ARB/USDT" ,
293+ orderAuthor : "maker-alpha" ,
289294 clientOrderId : "client-1" ,
290295 makerActionId : "maker-1" ,
291296 } ,
@@ -311,6 +316,7 @@ describe("broker execution archive rows", () => {
311316 action : "CancelOrder" ,
312317 event_kind : "execute_action" ,
313318 order_id : "99" ,
319+ order_author : "maker-alpha" ,
314320 client_order_id : "client-1" ,
315321 maker_action_id : "maker-1" ,
316322 } ) ;
@@ -368,6 +374,7 @@ describe("broker execution archive rows", () => {
368374 ] ) {
369375 expect ( orderRow . row ) . not . toHaveProperty ( key ) ;
370376 }
377+ expect ( orderRow . row . order_author ) . toBe ( "" ) ;
371378
372379 const snapshotRow = buildMarketMetadataSnapshotRow ( {
373380 tags : buildCommonArchiveTags ( {
@@ -661,6 +668,117 @@ describe("broker execution archive rows", () => {
661668 } ) ;
662669} ) ;
663670
671+ describe ( "order author archive plumbing" , ( ) => {
672+ test ( "archives authors from typed and Call createOrder without forwarding them to the venue" , async ( ) => {
673+ const forwarder = await startForwarderServer ( ) ;
674+ const archiver = BrokerExecutionArchiver . create ( {
675+ forwarderUrl : forwarder . url ,
676+ deadLetterPath : createDeadLetterPath ( ) ,
677+ deploymentId : "test-deploy" ,
678+ batchSize : 100 ,
679+ flushIntervalMs : 60_000 ,
680+ } ) ;
681+ const createOrderCalls : unknown [ ] [ ] = [ ] ;
682+ const broker = {
683+ loadMarkets : async ( ) => { } ,
684+ markets : {
685+ "USDC/USDT" : {
686+ symbol : "USDC/USDT" ,
687+ base : "USDC" ,
688+ quote : "USDT" ,
689+ spot : true ,
690+ type : "spot" ,
691+ } ,
692+ } ,
693+ createOrder : async ( ...args : unknown [ ] ) => {
694+ createOrderCalls . push ( args ) ;
695+ return {
696+ id : `order-${ createOrderCalls . length } ` ,
697+ symbol : args [ 0 ] ,
698+ type : args [ 1 ] ,
699+ side : args [ 2 ] ,
700+ amount : args [ 3 ] ,
701+ status : "open" ,
702+ filled : 0 ,
703+ } ;
704+ } ,
705+ } as unknown as Exchange ;
706+ const policy = {
707+ order : { rule : { markets : [ "*" ] , limits : [ ] } } ,
708+ } as unknown as PolicyConfig ;
709+ const context = (
710+ action : ( typeof Action ) [ keyof typeof Action ] ,
711+ payload : Record < string , unknown > ,
712+ ) =>
713+ ( {
714+ action,
715+ call : { request : { payload } } ,
716+ wrappedCallback : ( ) => { } ,
717+ policy,
718+ brokers : { } ,
719+ normalizedCex : "binance" ,
720+ cex : "binance" ,
721+ symbol : "USDC/USDT" ,
722+ selectedBrokerAccount : { exchange : broker , label : "primary" } ,
723+ broker,
724+ verity : { proof : "" } ,
725+ brokerArchiver : archiver ,
726+ } ) as unknown as ExecuteActionContext ;
727+ const typedPayload = ( orderAuthor ?: string ) => ( {
728+ orderType : "limit" ,
729+ amount : "10" ,
730+ fromToken : "USDC" ,
731+ toToken : "USDT" ,
732+ price : "1" ,
733+ marketType : "spot" ,
734+ ...( orderAuthor !== undefined && { orderAuthor } ) ,
735+ params : JSON . stringify ( { timeInForce : "GTC" } ) ,
736+ } ) ;
737+
738+ try {
739+ await handleOrders (
740+ context ( Action . CreateOrder , typedPayload ( "maker-alpha" ) ) ,
741+ ) ;
742+ await handleTreasuryCall (
743+ context ( Action . Call , {
744+ functionName : "createOrder" ,
745+ args : JSON . stringify ( [ "USDC/USDT" , "limit" , "buy" , 5 , 1 ] ) ,
746+ orderAuthor : "funding-executor" ,
747+ params : JSON . stringify ( { postOnly : true } ) ,
748+ } ) ,
749+ ) ;
750+ await handleOrders ( context ( Action . CreateOrder , typedPayload ( ) ) ) ;
751+
752+ expect ( createOrderCalls [ 0 ] ?. [ 5 ] ) . toEqual ( { timeInForce : "GTC" } ) ;
753+ expect ( createOrderCalls [ 1 ] ?. [ 5 ] ) . toEqual ( { postOnly : true } ) ;
754+ expect ( createOrderCalls [ 2 ] ?. [ 5 ] ) . toEqual ( { timeInForce : "GTC" } ) ;
755+ for ( const call of createOrderCalls ) {
756+ expect ( call [ 5 ] ) . not . toHaveProperty ( "orderAuthor" ) ;
757+ }
758+
759+ await Promise . resolve ( ) ;
760+ await archiver . flush ( ) ;
761+ const orderRows = forwarder . requests
762+ . flatMap ( ( request ) => request . body . rows ?? [ ] )
763+ . filter (
764+ ( entry ) => entry . table === "broker_execution.order_events" ,
765+ ) as Array < { row : Record < string , unknown > } > ;
766+ const orderAuthors = Object . fromEntries (
767+ orderRows . map ( ( { row } ) => [ row . order_id , row . order_author ] ) ,
768+ ) ;
769+
770+ expect ( orderAuthors ) . toEqual ( {
771+ "order-1" : "maker-alpha" ,
772+ "order-2" : "funding-executor" ,
773+ "order-3" : "" ,
774+ } ) ;
775+ } finally {
776+ await archiver . close ( ) ;
777+ await forwarder . close ( ) ;
778+ }
779+ } ) ;
780+ } ) ;
781+
664782describe ( "withdrawal observation tracker" , ( ) => {
665783 function shouldArchive (
666784 tracker : WithdrawalObservationTracker ,
0 commit comments