-
Notifications
You must be signed in to change notification settings - Fork 7
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 SEP6 deposit and withdrawal #77
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
import { AxiosInstance } from "axios"; | ||
import queryString from "query-string"; | ||
|
||
import { Anchor } from "../Anchor"; | ||
import { ServerRequestFailedError } from "../Exceptions"; | ||
import { Sep6Info } from "../Types"; | ||
|
||
type Sep6Params = { | ||
anchor: Anchor; | ||
httpClient: AxiosInstance; | ||
}; | ||
import { | ||
ServerRequestFailedError, | ||
Sep12MissingInfoError, | ||
Sep6DepositDeniedError, | ||
} from "../Exceptions"; | ||
import { | ||
Sep6Info, | ||
Sep6Params, | ||
Sep6DepositParams, | ||
Sep6WithdrawParams, | ||
Sep6DepositResponse, | ||
Sep6WithdrawResponse, | ||
} from "../Types"; | ||
|
||
/** | ||
* Flow for creating deposits and withdrawals with an anchor using SEP-6. | ||
|
@@ -53,4 +60,79 @@ export class Sep6 { | |
throw new ServerRequestFailedError(e); | ||
} | ||
} | ||
|
||
/** | ||
* Deposits funds using the SEP-6 protocol. Next steps by | ||
* the anchor are given in the response. | ||
* | ||
* @param {object} options - The options for the deposit. | ||
* @param {string} options.authToken - The authentication token. | ||
* @param {Sep6DepositParams} options.params - The parameters for the deposit request. | ||
* | ||
* @returns {Promise<Sep6DepositResponse>} Sep6 deposit response, containing next steps if needed | ||
* to complete the deposit. | ||
* | ||
* @throws {Sep12MissingInfoError} If Sep-12 KYC info is missing for the user. | ||
* @throws {Sep6DepositDeniedError} If the deposit is still being processed or not accepted. | ||
* @throws {Error} If an unexpected error occurs during the deposit operation. | ||
*/ | ||
async deposit({ | ||
authToken, | ||
params, | ||
}: { | ||
authToken: string; | ||
params: Sep6DepositParams; | ||
}): Promise<Sep6DepositResponse> { | ||
const { transferServer } = await this.anchor.sep1(); | ||
|
||
try { | ||
const resp = await this.httpClient.get( | ||
`${transferServer}/deposit?${queryString.stringify(params)}`, | ||
{ | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${authToken}`, | ||
}, | ||
}, | ||
); | ||
return resp.data; | ||
} catch (e) { | ||
if (e.response?.data?.type === "non_interactive_customer_info_needed") { | ||
throw new Sep12MissingInfoError(e.response?.data?.fields); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's an error. It's expected behavior in the flow. I think we should have different types of the Sep6Response which wallets must handle accordingly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW you can find flow diagrams for SEP-6 here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, how about this? fb25d34 also I realized those resposnes can be for deposit or withdrawal so i added a helper flow function similar to sep24 |
||
} else if (e.response?.data?.type === "customer_info_status") { | ||
throw new Sep6DepositDeniedError(e.response?.data); | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
/** | ||
* Initiates a withdrawal using SEP-6. | ||
* | ||
* @param {object} options - The options for the withdrawal operation. | ||
* @param {string} options.authToken - The authentication token. | ||
* @param {Sep6WithdrawParams} options.params - The parameters for the withdrawal request. | ||
* | ||
* @returns {Promise<Sep6WithdrawResponse>} Sep6 withdraw response. | ||
*/ | ||
async withdraw({ | ||
authToken, | ||
params, | ||
}: { | ||
authToken: string; | ||
params: Sep6WithdrawParams; | ||
}): Promise<Sep6WithdrawResponse> { | ||
const { transferServer } = await this.anchor.sep1(); | ||
|
||
const resp = await this.httpClient.get( | ||
`${transferServer}/withdraw?${queryString.stringify(params)}`, | ||
{ | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${authToken}`, | ||
}, | ||
}, | ||
); | ||
return resp.data; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
import { Wallet } from "../src"; | ||
import axios from "axios"; | ||
|
||
let wallet; | ||
let anchor; | ||
let sep6; | ||
let accountKp; | ||
|
||
describe("SEP-6", () => { | ||
beforeAll(async () => { | ||
wallet = Wallet.TestNet(); | ||
anchor = wallet.anchor({ homeDomain: "testanchor.stellar.org" }); | ||
sep6 = anchor.sep6(); | ||
|
||
accountKp = wallet.stellar().account().createKeypair(); | ||
await axios.get( | ||
"https://friendbot.stellar.org/?addr=" + accountKp.publicKey, | ||
); | ||
}, 10000); | ||
it("should get anchor info", async () => { | ||
const wallet = Wallet.TestNet(); | ||
const anchor = wallet.anchor({ homeDomain: "testanchor.stellar.org" }); | ||
const sep6 = anchor.sep6(); | ||
const resp = await sep6.info(); | ||
expect(resp.deposit).toBeTruthy(); | ||
expect(resp.withdraw).toBeTruthy(); | ||
|
@@ -13,4 +26,58 @@ describe("SEP-6", () => { | |
expect(refreshed.deposit).toBeTruthy(); | ||
expect(refreshed.withdraw).toBeTruthy(); | ||
}); | ||
it("should deposit", 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 resp = await sep6.deposit({ | ||
authToken, | ||
params: { | ||
asset_code: "SRT", | ||
account: accountKp.publicKey, | ||
type: "bank_account", | ||
}, | ||
}); | ||
expect(resp.id).toBeTruthy(); | ||
}); | ||
it("should withdraw", 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 resp = await sep6.withdraw({ | ||
authToken, | ||
params: { | ||
asset_code: "SRT", | ||
account: accountKp.publicKey, | ||
type: "bank_account", | ||
dest: "123", | ||
dest_extra: "12345", | ||
}, | ||
}); | ||
expect(resp.id).toBeTruthy(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about exchange endoints, are they out of scope?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya I thought that's what this ticket is for right?
https://stellarorg.atlassian.net/browse/WAL-1060?atlOrigin=eyJpIjoiNjYzZmI4ZWExYjI3NGVmMjljYTgwYmI4MjIwMjUzZWQiLCJwIjoiaiJ9