diff --git a/docs/l2mp_swap/l2mp_swap.md b/docs/l2mp_swap/l2mp_swap.md index c7907de7..37f0c484 100644 --- a/docs/l2mp_swap/l2mp_swap.md +++ b/docs/l2mp_swap/l2mp_swap.md @@ -15,20 +15,12 @@ # Functions -## Swap -- Should be with 1 payment in XTN -``` -@Callable(i) -func swap() -``` - ## Swap and Stake - Should be with 1 payment in XTN Arguments: - `stakingNode` - base58 string of staking node address -_Note: If `stakingNode` is equal to empty string (`""`) it swaps without staking_ ``` @Callable(i) func swapAndStake(stakingNode: String) diff --git a/ride/l2mp_swap.ride b/ride/l2mp_swap.ride index 0e25957f..8840b96a 100644 --- a/ride/l2mp_swap.ride +++ b/ride/l2mp_swap.ride @@ -160,7 +160,6 @@ func getSwapActions(i: Invocation, stakingNode: String) = { let payment = i.payments[0] let assetInAmount = payment.amount let assetOutAmount = fraction(assetInAmount, scale8, assetOutPrice) - let stake = if (stakingNode.isValidAddress()) then true else false strict checks = [ i.payments.size() == 1 || throwErr("invalid payments size"), @@ -169,16 +168,10 @@ func getSwapActions(i: Invocation, stakingNode: String) = { assetOutAmount > 0 || throwErr("invalid assetOutAmount") ] - let stakeAction = if (!stake) - then [ ScriptTransfer(userAddress, assetOutAmount, assetOutId) ] - else { - strict stakeInvoke = stakingAddress.invoke( - "leaseByAddress", - [stakingNode, userAddress.toString()], - [AttachedPayment(assetOutId, assetOutAmount)]) - - [] - } + strict stakeInvoke = stakingAddress.invoke( + "leaseByAddress", + [stakingNode, userAddress.toString()], + [AttachedPayment(assetOutId, assetOutAmount)]) ( [ @@ -200,27 +193,20 @@ func getSwapActions(i: Invocation, stakingNode: String) = { ), StringEntry( keyHistory(userAddress, i.transactionId), - formatHistory(assetInAmount, assetOutAmount, stake, stakingNode) + formatHistory(assetInAmount, assetOutAmount, true, stakingNode) ) - ] ++ stakeAction, + ], assetOutAmount ) } -@Callable(i) -func swap() = { - getSwapActions(i, "NULL") -} - @Callable(i) func swapAndStake(stakingNode: String) = { strict check = [ - stakingNode.isValidAddress() || stakingNode == "" || "staking node address is no valid".throwErr() + stakingNode.isValidAddress() || "staking node address is no valid".throwErr() ] - let node = if (stakingNode == "") then "NULL" else stakingNode - - getSwapActions(i, node) + getSwapActions(i, stakingNode) } @Callable(i) diff --git a/test/components/l2mp_swap/swap.spec.mjs b/test/components/l2mp_swap/swap.spec.mjs deleted file mode 100644 index 61ebb1b0..00000000 --- a/test/components/l2mp_swap/swap.spec.mjs +++ /dev/null @@ -1,66 +0,0 @@ -import chai from 'chai'; -import chaiAsPromised from 'chai-as-promised'; -import { invokeScript } from '@waves/waves-transactions'; -import { - chainId, broadcastAndWait, -} from '../../utils/api.mjs'; - -chai.use(chaiAsPromised); -const { expect } = chai; - -describe('l2mp_swap: swap', /** @this {MochaSuiteModified} */() => { - it( - 'should successfully swap without staking and receive tokens', - async function () { - const assetInAmount = 1e6; - const price = 1e6; - const expectedAssetOutAmount = (assetInAmount * 1e8) / price; - - const { id: txId, stateChanges } = await broadcastAndWait(invokeScript({ - dApp: this.accounts.l2mpSwap.addr, - call: { - function: 'swap', - }, - payment: [ - { assetId: this.xtnAssetId, amount: assetInAmount }, - ], - additionalFee: 4e5, - chainId, - }, this.accounts.user1.seed)); - - expect(stateChanges.burns).to.have.lengthOf(0); - expect(stateChanges.transfers).to.deep.equal([{ - asset: this.l2mpAssetId, - amount: expectedAssetOutAmount, - address: this.accounts.user1.addr, - }]); - expect(stateChanges.data).to.deep.equal([ - { - key: '%s%s__stats__totalIn', - type: 'integer', - value: assetInAmount, - }, - { - key: '%s%s__stats__totalOut', - type: 'integer', - value: expectedAssetOutAmount, - }, - { - key: `%s%s%s__stats__totalIn__${this.accounts.user1.addr}`, - type: 'integer', - value: assetInAmount, - }, - { - key: `%s%s%s__stats__totalOut__${this.accounts.user1.addr}`, - type: 'integer', - value: expectedAssetOutAmount, - }, - { - key: `%s%s%s__history__${this.accounts.user1.addr}__${txId}`, - type: 'string', - value: `%d%d%b%s__${assetInAmount}__${expectedAssetOutAmount}__false__NULL`, - }, - ]); - }, - ); -}); diff --git a/test/components/l2mp_swap/swapAndStake_without_stake.spec.mjs b/test/components/l2mp_swap/swapAndStake_without_stake.spec.mjs index d6ccbc8c..19425c9b 100644 --- a/test/components/l2mp_swap/swapAndStake_without_stake.spec.mjs +++ b/test/components/l2mp_swap/swapAndStake_without_stake.spec.mjs @@ -1,22 +1,18 @@ import chai from 'chai'; import chaiAsPromised from 'chai-as-promised'; import { invokeScript } from '@waves/waves-transactions'; -import { - chainId, broadcastAndWait, -} from '../../utils/api.mjs'; +import { broadcastAndWait, chainId } from '../../utils/api.mjs'; chai.use(chaiAsPromised); const { expect } = chai; describe('l2mp_swap: swapAndStake', /** @this {MochaSuiteModified} */() => { it( - 'should successfully swap without staking and receive tokens', + 'should be rejected is nodeAddress is not valid', async function () { const assetInAmount = 1e6; - const price = 1e6; - const expectedAssetOutAmount = (assetInAmount * 1e8) / price; - const { id: txId, stateChanges } = await broadcastAndWait(invokeScript({ + const invokeTx = invokeScript({ dApp: this.accounts.l2mpSwap.addr, call: { function: 'swapAndStake', @@ -29,42 +25,9 @@ describe('l2mp_swap: swapAndStake', /** @this {MochaSuiteModified} */() => { ], additionalFee: 4e5, chainId, - }, this.accounts.user1.seed)); + }, this.accounts.user1.seed); - expect(stateChanges.burns).to.have.lengthOf(0); - expect(stateChanges.invokes).to.have.lengthOf(0); - expect(stateChanges.transfers).to.deep.equal([{ - asset: this.l2mpAssetId, - amount: expectedAssetOutAmount, - address: this.accounts.user1.addr, - }]); - expect(stateChanges.data).to.deep.equal([ - { - key: '%s%s__stats__totalIn', - type: 'integer', - value: assetInAmount, - }, - { - key: '%s%s__stats__totalOut', - type: 'integer', - value: expectedAssetOutAmount, - }, - { - key: `%s%s%s__stats__totalIn__${this.accounts.user1.addr}`, - type: 'integer', - value: assetInAmount, - }, - { - key: `%s%s%s__stats__totalOut__${this.accounts.user1.addr}`, - type: 'integer', - value: expectedAssetOutAmount, - }, - { - key: `%s%s%s__history__${this.accounts.user1.addr}__${txId}`, - type: 'string', - value: `%d%d%b%s__${assetInAmount}__${expectedAssetOutAmount}__false__NULL`, - }, - ]); + return expect(broadcastAndWait(invokeTx)).to.rejectedWith('staking node address is no valid'); }, ); });