All files / src/generated/transactions createAccount.js

58.33% Statements 7/12
0% Branches 0/4
50% Functions 1/2
58.33% Lines 7/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                    12x                                                         12x 37x 37x           37x   37x                   12x                
/** pragma type transaction **/
 
import {
  getEnvironment,
  replaceImportAddresses,
  reportMissingImports,
  reportMissing,
  sendTransaction
} from '@onflow/flow-cadut'
 
export const CODE = `
import FlowManager from 0x01
 
transaction (_ name: String?, pubKey: [String], manager: Address) {
    prepare( admin: AuthAccount) {
        let newAccount = AuthAccount(payer:admin)
        for key in pubKey {
            newAccount.addPublicKey(key.decodeHex())
        }
 
        if name != nil {
            let linkPath = FlowManager.accountManagerPath
            let accountManager = getAccount(manager)
                                .getCapability(linkPath)!
                                .borrow<&FlowManager.Mapper>()!
            
            // Create a record in account database
            let address = newAccount.address
            accountManager.setAddress(name!, address: address)
        }
    }
}
 
`;
 
/**
* Method to generate cadence code for createAccount transaction
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
export const createAccountTemplate = async (addressMap = {}) => {
  const envMap = await getEnvironment();
  const fullMap = {
  ...envMap,
  ...addressMap,
  };
 
  // If there are any missing imports in fullMap it will be reported via console
  reportMissingImports(CODE, fullMap, `createAccount =>`)
 
  return replaceImportAddresses(CODE, fullMap);
};
 
 
/**
* Sends createAccount transaction to the network
* @param {Object.<string, string>} props.addressMap - contract name as a key and address where it's deployed as value
* @param Array<*> props.args - list of arguments
* @param Array<*> props.signers - list of signers
*/
export const createAccount = async (props = {}) => {
  const { addressMap, args = [], signers = [] } = props;
  const code = await createAccountTemplate(addressMap);
 
  reportMissing("arguments", args.length, 3, `createAccount =>`);
  reportMissing("signers", signers.length, 1, `createAccount =>`);
 
  return sendTransaction({code, processed: true, ...props})
}