From 1f1112731d226b846faa5affcceea04db609aaf4 Mon Sep 17 00:00:00 2001 From: Alec Charbonneau Date: Mon, 20 Nov 2023 15:42:40 -0600 Subject: [PATCH] add exchange endpoints --- src/walletSdk/Anchor/Sep6.ts | 51 ++++++++++++++++++++++++++++++-- src/walletSdk/Types/sep6.ts | 19 ++++++++++++ test/sep6.test.ts | 57 ++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) diff --git a/src/walletSdk/Anchor/Sep6.ts b/src/walletSdk/Anchor/Sep6.ts index 8660476..40dd685 100644 --- a/src/walletSdk/Anchor/Sep6.ts +++ b/src/walletSdk/Anchor/Sep6.ts @@ -10,6 +10,7 @@ import { Sep6WithdrawParams, Sep6DepositResponse, Sep6WithdrawResponse, + Sep6ExchangeParams, } from "../Types"; /** @@ -99,14 +100,60 @@ export class Sep6 { return this.flow({ type: "withdraw", authToken, params }); } + /** + * Similar to the SEP-6 deposit function, but for non-equivalent assets + * that require an exchange. + * + * @param {object} options - The options for the deposit exchange. + * @param {string} options.authToken - The authentication token. + * @param {Sep6ExchangeParams} options.params - The parameters for the deposit request. + * + * @returns {Promise} Sep6 deposit response, containing next steps if needed + * to complete the deposit. + * + * @throws {Error} If an unexpected error occurs during the deposit operation. + */ + async depositExchange({ + authToken, + params, + }: { + authToken: string; + params: Sep6ExchangeParams; + }): Promise { + return this.flow({ type: "deposit-exchange", authToken, params }); + } + + /** + * Similar to the SEP-6 withdraw function, but for non-equivalent assets + * that require an exchange. + * + * @param {object} options - The options for the deposit exchange. + * @param {string} options.authToken - The authentication token. + * @param {Sep6ExchangeParams} options.params - The parameters for the deposit request. + * + * @returns {Promise} Sep6 withdraw response, containing next steps if needed + * to complete the withdrawal. + * + * @throws {Error} If an unexpected error occurs during the deposit operation. + */ + async withdrawExchange({ + authToken, + params, + }: { + authToken: string; + params: Sep6ExchangeParams; + }): Promise { + return this.flow({ type: "withdraw-exchange", authToken, params }); + } + private async flow({ type, authToken, params, }: { - type: "deposit" | "withdraw"; + type: "deposit" | "withdraw" | "deposit-exchange" | "withdraw-exchange"; authToken: string; - params: Sep6DepositParams | Sep6WithdrawParams; + params: Sep6DepositParams | Sep6WithdrawParams | Sep6ExchangeParams; }) { const { transferServer } = await this.anchor.sep1(); diff --git a/src/walletSdk/Types/sep6.ts b/src/walletSdk/Types/sep6.ts index 88deaec..6e8bc02 100644 --- a/src/walletSdk/Types/sep6.ts +++ b/src/walletSdk/Types/sep6.ts @@ -147,3 +147,22 @@ export interface Sep6WithdrawSuccess { fee_percent?: number; extra_info?: { message?: string }; } + +export interface Sep6ExchangeParams { + destination_asset: string; + source_asset: string; + amount: string; + account?: string; + quote_id?: string; + memo_type?: string; + memo?: string; + email_address?: string; + type?: string; + lang?: string; + on_change_callback?: string; + country_code?: string; + claimable_balance_supported?: string; + customer_id?: string; + refund_memo?: string; + refund_memo_type?: string; +} diff --git a/test/sep6.test.ts b/test/sep6.test.ts index 923a8e7..cda0516 100644 --- a/test/sep6.test.ts +++ b/test/sep6.test.ts @@ -93,4 +93,61 @@ describe("SEP-6", () => { }); expect(resp.id).toBeTruthy(); }); + + it("deposit-exchange should work", async () => { + const auth = await anchor.sep10(); + const authToken = await auth.authenticate({ accountKp }); + + const sep12 = await anchor.sep12(authToken); + await sep12.add({ + sep9Info: { + first_name: "john", + last_name: "smith", + email_address: "123@gmail.com", + bank_number: "12345", + bank_account_number: "12345", + }, + }); + + const params = { + destination_asset: + "stellar:SRT:GCDNJUBQSX7AJWLJACMJ7I4BC3Z47BQUTMHEICZLE6MU4KQBRYG5JY6B", + source_asset: "iso4217:USD", + amount: "1", + account: accountKp.publicKey, + type: "bank_account", + }; + + const resp = await sep6.depositExchange({ authToken, params }); + expect(resp.id).toBeTruthy(); + }); + + it("withdraw-exchange should work", async () => { + const auth = await anchor.sep10(); + const authToken = await auth.authenticate({ accountKp }); + + const sep12 = await anchor.sep12(authToken); + await sep12.add({ + sep9Info: { + first_name: "john", + last_name: "smith", + email_address: "123@gmail.com", + bank_number: "12345", + bank_account_number: "12345", + }, + }); + + const params = { + destination_asset: "iso4217:USD", + source_asset: + "stellar:SRT:GCDNJUBQSX7AJWLJACMJ7I4BC3Z47BQUTMHEICZLE6MU4KQBRYG5JY6B", + amount: "1", + dest: accountKp.publicKey, + dest_extra: "1234", + type: "bank_account", + }; + + const resp = await sep6.withdrawExchange({ authToken, params }); + expect(resp.id).toBeTruthy(); + }); });