Developers Forum for XinFin XDC Network

Cover image for Automating Agreements with the XDC network
Quincy Jones
Quincy Jones

Posted on

Automating Agreements with the XDC network

I started a series of templates to help other developers with their projects. I created a basic contract that allows you to manage and represent an asset, such as a stock, commodity, or even debt. The contract can also programmatically manage the means of management of the asset and assign metadata to describe its attributes.

The contract's terms and conditions are embedded in the code and stored on the blockchain. Once deployed, the contract becomes immutable, meaning that its contents cannot be altered or tampered with. This immutability ensures that the agreed-upon terms cannot be changed without the consent of all parties involved. Additionally, the contract's transparency allows all parties to view and verify its content, eliminating the need for trust in a central authority.

The contract assigns a document hash to a variable, which represents the agreement between the parties. The document hash serves as a unique identifier for the agreement's content and can be used to verify its integrity. By comparing the document hash provided by any party to the stored document hash, the contract can determine whether the agreement has been modified or tampered with.

The contract enables parties to sign the agreement by interacting with the smart contract using their respective addresses. Once a party signs the contract, their address is recorded, indicating their consent and commitment to the agreement. This digital signature provides a non-repudiable proof of the party's involvement and prevents them from denying their participation later.

The contract includes functions for counterproposals, allowing either party to suggest modifications to the agreement by reassigning the document hash. This automation eliminates the need for manual communication and revision of physical documents. Furthermore, the contract keeps track of the total number of counterproposals, enabling parties to track the negotiation process accurately.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
//Smart contract
contract Contract is ERC1155 {
    uint256 public constant PartyA = 0;
    uint256 public constant PartyB = 1;

    uint256 public constant witnessA = 2;
    uint256 public constant witnessB = 3;

    string public DocumentHash = "";
    address PartyASignature;
    address PartyBSignature;
    uint public total_Counter_Proposals =0;

    bool public contractComplete = false;
    //Create 2 tokens for Party A-B
    constructor(string memory _DocumentHash, string memory _URI) ERC1155(_URI) {
        DocumentHash = _DocumentHash;
        _mint(msg.sender,PartyA,1, "");
        _mint(msg.sender,PartyB,1, "");
    }
    //only Party A-B can use functions with their modifier
    modifier PartyA_Sign {
        require(contractComplete == false, "contract has already been signed");
        require(balanceOf(msg.sender,PartyA) == 1);
        _;
    }
    modifier PartyB_Sign {
        require(contractComplete == false, "contract has already been signed");
        require(balanceOf(msg.sender,PartyB) == 1);
        _;
    }
    ///once both parties have sign you can no loger modify the contract
    modifier contractSigned {
        require(contractComplete == false,"Contract has been signed by both parties");
        require(PartyASignature == address(0) || PartyBSignature == address(0), "contract has been signed");
        _;
    }

    //this function will compare any data of hash to see if it matches the DocumentHash
    function verify(string memory _DocumentHash)public view returns(bool){
        if(keccak256(abi.encodePacked(_DocumentHash)) == keccak256(abi.encodePacked(DocumentHash)) ){
            return true;
        } else {
            return false;
        }
    }
    // Party A or party B can make a counter proposal and  reassign the hash
    function counter_proposal_A(string memory _DocumentHash)public  PartyA_Sign returns(bool){
        DocumentHash = _DocumentHash;
        total_Counter_Proposals++;
        return true;
    }
    function counter_proposal_B(string memory _DocumentHash)public PartyB_Sign returns(bool){
        DocumentHash = _DocumentHash;
        total_Counter_Proposals++;
        return true;
    }
    //Both parties can sign an make the contract official
    function sign_proposal()public contractSigned returns(bool,string memory){
        if(balanceOf(msg.sender,PartyA) == 1){
            PartyASignature = msg.sender;
            return (true, "Party A has signed Document");
        } else if(balanceOf(msg.sender,PartyB) ==1 ){
            PartyBSignature = msg.sender;
            return (true, "Party A has signed Document");
        }
        if(PartyASignature != address(0) && PartyBSignature != address(0) ){
            contractComplete = true;
            return (true, "only one party  has signed");
        }
        return (false, "error user does not hold token to authorize this call");
    }
}
Enter fullscreen mode Exit fullscreen mode

The contract utilizes modifiers to control the execution of certain functions. For example, the PartyA_Sign and PartyB_Sign modifiers ensure that only Party A or Party B, respectively, can call the counter-proposal functions. The contractSigned modifier restricts further modifications to the agreement once both parties have signed, preventing any unauthorized changes.

These features provided by smart contracts on XDC offer increased efficiency, security, transparency, and automation to contractual agreements. They have the potential to revolutionize the way parties engage in business interactions, potentially reducing the need for intermediaries in certain scenarios.

GitHub: https://github.com/CoinClubQuincy/XDC-Automated-Asset-Management

Discussion (0)