Most projects I know that revolves around Blinks or to develop and deploy Blinks. But did you know that apart from Next.js, you can actually build Blinks on your favorite Node backend? In this article, we'll use
Dialect Blinks
Blockchain Links, or Blinks turn any .
What are Solana Actions?
.
What is SST?
Serverless Stack (SST) is a framework that makes it easy to build modern full-stack applications on your own infrastructure .
If you want to know more about how Blinks work, you can check out the installed on your machine.
AWS Account
Project Setup
If you want to check the reference code, you can check it out !
Configure AWS CLI
SST uses the AWS CLI to deploy your project. Make sure you have the AWS CLI installed and configured to your AWS account as SST will deploy the resources there you can read more .
Within
links.actions, it specifies an array of actions that can be performed. In this case, we have a list of donation amounts (1, 5, 10) and a custom donation amount.
Every action has a corresponding
hrefthat points to the API endpoint that will handle the action.
The
getfunction returns the metadata of the action as well as the CORS headers.
The
optionsfunction is a simple copy of thegetfunction. It is used to handle the preflight request for CORS.
Configure the endpoint insst.config.ts
CODEexport default $config({
app(input) {
return {
name: 'sst-blinks',
removal: input?.stage === 'production' ? 'retain' : 'remove',
home: 'aws',
};
},
async run() {
const api = new sst.aws.ApiGatewayV2('Actions');
api.route('GET /api/donate', {
handler: 'src/donate.get',
});
api.route('OPTIONS /api/donate', {
handler: 'src/donate.options',
});
},
});
Upon initializing
sstin your project, you will have a minimal config ofsst.
What we've added is in the
runfunction where we create an API Gateway with the nameActionsand add two routes:
Method
url
GET
/api/donate
OPTIONS
/api/donate
This means that the API will have two endpoints that will handle the
GETandOPTIONSrequests.
Run the command
CODE# Development mode
npx sst dev
SST may take a while to deploy the resources on your AWS Account, but once it is successful, it will output the URL of the API.
https://<api-id>.execute-api.<region>.amazonaws.com/<api-endpoint>
Test the blink
You can check the blink by going to
You will see a warning that the actions has not yet been registered. That is normal as Dialect requires Blinks to be registered first before using it on different websites for security purposes.
Creating the POST donate endpoint
Now that we have the GET and OPTIONS endpoints, let's create the POST endpoint that will handle the donation.
CODEexport const post: Handler = async (event: APIGatewayProxyEvent, context) => {
const amount = event.pathParameters?.amount ?? DEFAULT_DONATION_AMOUNT_SOL.toString();
const body = await JSON.parse(event.body || '{}');
let account;
try {
account = new PublicKey(body.account);
} catch (error) {
return {
statusCode: 400,
body: 'Invalid account',
headers: ACTIONS_CORS_HEADERS,
};
}
const parsedAmount = parseFloat(amount);
const transaction = await prepareDonateTransaction(
new PublicKey(account),
new PublicKey(DONATION_DESTINATION_WALLET),
parsedAmount * LAMPORTS_PER_SOL,
);
const response = await createPostResponse({
fields: {
type: 'transaction',
transaction: transaction,
},
});
return {
statusCode: 200,
body: JSON.stringify(response),
headers: ACTIONS_CORS_HEADERS,
};
};
Code walkthrough
- We first get the
amountfrom the URL path parameters. If it is not present, we use the default donation amount (1 SOL).
- We then parse the body of the request to get the
accountof the user.
- After that, we prepare the transaction using the
prepareDonateTransactionfunction.
- The
prepareDonateTransactionfunction is a custom function that prepares the transaction to send the donation to the wallet address. For further details, check the docs .
Configureactions.jsonendpoint insst.config.ts
CODEasync run() {
const api = new sst.aws.ApiGatewayV2('Actions');
api.route('GET /api/donate', {
handler: 'src/donate.get',
});
api.route('OPTIONS /api/donate', {
handler: 'src/donate.options',
});
api.route('POST /api/donate/{amount}', { handler: 'src/donate.post' });
api.route('GET /actions.json', { handler: 'src/actions.get' });
api.route('OPTIONS /actions.json', { handler: 'src/actions.options' });
},
Finally, our API now has five endpoints:
Method
url
GET
/api/donate
OPTIONS
/api/donate
POST
/api/donate/{amount}
GET
/actions.json
OPTIONS
/actions.json
Deploy to Production
Deploying on production with SST is easy. Just run the following command:
CODEnpx sst deploy --stage production
It will output a new URL that you can use to test your blink.
Demo
Donating 1 SOL
Cleanup
Removing the resources is as easy as deploying them. Just run the following command:
CODEnpx sst remove # to remove the resources in the development stage
npx sst remove --stage production # to remove the resources in the production stage
Conclusion
And that's it! You've successfully deployed your Blinks on AWS using SST. You can now create more Blinks and deploy them on AWS with ease!
Feel free to create a git repository and push your code to GitHub!
If you have any questions or found any issues, feel free to comment below.
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
SOCIAL SHARE CARD GENERATOR