Skip to content

Commit

Permalink
Merge pull request #328 from MarcoMandar/main
Browse files Browse the repository at this point in the history
Save Trade on creation to the backend
  • Loading branch information
lalalune authored Nov 15, 2024
2 parents 4fdd584 + e2a3f62 commit 0d1b1fb
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 0d1b1fb

Please sign in to comment.