Skip to content

Instantly share code, notes, and snippets.

@justmoon
Last active July 12, 2016 23:53
Show Gist options
  • Save justmoon/6ca30358d4ad9385cdf3d2827fbffb9a to your computer and use it in GitHub Desktop.
Save justmoon/6ca30358d4ad9385cdf3d2827fbffb9a to your computer and use it in GitHub Desktop.
Comparing ILP Receiver Event API Options

With receive:

client.on('receive', (transfer) => {
  // Check amount, etc.
  if (transfer.condition) {
    // Try to fulfill the condition or throw if unable
  }
  // Process the payment
})

With transfer_prepare, transfer_execute:

client.on('transfer_prepare', (transfer) => {
  if (transfer.direction === 'incoming') {
    // Check amount, etc.
    // Try to fulfill the condition or throw if unable
    // Process the payment
  }
})

client.on('transfer_execute', (transfer) => {
  if (transfer.direction === 'incoming' && !transfer.condition) {
    // Check amount, etc.
    // Process the payment
  }
})

By splitting receive into two different events I now have two different entry points to worry about. One of those entry points groups another event (Universal payments being fulfilled) that I don't care about, so I have to go back and filter that out.

I could also do this:

client.on('transfer_prepare', (transfer) => {
  if (transfer.direction === 'incoming') {
    // Check amount, etc.
    // Try to fulfill the condition or throw if unable
  }
})

client.on('transfer_execute', (transfer) => {
  if (transfer.direction === 'incoming') {
    // Check amount, etc.
    // Process the payment
  }
})

But now I'm loading all of the context from the database twice for universal payments which is probably not very efficient. (Once when I get the prepare, then I throw it all away and load it again for the execution.)

With all four events:

client.on('transfer_prepare', (transfer) => {
  if (transfer.direction === 'incoming') {
    // Check amount, etc.
    // Try to fulfill the condition or throw if unable
    // Process the payment
  }
})

client.on('transfer_simple', (transfer) => {
  if (transfer.direction === 'incoming') {
    // Check amount, etc.
    // Process the payment
  }
})

Here I still have the duplication, but at least I don't have to filter out the stuff I don't care about.

Overall, as a receiver or connector, I like the receive option best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment