Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add btc babylon #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions projects/babylon/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"private": true,
"name": "@sentio-example/btc",
"version": "1.0.0",
"license": "Apache-2.0",
"type": "module",
"scripts": {
"build": "sentio build --skip-deps",
"build:all": "pnpm --filter=$(node -p \"require('./package.json').name\")... build",
"compile": "tsc -p .",
"test": "sentio test",
"upload": "sentio upload"
},
"dependencies": {
"@sentio/sdk": "v2.42.0-rc.9"
},
"devDependencies": {
"@sentio/cli": "v2.20.2",
"typescript": "^5.5.4"
}
}
1 change: 1 addition & 0 deletions projects/babylon/sentio.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
project: btc-babylon
16 changes: 16 additions & 0 deletions projects/babylon/src/processor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import assert from 'assert'
import { before, describe, test } from 'node:test'
import { TestProcessorServer } from '@sentio/sdk/testing'

describe('Test Processor', () => {
const service = new TestProcessorServer(async () => await import('./processor.js'))

before(async () => {
await service.start()
})

test('has config', async () => {
const config = await service.getConfig({})
assert(config.contractConfigs.length > 0)
})
})
104 changes: 104 additions & 0 deletions projects/babylon/src/processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { BTCProcessor, Transaction } from '@sentio/sdk/btc'
import { ChainId } from '@sentio/chain'

BTCProcessor.bind({
chainId: ChainId.BTC_MAINNET,
startBlock: 857589n,
}).onTransaction((tx, ctx) => {
const from = tx.vin[0]?.pre_vout?.scriptPubKey.address
const to = tx.vout[0].scriptPubKey.address
// handle staking transactions
const babylonInfo = extractBabylonInfo(tx)
if (babylonInfo) {
const amount = tx.vout[0].value
const staker = babylonInfo.stakerPubkey
const provider = babylonInfo.providerPubkey
const stakingTime = babylonInfo.stakingTime

ctx.eventLogger.emit('staking', {
distinctId: tx.txid,
amount,
staker,
provider,
from,
to,
stakingTime,
message: `staking ${amount} from ${staker} to ${provider}`
})
}
}, {
filter: [{ blockheight: { gte: 857589 } }],
outputFilter: {
n: 1,
script_asm: {
prefix: 'OP_RETURN 62626e31',
length: 152
}
}
}).onTransaction((tx, ctx) => {

// handle unbonding transactions
for (const vin of tx.vin) {
const preTx = vin.pre_transaction
if (preTx) {
const babylonInfo = extractBabylonInfo(preTx)
console.log('babylonInfo', babylonInfo)
if (babylonInfo) {
const staker = babylonInfo.stakerPubkey
const provider = babylonInfo.providerPubkey
const stakingTime = babylonInfo.stakingTime

const amount = tx.vout[0]?.value
const to = tx.vout[0].scriptPubKey.address
const from = vin?.pre_vout?.scriptPubKey.address

ctx.eventLogger.emit('staking', {
distinctId: tx.txid,
amount: -amount,
staker,
provider,
from,
to,
stakingTime,
message: `unbonding ${amount} from ${provider} to ${staker}`
})
}
}
}
}, {
filter: [{ blockheight: { gte: 857589 } }],
inputFilter: {
preTransaction: { // the outbonding transaction's input should be the staking transaction
outputFilter: {
script_asm: {
prefix: 'OP_RETURN 62626e31',
length: 152
}
}
}
}
})


function extractBabylonInfo(tx: Transaction) {
for (const vout of tx.vout) {
const script = vout.scriptPubKey.hex as string
if (script.startsWith('6a4762626e31') && script.length == 146) {
const version = script.substring(12, 12 + 2)
const stakerPubkey = script.substring(14, 14 + 64)
const providerPubkey = script.substring(78, 78 + 64)
const stakingTimeHex = script.substring(142, 142 + 4)
const stakingTime = parseInt(stakingTimeHex, 16)
if (version == '00') {
return {
stakerPubkey,
providerPubkey,
stakingTime
}
}
}
}

return undefined
}

27 changes: 27 additions & 0 deletions projects/babylon/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"importHelpers": true,
"alwaysStrict": true,
"sourceMap": true,
"target": "esnext",
"esModuleInterop": true,
"noImplicitReturns": true,
"noImplicitAny": true,
"module": "nodenext",
"moduleResolution": "nodenext",
"strictNullChecks": true,
"stripInternal": true,
"noFallthroughCasesInSwitch": true,
"noEmitOnError": false,
"rootDir": "./src",
"outDir": "./dist",
"skipLibCheck": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"resolveJsonModule": true
},
"exclude": [
"dist",
"jest.config.ts"
]
}