Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save Trade on creation to the backend #328

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading