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.
[link] [comments]
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