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

add SEP-6 exchange endpoints #78

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
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
51 changes: 49 additions & 2 deletions src/walletSdk/Anchor/Sep6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Sep6WithdrawParams,
Sep6DepositResponse,
Sep6WithdrawResponse,
Sep6ExchangeParams,
} from "../Types";

/**
Expand Down Expand Up @@ -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<Sep6DepositResponse>} 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<Sep6DepositResponse> {
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<Sep6WithdrawResponse>} 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<Sep6WithdrawResponse> {
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();

Expand Down
19 changes: 19 additions & 0 deletions src/walletSdk/Types/sep6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
57 changes: 57 additions & 0 deletions test/sep6.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
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: "[email protected]",
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();
});
});
Loading