0

Once a transaction has been completed, the application I'm building needs to send a POST to my API.I believe that posting on chain is the most secure approach to go about it (instead of posting from the front). Could I achieve this using an oracle in some way? I know that it is not recommended mixing off-chain things inside the on-chain porgrams but I can't think of any other way.

Thanks in advance :)

2 Answers 2

2

Short answer is no, you can't make any external connections from an on-chain application.

Long answer, you can emit an event on-chain, and you can have an external application listen for on-chain events that match the ix you are expecting to POST after, and you can read the properties of that ix as needed. There's no guarantee your listener will check the ix within a certain time frame. It'll also be expensive to listen constantly and often, and there's no great solution at this time.

For example you can use https://github.com/coral-xyz/anchor/blob/master/tests/events/tests/events.js, but if your server running this goes down, you'll miss all the events that happened while it was down.

So instead you might use getSignaturesForAddress to scan all the transactions sent with your program, filter those by ones you haven't processed and then by the event you want. You can getTransaction and read the logMessages for your event, then extract the info you need for your POST from other Transaction data loaded with getTransaction. This is also slow and expensive, and only gets slower the more your application gets used.

-1

Yes I guess it is possible to make an HTTP POST request inside a solana smart contract but also on chain programs on solana do not have access to internet so it will require you to use an off-chain worker to make HTTP POST request. the worker you make off chain can be activated by the on-chain program using a message event sent by the Solana Network. also you should be careful considering the security implications of making an HTTP POST request inside an on-chain program. the off-chain worker could be vulnerable hence an attacker could use that as an advantage.


Node_Fetch Sample Code

import fetch from 'node-fetch';

const solanaUrl = 'https://api.solana.com';

const response = await fetch(solanaUrl, {
  method: 'POST',
  body: JSON.stringify({
    someData: 'data', //add some data here
  }),
  headers: {
    'Content-Type': 'application/json',
  },
});

const data = await response.json();
5
  • Thanks @mamedaltechie.sol :) And do you know which library I can use for this?
    – cris.sol
    Commented Dec 16, 2022 at 13:28
  • you can make use of node_fetch library for that Click to use Node_Fetch Library Commented Dec 16, 2022 at 15:21
  • I need a rust library that can be compiled for solana programs using cargo-build-bpf. node-fetch lib is used for doing this off-chain.
    – cris.sol
    Commented Dec 16, 2022 at 15:45
  • but you can add node_fetch library to your rust [dependencies] node-fetch = "0.11.1" // In your Rust code use node_fetch::{Fetch, RequestInit}; Commented Dec 16, 2022 at 16:02
  • node-fetch library does not exist in crates. I have tried with the reqwest library but it is not supported by the solana compiler. I would like to know if there is any library to make post that is supported by the solana compiler.
    – cris.sol
    Commented Dec 16, 2022 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.