-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨Add horizon swaps handling in useSwapCallback
- Loading branch information
1 parent
87939dc
commit 5d9d3b4
Showing
3 changed files
with
182 additions
and
72 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { SorobanContextType, useSorobanReact } from "@soroban-react/core"; | ||
import { InterfaceTrade, TradeType } from "state/routing/types"; | ||
import {Asset, TransactionBuilder, Operation, BASE_FEE} from "@stellar/stellar-sdk"; | ||
import { getAmount } from "./getHorizonPath"; | ||
|
||
export const createStellarPathPayment = async (trade: InterfaceTrade, sorobanContext: SorobanContextType) => { | ||
const {address, activeConnector, serverHorizon, activeChain} = sorobanContext; | ||
console.log(trade) | ||
if(trade.tradeType == TradeType.EXACT_INPUT){ | ||
const amount = getAmount(trade.inputAmount?.value!); | ||
const sourceAsset = new Asset(trade.inputAmount?.currency.code!, trade.inputAmount?.currency.issuer) | ||
const destinationAsset = new Asset(trade.outputAmount?.currency.code!, trade.outputAmount?.currency.issuer) | ||
const account = await serverHorizon?.loadAccount(address!); | ||
const path = trade.path?.map((asset) => { | ||
const assetParts = asset.split(":") | ||
if(assetParts.length == 1 && assetParts[0] == "native"){ | ||
return Asset.native() | ||
} | ||
return new Asset(assetParts[0], assetParts[1]) | ||
}) | ||
if(!account){ | ||
throw new Error("Account not found") | ||
} | ||
const transaction = new TransactionBuilder(account, { | ||
fee: BASE_FEE, | ||
networkPassphrase: activeChain?.networkPassphrase | ||
}).addOperation(Operation.pathPaymentStrictSend({ | ||
sendAsset: sourceAsset, | ||
sendAmount: amount!, | ||
destination: address!, | ||
destAsset: destinationAsset, | ||
destMin: amount!, | ||
path: path | ||
})).setTimeout(180).build(); | ||
const transactionXDR = transaction.toXDR(); | ||
const signedTransaction = await activeConnector?.signTransaction(transactionXDR, { | ||
networkPassphrase: activeChain?.networkPassphrase, | ||
}); | ||
if(!signedTransaction){ | ||
throw new Error("Couldn't sign transaction"); | ||
} | ||
const transactionToSubmit = TransactionBuilder.fromXDR( | ||
signedTransaction!, | ||
activeChain?.networkPassphrase ?? '', | ||
); | ||
const transactionResult = await serverHorizon?.submitTransaction(transactionToSubmit); | ||
return transactionResult; | ||
} | ||
if(trade.tradeType == TradeType.EXACT_OUTPUT){ | ||
const amount = getAmount(trade.outputAmount?.value!); | ||
const sourceAsset = new Asset(trade.inputAmount?.currency.code!, trade.inputAmount?.currency.issuer) | ||
const destinationAsset = new Asset(trade.outputAmount?.currency.code!, trade.outputAmount?.currency.issuer) | ||
const account = await serverHorizon?.loadAccount(address!); | ||
const path = trade.path?.map((asset) => { | ||
const assetParts = asset.split(":") | ||
if(assetParts.length == 1 && assetParts[0] == "native"){ | ||
return Asset.native() | ||
} | ||
return new Asset(assetParts[0], assetParts[1]) | ||
}) | ||
if(!account){ | ||
throw new Error("Account not found") | ||
} | ||
const transaction = new TransactionBuilder(account, { | ||
fee: BASE_FEE, | ||
networkPassphrase: activeChain?.networkPassphrase | ||
}).addOperation(Operation.pathPaymentStrictReceive({ | ||
sendAsset: sourceAsset, | ||
sendMax: amount!, | ||
destination: address!, | ||
destAsset: destinationAsset, | ||
destAmount: amount!, | ||
})).setTimeout(180).build(); | ||
const transactionXDR = transaction.toXDR(); | ||
const signedTransaction = await activeConnector?.signTransaction(transactionXDR, { | ||
networkPassphrase: activeChain?.networkPassphrase, | ||
}); | ||
if(!signedTransaction){ | ||
throw new Error("Couldn't sign transaction"); | ||
} | ||
const transactionToSubmit = TransactionBuilder.fromXDR( | ||
signedTransaction!, | ||
activeChain?.networkPassphrase ?? '', | ||
); | ||
const transactionResult = await serverHorizon?.submitTransaction(transactionToSubmit); | ||
return transactionResult; | ||
} | ||
} |
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