Irys-xyz / arweave-js-sdk

JS SDK for Irys on Arweave
111 stars 96 forks source link

Utils return wrong price ? #58

Closed abrzezinski94 closed 2 years ago

abrzezinski94 commented 2 years ago

Hi runned into issue with uploading imgs im using solana + WebBundlr and randomly getting not enough funds error when trying to upload img. i also tried different functions to get img byte size and getting same results for example Buffer.byteLength(buff). when i add something like 500 bytes "margin" for all imgs it works all the time.

is this some problem with price fcn or im missing something in my code? thanks

code:

export const uploadImage = async (
  imageFile: Buffer,
  cluster: string,
  rpcEndpoint: string,
  wallet: SignerWalletAdapter
) => {
  const bundlr = await initBundlr(cluster, rpcEndpoint, wallet);
  if (!bundlr) return;
  if (imageFile == null) return;

  const loadedBalance = await bundlr.getLoadedBalance();
  const balance = bundlr.utils.unitConverter(loadedBalance.toNumber());
  const balanceNum = balance.toNumber();

  const price = await bundlr.utils.getPrice("solana", imageFile.length);
  const amount = bundlr.utils.unitConverter(price);
  const amountNum = amount.toNumber();

  if (balanceNum < amountNum) {
    await bundlr.fund(Math.ceil((amountNum - balanceNum) * LAMPORTS_PER_SOL));
  }

  const imageResult = await bundlr.uploader.upload(imageFile, {
    tags: [{ name: "Content-Type", value: "image/png" }],
  });

  const arweaveImageUrl = `https://arweave.net/${imageResult.data.id}?ext=png`;

  return arweaveImageUrl;
};
JesseTheRobot commented 2 years ago

So our nodes update their prices every few seconds - so it's rather common for the price users get to be incorrect at the time the node tries to charge their account. as you have noticed, adding a "margin" helps alleviate this issue - we recommend funding about 110% of the item price.