Git Product home page Git Product logo

educationfund's Introduction

EducationFund

This example demonstrates how to identify the caller - msg.sender using resource ownership in cadence.

Description

A parent or guardian wishes to establish an education fund for their child in order help them pay for post-secondary studies. As a forward-thinking parent, they want to put the fund on a blockchain, and Flow is their top choice.

The parent would like to make regular deposits of FLOW to the fund over a period of time until the child reaches adulthood. Nobody, including the parent, will be authorized to withdraw or transfer FLOW out of the fund during this time.

The parent can set a withdrawal limit that determines how much FLOW the child can withdraw, but the parent can never decrease this limit. The limit decreases by the amount the child withdraws until it reaches zero, at which point the child cannot withdraw any more until the parent increases the limit again.

Requirements

  • The fund may only accept deposits in FLOW (FlowToken).
  • The fund can be stored in an account that is controlled by a third-party, meaning that it cannot be controlled by the parent or the child.
  • Anybody may deposit FLOW into the fund.
  • The child is the only person who can withdraw FLOW from the fund.
  • The child may not withdraw FLOW beyond the limits defined by the parent.
  • Limits do not need to be adjusted automatically. For example, the parent could submit one transaction per year, or one per week, that makes necessary configuration changes.

Usage

Use 0x01 for  FungibleToken
Use 0x02 for  FlowToken
Use 0x03 for  Fund
Use 0x04 for  Guardian/Parent
Use 0x05 for  Recipient/Child

First, Deploy Contracts

  1. Deploy FungibleToken to Account 0x01
  2. Deploy FlowToken to Account 0x02
  3. Deploy EducationFund to Account 0x03

Second, Submit Transactions

  1. Submit FTCreateMinter, signed by Account 0x02. This transaction will create a minter for flow tokens.
  2. Submit FTCreateVault, signed by Account 0x03, 0x04, 0x05. This transaction will set publish and storage path for accounts holding flow tokens.
  3. Submit FTMint, signed by Account 0x02. This transaction will let a created minter to mint some flow tokens for testing and send tokens to guardians.
  4. Submit EFCreateSafeFund, signed by Account 0x03. This transaction will create a EducationFund and its fundvault.
  5. Submit EFDeposit, signed by Account 0x03. This transaction is used to deposit flow tokens to the safefund. It could be executed by any account holding flow tokens.
  6. Submit EFIncreaseLimit, signed by Account 0x04. This transaction is used to increate the limit. It is supposed to be executed only by registered guaridan(parent).
  7. Submit EFWithdraw,signed by Account 0x05. This transaction is supposed to be executed only by registered recipient(child)

Third, Execute Scripts

  1. Execute FTCheckBalance to check flowtoken balance of accounts used for testing
  2. Execute EFCheckSafeFundParams to check current params of safefund used for this test.

Msg.Sender Idenfication

    pub resource IDToken {
        pub var amount:UFix64
        pub var address:Address?
        init(address:Address?, amount:UFix64){
            //pre {
            //    address != nil:
            //    "address is invalid"
            //}
            self.address=address
            self.amount=amount

        }
    }

    pub resource IDTokenGenerator {
        pub fun generateIDToken(amount:UFix64):@IDToken {
            return <- create IDToken(address:self.owner?.address,amount:amount)
        }
    }

    pub fun createIDTokenGenerator():@IDTokenGenerator {
        return <- create IDTokenGenerator()
    }
    pub fun withdraw(idtoken:@IDToken){
        pre {
            idtoken != nil:
                "Invalid idtoken!"
            idtoken.address == self.recipient:
                "Only registered recipient is allowed to call this function"
            self.recipientVault.borrow != nil:
                "Invalid receiver capability!"
        }
        _;
    }

Disclaimer

This solution is not fully battle-tested. Not proved that resource-oriented architecture is free from security issues like duplicated deploy attack.

educationfund's People

Contributors

jtn-ms avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.