Skip to content

Commit

Permalink
feature: simple route (#9)
Browse files Browse the repository at this point in the history
* get all pairs

* update

* getAllLPCoinResourcesWithAdmin

* [WIP]bestTradeExactIn

* add swap payload

* add price impact

* update route readme
  • Loading branch information
xiaozzz authored Sep 16, 2022
1 parent 7e96249 commit 46e9f0b
Show file tree
Hide file tree
Showing 9 changed files with 706 additions and 58 deletions.
87 changes: 47 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,86 +145,93 @@ const sdk = new SDK({
const BTC = '0x16fe2df00ea7dde4a63409201f7f4e536bde7bb7335526a35d05111e68aa322c::TestCoinsV1::BTC'
const aptosAmount = 1e6

const output = await sdk.swap.swapRates({
const trades = await sdk.route.getRouteSwapExactCoinForCoin({
fromCoin: APTOS,
toCoin: BTC,
amount: aptosAmount,
fixedCoin: 'from', // fixed input coin
slippage: 0.05, // 5%
amount: aptosAmount.toString(),
});

if (trades.length == 0) throw("No route error")
const bestTrade = trades[0]
/**
output type:
bestTrade type:
{
amount: Decimal
amountWithSlippage: Decimal
coinPairList: LiquidityPoolResource[]
amountList: string[]
coinTypeList: string[]
priceImpact: Decimal
coinFromDivCoinTo: Decimal
coinToDivCoinFrom: Decimal
}
*/

const txPayload = sdk.swap.swapPayload({
fromCoin: APTOS,
toCoin: BTC,
fromAmount: aptosAmount,
toAmount: output.amount,
fixedCoin: 'from', // fixed input coin
toAddress: '0xA11ce', // receive `toCoin` address. In the most case, should be the same as sender address
slippage: 0.05, // 5%
deadline: 20, // 20 minutes
})
const output = sdk.route.swapExactCoinForCoinPayload(
trade: bestTrade,
toAddress: SenderAddress, // receive `toCoin` address. In the most case, should be the same as sender address
slippage: 0.05, // 5%
deadline: 20, // 20 minutes
)

/**
output type: tx payload
*/
})()
```


### Swap (exact out) rate calculation and tx payload. Swap coin to exact coin mode
```typescript
(async () => {
const APTOS = '0x1::aptos_coin::AptosCoin'
const BTC = '0x16fe2df00ea7dde4a63409201f7f4e536bde7bb7335526a35d05111e68aa322c::TestCoinsV1::BTC'
const btcAmount = 1e6

const output = await sdk.swap.swapRates({
const trades = await sdk.route.getRouteSwapCoinForExactCoin({
fromCoin: APTOS,
toCoin: BTC,
amount: btcAmount,
fixedCoin: 'to', // fixed output coin
slippage: 0.05, // 5%
})

amount: btcAmount.toString(),
});
if (trades.length == 0) throw("No route error")
const bestTrade = trades[0]
/**
output type:
bestTrade type:
{
amount: Decimal
amountWithSlippage: Decimal
coinPairList: LiquidityPoolResource[]
amountList: string[]
coinTypeList: string[]
priceImpact: Decimal
coinFromDivCoinTo: Decimal
coinToDivCoinFrom: Decimal
}
*/

const txPayload = sdk.swap.swapPayload({
fromCoin: APTOS,
toCoin: BTC,
fromAmount: output.amount,
toAmount: btcAmount,
fixedCoin: 'to', // fixed output coin
toAddress: '0xA11ce', // receive `toCoin` address. In the most case, should be the same as sender address
const output = sdk.route.swapCoinForExactCoinPayload(
trade: bestTrade,
toAddress: SenderAddress, // receive `toCoin` address. In the most case, should be the same as sender address
slippage: 0.05, // 5%
deadline: 20, // 20 minutes
})
)

/**
output type: tx payload
*/
})()
```

### Get all LPCoin by address
```typescript
(async () => {
const queryAddress = '0xA11ce'
const output = await sdk.swap.getAllLPCoinResourcesByAddress({
address: queryAddress,
})

/**
output type:
[{
coinX: AptosResourceType
coinY: AptosResourceType
lpCoin: AptosResourceType
value: string
}]
*/
})()
```

### Get LPCoin amount
```typescript
(async () => {
Expand Down
Loading

0 comments on commit 46e9f0b

Please sign in to comment.