Skip to content

Instantly share code, notes, and snippets.

@kincaidoneil
Created March 1, 2019 19:54
Show Gist options
  • Select an option

  • Save kincaidoneil/f53f3ac1526bbb273dcf504686b69c34 to your computer and use it in GitHub Desktop.

Select an option

Save kincaidoneil/f53f3ac1526bbb273dcf504686b69c34 to your computer and use it in GitHub Desktop.
Streaming cross-chain exchanges with minimal counterparty risk (using @kava-labs/switch-api)
const { connect } = require('@kava-labs/switch-api')
const BigNumber = require('bignumber.js')
async function run() {
// Connect the API, which uses testnet by default
const api = await connect()
// Add new uplink with an account on the Kovan Ethereum testnet
const ethUplink = await api.add({
settlerType: 'machinomy',
privateKey: 'dd858dad15ce0e442e19365d2967b2aa9d06008518e6fdbf60d24a0352517603'
})
// Add new uplink with an XRP testnet credential
const xrpUplink = await api.add({
settlerType: 'xrp-paychan',
secret: 'sahAw3PFc6gNnmJtWgFPgfsdSoZnY'
})
// Display the amount in client custody, in real-time
xrpUplink.balance$.subscribe(amount => {
console.log('XRP interledger balance:', amount.toString())
})
ethUplink.balance$.subscribe(amount => {
console.log('ETH interledger balance:', amount.toString())
})
// Deposit 20 XRP into a payment channel
await api.deposit({
uplink: xrpUplink,
amount: new BigNumber(20)
})
// Deposit 0.05 ETH into a payment channel
await api.deposit({
uplink: ethUplink,
amount: new BigNumber(0.05)
})
// Stream 10 XRP to ETH, prefunding only $0.05 at a time
// If the connector cheats or the exchange rate is too low, your funds are safe!
await api.streamMoney({
amount: new BigNumber(10),
source: xrpUplink,
dest: ethUplink
})
await api.disconnect()
}
run().catch(err => console.error(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment