Skip to content

Commit

Permalink
save trade to the backend
Browse files Browse the repository at this point in the history
Signed-off-by: MarcoMandar <malicemandar@gmail.com>
  • Loading branch information
MarcoMandar committed Nov 15, 2024
1 parent 0693c93 commit e2a3f62
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/plugin-solana/src/providers/trustScoreProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export class TrustScoreManager {
private baseMint: PublicKey = new PublicKey(settings.BASE_MINT!);
private DECAY_RATE = 0.95;
private MAX_DECAY_DAYS = 30;
private backend = settings.BACKEND_URL; // TODO add to .env
private backendToken = settings.BACKEND_TOKEN; // TODO add to .env
constructor(
tokenProvider: TokenProvider,
trustScoreDb: TrustScoreDatabase
Expand Down Expand Up @@ -378,9 +380,56 @@ export class TrustScoreManager {
rapidDump: false,
};
this.trustScoreDb.addTradePerformance(creationData, data.is_simulation);
// api call to update trade performance
this.createTradeInBe(tokenAddress, recommenderId, data);
return creationData;
}

async delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

async createTradeInBe(
tokenAddress: string,
recommenderId: string,
data: TradeData,
retries = 3,
delayMs = 2000
) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
await fetch(
`${this.backend}/api/updaters/createTradePerformance`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.backendToken}`,
},
body: JSON.stringify({
tokenAddress: tokenAddress,
tradeData: data,
recommenderId: recommenderId,
}),
}
);
// If the request is successful, exit the loop
return;
} catch (error) {
console.error(
`Attempt ${attempt} failed: Error creating trade in backend`,
error
);
if (attempt < retries) {
console.log(`Retrying in ${delayMs} ms...`);
await this.delay(delayMs); // Wait for the specified delay before retrying
} else {
console.error("All attempts failed.");
}
}
}
}

/**
* Updates a trade with sell details.
* @param tokenAddress The address of the token.
Expand Down

0 comments on commit e2a3f62

Please sign in to comment.