Skip to content

Commit

Permalink
add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
acharb committed Oct 31, 2023
1 parent 5b1b10f commit c4dc48a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/walletSdk/Anchor/Sep6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,35 @@ type Sep6Params = {
export class Sep6 {
private anchor: Anchor;
private httpClient: AxiosInstance;
private anchorInfo: Sep6Info;

/**
* Creates a new instance of the Sep6 class.
* @constructor
* @param {Sep6Params} params - Parameters to initialize the Sep6 instance.
*/
constructor(params: Sep6Params) {
const { anchor, httpClient } = params;

this.anchor = anchor;
this.httpClient = httpClient;
}

async info(): Promise<Sep6Info> {
/**
* Get SEP-6 anchor information.
* If `shouldRefresh` is set to `true`, it fetches fresh values; otherwise, it returns cached values if available.
* @param {boolean} [shouldRefresh=false] - Flag to force a refresh of TOML values.
* @returns {Promise<Sep6Info>} - SEP-6 information about the anchor.
*/
async info(shouldRefresh?: boolean): Promise<Sep6Info> {
if (this.anchorInfo && !shouldRefresh) {
return this.anchorInfo;
}

const { transferServer } = await this.anchor.sep1();
try {
const resp = await this.httpClient.get(`${transferServer}/info`);
this.anchorInfo = resp.data;
return resp.data;
} catch (e) {
throw new ServerRequestFailedError(e);
Expand Down
4 changes: 4 additions & 0 deletions test/sep6.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ describe("SEP-6", () => {
const resp = await sep6.info();
expect(resp.deposit).toBeTruthy();
expect(resp.withdraw).toBeTruthy();

const refreshed = await sep6.info(true);
expect(refreshed.deposit).toBeTruthy();
expect(refreshed.withdraw).toBeTruthy();
});
});

0 comments on commit c4dc48a

Please sign in to comment.