Skip to content

Conversation

@ross-spencer
Copy link
Member

@ross-spencer ross-spencer commented Apr 9, 2024

Example: https://arweave.app/tx/L2TJ1A7HsdOqJMpMi75dPavHfSgftQVW2fanTluHP88

http://137.184.172.9:8000/docs#/packaging/create_transaction_create_transaction__post

Example cURL:

curl -X 'POST' \
  'http://137.184.172.9:8000/create_transaction/?mime_type=image%2Fpng' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F '[email protected];type=application/json' \
  -F '[email protected];type=image/png' \
  -F 'tags={
    "tags": [
        {
        "name": "Original filename",
        "value": "test_nft_0002.png"
        },
        {
        "name": "tag_name_2",
        "value": "tag_value_2"
        },
        {
        "name": "tag_name_3",
        "value": "tag_value_3"
        }
    ]
}'

Code to leverage to turn into a CLI (e.g. modify arweave_api.py to remove fastapi entry points for our own custom args):

@app.post("/create_transaction/", tags=[TAG_ARKLY])
async def create_transaction(
    wallet: UploadFile,
    file: list[UploadFile] = File(...),
    mime_type: str = None,
    tags: Tags | None = None,
):
    """Create an Arkly package and Arweave transaction."""
    return await primary_functions._create_transaction(
        wallet=wallet,
        files=file,
        mime_type=mime_type,
        tags=tags,
    )
async def _create_transaction(
    wallet: UploadFile,
    files: list[UploadFile] = File(...),
    mime_type: str = None,
    tags: Tags = None,
) -> dict:
    """Create an Arkly package and Arweave transaction.

    We do so as follows:
        - Create a folder for the wallet user to place their uploads in
          as well as any additional folders and metadata required.
        - Create a bagit file from that folder.
        - Compresses and packages uploaded files into .tar.gz files.
        - Uploads the compressed tarball to Arweave for the current
          Arweave price.
    """
    wallet = await create_temp_wallet(wallet)
    if wallet == ERR_WALLET:
        return {"transaction_id": "Error creating transaction."}
    if wallet.balance <= 0:
        return {"transaction_id": f"Error: wallet balance is: {wallet.balance}"}
    if not files:
        return {"transaction_id": "Error: no files selected for upload"}

    tag_list = []
    try:
        # Process user-defined tags.
        tag_list = tags.tags
    except AttributeError:
        logger.info("no user-defined tags provided by caller")

    logger.info("Wallet balance before upload: %s", wallet.balance)

    tmp_dir = tempfile.mkdtemp()
    file_path = Path(tmp_dir, "upload_path")
    file_path.mkdir()

    logger.info("Location to write object to: %s", file_path)

    output_file = None
    for file in files:
        read_file = await file.read()
        output_file = Path(file_path, file.filename)
        output_file.write_bytes(read_file)
        break

    logger.info("attempting to upload: %s", output_file)

    with open(output_file, "rb", buffering=0) as file_handler:
        new_transaction = Transaction(
            wallet, file_handler=file_handler, file_path=output_file,
        )
        # Default tags for the tar/gzip file that we create.
        new_transaction.add_tag("Content-Type", mime_type)
        for tag in tag_list:
            logger.info("Adding tag: %s: %s", tag.name, tag.value)
            new_transaction.add_tag(tag.name, tag.value)

        new_transaction.sign()
        uploader = get_uploader(new_transaction, file_handler)
        while not uploader.is_complete:
            uploader.upload_chunk()

    logger.info("Finished uploading to Arkly!")
    tx_status = new_transaction.get_status()
    logger.info("Transaction status: %s", tx_status)
    logger.info("Transaction ID: %s", new_transaction.id)
    logger.info("New wallet balance: %s", wallet.balance)
    return {
        "transaction_id": f"{new_transaction.id}",
        "transaction_link": f"{ARWEAVE_VIEW_BASEURL}/tx/{new_transaction.id}",
        "transaction_status": f"{tx_status}",
        "wallet_balance": f"{wallet.balance}",
    }

@ross-spencer ross-spencer force-pushed the dev/generic-arweave branch from a7e2880 to aa015ee Compare April 9, 2024 20:52
@ross-spencer ross-spencer changed the title WIP: Generic arweave capability WIP: Generic arweave capability (NOT TO BE MERGED) Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants