Skip to content

Commit

Permalink
Merge branch 'main' of github.com:wormhole-foundation/example-grant-p…
Browse files Browse the repository at this point in the history
…rogram into influxdb-event-scripts
  • Loading branch information
abhidtu2014 committed Apr 2, 2024
2 parents 85e7a8d + d041392 commit 213b12f
Show file tree
Hide file tree
Showing 68 changed files with 7,935 additions and 1,454 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ jobs:
env:
ENDPOINT: http://localhost:8899

env:
PROGRAM_ID: WabZqXyytFA2XeXswf9DdQkQuSZuRiEgKdDmq7c5Mnp

defaults:
run:
working-directory: ./frontend
Expand Down
1 change: 0 additions & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ Following env vars are required:

- `DISPENSER_KEY_SECRET_NAME`: private key of the wallet that will be used to sign the discord message
- `FUNDER_WALLET_KEY_SECRET_NAME`: private key of the wallet that will be used to fund the transactions
- `TOKEN_DISPENSER_PROGRAM_ID`: the program id of the token dispenser
8 changes: 8 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
"dependencies": {
"@aws-sdk/client-secrets-manager": "^3.535.0",
"@coral-xyz/anchor": "^0.29.0",
"@cosmjs/amino": "^0.32.3",
"@cosmjs/crypto": "^0.32.3",
"@cosmjs/encoding": "^0.32.3",
"@influxdata/influxdb-client": "^1.33.2",
"@solana/spl-token": "^0.4.3",
"@solana/web3.js": "^1.91.1",
"bs58": "^5.0.0",
"hi-base32": "^0.5.1",
"tweetnacl": "^1.0.3"
},
"devDependencies": {
Expand All @@ -29,6 +35,7 @@
"@types/aws-lambda": "^8.10.136",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^18",
"@typescript-eslint/eslint-plugin": "^7.3.1",
"@typescript-eslint/parser": "^7.3.1",
"body-parser": "^1.20.2",
Expand All @@ -37,6 +44,7 @@
"jest": "^29.7.0",
"msw": "^2.2.9",
"prettier": "^2.7.1",
"testcontainers": "^10.8.0",
"typescript": "^5.4.2"
},
"jest": {
Expand Down
19 changes: 16 additions & 3 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export default {
aws: {
region: process.env.AWS_REGION ?? 'us-east-2'
},
tokenDispenserProgramId: () => process.env.TOKEN_DISPENSER_PROGRAM_ID,
keys: {
tokenDispenserProgramId: () => 'Wapq3Hpv2aSKjWrh4pM8eweh8jVJB7D1nLBw9ikjVYx',
secrets: {
dispenserGuard: {
/** optional. mostly for local testing */
key: process.env.DISPENSER_WALLET_KEY,
Expand All @@ -16,11 +16,24 @@ export default {
},
funding: {
/** optional. mostly for local testing */
key: process.env.FUNDING_WALLET_KEY,
key: () => process.env.FUNDING_WALLET_KEY,
/** required. with a default value and used when when key not set */
secretName:
process.env.FUNDER_WALLET_KEY_SECRET_NAME ??
'xli-test-secret-funder-wallet'
},
influx: {
key: () => process.env.INFLUXDB_TOKEN,
/** required. with a default value */
secretName: process.env.IDB_SECRET_NAME ?? 'xl-ad-idb'
}
},
influx: {
url: () => process.env.INFLUXDB_URL ?? 'http://localhost:8086',
org: () => process.env.INFLUXDB_ORG ?? 'xl',
bucket: () => process.env.INFLUXDB_BUCKET ?? 'ad',
token: () => process.env.INFLUXDB_TOKEN,
timeout: () => parseInt(process.env.INFLUXDB_TIMEOUT_MS ?? '20500'),
isFlushEnabled: () => process.env.INFLUXDB_FLUSH_ENABLED === 'true' ?? false
}
}
61 changes: 40 additions & 21 deletions backend/src/handlers/fund-transactions.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet'
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'
import { getFundingKey } from '../utils/secrets'
import { getFundingKeys } from '../utils/secrets'
import {
checkTransactions,
deserializeTransactions
deserializeTransactions,
extractCallData
} from '../utils/fund-transactions'
import { Keypair, VersionedTransaction } from '@solana/web3.js'
import { VersionedTransaction } from '@solana/web3.js'
import bs58 from 'bs58'
import { HandlerError } from '../utils/errors'
import { asJsonResponse } from '../utils/response'
import { ClaimSignature } from '../types'
import { saveSignedTransactions } from '../utils/persistence'

export type FundTransactionRequest = Uint8Array[]

let funderWallet: NodeWallet
const funderWallets: Record<string, NodeWallet> = {}

export const fundTransactions = async (
event: APIGatewayProxyEvent
Expand All @@ -21,16 +24,31 @@ export const fundTransactions = async (
const requestBody = JSON.parse(event.body!)
validateFundTransactions(requestBody)
const transactions = deserializeTransactions(requestBody)
const isTransactionsValid = await checkTransactions(transactions)
const isTransactionsValid = await checkTransactions(
transactions.map((txWithFunder) => txWithFunder.transaction)
)

if (!isTransactionsValid) {
return asJsonResponse(403, { error: 'Unauthorized transactions' })
}

const wallet = await loadFunderWallet()
const wallets = await loadFunderWallets()

const signedTransactions: VersionedTransaction[] = []

for (const txWithFunder of transactions) {
const funderWallet = wallets[txWithFunder.funder]
if (!funderWallet) {
return asJsonResponse(403, { error: 'Unauthorized funder' })
}

signedTransactions.push(
await funderWallet.signTransaction(txWithFunder.transaction)
)
}

await saveSignedTransactions(getSignatures(signedTransactions))

const signedTransactions = await wallet.signAllTransactions(transactions)
logSignatures(signedTransactions)
return asJsonResponse(
200,
signedTransactions.map((tx) => Buffer.from(tx.serialize()))
Expand All @@ -56,19 +74,18 @@ function validateFundTransactions(transactions: unknown) {
}
}

async function loadFunderWallet(): Promise<NodeWallet> {
if (funderWallet) {
return funderWallet
async function loadFunderWallets(): Promise<Record<string, NodeWallet>> {
if (Object.keys(funderWallets).length > 0) {
return funderWallets
}

const secretData = await getFundingKey()
const funderWalletKey = secretData.key
const secretData = await getFundingKeys()

const keypair = Keypair.fromSecretKey(Uint8Array.from(funderWalletKey))
secretData.forEach((keypair) => {
funderWallets[keypair.publicKey.toBase58()] = new NodeWallet(keypair)
})

funderWallet = new NodeWallet(keypair)
console.log('Loaded funder wallet')
return funderWallet
return funderWallets
}

function getSignature(tx: VersionedTransaction): string {
Expand All @@ -79,10 +96,12 @@ function getSignature(tx: VersionedTransaction): string {
return 'unkown signature'
}

function logSignatures(signedTransactions: VersionedTransaction[]) {
const sigs: string[] = []
function getSignatures(signedTransactions: VersionedTransaction[]) {
const sigs: ClaimSignature[] = []
signedTransactions.forEach((tx) => {
sigs.push(getSignature(tx))
sigs.push({ sig: getSignature(tx), instruction: extractCallData(tx) })
})
console.log(`Signed transactions: ${sigs}`)
console.log(`Signed transactions: ${JSON.stringify(sigs)}`)

return sigs
}
Loading

0 comments on commit 213b12f

Please sign in to comment.