MultiversX Tracker is Live!

Chainlink Functions: How can I get array from my JS file and use it in the contract?

Etherum Reddit

More / Etherum Reddit 99 Views

I am trying to use Chainlink Functions, and I have a requirement to actually get values in array form from my JS file and input it in the contract using functions. I have created this code:

// Define the array of uint256 values to be returned const uint256Array = [1, 2, 3]; // Function to encode a uint256 value to a 32-byte Uint8Array function encodeUint256(value) { const buffer = new Uint8Array(32); // Allocate a 32-byte buffer const valueBigInt = BigInt(value); // Write the value as a big-endian 256-bit integer for (let i = 31; i >= 0; i--) { buffer[i] = Number(valueBigInt & BigInt(0xff)); valueBigInt >>= BigInt(8); } return buffer; } // Allocate a buffer large enough to hold all the elements (each uint256 is 32 bytes) const bufferLength = uint256Array.length * 32; const buffer = new Uint8Array(bufferLength); // Write each encoded element into the buffer at the correct offset for (let i = 0; i < uint256Array.length; i++) { const encodedElement = encodeUint256(uint256Array[i]); buffer.set(encodedElement, i * 32); } // Return the encoded array as the function result return buffer; 

This function is returning the correct byte as expected.

Smart Contract:

function fulfillRequest( bytes32 requestId, bytes memory response, bytes memory err ) internal override { s_lastError = err; if (response.length > 0) { uint256[] memory array = abi.decode(response, (uint256[])); influencerShare[1] = array; } } 

Now I am sure my code is not right. How can I fetch the array from the functions, and use it in my contract? Any help would be good.

submitted by /u/shubhamskatel
[link] [comments]
Get BONUS $200 for FREE!

You can get bonuses upto $100 FREE BONUS when you:
πŸ’° Install these recommended apps:
πŸ’² SocialGood - 100% Crypto Back on Everyday Shopping
πŸ’² xPortal - The DeFi For The Next Billion
πŸ’² CryptoTab Browser - Lightweight, fast, and ready to mine!
πŸ’° Register on these recommended exchanges:
🟑 Binance🟑 Bitfinex🟑 Bitmart🟑 Bittrex🟑 Bitget
🟑 CoinEx🟑 Crypto.com🟑 Gate.io🟑 Huobi🟑 Kucoin.



Comments