egg_thebest
๐Ÿ“Ž 1 file 7,359 chars
๐Ÿ“„
pasted-code-1787342219824.txt
txt 429 lines
โ–ถ
Build a complete Discord LTC middleman/escrow bot in Python using `discord.py`.

The bot will eventually be hosted on Railway.

IMPORTANT:

* Build Litecoin TESTNET support first.
* Do not use real LTC during development.
* Never store or request seed phrases or private keys.
* Never hardcode API keys, bot tokens, passwords, or wallet credentials.
* Use environment variables for secrets.
* Use Discord User IDs internally, not usernames.
* All permissions and trade-state checks must be performed server-side.

## TRADE FLOW

### 1. Request LTC

Create a **Request LTC** button.

When clicked:

* Create a private ticket.
* The ticket is visible to the ticket creator, the other participant, and staff.
* Ask the creator for:

  * The other Discord user's username/ID
  * What the creator is trading
  * What the other user is giving
  * LTC amount
* Check that the other user is currently in the Discord server.

### 2. Confirmation

Show both users:

**Trade Information**

User 1: @User1
User 2: @User2

User 1 gives:
`...`

User 2 gives:
`...`

LTC amount:
`...`

Buttons:

**โœ… Confirm**

**โŒ Incorrect**

Both users must click Confirm.

If either user clicks Incorrect:

* Reset the confirmations.
* Allow the ticket creator to enter the information again.
* Require both users to confirm again.

Do not continue until both users confirm.

### 3. Sender / Receiver

After both users confirm, ask:

**Who is the LTC sender?**

[User 1] [User 2]

The person not selected becomes the receiver.

Store:

* `sender_id`
* `receiver_id`

Prevent both users from becoming sender.

### 4. LTC Deposit

After identifying the sender:

Create a unique Litecoin TESTNET deposit address for the trade using an appropriate wallet/payment API or Litecoin node.

Show:

**๐Ÿ’ฐ LTC Deposit**

Amount required:
`X LTC`

Deposit address:
`ADDRESS`

Waiting for payment...

Each trade must have a unique deposit address or another reliable way to associate the transaction with the correct trade.

### 5. Detect Payment

Monitor the Litecoin TESTNET blockchain.

Use a WebSocket if supported, with polling as a fallback.

Detect:

* Transaction ID
* Amount received
* Deposit address
* Confirmation count
* Timestamp

Do not trust screenshots or user-submitted transaction IDs.

When a payment is detected:

**๐Ÿ’ฐ Payment detected**

Amount:
`X LTC`

Transaction:
`TXID`

Confirmations:
`0/X`

Wait until the required number of confirmations is reached.

### 6. Confirm Deposit

Once confirmed:

**โœ… LTC deposit confirmed**

Amount secured:
`X LTC`

Transaction:
`TXID`

Only then allow the trade to continue.

### 7. Trade Instructions

Display:

1. Send the item to the sender.
2. Once the sender has received and verified the item, the sender must click **Release LTC**.
3. The receiver will then enter an LTC address.
4. The escrow system will send the LTC to the receiver.
5. If the sender does not release the LTC, users should ping staff for assistance.

### 8. Release LTC

Create:

**๐Ÿ”“ Release LTC**

IMPORTANT:

Only `sender_id` can use this button.

The receiver must NOT be able to click it.

If the receiver attempts:

**โŒ You are not authorized to release this trade.**

When the sender clicks Release:

* Verify the Discord user ID.
* Verify the deposit is confirmed.
* Verify the trade is active.
* Verify the trade hasn't already been released.
* Prevent double release.
* Ask for final confirmation:

**Are you sure you want to release the LTC?**

[Confirm Release]

[Cancel]

### 9. Receiver LTC Address

After the sender confirms release:

Only the receiver can use:

**๐Ÿ’ฐ Enter LTC Address**

The sender must not be able to use this button.

The receiver enters their LTC payout address.

Validate the address.

Then show:

Receiver address:
`ADDRESS`

Amount:
`X LTC`

[Confirm]

[Change Address]

Only the receiver can confirm.

### 10. LTC Payout

After the receiver confirms:

Verify:

* Trade exists
* Trade is active
* Sender authorized release
* Deposit is confirmed
* Receiver address is valid
* Trade has not already been paid
* No payout is currently running

Send the LTC using the configured secure wallet/payment provider.

Record:

* Amount
* Destination address
* Transaction ID
* Timestamp
* Status

Then display:

**๐Ÿ’ธ LTC sent**

Amount:
`X LTC`

Transaction:
`TXID`

Mark the trade as completed.

Never allow a trade to pay twice.

## DATABASE

Use PostgreSQL because the bot will be hosted on Railway.

Create tables for:

* Users
* Trades
* Wallet addresses
* Deposits
* Payouts
* Trade events/audit logs

A trade should contain:

* Trade ID
* Creator ID
* User 1 ID
* User 2 ID
* Sender ID
* Receiver ID
* Item information
* LTC amount
* Deposit address
* Deposit transaction ID
* Confirmation count
* Receiver payout address
* Payout transaction ID
* Status
* Created timestamp
* Updated timestamp

## TRADE STATES

Use a strict state machine:

`CREATED`

โ†’ `INFORMATION_SUBMITTED`

โ†’ `AWAITING_CONFIRMATION`

โ†’ `ROLES_SELECTED`

โ†’ `AWAITING_DEPOSIT`

โ†’ `DEPOSIT_DETECTED`

โ†’ `LTC_CONFIRMED`

โ†’ `TRADE_IN_PROGRESS`

โ†’ `RELEASE_REQUESTED`

โ†’ `AWAITING_PAYOUT_ADDRESS`

โ†’ `PAYOUT_ADDRESS_CONFIRMED`

โ†’ `LTC_SENT`

โ†’ `COMPLETED`

Also support:

`CANCELLED`

`FAILED`

`EXPIRED`

`DISPUTED`

Users must not be able to skip trade states.

## STAFF SYSTEM

Create a configurable staff role.

Staff can:

* View trades
* View deposit transaction IDs
* View payout transaction IDs
* View payment status
* Cancel trades
* Lock trades
* Mark trades as disputed
* Assist users
* View audit logs

Every staff action that changes a trade must be logged.

## SECURITY

Implement:

* Discord User ID authorization
* Private ticket permissions
* Server-side button validation
* Trade-state validation
* Double-release protection
* Double-payout protection
* Duplicate transaction protection
* Database transactions/locking
* LTC address validation
* Rate limiting
* WebSocket reconnect handling
* Error handling
* Audit logs
* Trade expiration

Never trust client-side UI state.

## RAILWAY

Make the project compatible with Railway.

Include:

* `requirements.txt`
* `.env.example`
* `.gitignore`
* `README.md`
* Railway deployment instructions

Use environment variables such as:

`DISCORD_BOT_TOKEN`

`DATABASE_URL`

`LTC_NETWORK=testnet`

`LTC_API_KEY`

`LTC_RPC_URL`

`LTC_RPC_USER`

`LTC_RPC_PASSWORD`

`STAFF_ROLE_ID`

`CONFIRMATIONS_REQUIRED=2`

Only include credentials that the selected LTC provider actually requires.

## IMPORTANT DEVELOPMENT REQUIREMENT

Do not simply generate a basic Discord bot and pretend the blockchain functionality works.

Build the actual architecture for:

Discord bot โ†’ Database โ†’ Litecoin blockchain monitoring โ†’ Escrow state โ†’ Secure LTC payout.

If a Litecoin provider/API is required, clearly tell me which one you selected and why.

Keep the Litecoin functionality separate from the Discord functionality so it can be replaced later.

Build and test everything with Litecoin TESTNET first.

At the end, provide clear instructions for:

1. Creating the Discord application
2. Adding the bot token
3. Installing dependencies
4. Setting environment variables
5. Creating the Railway PostgreSQL database
6. Deploying to Railway
7. Setting up Litecoin TESTNET
8. Getting testnet LTC
9. Testing a complete trade

Do not enable real-LTC payouts until the complete testnet system has been successfully tested.
Kodari AI
KodariAug 21 at 8:03 pm 3
  1. Build the testnet-only Litecoin Core RPC adapter and strict escrow state/database layer.

  2. Add the Discord ticket, confirmation, sender/receiver, deposit, release, and payout workflows.

  3. Add staff controls, background blockchain monitoring/expiration, Railway configuration, and deployment documentation.

  1. Built a complete Litecoin TESTNET Discord escrow bot with:

    • Private trade tickets

    • Discord ID-based participant validation

    • Trade confirmation and reset flow

    • Sender/receiver selection

    • Unique Litecoin Core testnet deposit addresses

    • RPC-based deposit monitoring and confirmation tracking

    • Sender-only LTC release authorization

    • Receiver-only payout address entry

    • Litecoin Core payout execution

    • Double-release and double-payout protection

    • Strict server-side trade state validation

  2. Added PostgreSQL persistence for:

    • Users

    • Trades

    • Wallet addresses

    • Deposits

    • Payouts

    • Audit events

  3. Added staff functionality:

    • /trade_status

    • /trade_cancel

    • /trade_dispute

    • /trade_lock

    • /trade_audit

  4. Added background services for:

    • Deposit polling through Litecoin Core JSON-RPC

    • Confirmation tracking

    • Trade expiration

    • Persistent Discord views after bot restarts

  5. Added Railway and deployment files:

    • requirements.txt

    • .env.example

    • .gitignore

    • README.md

    • PostgreSQL initialization

    • Litecoin TESTNET setup instructions

    • Discord application setup

    • Railway deployment steps

    • Full end-to-end testing instructions

The selected Litecoin provider is Litecoin Core JSON-RPC because it supports wallet-controlled testnet addresses, transaction detection, address validation, and payouts without storing private keys in the bot.

Kodari can make mistakes. Review important code before using.
Files
blockchain
.gitignore
bot.py
config.py
database.py
main.py
models.py
README.md
requirements.txt
service.py
ui.py
Code
13 1,162
main.py
import logging

from bot import EscrowBot, register_commands
from blockchain.litecoin_rpc import LitecoinRpcProvider
from config import Settings
from database import Database
from service import EscrowService


def build_bot() -> EscrowBot:
    settings = Settings.from_env()
    database = Database(settings.database_url)
    service = EscrowService(database.sessions, settings.confirmations_required)
    provider = LitecoinRpcProvider(settings.ltc_rpc_url, settings.ltc_rpc_user, settings.ltc_rpc_password)
    bot = EscrowBot(settings, database, service, provider)
    register_commands(bot)
    return bot


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
    build_bot().run(Settings.from_env().discord_token)