nectar-contracts 0.1.1

Swarm storage incentive contract bindings and addresses
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Swarm contract bindings and deployment information.
//!
//! This crate provides type-safe Solidity contract bindings using Alloy's `sol!` macro,
//! along with deployment information for mainnet and testnet.
//!
//! # Deployment Information
//!
//! Each contract has a deployment struct that bundles address and deployment block:
//!
//! ```
//! use nectar_contracts::mainnet;
//!
//! let postage = mainnet::POSTAGE_STAMP;
//! assert_ne!(postage.address, alloy_primitives::Address::ZERO);
//! assert!(postage.block > 0);
//! ```
//!
//! # Contract Bindings
//!
//! The `sol!` macro generates call types, return types, and event types that can be
//! used with alloy providers:
//!
//! ```ignore
//! use alloy_sol_types::SolCall;
//! use nectar_contracts::{IPostageStamp, mainnet};
//!
//! // Encode a call
//! let call = IPostageStamp::batchOwnerCall { batchId: batch_id };
//! let encoded = call.abi_encode();
//! ```

#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]

use alloy_primitives::{Address, address};
use alloy_sol_types::sol;

// Deployment Info Macro

/// Macro to define a contract deployment struct with address and block.
macro_rules! define_deployment {
    ($(#[$meta:meta])* $name:ident) => {
        $(#[$meta])*
        #[derive(Debug, Clone, Copy, PartialEq, Eq)]
        pub struct $name {
            /// Contract address.
            pub address: Address,
            /// Deployment block number.
            pub block: u64,
        }

        impl $name {
            /// Creates a new deployment.
            #[must_use]
            pub const fn new(address: Address, block: u64) -> Self {
                Self { address, block }
            }
        }
    };
}

// Deployment Information Types

define_deployment!(
    /// BZZ token deployment information.
    Token
);

define_deployment!(
    /// Postage stamp contract deployment information.
    PostageStamp
);

define_deployment!(
    /// Stake registry contract deployment information.
    StakeRegistry
);

define_deployment!(
    /// Redistribution contract deployment information.
    Redistribution
);

define_deployment!(
    /// Storage price oracle contract deployment information.
    StoragePriceOracle
);

define_deployment!(
    /// Chequebook factory contract deployment information.
    ChequebookFactory
);

define_deployment!(
    /// Swap price oracle contract deployment information.
    SwapPriceOracle
);

// Token Interface

sol! {
    /// Standard ERC20 token interface.
    #[derive(Debug, PartialEq, Eq)]
    interface IERC20 {
        function name() external view returns (string memory);
        function symbol() external view returns (string memory);
        function decimals() external view returns (uint8);
        function totalSupply() external view returns (uint256);
        function balanceOf(address account) external view returns (uint256);
        function transfer(address to, uint256 amount) external returns (bool);
        function allowance(address owner, address spender) external view returns (uint256);
        function approve(address spender, uint256 amount) external returns (bool);
        function transferFrom(address from, address to, uint256 amount) external returns (bool);

        event Transfer(address indexed from, address indexed to, uint256 value);
        event Approval(address indexed owner, address indexed spender, uint256 value);
    }
}

// Storage Incentive Contract Interfaces

sol! {
    /// Postage stamp contract interface.
    ///
    /// Manages postage stamp batches required for uploading data to Swarm.
    #[derive(Debug, PartialEq, Eq)]
    interface IPostageStamp {
        function withdraw(address beneficiary) external;
        function setPrice(uint256 price) external;
        function validChunkCount() external view returns (uint256);
        function batchOwner(bytes32 batchId) external view returns (address);
        function batchDepth(bytes32 batchId) external view returns (uint8);
        function batchBucketDepth(bytes32 batchId) external view returns (uint8);
        function remainingBalance(bytes32 batchId) external view returns (uint256);
        function minimumInitialBalancePerChunk() external view returns (uint256);
        function batches(bytes32 batchId) external view returns (
            address owner,
            uint8 depth,
            uint8 bucketDepth,
            bool immutableFlag,
            uint256 normalisedBalance,
            uint256 lastUpdatedBlockNumber
        );
    }

    /// Stake registry contract interface.
    ///
    /// Manages staking for nodes participating in storage incentives.
    #[derive(Debug, PartialEq, Eq)]
    interface IStakeRegistry {
        function stakes(address owner) external view returns (
            bytes32 overlay,
            uint256 committedStake,
            uint256 potentialStake,
            uint256 lastUpdatedBlockNumber,
            uint8 height
        );
        function overlayOfAddress(address owner) external view returns (bytes32);
        function heightOfAddress(address owner) external view returns (uint8);
        function nodeEffectiveStake(address owner) external view returns (uint256);
        function lastUpdatedBlockNumberOfAddress(address owner) external view returns (uint256);
        function freezeDeposit(address owner, uint256 time) external;

        event StakeUpdated(
            address indexed owner,
            uint256 committedStake,
            uint256 potentialStake,
            bytes32 overlay,
            uint256 lastUpdatedBlock,
            uint8 height
        );
        event StakeSlashed(address slashed, bytes32 overlay, uint256 amount);
        event StakeFrozen(address frozen, bytes32 overlay, uint256 time);
        event StakeWithdrawn(address node, uint256 amount);
    }

    /// Redistribution contract interface.
    ///
    /// Implements the Schelling coordination game for storage reward distribution.
    #[derive(Debug, PartialEq, Eq)]
    interface IRedistribution {
        function currentRound() external view returns (uint64);
        function currentPhaseCommit() external view returns (bool);
        function currentPhaseReveal() external view returns (bool);
        function currentPhaseClaim() external view returns (bool);
        function isParticipatingInUpcomingRound(bytes32 overlay, uint8 depth) external view returns (bool);
        function isWinner(bytes32 overlay) external view returns (bool);
        function claim(
            bytes32[] calldata proofSegments,
            bytes32 proveSegment,
            bytes32[] calldata proofSegments2,
            bytes32 proveSegment2,
            uint64 chunkSpan,
            bytes32[] calldata proofSegments3
        ) external;
    }

    /// Storage price oracle contract interface.
    ///
    /// Controls the price per chunk for postage stamp batches.
    #[derive(Debug, PartialEq, Eq)]
    interface IStoragePriceOracle {
        function PRICE_UPDATER_ROLE() external view returns (uint256);
        function postageStamp() external view returns (address);
        function currentPrice() external view returns (uint32);
        function minimumPrice() external view returns (uint32);
        function currentRound() external view returns (uint64);
        function lastAdjustedRound() external view returns (uint64);
        function isPaused() external view returns (bool);
        function setPrice(uint32 price) external returns (bool);
        function adjustPrice(uint16 redundancy) external returns (bool);
        function pause() external;
        function unPause() external;

        event PriceUpdate(uint256 price);
        event StampPriceUpdateFailed(uint256 attemptedPrice);
    }
}

// Swap Contract Interfaces (Chequebook)

#[cfg(feature = "serde")]
sol! {
    /// EIP-712 cheque struct for chequebook payments.
    ///
    /// This is the typed data structure used for signing cheques off-chain.
    /// The EIP-712 domain uses:
    /// - Name: "Chequebook"
    /// - Version: "1.0"
    /// - ChainId: network-specific (100 for Gnosis, 11155111 for Sepolia)
    #[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    struct Cheque {
        address chequebook;
        address beneficiary;
        uint256 cumulativePayout;
    }
}

#[cfg(not(feature = "serde"))]
sol! {
    /// EIP-712 cheque struct for chequebook payments.
    ///
    /// This is the typed data structure used for signing cheques off-chain.
    /// The EIP-712 domain uses:
    /// - Name: "Chequebook"
    /// - Version: "1.0"
    /// - ChainId: network-specific (100 for Gnosis, 11155111 for Sepolia)
    #[derive(Debug, PartialEq, Eq)]
    struct Cheque {
        address chequebook;
        address beneficiary;
        uint256 cumulativePayout;
    }
}

sol! {
    /// Chequebook contract interface (ERC20SimpleSwap).
    ///
    /// Allows the issuer to send cheques to counterparties for peer-to-peer payments.
    #[derive(Debug, PartialEq, Eq)]
    interface IChequebook {
        function issuer() external view returns (address);
        function token() external view returns (address);
        function balance() external view returns (uint256);
        function liquidBalance() external view returns (uint256);
        function liquidBalanceFor(address beneficiary) external view returns (uint256);
        function paidOut(address beneficiary) external view returns (uint256);
        function totalPaidOut() external view returns (uint256);
        function totalHardDeposit() external view returns (uint256);
        function defaultHardDepositTimeout() external view returns (uint256);
        function bounced() external view returns (bool);
        function hardDeposits(address beneficiary) external view returns (
            uint256 amount,
            uint256 decreaseAmount,
            uint256 timeout,
            uint256 canBeDecreasedAt
        );
        function init(address _issuer, address _token, uint256 _defaultHardDepositTimeout) external;
        function cashCheque(
            address beneficiary,
            address recipient,
            uint256 cumulativePayout,
            bytes memory beneficiarySig,
            uint256 callerPayout,
            bytes memory issuerSig
        ) external;
        function cashChequeBeneficiary(address recipient, uint256 cumulativePayout, bytes memory issuerSig) external;
        function increaseHardDeposit(address beneficiary, uint256 amount) external;
        function prepareDecreaseHardDeposit(address beneficiary, uint256 decreaseAmount) external;
        function decreaseHardDeposit(address beneficiary) external;
        function setCustomHardDepositTimeout(address beneficiary, uint256 hardDepositTimeout, bytes memory beneficiarySig) external;
        function withdraw(uint256 amount) external;

        event ChequeCashed(
            address indexed beneficiary,
            address indexed recipient,
            address indexed caller,
            uint256 totalPayout,
            uint256 cumulativePayout,
            uint256 callerPayout
        );
        event ChequeBounced();
        event HardDepositAmountChanged(address indexed beneficiary, uint256 amount);
        event HardDepositDecreasePrepared(address indexed beneficiary, uint256 decreaseAmount);
        event HardDepositTimeoutChanged(address indexed beneficiary, uint256 timeout);
        event Withdraw(uint256 amount);
    }

    /// Chequebook factory contract interface (SimpleSwapFactory).
    #[derive(Debug, PartialEq, Eq)]
    interface IChequebookFactory {
        function ERC20Address() external view returns (address);
        function master() external view returns (address);
        function deployedContracts(address addr) external view returns (bool);
        function deploySimpleSwap(address issuer, uint256 defaultHardDepositTimeoutDuration, bytes32 salt) external returns (address);

        event SimpleSwapDeployed(address contractAddress);
    }

    /// Swap price oracle contract interface.
    #[derive(Debug, PartialEq, Eq)]
    interface ISwapPriceOracle {
        function price() external view returns (uint256);
        function chequeValueDeduction() external view returns (uint256);
        function getPrice() external view returns (uint256 price, uint256 chequeValueDeduction);
        function updatePrice(uint256 newPrice) external;
        function updateChequeValueDeduction(uint256 newChequeValueDeduction) external;

        event PriceUpdate(uint256 price);
        event ChequeValueDeductionUpdate(uint256 chequeValueDeduction);
    }
}

// Gnosis Chain Mainnet Deployments

/// Gnosis Chain mainnet contract deployments.
pub mod mainnet {
    use super::*;

    /// BZZ token (xBZZ on Gnosis Chain).
    pub const BZZ_TOKEN: Token =
        Token::new(address!("dBF3Ea6F5beE45c02255B2c26a16F300502F68da"), 0);

    /// Postage stamp contract.
    pub const POSTAGE_STAMP: PostageStamp = PostageStamp::new(
        address!("5b53f7a1975eb212d4b20b7cdd443baa189af7c9"),
        31305656,
    );

    /// Stake registry contract.
    pub const STAKING: StakeRegistry = StakeRegistry::new(
        address!("0c6aa197271466f0afe3818ca03ac47d8f5c2f8a"),
        40430237,
    );

    /// Redistribution contract.
    pub const REDISTRIBUTION: Redistribution = Redistribution::new(
        address!("eb210c2e166f61b3fd32246d53893f8b9d2a624c"),
        41105199,
    );

    /// Storage price oracle contract.
    pub const STORAGE_PRICE_ORACLE: StoragePriceOracle = StoragePriceOracle::new(
        address!("47EeF336e7fE5bED98499A4696bce8f28c1B0a8b"),
        37339168,
    );

    /// Chequebook factory contract.
    pub const CHEQUEBOOK_FACTORY: ChequebookFactory = ChequebookFactory::new(
        address!("c2d5a532cf69aa9a1378737d8ccdef884b6e7420"),
        39939970,
    );

    /// Swap price oracle contract.
    pub const SWAP_PRICE_ORACLE: SwapPriceOracle = SwapPriceOracle::new(
        address!("A57A50a831B31c904A770edBCb706E03afCdbd94"),
        39939970,
    );
}

// Sepolia Testnet Deployments

/// Sepolia testnet contract deployments.
pub mod testnet {
    use super::*;

    /// BZZ token (sBZZ on Sepolia).
    pub const BZZ_TOKEN: Token =
        Token::new(address!("6e01ee6183721ae9a006fd4906970c1583863765"), 0);

    /// Postage stamp contract.
    pub const POSTAGE_STAMP: PostageStamp = PostageStamp::new(
        address!("621c2e0fa5ed488c7124eb55cc7eb3af75d0d9e8"),
        6596277,
    );

    /// Stake registry contract.
    pub const STAKING: StakeRegistry = StakeRegistry::new(
        address!("6f252dd6f340f6c6d2f6ee8954b011dd5aba4350"),
        8262529,
    );

    /// Redistribution contract.
    pub const REDISTRIBUTION: Redistribution = Redistribution::new(
        address!("fb6c7d33be1fb12f4c5da71df7c9d5c22970ba7a"),
        8646721,
    );

    /// Storage price oracle contract.
    pub const STORAGE_PRICE_ORACLE: StoragePriceOracle = StoragePriceOracle::new(
        address!("95Dc18380e92C13E4F8a4e94C99FB1b97250174B"),
        8226873,
    );

    /// Chequebook factory contract.
    pub const CHEQUEBOOK_FACTORY: ChequebookFactory = ChequebookFactory::new(
        address!("0fF044F6bB4F684a5A149B46D7eC03ea659F98A1"),
        4752810,
    );

    /// Swap price oracle contract.
    pub const SWAP_PRICE_ORACLE: SwapPriceOracle = SwapPriceOracle::new(
        address!("1814e9b3951Df0CB8e12b2bB99c5594514588936"),
        4752810,
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloy_primitives::U256;

    #[test]
    fn test_deployments_non_zero() {
        // Mainnet
        assert_ne!(mainnet::BZZ_TOKEN.address, Address::ZERO);
        assert_ne!(mainnet::POSTAGE_STAMP.address, Address::ZERO);
        assert_ne!(mainnet::STAKING.address, Address::ZERO);
        assert_ne!(mainnet::REDISTRIBUTION.address, Address::ZERO);
        assert_ne!(mainnet::STORAGE_PRICE_ORACLE.address, Address::ZERO);
        assert_ne!(mainnet::CHEQUEBOOK_FACTORY.address, Address::ZERO);
        assert_ne!(mainnet::SWAP_PRICE_ORACLE.address, Address::ZERO);

        // Testnet
        assert_ne!(testnet::BZZ_TOKEN.address, Address::ZERO);
        assert_ne!(testnet::POSTAGE_STAMP.address, Address::ZERO);
        assert_ne!(testnet::STAKING.address, Address::ZERO);
        assert_ne!(testnet::REDISTRIBUTION.address, Address::ZERO);
        assert_ne!(testnet::STORAGE_PRICE_ORACLE.address, Address::ZERO);
        assert_ne!(testnet::CHEQUEBOOK_FACTORY.address, Address::ZERO);
        assert_ne!(testnet::SWAP_PRICE_ORACLE.address, Address::ZERO);
    }

    #[test]
    fn test_sol_types_generated() {
        let _ = IERC20::balanceOfCall {
            account: Address::ZERO,
        };
        let _ = IPostageStamp::batchOwnerCall {
            batchId: [0u8; 32].into(),
        };
        let _ = IStakeRegistry::overlayOfAddressCall {
            owner: Address::ZERO,
        };
        let _ = IChequebookFactory::deploySimpleSwapCall {
            issuer: Address::ZERO,
            defaultHardDepositTimeoutDuration: U256::ZERO,
            salt: [0u8; 32].into(),
        };
    }
}