Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 0 additions & 8 deletions docs/l2mp_swap/l2mp_swap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 8 additions & 22 deletions ride/l2mp_swap.ride
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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)])

(
[
Expand All @@ -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)
Expand Down
66 changes: 0 additions & 66 deletions test/components/l2mp_swap/swap.spec.mjs

This file was deleted.

47 changes: 5 additions & 42 deletions test/components/l2mp_swap/swapAndStake_without_stake.spec.mjs
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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');
},
);
});