Skip to content

Instantly share code, notes, and snippets.

@shortthefomo
Last active January 17, 2023 00:45
Show Gist options
  • Save shortthefomo/fd55173ab29ea5e9dd210f33258826f7 to your computer and use it in GitHub Desktop.
Save shortthefomo/fd55173ab29ea5e9dd210f33258826f7 to your computer and use it in GitHub Desktop.
pathing-example.js
// to start off call pathFind() that will get all paths to what you define in the command aka what currency you want ouy.
// then you will need to render a button for each paths result (commented below) I use vue so can get boiler plate as well for that but wietses example where i took it from
// so when user selects pair it then calls into makepathingPayment(path) with the path they are paying with.
rand(size) {
return [...Array(size)]
.map(() => Math.floor(Math.random() * 16).toString(16))
.join('');
},
async pathFind() {
if (this.lastID == null) {
this.lastID = this.rand(6)
}
let issuer = 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B'
const command = {
id: this.lastID,
command: 'path_find',
subcommand: 'create',
source_account: this.$store.getters.getAccount,
destination_account: import.meta.env.VITE_APP_PAYMENT_ADDRESS,
destination_amount: {
value: import.meta.env.VITE_APP_PRO_AMOUNT,
currency: import.meta.env.VITE_APP_PRO_CURRENCY,
issuer: issuer,
}
}
this.command.destination_amount = {
value: import.meta.env.VITE_APP_PRO_AMOUNT,
currency: import.meta.env.VITE_APP_PRO_CURRENCY,
issuer: issuer,
}
const results = await client.send(command)
console.log('pfcall', { command, results })
const self = this
if (this.listener == null) {
this.listener = this.client.on('path', (path) => {
// console.log('Async path', path)
if (path.destination_amount) {
if (path.id === self.lastID) {
if ('alternatives' in path) {
self.paths = path.alternatives // this needs to become a reference with the "path" in a button to the function below
}
else {
self.paths = []
}
console.log('found paths', self.paths)
} else {
// Old command, close
self.client.send({
id: path.id,
command: 'path_find',
subcommand: 'close',
})
}
}
})
}
},
async makepathingPayment(path) {
console.log('requestProService', path)
const memos = [{
Memo: {
MemoData: Buffer.from('Purchase pro service on three via pathing.', 'utf-8').toString('hex').toUpperCase(),
}
}]
const XrplPayload = {
TransactionType: 'Payment',
Destination: import.meta.env.VITE_APP_PAYMENT_ADDRESS,
Account: this.$store.getters.getAccount,
SendMax: path.source_amount,
DeliverMin: {
value: import.meta.env.VITE_APP_PRO_AMOUNT,
currency: import.meta.env.VITE_APP_PRO_CURRENCY,
issuer: this.command.destination_amount.issuer,
},
Amount: {
value: import.meta.env.VITE_APP_PRO_AMOUNT,
currency: import.meta.env.VITE_APP_PRO_CURRENCY,
issuer: this.command.destination_amount.issuer,
},
Paths: path.computed,
Flags: 131072,
Memos: memos
}
const XummPayload = {
'user_token': this.$store.getters.getUserToken,
'txjson': XrplPayload,
'pathfinding': true,
custom_meta: {
blob: 'Purchase pro service on three via users xumm wallet via pathing'
}
}
console.log('command', XummPayload)
const payload = await Sdk.payload.createAndSubscribe(XummPayload, async event => {
console.log('New payload event:', event.data)
if (event.data.signed === true) {
console.log('Woohoo! The sign request was signed :)')
return event.data
}
if (event.data.signed === false) {
console.log('The sign request was rejected :(')
return false
}
})
console.log('payload', payload)
xapp.openSignRequest({ uuid: payload.created.uuid })
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment