Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TXS: Improve transactions so that they all use correct prepare and execute syntax #75

Open
joshuahannan opened this issue Jul 15, 2020 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@joshuahannan
Copy link
Collaborator

Issue To Be Solved

It is a best practice in cadence to only perform actions that are associated with the transaction signer's account in the prepare phase of a transaction, and perform all external interaction in the execute phase.

Suggest A Solution

  • Update the sample transactions in transactions/ to use both phases.

Example: transactions/admin/add_plays_to_set.cdc would be changed from:

transaction(setID: UInt32, plays: [UInt32]) {

    prepare(acct: AuthAccount) {

        // borrow a reference to the Admin resource in storage
        let admin = acct.borrow<&TopShot.Admin>(from: /storage/TopShotAdmin)
            ?? panic("Cannot borrow a reference to the admin resource")

        // borrow a reference to the set to be added to
        let setRef = admin.borrowSet(setID: setID)

        // Add the specified play IDs
        setRef.addPlays(playIDs: plays)
    }
}

to:

import TopShot from 0xTOPSHOTADDRESS

// This transaction adds multiple plays to a set
		
transaction(setID: UInt32, plays: [UInt32]) {

    // Local variable for the topshot Admin object
    let adminRef: &TopShot.Admin

    prepare(acct: AuthAccount) {

        // borrow a reference to the Admin resource in storage
        self.adminRef = acct.borrow<&TopShot.Admin>(from: /storage/TopShotAdmin)!
    }

    execute() {

        // borrow a reference to the set to be added to
        let setRef = self.adminRef.borrowSet(setID: setID)

        // Add the specified play IDs
        setRef.addPlays(playIDs: plays)
    }
}
@joshuahannan joshuahannan added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Jul 15, 2020
joshuahannan pushed a commit that referenced this issue Mar 9, 2021
* changed transactions to use prepare/execute

* changed more transactions to have prepare/execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant