Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update snippets to be compatible with xrpl.js #1761

Merged
merged 33 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
1ca94e0
update getTransaction snippet
khancode Oct 7, 2021
4e586c6
update paths snippet
khancode Oct 7, 2021
ac39842
add payment logic to paths.ts
khancode Oct 12, 2021
5be03a4
Merge branch 'develop' into update-snippets
khancode Oct 12, 2021
483b4b7
use new wallet sign function
khancode Oct 12, 2021
cd4c6be
Merge branch 'develop' into update-snippets
khancode Oct 18, 2021
2301334
change void to await
khancode Oct 18, 2021
8650385
export TransactionMetadata to top level of package
khancode Oct 18, 2021
e7663d9
add comment describing why delivered_amount should be printed
khancode Oct 19, 2021
ef327b8
use fundWallet
khancode Oct 19, 2021
4cca6ff
put destination_account & destination_amount into separate constants
khancode Oct 19, 2021
9ceaf70
make method name more descriptive in paths snippet
khancode Oct 19, 2021
bbfdfc0
Merge branch 'develop' into update-snippets
khancode Oct 19, 2021
06bc57a
resolve lint rules
khancode Oct 19, 2021
0215661
refactor: add snippets for sendEscrow, claimPayChannel, reliableSub
mukulljangid Oct 14, 2021
a8cda82
refactor: delete decoder & parseAccountFlags
mukulljangid Oct 15, 2021
576e207
refactor: use client for xrp balance
mukulljangid Oct 15, 2021
3679f66
refactor: add RegularKey
mukulljangid Oct 15, 2021
5b3be65
refactor: add partial payment code
mukulljangid Oct 15, 2021
6b58f96
refactor: nit
mukulljangid Oct 15, 2021
6ae5fb8
refactor: resolve linter issues
mukulljangid Oct 15, 2021
df78324
refactor: wait time changed to one ledger close time
mukulljangid Oct 15, 2021
2fc55cc
refactor: use submitAndWait instead of wait for some time
mukulljangid Oct 15, 2021
a24d3b6
refactor: resolve rebase issues
mukulljangid Oct 18, 2021
7a0177b
refactor: address comments
mukulljangid Oct 19, 2021
50dba6e
refactor: change comment and function name
mukulljangid Oct 19, 2021
8c5312e
disable eslint rules for snippets
khancode Oct 19, 2021
e2d2eb9
invoke functions below their definition
khancode Oct 19, 2021
879c7be
Merge branch 'develop' into update-snippets
khancode Oct 19, 2021
3fe4e4d
refactor: add descriptive comments to snippets
mukulljangid Oct 19, 2021
a54c7c7
Merge branch 'mj/snippets' into update-snippets
khancode Oct 19, 2021
7f61a10
Merge branch 'develop' into update-snippets
khancode Oct 19, 2021
ab6cb97
Merge branch 'mj/snippets' into update-snippets
khancode Oct 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {

plugins: [],
extends: ['@xrplf/eslint-config/base', 'plugin:mocha/recommended'],
ignorePatterns: ['snippets/src/*.ts'],
rules: {
// Certain rippled APIs require snake_case naming
'@typescript-eslint/naming-convention': [
Expand Down Expand Up @@ -61,18 +62,6 @@ module.exports = {
'@typescript-eslint/no-magic-numbers': 'off',
},
},
{
files: ['snippets/src/*.ts'],
rules: {
'import/unambiguous': 'off',
'import/no-unused-modules': 'off',
// Each file has a particular flow.
'max-lines-per-function': 'off',
'max-statements': 'off',
// Snippets have logs on console to better understand the working.
'no-console': 'off',
},
},
{
files: ['test/**/*.ts'],
rules: {
Expand Down
61 changes: 38 additions & 23 deletions snippets/src/getTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
/*
* import {Client} from '../../dist/npm'
* import {TransactionMetadata} from 'xrpl-local/models/common/transaction'
*/
import {
Client,
LedgerResponse,
TxResponse,
TransactionMetadata,
} from '../../dist/npm'

// const client = new Client('wss://s.altnet.rippletest.net:51233')
const client = new Client('wss://s.altnet.rippletest.net:51233')

// getTransaction()
async function getTransaction(): Promise<void> {
await client.connect()
const ledger: LedgerResponse = await client.request({
command: 'ledger',
transactions: true,
ledger_index: 'validated',
})
console.log(ledger)

/*
* async function getTransaction() {
* await client.connect()
* const ledger = await client.request({command: 'ledger', transactions: true})
* console.log(ledger)
* const tx = await client.request({
* command: 'tx',
* transaction: ledger.result.ledger.transactions[0] as string
* })
* console.log(tx)
* console.log(
* 'deliveredAmount:',
* (tx.result.meta as TransactionMetadata).DeliveredAmount
* )
* process.exit(0)
* }
*/
const transactions = ledger.result.ledger.transactions
if (transactions) {
const tx: TxResponse = await client.request({
command: 'tx',
transaction: transactions[0],
})
console.log(tx)

/*
* delivered_amount is the amount actually received by the destination account.
* Use this field to determine how much was delivered, regardless of whether the transaction is a partial payment.
* https://xrpl.org/transaction-metadata.html#delivered_amount
*/
console.log(
'delivered_amount:',
(tx.result.meta as TransactionMetadata).delivered_amount,
)
}

await client.disconnect()
}

void getTransaction()
112 changes: 49 additions & 63 deletions snippets/src/paths.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,49 @@
// import {Client} from '../../dist/npm'

/*
* const client = new Client(
* // 'wss://s.altnet.rippletest.net:51233'
* // 'ws://35.158.96.209:51233'
* 'ws://34.210.87.206:51233'
* )
*/

// sign()

/*
* async function sign() {
* await client.connect()
* const pathfind: any = {
* source: {
* address: 'r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59',
* amount: {
* currency: 'drops',
* value: '100'
* }
* },
* destination: {
* address: 'rKT4JX4cCof6LcDYRz8o3rGRu7qxzZ2Zwj',
* amount: {
* currency: 'USD',
* counterparty: 'rVnYNK9yuxBz4uP8zC8LEFokM2nqH3poc'
* }
* }
* }
*/

/*
* await client
* .getPaths(pathfind)
* .then(async (data) => {
* console.log('paths:', JSON.stringify(data))
* const fakeSecret = 'shsWGZcmZz6YsWWmcnpfr6fLTdtFV'
*/

/*
* pathfind.paths = data[0].paths
* pathfind.destination = data[0].destination
* await client
* .preparePayment('r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59', pathfind)
* .then((ret) => {
* const signed = client.sign(ret.txJSON, fakeSecret)
* console.log('signed:', signed)
* })
* .catch((err) => {
* console.log('ERR 1:', JSON.stringify(err))
* })
* })
* .catch((err) => {
* console.log('ERR 2:', err)
* })
*/

/*
* client.disconnect()
* }
*/
import { Client, Payment, RipplePathFindResponse } from '../../dist/npm'

const client = new Client('wss://s.altnet.rippletest.net:51233')

async function createTxWithPaths(): Promise<void> {
await client.connect()

const { wallet } = await client.fundWallet()
const destination_account = 'rKT4JX4cCof6LcDYRz8o3rGRu7qxzZ2Zwj'
const destination_amount = {
value: '0.001',
currency: 'USD',
issuer: 'rVnYNK9yuxBz4uP8zC8LEFokM2nqH3poc',
}

const request = {
command: 'ripple_path_find',
source_account: wallet.classicAddress,
source_currencies: [
{
currency: 'XRP',
},
],
destination_account,
destination_amount,
}

const resp: RipplePathFindResponse = await client.request(request)
console.log(resp)

const paths = resp.result.alternatives[0].paths_computed
console.log(paths)

const tx: Payment = {
TransactionType: 'Payment',
Account: wallet.classicAddress,
Amount: destination_amount,
Destination: destination_account,
Paths: paths,
}

await client.autofill(tx)
const signed = wallet.sign(tx)
console.log('signed:', signed)

await client.disconnect()
}

void createTxWithPaths()
3 changes: 1 addition & 2 deletions src/models/ledger/Ledger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Transaction } from '../transactions'
import { TransactionMetadata } from '../transactions/metadata'
import { Transaction, TransactionMetadata } from '../transactions'

import LedgerEntry from './LedgerEntry'

Expand Down
3 changes: 1 addition & 2 deletions src/models/methods/accountTx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { LedgerIndex } from '../common'
import { Transaction } from '../transactions'
import { TransactionMetadata } from '../transactions/metadata'
import { Transaction, TransactionMetadata } from '../transactions'

import { BaseRequest, BaseResponse } from './baseMethod'

Expand Down
3 changes: 1 addition & 2 deletions src/models/methods/transactionEntry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { LedgerIndex } from '../common'
import { Transaction } from '../transactions'
import { TransactionMetadata } from '../transactions/metadata'
import { Transaction, TransactionMetadata } from '../transactions'

import { BaseRequest, BaseResponse } from './baseMethod'

Expand Down
3 changes: 1 addition & 2 deletions src/models/methods/tx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Transaction } from '../transactions'
import { TransactionMetadata } from '../transactions/metadata'
import { Transaction, TransactionMetadata } from '../transactions'

import { BaseRequest, BaseResponse } from './baseMethod'

Expand Down
1 change: 1 addition & 0 deletions src/models/transactions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { validate, TransactionAndMetadata, Transaction } from './transaction'
export { TransactionMetadata } from './metadata'
export {
AccountSetAsfFlags,
AccountSetTfFlags,
Expand Down
3 changes: 1 addition & 2 deletions src/utils/hashes/hashLedger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { decode, encode } from 'ripple-binary-codec'
import { ValidationError, XrplError } from '../../errors'
import type { Ledger } from '../../models/ledger'
import { LedgerEntry } from '../../models/ledger'
import { Transaction } from '../../models/transactions'
import { TransactionMetadata } from '../../models/transactions/metadata'
import { Transaction, TransactionMetadata } from '../../models/transactions'

import HashPrefix from './HashPrefix'
import sha512Half from './sha512Half'
Expand Down