Skip to content

Commit 723aaa1

Browse files
delegate voucher snippets and cosmetic updates
1 parent 9e844ac commit 723aaa1

8 files changed

Lines changed: 110 additions & 17 deletions

File tree

cartesi-rollups_versioned_docs/version-2.0/api-reference/backend/vouchers.md

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func HandleAdvance(data *rollups.AdvanceResponse) error {
204204
voucher := rollups.VoucherRequest{
205205
Destination: "0x784f0c076CC55EAD0a585a9A13e57c467c91Dc3a", // sample ERC-20 token
206206
Payload: callData,
207-
Value: "0x0",
207+
Value: "0x00",
208208
}
209209

210210
if _, err := rollups.SendVoucher(&voucher); err != nil {
@@ -235,7 +235,7 @@ std::string handle_advance(httplib::Client &cli, picojson::value data)
235235
picojson::object voucher;
236236
voucher["destination"] = picojson::value("0x784f0c076CC55EAD0a585a9A13e57c467c91Dc3a");
237237
voucher["payload"] = picojson::value(call_data);
238-
voucher["value"] = picojson::value("0x0");
238+
voucher["value"] = picojson::value("0x00");
239239

240240
auto response = cli.Post(
241241
"/voucher",
@@ -295,6 +295,9 @@ This mechanism, where the Application contract maintains the state and funds whi
295295

296296
- **Ordered Vouchers**: Vouchers that must be executed in a specific sequence. For example, voucher A can only be executed after voucher B has been executed.
297297

298+
The examples below emit a DELEGATECALL voucher by posting to `/delegate-call-voucher`.
299+
In these snippets, `destination` is the contract that contains the logic to run, while `payload` is the ABI-encoded function call data (for example, `safeTransfer(address,address,uint256)`).
300+
298301
<Tabs>
299302
<TabItem value="JavaScript" label="JavaScript" default>
300303
<pre><code>
@@ -352,6 +355,56 @@ def emit_safe_erc20_transfer(token, to, amount):
352355
</code></pre>
353356
</TabItem>
354357

358+
<TabItem value="Rust" label="Rust">
359+
<pre><code>
360+
361+
```rust
362+
use ethers_core::abi::{Function, Param, ParamType, StateMutability, Token};
363+
use json::object;
364+
365+
pub async fn emit_safe_erc20_transfer(
366+
client: &hyper::Client<hyper::client::HttpConnector>,
367+
server_addr: &str,
368+
token: &str,
369+
to: &str,
370+
amount: u64,
371+
) -> Result<(), Box<dyn std::error::Error>> {
372+
let safe_transfer = Function {
373+
name: "safeTransfer".to_string(),
374+
inputs: vec![
375+
Param { name: "token".to_string(), kind: ParamType::Address, internal_type: None },
376+
Param { name: "to".to_string(), kind: ParamType::Address, internal_type: None },
377+
Param { name: "amount".to_string(), kind: ParamType::Uint(256), internal_type: None },
378+
],
379+
outputs: vec![],
380+
constant: None,
381+
state_mutability: StateMutability::NonPayable,
382+
};
383+
384+
let payload_bytes = safe_transfer.encode_input(&[
385+
Token::Address(token.parse()?),
386+
Token::Address(to.parse()?),
387+
Token::Uint(amount.into()),
388+
])?;
389+
390+
let voucher = object! {
391+
destination: "0xfafafafafafafafafafafafafafafafafafafafa", // address of the contract containing the logic
392+
payload: format!("0x{}", hex::encode(payload_bytes)),
393+
};
394+
395+
let request = hyper::Request::builder()
396+
.method(hyper::Method::POST)
397+
.header(hyper::header::CONTENT_TYPE, "application/json")
398+
.uri(format!("{}/delegate-call-voucher", server_addr))
399+
.body(hyper::Body::from(voucher.dump()))?;
400+
client.request(request).await?;
401+
Ok(())
402+
}
403+
```
404+
405+
</code></pre>
406+
</TabItem>
407+
355408
<TabItem value="Go" label="Go">
356409
<pre><code>
357410

@@ -409,6 +462,50 @@ func emitSafeERC20Transfer(token, to common.Address, amount *big.Int) error {
409462
}
410463
```
411464

465+
</code></pre>
466+
</TabItem>
467+
468+
<TabItem value="C++" label="C++">
469+
<pre><code>
470+
471+
```cpp
472+
#include <string>
473+
474+
#include "3rdparty/cpp-httplib/httplib.h"
475+
#include "3rdparty/picojson/picojson.h"
476+
477+
std::string emit_safe_erc20_transfer(
478+
httplib::Client &cli,
479+
const std::string &token,
480+
const std::string &to,
481+
const std::string &amount_hex_32bytes
482+
)
483+
{
484+
// safeTransfer(address,address,uint256)
485+
const std::string selector = "eb795549";
486+
const std::string token_word = "000000000000000000000000" + token.substr(2);
487+
const std::string to_word = "000000000000000000000000" + to.substr(2);
488+
const std::string payload = "0x" + selector + token_word + to_word + amount_hex_32bytes;
489+
490+
picojson::object voucher;
491+
voucher["destination"] = picojson::value("0xfafafafafafafafafafafafafafafafafafafafa");
492+
voucher["payload"] = picojson::value(payload);
493+
494+
auto response = cli.Post(
495+
"/delegate-call-voucher",
496+
picojson::value(voucher).serialize(),
497+
"application/json"
498+
);
499+
500+
if (!response || response->status >= 400)
501+
{
502+
return "error";
503+
}
504+
505+
return "ok";
506+
}
507+
```
508+
412509
</code></pre>
413510
</TabItem>
414511
</Tabs>

cartesi-rollups_versioned_docs/version-2.0/development/snippets/asset_withdraw_erc20_cpp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ std::string handle_advance(httplib::Client &cli, picojson::value data)
2020
picojson::object voucher;
2121
voucher["destination"] = picojson::value(kErc20TokenAddress);
2222
voucher["payload"] = picojson::value(call_data);
23-
voucher["value"] = picojson::value("0x0");
23+
voucher["value"] = picojson::value("0x00");
2424

2525
auto response = cli.Post(
2626
"/voucher",

cartesi-rollups_versioned_docs/version-2.0/development/snippets/asset_withdraw_erc20_go.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func HandleAdvance(data *rollups.AdvanceResponse) error {
2626
voucher := rollups.VoucherRequest{
2727
Destination: erc20TokenAddress,
2828
Payload: callData,
29-
Value: "0x0",
29+
Value: "0x00",
3030
}
3131

3232
if _, err := rollups.SendVoucher(&voucher); err != nil {

cartesi-rollups_versioned_docs/version-2.0/development/snippets/implementing_outputs_go.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func HandleAdvance(data *rollups.AdvanceResponse) error {
7373
}
7474
voucher := rollups.VoucherRequest{
7575
Destination: tokenAddress,
76-
Value: "0x0",
76+
Value: "0x00",
7777
Payload: voucherPayload,
7878
}
7979
if _, err = rollups.SendVoucher(&voucher); err != nil {

cartesi-rollups_versioned_docs/version-2.0/tutorials/calculator.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
22
id: calculator
3-
title: Build a calculator dApp
3+
title: Build a Calculator Application
44
resources:
55
- url: https://github.com/Mugen-Builders/calculator
6-
title: Source code for the Calculator dApp
6+
title: Source code for the Calculator App
77
---
88

9-
In this tutorial, we will build a simple Calculator dApp to illustrate how requests are sent and processed within Cartesi Rollups Infrastructure.
9+
In this tutorial, we will build a simple Calculator application to illustrate how requests are sent and processed within Cartesi Rollups Infrastructure.
1010

1111
We provide JavaScript, Python, Rust, Go, and C++ implementations so you can use your preferred backend language.
1212

cartesi-rollups_versioned_docs/version-2.0/tutorials/counter.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
id: counter
3-
title: Build a counter Application
3+
title: Build a Counter Application
44
resources:
55
- url: https://github.com/Mugen-Builders/Counter-X-Marketplace-apps
6-
title: Source code for the counter Application
6+
title: Source code for the Counter Application
77
---
88

99
This tutorial aims to guide you through creating and interacting with a basic Cartesi application, it'll take you through setting up your dev environment, creating a project then finally running and interacting with your application locally.

cartesi-rollups_versioned_docs/version-2.0/tutorials/marketplace.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
id: marketplace
3-
title: Build a marketplace Application
3+
title: Build a Marketplace Application
44
resources:
55
- url: https://github.com/Mugen-Builders/Counter-X-Marketplace-apps
6-
title: Source code for the marketplace Application
6+
title: Source code for the Marketplace Application
77
---
88

99
In this tutorial we'll be building a simple NFT Marketplace application, where users are able to deposit a unique token to be sold at a fixed price, then other users are able to purchase and withdraw these purchased tokens to their wallet.

cartesi-rollups_versions.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
[
22
"2.0",
3-
"1.5",
4-
"1.3",
5-
"1.0",
6-
"0.9",
7-
"0.8"
3+
"1.5"
84
]

0 commit comments

Comments
 (0)