Skip to main content

Provisioning a serverless function

Oyster Serverless is built using the workerd sandbox which is built using the V8 engine. Therefore, it supports JS and WASM bsed workers natively. By extension, any language that compiles/transpiles down to JS or WASM can be used as well.

The testnet deployment of Oyster Serverless currently only supports JS code files that are embedded as service workers in the sandbox. Support for more elaborate and flexible ways of deployment are under development.

Code structure

The worker code should contain one single script file that uses addEventListener to register handlers. The most common handler is the fetch handler that is triggered on every request.

Here is an example that performs factorization of the number provided.

addEventListener('fetch', event => {
event.respondWith(handle(event.request));
});

async function handle(request) {
try {
const data = await request.json();
console.log(data);

let num = data.num;
let factors = [];
let divisor = 2;
if(typeof num != 'number') {
return new Response(`Please provide a valid integer as input in the format {'num':10}`);
}

while (num > 2) {
if (num % divisor === 0) {
factors.push(divisor);
num /= divisor;
} else {
divisor++;
}
}

if(factors.length == 0) {
return new Response('No prime factor for the provided number');
}

return new Response(factors);
} catch(err) {
console.log(err);
return new Response('Please provide a valid JSON input');
}
}

Here is the same example minified so it costs less to provision it.

addEventListener('fetch', event => {event.respondWith(handle(event.request));});async function handle(request){try{const data = await request.json();console.log(data);let num = data.num;let factors = [];let divisor = 2;if(typeof num != 'number'){return new Response(`Please provide a valid integer as input in the format{'num':10}`);}while (num > 2) {if (num % divisor === 0) {factors.push(divisor);num /= divisor;} else {divisor++;}}if(factors.length == 0){return new Response('No prime factor for the provided number');}return new Response(factors);}catch(err){console.log(err);return new Response('Please provide a valid JSON input');}}

Provisioning

Oyster expects the code to be stored as calldata in the 0x44FE06D2940b8782A0a9a9FFD09c65852c0156b1 contract on the Arbitrum Sepolia chain.

Paste the code into the saveCodeInCalldata function and create the transaction.

It should look something like this - https://sepolia.arbiscan.io/tx/0x2c9eb7c03d2062516ca0a828c84b3a87d0365c47f71d326ea196f91031eefcac