-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Can fetch and prepare swap * Don't override common headers
- Loading branch information
1 parent
aabdf98
commit 2341e25
Showing
8 changed files
with
310 additions
and
5 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,11 @@ | ||
import { Algodv2, generateAccount, Indexer, mnemonicToSecretKey } from "algosdk"; | ||
|
||
// TODO: Replace | ||
// export const sender = mnemonicToSecretKey(""); | ||
export const sender = generateAccount(); | ||
|
||
export const MainnetAlgodClient = new Algodv2("", "https://mainnet-api.algonode.cloud/", 443); | ||
export const MainnetIndexerClient = new Indexer("", "https://mainnet-idx.algonode.cloud/", 443); | ||
|
||
export const TestnetAlgodClient = new Algodv2("", "https://testnet-api.algonode.cloud/", 443); | ||
export const TestnetIndexerClient = new Indexer("", "https://testnet-idx.algonode.cloud/", 443); |
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,27 @@ | ||
import { decodeUnsignedTransaction } from "algosdk"; | ||
import { FolksRouterClient, Network, SwapMode } from "../src"; | ||
import { MainnetAlgodClient, sender } from "./config"; | ||
|
||
async function main() { | ||
const user = sender; | ||
const algod = MainnetAlgodClient; | ||
const client = new FolksRouterClient(Network.MAINNET); | ||
|
||
// fetch quote | ||
const quote = await client.fetchSwapQuote( | ||
0, | ||
31566704, | ||
BigInt(10e6), | ||
SwapMode.FIXED_INPUT, | ||
); | ||
|
||
// prepare swap | ||
const base64txns = await client.prepareSwapTransactions(user.addr, BigInt(10), quote); | ||
const unsignedTxns = base64txns.map(txn => decodeUnsignedTransaction(Buffer.from(txn, "base64"))); | ||
const signedTxns = unsignedTxns.map(txn => txn.signTxn(user.sk)); | ||
|
||
// submit | ||
await algod.sendRawTransaction(signedTxns).do(); | ||
} | ||
|
||
main().catch(console.error); |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import axios, { AxiosInstance} from "axios"; | ||
import { Network, SwapMode, SwapQuote, SwapTransactions } from "./types"; | ||
|
||
const BASE_URL = "https://api.folksrouter.io"; | ||
|
||
export class FolksRouterClient { | ||
private readonly network: Network; | ||
private readonly api: AxiosInstance; | ||
|
||
constructor(network: Network, apiKey?: string) { | ||
// construct url | ||
let url = BASE_URL; | ||
if (network === Network.TESTNET) url += "/testnet"; | ||
url += "/v1"; | ||
if (apiKey !== undefined) { | ||
url += "/pro"; | ||
} | ||
|
||
// set | ||
this.network = network; | ||
this.api = axios.create({ baseURL: url, headers: { "x-api-key": apiKey }}); | ||
} | ||
|
||
public async fetchSwapQuote( | ||
fromAssetId: number, | ||
toAssetId: number, | ||
amount: number | bigint, | ||
swapMode: SwapMode, | ||
maxGroupSize?: number, | ||
feeBps?: number | bigint, | ||
referrer?: string, | ||
): Promise<SwapQuote> { | ||
const { data } = await this.api.get("/fetch/quote", { | ||
params: { | ||
network: this.network, | ||
fromAsset: fromAssetId, | ||
toAsset: toAssetId, | ||
amount, | ||
type: swapMode, | ||
maxGroupSize, | ||
feeBps, | ||
referrer, | ||
} | ||
}); | ||
if (!data.success) throw Error(data.errors); | ||
|
||
const { quoteAmount, priceImpact, microalgoTxnsFee, txnPayload } = data.result; | ||
|
||
return { | ||
quoteAmount: BigInt(quoteAmount), | ||
priceImpact, | ||
microalgoTxnsFee, | ||
txnPayload, | ||
}; | ||
} | ||
|
||
public async prepareSwapTransactions( | ||
userAddress: string, | ||
slippageBps: number | bigint, | ||
swapQuote: SwapQuote, | ||
): Promise<SwapTransactions> { | ||
const { data } = await this.api.get("/prepare/swap", { | ||
params: { | ||
userAddress, | ||
slippageBps, | ||
txnPayload: swapQuote.txnPayload, | ||
} | ||
}); | ||
if (!data.success) throw Error(data.errors); | ||
|
||
return data.result; | ||
} | ||
} |
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,4 +1,25 @@ | ||
export type ReferrerGroupTransaction = { | ||
type ReferrerGroupTransaction = { | ||
unsignedTxn: Uint8Array; | ||
lsig?: Uint8Array; | ||
}[]; | ||
|
||
enum Network { | ||
MAINNET = "mainnet", | ||
TESTNET = "testnet", | ||
} | ||
|
||
enum SwapMode { | ||
FIXED_INPUT = "FIXED_INPUT", | ||
FIXED_OUTPUT = "FIXED_OUTPUT", | ||
} | ||
|
||
interface SwapQuote { | ||
quoteAmount: bigint; | ||
priceImpact: number; | ||
microalgoTxnsFee: number; | ||
txnPayload: string; | ||
} | ||
|
||
type SwapTransactions = string[]; | ||
|
||
export { ReferrerGroupTransaction, Network, SwapMode, SwapQuote, SwapTransactions } |
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.