-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add StackingDAO protocol (#12)
- Loading branch information
Showing
12 changed files
with
353 additions
and
4 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,23 @@ | ||
{ | ||
"chain": "stacks", | ||
"uuid": "1", | ||
"name": "stacking-dao-core-v1.deposit", | ||
"version": 1, | ||
"networks": { | ||
"mainnet": { | ||
"if_this": { | ||
"scope": "contract_call", | ||
"contract_identifier": "SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.stacking-dao-core-v1", | ||
"method": "deposit" | ||
}, | ||
"then_that": { | ||
"http_post": { | ||
"url": "http://localhost:3000/api/chainhooks/stackingdao/deposit", | ||
"authorization_header": "Bearer dev-api-token" | ||
} | ||
}, | ||
"start_block": 132118, | ||
"decode_clarity_values": false | ||
} | ||
} | ||
} |
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,23 @@ | ||
{ | ||
"chain": "stacks", | ||
"uuid": "1", | ||
"name": "stacking-dao-core-v1.withdraw", | ||
"version": 1, | ||
"networks": { | ||
"mainnet": { | ||
"if_this": { | ||
"scope": "contract_call", | ||
"contract_identifier": "SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.stacking-dao-core-v1", | ||
"method": "withdraw" | ||
}, | ||
"then_that": { | ||
"http_post": { | ||
"url": "http://localhost:3000/api/chainhooks/stackingdao/withdraw", | ||
"authorization_header": "Bearer dev-api-token" | ||
} | ||
}, | ||
"start_block": 132118, | ||
"decode_clarity_values": false | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,72 @@ | ||
import { db } from "@/db/db"; | ||
import { type InsertTransaction, transactionTable } from "@/db/schema"; | ||
import { conflictUpdateSetAllColumns } from "@/db/utils"; | ||
import type { | ||
ChainhookPayload, | ||
ChainhookReceiptEventFTMintEvent, | ||
ChainhookReceiptEventSTXTransferEvent, | ||
} from "@/lib/chainhooks"; | ||
import { getOrInsertToken } from "@/lib/currencies"; | ||
|
||
export const dynamic = "force-dynamic"; | ||
|
||
export async function POST(request: Request) { | ||
const data: ChainhookPayload = await request.json(); | ||
|
||
const transactionsToInsert = data.apply[0].transactions | ||
.filter((transactionToProcess) => transactionToProcess.metadata.success) | ||
.map((transactionToProcess) => { | ||
const sender = transactionToProcess.metadata.sender; | ||
const transferEvents = transactionToProcess.metadata.receipt.events | ||
// Events are not always in order so we sort them by index | ||
.sort((a, b) => a.position.index - b.position.index) | ||
.filter( | ||
( | ||
event, | ||
): event is | ||
| ChainhookReceiptEventSTXTransferEvent | ||
| ChainhookReceiptEventFTMintEvent => | ||
event.type === "STXTransferEvent" || event.type === "FTMintEvent", | ||
); | ||
const stxTransferEvent = transferEvents.filter( | ||
(event) => | ||
event.type === "STXTransferEvent" && event.data.sender === sender, | ||
)[0]; | ||
const stStxMintEvent = transferEvents.filter( | ||
(event): event is ChainhookReceiptEventFTMintEvent => | ||
event.type === "FTMintEvent" && event.data.recipient === sender, | ||
)[0]; | ||
|
||
return { | ||
txId: transactionToProcess.transaction_identifier.hash, | ||
protocol: "stackingdao", | ||
blockHeight: BigInt(data.apply[0].block_identifier.index), | ||
timestamp: new Date(data.apply[0].timestamp * 1000), | ||
sender, | ||
action: "stackingdao-deposit", | ||
data: { | ||
outAmount: BigInt(stxTransferEvent.data.amount), | ||
outToken: "STX", | ||
inAmount: BigInt(stStxMintEvent.data.amount), | ||
inToken: stStxMintEvent.data.asset_identifier, | ||
}, | ||
} satisfies InsertTransaction; | ||
}); | ||
|
||
for (const transaction of transactionsToInsert) { | ||
await getOrInsertToken(transaction.data.inToken); | ||
await getOrInsertToken(transaction.data.outToken); | ||
} | ||
|
||
if (transactionsToInsert.length > 0) { | ||
await db | ||
.insert(transactionTable) | ||
.values(transactionsToInsert) | ||
.onConflictDoUpdate({ | ||
target: transactionTable.txId, | ||
set: conflictUpdateSetAllColumns(transactionTable), | ||
}); | ||
} | ||
|
||
return Response.json({ ok: true }); | ||
} |
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,72 @@ | ||
import { db } from "@/db/db"; | ||
import { type InsertTransaction, transactionTable } from "@/db/schema"; | ||
import { conflictUpdateSetAllColumns } from "@/db/utils"; | ||
import type { | ||
ChainhookPayload, | ||
ChainhookReceiptEventFTBurnEvent, | ||
ChainhookReceiptEventSTXTransferEvent, | ||
} from "@/lib/chainhooks"; | ||
import { getOrInsertToken } from "@/lib/currencies"; | ||
|
||
export const dynamic = "force-dynamic"; | ||
|
||
export async function POST(request: Request) { | ||
const data: ChainhookPayload = await request.json(); | ||
|
||
const transactionsToInsert = data.apply[0].transactions | ||
.filter((transactionToProcess) => transactionToProcess.metadata.success) | ||
.map((transactionToProcess) => { | ||
const sender = transactionToProcess.metadata.sender; | ||
const transferEvents = transactionToProcess.metadata.receipt.events | ||
// Events are not always in order so we sort them by index | ||
.sort((a, b) => a.position.index - b.position.index) | ||
.filter( | ||
( | ||
event, | ||
): event is | ||
| ChainhookReceiptEventSTXTransferEvent | ||
| ChainhookReceiptEventFTBurnEvent => | ||
event.type === "STXTransferEvent" || event.type === "FTBurnEvent", | ||
); | ||
const stxTransferEvent = transferEvents.filter( | ||
(event) => | ||
event.type === "STXTransferEvent" && event.data.recipient === sender, | ||
)[0]; | ||
const stStxBurnEvent = transferEvents.filter( | ||
(event): event is ChainhookReceiptEventFTBurnEvent => | ||
event.type === "FTBurnEvent", | ||
)[0]; | ||
|
||
return { | ||
txId: transactionToProcess.transaction_identifier.hash, | ||
protocol: "stackingdao", | ||
blockHeight: BigInt(data.apply[0].block_identifier.index), | ||
timestamp: new Date(data.apply[0].timestamp * 1000), | ||
sender, | ||
action: "stackingdao-withdraw", | ||
data: { | ||
inAmount: BigInt(stxTransferEvent.data.amount), | ||
inToken: "STX", | ||
outAmount: BigInt(stStxBurnEvent.data.amount), | ||
outToken: stStxBurnEvent.data.asset_identifier, | ||
}, | ||
} satisfies InsertTransaction; | ||
}); | ||
|
||
for (const transaction of transactionsToInsert) { | ||
await getOrInsertToken(transaction.data.inToken); | ||
await getOrInsertToken(transaction.data.outToken); | ||
} | ||
|
||
if (transactionsToInsert.length > 0) { | ||
await db | ||
.insert(transactionTable) | ||
.values(transactionsToInsert) | ||
.onConflictDoUpdate({ | ||
target: transactionTable.txId, | ||
set: conflictUpdateSetAllColumns(transactionTable), | ||
}); | ||
} | ||
|
||
return Response.json({ ok: true }); | ||
} |
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,28 @@ | ||
import type { | ||
SelectTransactionActionStackingDAODeposit, | ||
SelectTransactionActionStackingDAOWithdraw, | ||
} from "@/db/transactions"; | ||
import { displayPrice } from "@/lib/currencies"; | ||
import { Text } from "@radix-ui/themes"; | ||
|
||
interface TransactionActionStackingDAOProps { | ||
transaction: | ||
| SelectTransactionActionStackingDAODeposit | ||
| SelectTransactionActionStackingDAOWithdraw; | ||
} | ||
|
||
export const TransactionActionStackingDAO = ({ | ||
transaction, | ||
}: TransactionActionStackingDAOProps) => { | ||
return ( | ||
<> | ||
<Text color="gray"> | ||
{transaction.action === "stackingdao-deposit" ? "Deposit" : "Withdraw"} | ||
</Text>{" "} | ||
{displayPrice(transaction.data.outAmount, transaction.outToken.decimals)}{" "} | ||
{transaction.outToken.symbol} <Text color="gray">for</Text>{" "} | ||
{displayPrice(transaction.data.inAmount, transaction.inToken.decimals)}{" "} | ||
{transaction.inToken.symbol} | ||
</> | ||
); | ||
}; |
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
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.