From 016e82ab5d1116d19b8aa8c61c2af983c9547daf Mon Sep 17 00:00:00 2001 From: wilsonianb Date: Thu, 30 Mar 2017 19:01:58 -0700 Subject: [PATCH] Simplify EscrowCreate parameters --- docs/index.md | 44 ++++--------------- .../specifications/escrow-creation.json | 20 ++++++--- src/ledger/parse/escrow-creation.js | 26 +++-------- src/transaction/escrow-creation.js | 25 +++++------ .../prepare-escrow-creation-full.json | 22 +++------- .../requests/prepare-escrow-creation.json | 16 +------ .../get-transaction-escrow-create.json | 22 +++------- 7 files changed, 51 insertions(+), 124 deletions(-) diff --git a/docs/index.md b/docs/index.md index a4c4bb0b..9287366b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -524,38 +524,22 @@ See [Transaction Types](#transaction-types) for a description. Name | Type | Description ---- | ---- | ----------- -source | object | Fields pertaining to the source of the payment. -*source.* address | [address](#ripple-address) | The address to send from. -*source.* maxAmount | [laxAmount](#amount) | The maximum amount to send. (This field is exclusive with source.amount) -*source.* tag | integer | *Optional* An arbitrary unsigned 32-bit integer that identifies a reason for payment or a non-Ripple account. -destination | object | Fields pertaining to the destination of the payment. -*destination.* address | [address](#ripple-address) | The address to receive at. -*destination.* amount | [laxAmount](#amount) | An exact amount to deliver to the recipient. If the counterparty is not specified, amounts with any counterparty may be used. (This field is exclusive with destination.minAmount). -*destination.* tag | integer | *Optional* An arbitrary unsigned 32-bit integer that identifies a reason for payment or a non-Ripple account. +amount | [value](#value) | Amount of XRP for sender to escrow. +destination | [address](#ripple-address) | Address to receive escrowed XRP. allowCancelAfter | date-time string | *Optional* If present, the escrow may be cancelled after this time. allowExecuteAfter | date-time string | *Optional* If present, the escrow can not be executed before this time. condition | string | *Optional* If present, fulfillment is required upon execution. +destinationTag | integer | *Optional* Destination tag. memos | [memos](#transaction-memos) | *Optional* Array of memos to attach to the transaction. +sourceTag | integer | *Optional* Source tag. ### Example ```json { - "source": { - "address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", - "maxAmount": { - "value": "0.01", - "currency": "XRP" - } - }, - "destination": { - "address": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", - "amount": { - "value": "0.01", - "currency": "XRP" - } - }, + "destination": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", + "amount": "0.01", "allowCancelAfter": "2014-09-24T21:21:50.000Z" } ``` @@ -3165,20 +3149,8 @@ instructions | object | The instructions for how to execute the transaction afte ```javascript const address = 'r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59'; const escrowCreation = { - "source": { - "address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", - "maxAmount": { - "value": "0.01", - "currency": "XRP" - } - }, - "destination": { - "address": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", - "amount": { - "value": "0.01", - "currency": "XRP" - } - }, + "destination": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", + "amount": "0.01", "allowCancelAfter": "2014-09-24T21:21:50.000Z" }; return api.prepareEscrowCreation(address, escrowCreation).then(prepared => diff --git a/src/common/schemas/specifications/escrow-creation.json b/src/common/schemas/specifications/escrow-creation.json index eb4372d9..7580946d 100644 --- a/src/common/schemas/specifications/escrow-creation.json +++ b/src/common/schemas/specifications/escrow-creation.json @@ -4,13 +4,13 @@ "link": "escrow-creation", "type": "object", "properties": { - "source": { - "$ref": "maxAdjustment", - "description": "Fields pertaining to the source of the payment." + "amount": { + "$ref": "value", + "description": "Amount of XRP for sender to escrow." }, "destination": { - "$ref": "destinationExactAdjustment", - "description": "Fields pertaining to the destination of the payment." + "$ref": "address", + "description": "Address to receive escrowed XRP." }, "memos": {"$ref": "memos"}, "condition": { @@ -27,8 +27,16 @@ "type": "string", "format": "date-time", "description": "If present, the escrow can not be executed before this time." + }, + "sourceTag": { + "$ref": "tag", + "description": "Source tag." + }, + "destinationTag": { + "$ref": "tag", + "description": "Destination tag." } }, - "required": ["source", "destination"], + "required": ["amount", "destination"], "additionalProperties": false } diff --git a/src/ledger/parse/escrow-creation.js b/src/ledger/parse/escrow-creation.js index cb2d4adb..15646737 100644 --- a/src/ledger/parse/escrow-creation.js +++ b/src/ledger/parse/escrow-creation.js @@ -5,34 +5,18 @@ const assert = require('assert') const utils = require('./utils') const parseAmount = require('./amount') -function removeGenericCounterparty(amount, address) { - return amount.counterparty === address ? - _.omit(amount, 'counterparty') : amount -} - function parseEscrowCreation(tx: Object): Object { assert(tx.TransactionType === 'EscrowCreate') - const source = { - address: tx.Account, - maxAmount: removeGenericCounterparty( - parseAmount(tx.SendMax || tx.Amount), tx.Account), - tag: tx.SourceTag - } - - const destination = { - address: tx.Destination, - amount: removeGenericCounterparty(parseAmount(tx.Amount), tx.Destination), - tag: tx.DestinationTag - } - return utils.removeUndefined({ - source: utils.removeUndefined(source), - destination: utils.removeUndefined(destination), + amount: parseAmount(tx.Amount).value, + destination: tx.Destination, memos: utils.parseMemos(tx), condition: tx.Condition, allowCancelAfter: utils.parseTimestamp(tx.CancelAfter), - allowExecuteAfter: utils.parseTimestamp(tx.FinishAfter) + allowExecuteAfter: utils.parseTimestamp(tx.FinishAfter), + sourceTag: tx.SourceTag, + destinationTag: tx.DestinationTag }) } diff --git a/src/transaction/escrow-creation.js b/src/transaction/escrow-creation.js index 89a7b2fd..1eb0c163 100644 --- a/src/transaction/escrow-creation.js +++ b/src/transaction/escrow-creation.js @@ -2,18 +2,20 @@ 'use strict' // eslint-disable-line strict const _ = require('lodash') const utils = require('./utils') -const {validate, iso8601ToRippleTime, toRippledAmount} = utils.common +const {validate, iso8601ToRippleTime, xrpToDrops} = utils.common const ValidationError = utils.common.errors.ValidationError import type {Instructions, Prepare} from './types.js' import type {Adjustment, MaxAdjustment, Memo} from '../common/types.js' type EscrowCreation = { - source: MaxAdjustment, - destination: Adjustment, + amount: string, + destination: string, memos?: Array, condition?: string, allowCancelAfter?: string, - allowExecuteAfter?: string + allowExecuteAfter?: string, + sourceTag?: number, + destinationTag?: number } function createEscrowCreationTransaction(account: string, @@ -22,13 +24,10 @@ function createEscrowCreationTransaction(account: string, const txJSON: Object = { TransactionType: 'EscrowCreate', Account: account, - Destination: payment.destination.address, - Amount: toRippledAmount(payment.destination.amount) + Destination: payment.destination, + Amount: xrpToDrops(payment.amount), } - if (txJSON.Amount.currency !== undefined) { - throw new ValidationError('"Amount" currency must be XRP') - } if (payment.condition !== undefined) { txJSON.Condition = payment.condition } @@ -38,11 +37,11 @@ function createEscrowCreationTransaction(account: string, if (payment.allowExecuteAfter !== undefined) { txJSON.FinishAfter = iso8601ToRippleTime(payment.allowExecuteAfter) } - if (payment.source.tag !== undefined) { - txJSON.SourceTag = payment.source.tag + if (payment.sourceTag !== undefined) { + txJSON.SourceTag = payment.sourceTag } - if (payment.destination.tag !== undefined) { - txJSON.DestinationTag = payment.destination.tag + if (payment.destinationTag !== undefined) { + txJSON.DestinationTag = payment.destinationTag } if (payment.memos !== undefined) { txJSON.Memos = _.map(payment.memos, utils.convertMemo) diff --git a/test/fixtures/requests/prepare-escrow-creation-full.json b/test/fixtures/requests/prepare-escrow-creation-full.json index 5039c6e1..1decf8ef 100644 --- a/test/fixtures/requests/prepare-escrow-creation-full.json +++ b/test/fixtures/requests/prepare-escrow-creation-full.json @@ -1,20 +1,6 @@ { - "source": { - "address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", - "maxAmount": { - "value": "0.01", - "currency": "XRP" - }, - "tag": 1 - }, - "destination": { - "address": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", - "amount": { - "value": "0.01", - "currency": "XRP" - }, - "tag": 2 - }, + "destination": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", + "amount": "0.01", "condition": "8F434346648F6B96DF89DDA901C5176B10A6D83961DD3C1AC88B59B2DC327AA4", "allowExecuteAfter": "2014-09-24T21:21:50.000Z", "memos": [ @@ -22,5 +8,7 @@ "type": "test", "data": "texted data" } - ] + ], + "destinationTag": 2, + "sourceTag": 1 } diff --git a/test/fixtures/requests/prepare-escrow-creation.json b/test/fixtures/requests/prepare-escrow-creation.json index 7a6a611d..d3fcfa7b 100644 --- a/test/fixtures/requests/prepare-escrow-creation.json +++ b/test/fixtures/requests/prepare-escrow-creation.json @@ -1,17 +1,5 @@ { - "source": { - "address": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59", - "maxAmount": { - "value": "0.01", - "currency": "XRP" - } - }, - "destination": { - "address": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", - "amount": { - "value": "0.01", - "currency": "XRP" - } - }, + "destination": "rpZc4mVfWUif9CRoHRKKcmhu1nx2xktxBo", + "amount": "0.01", "allowCancelAfter": "2014-09-24T21:21:50.000Z" } diff --git a/test/fixtures/responses/get-transaction-escrow-create.json b/test/fixtures/responses/get-transaction-escrow-create.json index ec03794e..f0c37051 100644 --- a/test/fixtures/responses/get-transaction-escrow-create.json +++ b/test/fixtures/responses/get-transaction-escrow-create.json @@ -4,22 +4,8 @@ "sequence": 10, "id": "144F272380BDB4F1BD92329A2178BABB70C20F59042C495E10BF72EBFB408EE1", "specification": { - "source": { - "address": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", - "maxAmount": { - "currency": "XRP", - "value": "0.000002" - }, - "tag": 1 - }, - "destination": { - "address": "rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw", - "amount": { - "currency": "XRP", - "value": "0.000002" - }, - "tag": 2 - }, + "amount": "0.000002", + "destination": "rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw", "memos": [ { "type": "x2", @@ -29,7 +15,9 @@ ], "condition": "8F434346648F6B96DF89DDA901C5176B10A6D83961DD3C1AC88B59B2DC327AA4", "allowCancelAfter": "2015-11-16T06:53:42.000Z", - "allowExecuteAfter": "2015-11-16T06:47:42.000Z" + "allowExecuteAfter": "2015-11-16T06:47:42.000Z", + "destinationTag": 2, + "sourceTag": 1 }, "outcome": { "result": "tesSUCCESS",