Skip to content

Commit 5c3233c

Browse files
committed
R4D mixer script example
1 parent bc8db23 commit 5c3233c

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

ride4dapps/mixer/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# R4D Mixer
2+
This is an example of crypto coins mixer
3+
4+
1. A user can send money with an arbitrary Blake2b256 hash
5+
2. After that, anyone with address matching that hash and knowing how much bytes of address used can take the money back
6+
7+
Try now: https://cb73ac4e-d39f-4a9f-a196-3c46b445f8fe.pub.cloud.scaleway.com/ (Requires Waves Keeper with InvokeScript support in TESTNET mode)
8+
9+
# Example
10+
1. Taken destination address is `3Musn2zFuw3G71yg6G7PDRAq7CjWxK7Z4pk` and complexity is 3, caller takes first 3 and last 3 bytes of it: `3Mu + 4pk`, hashes it with Blake2b256 and invokes `deposit(hash)` with attached Waves payment
11+
2. Then owner of `3Musn2zFuw3G71yg6G7PDRAq7CjWxK7Z4pk` invokes `withdraw(3)`, script compares his address with stored hash and withdraws all money associated with it

ride4dapps/mixer/mixer.ride

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{-# STDLIB_VERSION 3 #-}
2+
{-# CONTENT_TYPE DAPP #-}
3+
{-# SCRIPT_TYPE ACCOUNT #-}
4+
5+
let FeePercent = 1
6+
7+
@Callable(i)
8+
func deposit(hash: ByteVector) = {
9+
let pmt = extract(i.payment)
10+
if (isDefined(pmt.assetId) || pmt.amount < 100) then throw("can hold waves only at the moment")
11+
else {
12+
let currentKey = toBase58String(hash)
13+
let currentAmount = match getInteger(this, currentKey) {
14+
case a:Int => a
15+
case _ => 0
16+
}
17+
let fee = pmt.amount * FeePercent / 100
18+
let newAmount = currentAmount + pmt.amount - fee
19+
WriteSet([DataEntry(currentKey, newAmount)])
20+
}
21+
}
22+
23+
@Callable(i)
24+
func withdraw(complexity: Int) = {
25+
let currentKey = toBase58String(blake2b256(take(drop(i.caller.bytes, 2), complexity) + takeRight(i.caller.bytes, complexity)))
26+
let amount = match getInteger(this, currentKey) {
27+
case a:Int => a
28+
case _ => 0
29+
}
30+
if (amount <= 0)
31+
then throw("Can't withdraw negative amount")
32+
else ScriptResult(
33+
WriteSet([DataEntry(currentKey, 0)]),
34+
TransferSet([ScriptTransfer(i.caller, amount, unit)])
35+
)
36+
}

ride4dapps/mixer/mixer_test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
describe('Mixer test', () => {
2+
const dappAddress = "3Musn2zFuw3G71yg6G7PDRAq7CjWxK7Z4pk"
3+
4+
const hash = "8N5gCoRKcCD4BxJNGn2kKUzMBieetEh4r7DUMHa1m2gq"
5+
6+
it('Deposit funds', async function(){
7+
const tx = invokeScript({ fee: 500000, dappAddress: dappAddress, call:{function:"deposit",args:[{"type": "binary", "value": hash}]},
8+
payment: [{amount: 1000000, assetId: null}]})
9+
await broadcast(tx)
10+
await waitForTx(tx.id)
11+
})
12+
13+
it('Withdraw funds', async function(){
14+
const tx = invokeScript({ fee: 500000, dappAddress: dappAddress, call:{function:"withdraw",args:[{"type": "integer", "value": 3}]}, payment: null)
15+
await broadcast(tx)
16+
await waitForTx(tx.id)
17+
})
18+
})

0 commit comments

Comments
 (0)