KomodoPlatform / komodo-defi-framework

This is the official Komodo DeFi Framework repository
https://komodoplatform.com/en/docs/komodo-defi-framework/
103 stars 94 forks source link

[Feature Request]: Offline staking #802

Closed Milerius closed 1 year ago

Milerius commented 3 years ago

Is your feature request related to a problem? Please describe.

My feature is not related to a problem but rather to get more users to use AtomicDEX.

As explained here it would be great to have equivalent features in AtomicDEX core to be able to offline staking on Mobile, Computer and Web

Describe the solution you'd like

The possibility via an rpc call to have the list of validators for offline staking and the possibility of activating staking after choosing a validator for the blockchains that require it. The ideal would be to be able to staker Cosmos, Tezos, Qtum or Band for example.

Describe alternatives you've considered

I have considered implementing it in the desktop version directly but it would be a shame not to share it with all the products, knowing that AWC staking also works on mobile

Milerius commented 3 years ago

QTUM Offline staking documentation: https://docs.qtum.site/en/OfflineStaking.html

Qtum Offline Staking allows the address for a non-staking wallet (capable of making the delegation assignment transaction) to be delegated to a Super Staker. Offline Staking is non-custodial: the delegation user keeps full control of their coins and private keys. The address delegation is made via a smart contract transaction from the delegation user's wallet which identifies the delegator's address, the Super Staker address, and the fee the delegator agrees to pay. If the Super Staker accepts this fee, it will begin staking the delegated address UTXOs.

The normal rules for staking UTXOs apply to delegated UTXOs:

    UTXOs may only be used for staking after they mature (500 confirmations)
    The Super Staker will set a minimum size of UTXOs to stake, defaulting to 100 QTUM. Delegated UTXOs below this amount will be ignored.
    It is best practice (for optimum returns) to break UTXOs up into sizes of 100 to 200 QTUM each. For users of the Qtum Core wallet, this can be easily accomplished with the command line version of splitutxosforaddress, described below.

To make the delegation assignment from the Qtum Core wallet, select Stake – Delegations, the Add delegation "+" button in the upper right corner, enter the Staker name (for local reference only), Staker address, Fee you agree to pay, and your Address to be delegated. Leave the default Gas settings alone unless you understand how to set these. The delegation transaction will require at least 0.9 QTUM in fees and any excess will be refunded.

https://github.com/qtumproject/qips/issues/25

New specs details: https://github.com/JB395/documents2/blob/master/README.md#offline-staking-address-delegation---undelegation-transaction-details

artemii235 commented 3 years ago

@Milerius Please do research on QTUM staking and propose staking API specification in this issue.

Milerius commented 3 years ago

last spec details: https://github.com/qtumproject/documents/blob/master/en/OfflineStakingDelegation/README.md

Milerius commented 3 years ago

according to the doc:

offline_staking_contract_address = bytes.fromhex("0000000000000000000000000000000000000086")
        pod = signmessage(delegator_eckey, staker_address_hex)
        calldata = bytes.fromhex("4c0e968c") # function abi
        calldata += b'\x00'*12 + bytes.fromhex(staker_address_hex)
        calldata += b'\x00'*31 + b'\x63'
        calldata += b'\x00'*31 + b'\x60'
        calldata += b'\x00'*31 + b'\x41'
        calldata += pod
        contract_call_script = CScript([b'\x04', CScriptNum(2250000), CScriptNum(40), calldata, offline_staking_contract_address, OP_CALL])

I see in the code we have:

pub fn generate_contract_call_script_pubkey(
    function_call: &[u8],
    gas_limit: u64,
    gas_price: u64,
    contract_address: &[u8],
) -> Qrc20AbiResult<Script> {
    if gas_limit == 0 || gas_price == 0 {
        // this is because the `encode_contract_number` will return an empty bytes
        return MmError::err(Qrc20AbiError::InvalidParams(
            "gas_limit and gas_price cannot be zero".to_owned(),
        ));
    }

    if contract_address.is_empty() {
        // this is because the `push_bytes` will panic
        return MmError::err(Qrc20AbiError::InvalidParams("token_addr cannot be empty".to_owned()));
    }

    let gas_limit = encode_contract_number(gas_limit as i64);
    let gas_price = encode_contract_number(gas_price as i64);

    Ok(ScriptBuilder::default()
        .push_opcode(Opcode::OP_4)
        .push_bytes(&gas_limit)
        .push_bytes(&gas_price)
        .push_data(function_call)
        .push_bytes(contract_address)
        .push_opcode(Opcode::OP_CALL)
        .into_script())
}

That look's very similar, what do you think @artemii235 ? can i use this function to generate the delegate tx?

Milerius commented 3 years ago

req:

{
    "userpass": "simplepassword123",
    "mmrpc": "2.0",
    "method": "add_delegation",
    "params": {
        "coin": "tQTUM",
        "staking_details": {
            "type": "Qtum",
            "address": "qcyBHeSct7Wr4mAw18iuQ1zW5mMFYmtmBE",
            "fee": 10
        }
    },
    "id": 0
}

resp:

{
    "mmrpc": "2.0",
    "result": {
        "tx_hex": "tx_hex...",
        "tx_hash": "tx_hash...",
        "from": [
            "qWY5jHd6jM4vRYikMQh6kvCPgWTFZVZwL6"
        ],
        "to": [
            "qWY5jHd6jM4vRYikMQh6kvCPgWTFZVZwL6"
        ],
        "total_amount": "189.95811134",
        "spent_by_me": "189.95811134",
        "received_by_me": "189.95811134",
        "my_balance_change": "0.00000000",
        "block_height": 0,
        "timestamp": 1633851284,
        "fee_details": {
            "type": "Qrc20",
            "coin": "tQTUM",
            "miner_fee": "0.004",
            "gas_limit": 2250000,
            "gas_price": 40,
            "total_gas_fee": "0.9"
        },
        "coin": "tQTUM",
        "internal_id": "",
        "transaction_type": "StakingDelegation"
    },
    "id": 0
}
Milerius commented 3 years ago

get_staking_infos rpc

req:

{
    "userpass": "simplepassword123",
    "mmrpc": "2.0",
    "method": "get_staking_infos",
    "params": {
        "coin": "tQTUM"
    },
    "id": 0
}
{
    "mmrpc": "2.0",
    "result": {
        "staking_infos_details": {
            "type": "Qtum",
            "amount": "111.591066",
            "staker": "qcyBHeSct7Wr4mAw18iuQ1zW5mMFYmtmBE",
            "am_i_staking": true
        }
    },
    "id": 0
}
artemii235 commented 3 years ago

@Milerius Are the examples above actual? Please update them if no, add missing RPCs requests samples, and assign @smk762 to this issue for testing and documentation.

artemii235 commented 2 years ago

@Milerius Do we consider this feature as complete for now? When the documentation is in progress, please assign it to @smk762 and move it to the "Documenting" column. You can do it in the Projects section as outlined in the screenshot below

Milerius commented 2 years ago

I think we already have the documentation in dev for offline staking!

https://developers.komodoplatform.com/basic-docs/atomicdex-api-20-dev/add_delegation.html https://developers.komodoplatform.com/basic-docs/atomicdex-api-20-dev/remove_delegation.html

smk762 commented 2 years ago

I still need to add get_staking_infos, will do tonight.

Milerius commented 2 years ago

I still need to add get_staking_infos, will do tonight.

Yeah was about to say https://github.com/KomodoPlatform/atomicDEX-API/blob/d78b56dfb85f548f1da1a1db148170872a23b81b/mm2src/rpc/dispatcher/dispatcher_v2.rs#L107 is not documented

smk762 commented 2 years ago

Doc was created, but not added to sidebar. Should be resolved in https://github.com/KomodoPlatform/developer-docs/pull/328 https://developers.komodoplatform.com/basic-docs/atomicdex-api-20-dev/get_staking_infos.html

artemii235 commented 2 years ago

Great! It looks like we can close this :slightly_smiling_face: Have we created issues to implement this functionality in GUIs?

artemii235 commented 2 years ago

cc @tonymorony @Milerius @yurii-khi in regards to the above comment.

artemii235 commented 2 years ago

NB https://github.com/KomodoPlatform/atomicDEX-Desktop/issues/719

smk762 commented 1 year ago

Closing, as the functionality exists in API. @yurii-khi @ca333 keep this (offline QTUM staking) on your radar for GUI roadmaps.