-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91ddff8
commit f88e661
Showing
16 changed files
with
220 additions
and
1,350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,40 @@ | ||
import { Swap } from '@yoroi/types'; | ||
import { SwapOrdersApi } from './orders'; | ||
import { SwapPoolsApi } from './pools'; | ||
import { SwapTokensApi } from './tokens'; | ||
import { cancelOrder, createOrder, getOrders } from './orders'; | ||
import { getPools } from './pools'; | ||
import { getTokens } from './tokens'; | ||
|
||
export class SwapApi { | ||
public readonly orders: SwapOrdersApi; | ||
public readonly pools: SwapPoolsApi; | ||
public readonly tokens: SwapTokensApi; | ||
export class SwapApi implements Swap.ISwapApi { | ||
constructor(public readonly network: Swap.Netowrk) {} | ||
|
||
constructor(network: Swap.Netowrk) { | ||
this.orders = new SwapOrdersApi(network); | ||
this.pools = new SwapPoolsApi(network); | ||
this.tokens = new SwapTokensApi(network); | ||
public async createOrder( | ||
order: Swap.CreateOrderData | ||
): ReturnType<typeof createOrder> { | ||
return createOrder(this.network, order); | ||
} | ||
|
||
public async cancelOrder( | ||
orderUTxO: string, | ||
collateralUTxO: string, | ||
walletAddress: string | ||
): ReturnType<typeof cancelOrder> { | ||
return cancelOrder(this.network, orderUTxO, collateralUTxO, walletAddress); | ||
} | ||
|
||
public async getOrders(stakeKeyHash: string): ReturnType<typeof getOrders> { | ||
return getOrders(this.network, stakeKeyHash); | ||
} | ||
|
||
public async getPools( | ||
tokenA: Swap.BaseTokenInfo, | ||
tokenB: Swap.BaseTokenInfo | ||
): ReturnType<typeof getPools> { | ||
return getPools(this.network, tokenA, tokenB); | ||
} | ||
|
||
public getTokens( | ||
policyId = '', | ||
assetName = '' | ||
): ReturnType<typeof getTokens> { | ||
return getTokens(this.network, policyId, assetName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,94 @@ | ||
import { Swap } from '@yoroi/types'; | ||
import { SWAP_API_ENDPOINTS, axiosClient } from './config'; | ||
|
||
// todo: use axios params | ||
export async function createOrder( | ||
network: Swap.Netowrk, | ||
order: Swap.CreateOrderData | ||
): Promise<Swap.CreateOrderResponse> { | ||
const apiUrl = SWAP_API_ENDPOINTS[network].constructSwapDatum; | ||
const response = await axiosClient.get< | ||
| { status: 'failed'; reason?: string } | ||
| { status: 'success'; hash: string; datum: string; address: string } | ||
>('/', { | ||
baseURL: apiUrl, | ||
params: { | ||
walletAddr: order.address, | ||
protocol: order.protocol, | ||
poolId: order.poolId, | ||
sellTokenPolicyID: order.sell.policyId, | ||
sellTokenNameHex: order.sell.assetName, | ||
sellAmount: order.sell.amount, | ||
buyTokenPolicyID: order.buy.policyId, | ||
buyTokenNameHex: order.buy.assetName, | ||
buyAmount: order.buy.amount, | ||
}, | ||
}); | ||
|
||
export class SwapOrdersApi { | ||
private readonly constructSwapDatumApiUrl: string; | ||
private readonly cancelSwapTransactionApiUrl: string; | ||
private readonly getOrdersApiUrl: string; | ||
|
||
constructor(public readonly network: Swap.Netowrk) { | ||
const { constructSwapDatum, cancelSwapTransaction, getOrders } = | ||
SWAP_API_ENDPOINTS[network]; | ||
this.constructSwapDatumApiUrl = constructSwapDatum; | ||
this.cancelSwapTransactionApiUrl = cancelSwapTransaction; | ||
this.getOrdersApiUrl = getOrders; | ||
} | ||
|
||
/** | ||
* @param order the order to construct the datum for and the address to send the order to. | ||
* @returns the order datum, order datum hash, and address to send the order to. | ||
*/ | ||
public async createOrder( | ||
order: Swap.Order | ||
): Promise<Record<'datumHash' | 'datum' | 'contractAddress', string>> { | ||
const response = await axiosClient.get< | ||
| { status: 'failed'; reason?: string } | ||
| { status: 'success'; hash: string; datum: string; address: string } | ||
>('/', { | ||
baseURL: this.constructSwapDatumApiUrl, | ||
params: { | ||
walletAddr: order.address, | ||
protocol: order.protocol, | ||
poolId: order.poolId, | ||
sellTokenPolicyID: order.sell.policyId, | ||
sellTokenNameHex: order.sell.assetName, | ||
sellAmount: order.sell.amount, | ||
buyTokenPolicyID: order.buy.policyId, | ||
buyTokenNameHex: order.buy.assetName, | ||
buyAmount: order.buy.amount, | ||
}, | ||
if (response.status !== 200) { | ||
throw new Error('Failed to construct swap datum', { | ||
cause: response.data, | ||
}); | ||
} | ||
|
||
if (response.status !== 200) { | ||
throw new Error('Failed to construct swap datum', { | ||
cause: response.data, | ||
}); | ||
} | ||
|
||
if (response.data.status === 'failed') { | ||
throw new Error(response.data.reason || 'Unexpected error occurred'); | ||
} | ||
|
||
return { | ||
datumHash: response.data.hash, | ||
datum: response.data.datum, | ||
contractAddress: response.data.address, | ||
}; | ||
if (response.data.status === 'failed') { | ||
throw new Error(response.data.reason || 'Unexpected error occurred'); | ||
} | ||
|
||
/** | ||
* @param orderUTxO order UTxO from the smart contract to cancel. e.g. "txhash#0" | ||
* @param collateralUTxOs collateral UTxOs to use for canceling the order in cbor format. | ||
* @param walletAddress address of the wallet that owns the order in cbor format. | ||
* @returns an unsigned transaction to cancel the order. | ||
*/ | ||
public async cancelOrder( | ||
orderUTxO: string, | ||
collateralUTxO: string, | ||
walletAddress: string | ||
): Promise<string> { | ||
const response = await axiosClient.get('/', { | ||
baseURL: this.cancelSwapTransactionApiUrl, | ||
params: { | ||
wallet: walletAddress, | ||
utxo: orderUTxO, | ||
collateralUTxO, | ||
}, | ||
}); | ||
return { | ||
datumHash: response.data.hash, | ||
datum: response.data.datum, | ||
contractAddress: response.data.address, | ||
}; | ||
} | ||
|
||
if (response.status !== 200) { | ||
throw new Error('Failed to cancel swap transaction', { | ||
cause: response.data, | ||
}); | ||
} | ||
/** | ||
* @param orderUTxO order UTxO from the smart contract to cancel. e.g. "txhash#0" | ||
* @param collateralUTxOs collateral UTxOs to use for canceling the order in cbor format. | ||
* @param walletAddress address of the wallet that owns the order in cbor format. | ||
* @returns an unsigned transaction to cancel the order. | ||
*/ | ||
export async function cancelOrder( | ||
network: Swap.Netowrk, | ||
orderUTxO: string, | ||
collateralUTxO: string, | ||
walletAddress: string | ||
): Promise<string> { | ||
const apiUrl = SWAP_API_ENDPOINTS[network].cancelSwapTransaction; | ||
const response = await axiosClient.get('/', { | ||
baseURL: apiUrl, | ||
params: { | ||
wallet: walletAddress, | ||
utxo: orderUTxO, | ||
collateralUTxO, | ||
}, | ||
}); | ||
|
||
return response.data.cbor; | ||
if (response.status !== 200) { | ||
throw new Error('Failed to cancel swap transaction', { | ||
cause: response.data, | ||
}); | ||
} | ||
|
||
/** | ||
* @param stakeKeyHash the stake key hash of the wallet to get orders for. | ||
* @returns all unfufilled orders for the given stake key hash. | ||
*/ | ||
public async getOrders(stakeKeyHash: string): Promise<Swap.OpenOrder[]> { | ||
const response = await axiosClient.get<Swap.OpenOrder[]>('/', { | ||
baseURL: this.getOrdersApiUrl, | ||
params: { | ||
'stake-key-hash': stakeKeyHash, | ||
}, | ||
}); | ||
return response.data.cbor; | ||
} | ||
|
||
if (response.status !== 200) { | ||
throw new Error(`Failed to get orders for ${stakeKeyHash}`, { | ||
cause: response.data, | ||
}); | ||
} | ||
export async function getOrders( | ||
network: Swap.Netowrk, | ||
stakeKeyHash: string | ||
): Promise<Swap.OpenOrder[]> { | ||
const apiUrl = SWAP_API_ENDPOINTS[network].getPools; | ||
const response = await axiosClient.get<Swap.OpenOrder[]>('/', { | ||
baseURL: apiUrl, | ||
params: { | ||
'stake-key-hash': stakeKeyHash, | ||
}, | ||
}); | ||
|
||
return response.data; | ||
if (response.status !== 200) { | ||
throw new Error(`Failed to get orders for ${stakeKeyHash}`, { | ||
cause: response.data, | ||
}); | ||
} | ||
|
||
return response.data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.