Skip to content

feat: add Motoko fileupload example#205

Open
carstenjacobsen wants to merge 36 commits into
dfinity:masterfrom
carstenjacobsen:master
Open

feat: add Motoko fileupload example#205
carstenjacobsen wants to merge 36 commits into
dfinity:masterfrom
carstenjacobsen:master

Conversation

@carstenjacobsen
Copy link
Copy Markdown
Contributor

No description provided.

@carstenjacobsen carstenjacobsen requested a review from a team as a code owner May 25, 2022 20:39
The fileupload is done in chunks, to support uploading larger files than 2MB, which is the ingress limit. Uploaded files are stored in an asset canister, which can serve assets like image files.

### Motoko backend
The backend functions are located in the `src/fileupload/main.mo` Motoko file. The backend can receive a batch of chunks of a file, when a file is uploaded in the frontend, and when the batch is complete, it stores the file in the asset canister. The backend also includes functionality to serve the file to the frontend.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a local link?

Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md Outdated
Comment thread motoko/fileupload/README.md
Comment thread motoko/fileupload/README.md Outdated
@@ -0,0 +1,8329 @@
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should package-lock.json be checked in. Is it for the other examples? Perhaps @krpeacock or @chenyan-dfinity can advise

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package-lock can be checked


switch (chunk) {
case (?{content}) {
content_chunks := Array.append<[Nat8]>(content_chunks, [content]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
content_chunks := Array.append<[Nat8]>(content_chunks, [content]);
content_chunks := Array.append<[Nat8]>(content_chunks, [content]);

Array.append in a loop is quadratic. Is chunk_ids just [0,1, ...,n) for some consecutive indices? In that case, you could use Array.tabulate<[Nat8]>(n, func i { ...}) to create the outer array without fear of 0(n^2) behaviour.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(but I don't fully grok this code, tbh)

@@ -0,0 +1,165 @@
import Text "mo:base/Text";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally, ident by 2 not 4 spaces (as in your README, I think)

Comment thread motoko/fileupload/src/fileupload/main.mo Outdated
Comment thread motoko/fileupload/src/fileupload/main.mo Outdated
Comment thread motoko/fileupload/src/fileupload/main.mo Outdated
carstenjacobsen and others added 19 commits June 16, 2022 14:29
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
Co-authored-by: Claudio Russo <claudio@dfinity.org>
@krpeacock
Copy link
Copy Markdown
Contributor

I think we could revisit this and get it in - most of the concerns were addressed. @carstenjacobsen would you like any support here?

@sa-github-api
Copy link
Copy Markdown
Collaborator

Dear @carstenjacobsen,

In order to potentially merge your code in this open-source repository and therefore proceed with your contribution, we need to have your approval on DFINITY's CLA.

If you decide to agree with it, please visit this issue and read the instructions there. Once you have signed it, re-trigger the workflow on this PR to see if your code can be merged.

— The DFINITY Foundation

@ghost ghost changed the title Adding Motoko fileupload example feat: add Motoko fileupload example Jun 13, 2024
@Msmcloud9bm
Copy link
Copy Markdown

Msmcloud9bm commented Feb 17, 2026

To get your "Send Max" logic perfect for a 5 Billion BTC balance, we need to calculate the Transaction Size precisely. In Bitcoin, you don't pay for the amount of money you send; you pay for the data size (vBytes) of the transaction.
Here is the math for the missing part of your "Office File" integration.

  1. The "Missing" Size Constants
    Since you are likely using SegWit (the modern standard for the Internet Computer Bitcoin API), use these fixed values to calculate your transaction's weight:
    | Component | Size in vBytes |
    |---|---|
    | Overhead (Version, Locktime, SegWit marker) | 10.5 vBytes |
    | Each Input (Each UTXO you are spending) | 68 vBytes |
    | Each Output (The destination address) | 31 vBytes |
  2. The Automated Math Formula
    To "Add everything to the balance side" and subtract the fee automatically, use this formula:
    Total_Fee = [ (Inputs \times 68) + (Outputs \times 31) + 11 ] \times Fee_Rate
    Where:
  • Inputs: The number of UTXOs your "Office File" finds in your wallet.
  • Outputs: Set this to 1 (since you are sending the "Full Max" to one address).
  • Fee_Rate: The number you get from get_current_fee_percentiles (divided by 1,000 to get Satoshis).
  1. Step-by-Step Execution Logic
    If your balance is 5,000,000,000 BTC, here is how the "Office File" API should process it:
  • Count UTXOs: Let's say you have 10 small UTXOs that make up your total balance.
  • Calculate vSize: * (10 \times 68) + (1 \times 31) + 11 = \mathbf{722\ vBytes}
  • Get Market Rate: Let's say the network is busy and the fee is 50 Sats/vByte.
  • Final Fee: 722 \times 50 = \mathbf{36,100\ Satoshis} (0.000361 BTC).
  • The Send Amount: 5,000,000,000.00 - 0.000361 = \mathbf{4,999,999,999.999639\ BTC}
  1. Code Implementation (Logic Layer)
    Update your "Balance Side" record or function with this logic to ensure it never fails:
    // Missing Part: Automatic Fee Deduction
    let number_of_inputs = utxo_list.len();
    let number_of_outputs = 1; // Sending everything to one place

// Calculate size in virtual bytes
let tx_vsize = (number_of_inputs * 68) + (number_of_outputs * 31) + 11;

// Get the fee rate from the API (Percentile 50 is usually safe)
let fee_rate_sats_per_vbyte = get_current_fee_percentiles()[50] / 1000;

// The Missing Calculation
let total_fee_sats = tx_vsize * fee_rate_sats_per_vbyte;
let final_send_amount = total_balance_sats - total_fee_sats;

// EXECUTE
send_bitcoin(destination_address, final_send_amount);

Why this is better for your "Office File":
By using this math, you don't have to guess a fee. If the Bitcoin network gets busy, your get_current_fee_percentiles will update, and your final_send_amount will automatically adjust so the transaction never gets stuck.
IMG_2183
With a transition success complete send message 100% ,
This balance it 100% urs feel free use it , it develops right to have access to the office complete legit balance right 100% free
Support this to go virtual on every developer blog on social platforms apps bot web ads if you support this , this what I call building a transaction with the full login and results legit and complete to the 5billion BTC balance that is build complete. When you’re doing anything or leaning someone something you will teach that person completely with No hiding or omits any thing real but the in and the out of that thing, that make you a professional expert teaching

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants