-
Notifications
You must be signed in to change notification settings - Fork 128
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 TS examples for equivalent SDK methods #177
Changes from 6 commits
a61a2fa
17338b3
1b05291
e277c64
6e2f49f
0e6c79a
c3ca86a
fc74902
3866819
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 |
---|---|---|
|
@@ -34,7 +34,7 @@ val wallet = Wallet(StellarConfiguration.Testnet) | |
``` | ||
|
||
```ts | ||
const wallet = walletSdk.Wallet.TestNet(); | ||
let wallet = walletSdk.Wallet.TestNet(); | ||
``` | ||
|
||
</CodeExample> | ||
|
@@ -47,6 +47,12 @@ The wallet instance can be further configured. For example, to connect to the pu | |
val walletMainnet = Wallet(StellarConfiguration(Network.PUBLIC, "https://horizon.stellar.org")) | ||
``` | ||
|
||
```typescript | ||
let wallet = new Wallet({ | ||
stellarConfiguration: StellarConfiguration.MainNet(), | ||
}); | ||
``` | ||
|
||
</CodeExample> | ||
|
||
There is one more available configuration for a wallet that allows it to configure internal logic of the SDK. For example, to test with local servers on an HTTP protocol, HTTP can be manually enabled. | ||
|
@@ -60,6 +66,19 @@ val walletCustom = Wallet( | |
) | ||
``` | ||
|
||
```typescript | ||
const customClient: AxiosInstance = axios.create({ | ||
baseURL: "https://some-url.com/api", | ||
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. Why do we even need to set baseUrl? I don't think it's good to show it in the example 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. You don't necessarily, it's just an example of the params 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. Think it's better to remove it, cause in the SDK it doesn't make any sense to do so |
||
timeout: 1000, | ||
headers: { "X-Custom-Header": "foobar" }, | ||
}); | ||
let appConfig = new ApplicationConfiguration(DefaultSigner, customClient); | ||
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. Shouldn't DefaultSigner be there by default? (I.e. you don't need to set it manually) 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. You don't, I can just pass 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. Can you just omit it or it's not possible? |
||
let wal = new Wallet({ | ||
stellarConfiguration: StellarConfiguration.TestNet(), | ||
applicationConfiguration: appConfig, | ||
}); | ||
``` | ||
|
||
</CodeExample> | ||
|
||
### Configuring the Client | ||
|
@@ -151,6 +170,10 @@ Primary use of the SDK is to provide an easy way to connect to anchors via sets | |
val anchor = wallet.anchor("https://testanchor.stellar.org") | ||
``` | ||
|
||
```typescript | ||
let anchor = wallet.anchor({ homeDomain: "testanchor.stellar.org" }); | ||
``` | ||
|
||
</CodeExample> | ||
|
||
And the most basic interaction of fetching a [SEP-1]: Stellar Info File: | ||
|
@@ -163,6 +186,10 @@ suspend fun anchorToml(): TomlInfo { | |
} | ||
``` | ||
|
||
```typescript | ||
let resp = await anchor.getInfo(); | ||
``` | ||
|
||
</CodeExample> | ||
|
||
The anchor class also supports [SEP-10]: Stellar Authentication and [SEP-24]: Hosted Deposit and Withdrawal features. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,10 @@ Let's start with getting an instance of `Interactive` class, responsible for all | |
val sep24 = anchor.sep24() | ||
``` | ||
|
||
```typescript | ||
let sep24 = await anchor.getInfo(); | ||
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.
|
||
``` | ||
|
||
</CodeExample> | ||
|
||
First, let's get the information about the anchor's support for [SEP-24]. This request doesn't require authentication, and will return generic info, such as supported currencies, and features supported by the anchor. You can get a full list of returned fields in the [SEP-24 specification](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0024.md#info). | ||
|
@@ -40,6 +44,12 @@ suspend fun getAnchorServices(): AnchorServiceInfo { | |
} | ||
``` | ||
|
||
```typescript | ||
const getAnchorServices = async (): Promise<AnchorServiceInfo> => { | ||
anchor.getServicesInfo(); | ||
}; | ||
``` | ||
|
||
</CodeExample> | ||
|
||
## Interactive Flows | ||
|
@@ -54,6 +64,10 @@ To initiate an operation, we need to know an asset. You may want to hard-code it | |
val asset = info.currencies.first { it.code == "USDC" }.assetId | ||
``` | ||
|
||
```typescript | ||
const asset = info.currencies.find(({ code }) => code === "USDC").assetId; | ||
``` | ||
|
||
</CodeExample> | ||
|
||
:::info | ||
|
@@ -74,6 +88,14 @@ Let's start with a basic deposit: | |
val deposit = sep24.deposit(asset, token) | ||
``` | ||
|
||
```typescript | ||
let deposit = await anchor.interactive().deposit({ | ||
accountAddress: accountKp.publicKey(), | ||
assetCode, | ||
authToken, | ||
}); | ||
``` | ||
|
||
</CodeExample> | ||
|
||
As a result, you will get an [interactive response](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0024.md#deposit-and-withdraw-shared-responses) from the anchor. | ||
|
@@ -87,6 +109,11 @@ val url = deposit.url | |
val id = deposit.id | ||
``` | ||
|
||
```typescript | ||
let url = deposit.url; | ||
let id = deposit.id; | ||
``` | ||
|
||
</CodeExample> | ||
|
||
Similarly to the deposit flow, a basic withdrawal flow has the same method signature and repose type: | ||
|
@@ -99,6 +126,16 @@ val url = withdrawal.url | |
val id = withdrawal.id | ||
``` | ||
|
||
```typescript | ||
let withdrawal = await anchor.interactive().withdraw({ | ||
accountAddress: accountKp.publicKey(), | ||
assetCode, | ||
authToken, | ||
}); | ||
let url = withdrawal.url; | ||
let id = withdrawal.id; | ||
``` | ||
|
||
</CodeExample> | ||
|
||
### Providing KYC Info | ||
|
@@ -125,6 +162,15 @@ val sep9 = mapOf("email_address" to "[email protected]") | |
val deposit = sep24.deposit(asset, token, sep9) | ||
``` | ||
|
||
```typescript | ||
let deposit = await anchor.interactive().deposit({ | ||
accountAddress: accountKp.publicKey(), | ||
assetCode, | ||
authToken, | ||
extraFields: { email_address: "[email protected]" }, | ||
}); | ||
``` | ||
|
||
</CodeExample> | ||
|
||
### Changing Stellar Transfer Account | ||
|
@@ -143,6 +189,18 @@ suspend fun depositDifferentAccount(): InteractiveFlowResponse { | |
} | ||
``` | ||
|
||
```typescript | ||
const recipientAccount = "G..."; | ||
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. Can you add TODO here to modify code when memo is supported? |
||
const depositDifferentAccount = async (): Promise<InteractiveFlowResponse> => { | ||
return anchor.interactive().deposit({ | ||
accountAddress: accountKp.publicKey(), | ||
assetCode, | ||
authToken, | ||
fundsAccountAddress: recipientAccount, | ||
}); | ||
}; | ||
``` | ||
|
||
</CodeExample> | ||
|
||
Similarly, for a withdrawal, the origin account of the Stellar transaction could be changed: | ||
|
@@ -154,6 +212,16 @@ val originAccount = "G..." | |
val withdrawal = sep24.withdraw(asset, token, withdrawalAccount = originAccount) | ||
``` | ||
|
||
```typescript | ||
const originAccount = "G..."; | ||
const withdrawal = await anchor.interactive().withdraw({ | ||
accountAddress: accountKp.publicKey(), | ||
assetCode, | ||
authToken, | ||
fundsAccountAddress: recipientAccount, | ||
}); | ||
``` | ||
|
||
</CodeExample> | ||
|
||
## Getting Transaction Info | ||
|
@@ -173,6 +241,19 @@ val watcher = sep24.watcher() | |
val result = watcher.watchOneTransaction(token, "transaction id") | ||
``` | ||
|
||
```typescript | ||
let watcher = anchor.watcher(); | ||
|
||
let result = watcher.watchOneTransaction({ | ||
authToken, | ||
assetCode, | ||
Ifropc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
id: successfulTransaction.id, | ||
onMessage, | ||
onSuccess, | ||
onError, | ||
}); | ||
``` | ||
|
||
</CodeExample> | ||
|
||
Alternatively, we can track multiple transactions for the same asset. | ||
|
@@ -226,6 +307,13 @@ While `Watcher` class offers powerful tracking capabilities, sometimes it's requ | |
val transaction = sep24.getTransactionBy(token, id = "transaction id") | ||
``` | ||
|
||
```typescript | ||
const transaction = await anchor.getTransactionBy({ | ||
authToken, | ||
id: transactionId, | ||
}); | ||
``` | ||
|
||
</CodeExample> | ||
|
||
It's also possible to fetch transaction by the asset | ||
|
@@ -236,6 +324,13 @@ It's also possible to fetch transaction by the asset | |
val transactions = sep24.getTransactionsForAsset(asset, token) | ||
``` | ||
|
||
```typescript | ||
const transactions = await anchor.getHistory({ | ||
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.
|
||
authToken, | ||
assetCode, | ||
}); | ||
``` | ||
|
||
</CodeExample> | ||
|
||
[sep-9]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-000p.md | ||
|
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.
I think all this code belongs to the "Configuring the Client" section