All files / src/generated/contracts FlowManager.js

100% Statements 11/11
100% Branches 2/2
100% Functions 2/2
100% Lines 11/11

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169                  12x                                                                                                                                                                                                                                                                         12x 58x 58x           58x   58x                   12x 57x 57x 57x   57x  
/** pragma type contract **/
 
import {
  getEnvironment,
  replaceImportAddresses,
  reportMissingImports,
  deployContract,
} from '@onflow/flow-cadut'
 
export const CODE = `
pub contract FlowManager {
 
    /// Account Manager
    pub event AccountAdded(address: Address)
 
    pub struct Mapper {
        pub let accounts: {String: Address}
 
        pub fun getAddress(_ name: String): Address? {
            return self.accounts[name]
        }
 
        pub fun setAddress(_ name: String, address: Address){
            self.accounts[name] = address
            emit FlowManager.AccountAdded(address: address)
        }
 
        init(){
            self.accounts = {}
        }
    }
 
    pub fun getAccountAddress(_ name: String): Address?{
        let accountManager = self.account
            .getCapability(self.accountManagerPath)
            .borrow<&FlowManager.Mapper>()!
 
        return accountManager.getAddress(name)
    }
 
    pub let defaultAccounts: {Address : String}
 
    pub fun resolveDefaultAccounts(_ address: Address): Address{
        let alias = self.defaultAccounts[address]!
        return self.getAccountAddress(alias)!
    }
 
    pub let accountManagerStorage: StoragePath
    pub let contractManagerStorage: StoragePath
    pub let accountManagerPath: PublicPath
    pub let contractManagerPath: PublicPath
 
    /// Environment Manager
    pub event BlockOffsetChanged(offset: UInt64)
    pub event TimestampOffsetChanged(offset: UFix64)
 
    pub struct MockBlock {
        pub let id: [UInt8; 32]
        pub let height: UInt64
        pub let view: UInt64
        pub let timestamp: UFix64
 
        init(_ id: [UInt8; 32], _ height: UInt64, _ view: UInt64, _ timestamp: UFix64){
            self.id = id
            self.height = height
            self.view = view
            self.timestamp = timestamp
        }
    }
 
    pub fun setBlockOffset(_ offset: UInt64){
        self.blockOffset = offset
        emit FlowManager.BlockOffsetChanged(offset: offset)
    }
 
    pub fun setTimestampOffset(_ offset: UFix64){
        self.timestampOffset = offset
        emit FlowManager.TimestampOffsetChanged(offset: offset)
    }
 
    pub fun getBlockHeight(): UInt64 {
        var block = getCurrentBlock()
        return block.height + self.blockOffset
    }
 
    pub fun getBlockTimestamp(): UFix64 {
        var block = getCurrentBlock()
        return block.timestamp + self.timestampOffset
    }
 
    pub fun getBlock(): MockBlock {
        var block =  getCurrentBlock()
        let mockBlock = MockBlock(block.id, block.height, block.view, block.timestamp);
        return mockBlock
    }
 
    pub var blockOffset: UInt64;
    pub var timestampOffset: UFix64;
 
 
    // Initialize contract
    init(){
        // Environment defaults
        self.blockOffset = 0;
        self.timestampOffset = 0.0;
 
        // Account Manager initialization
        let accountManager = Mapper()
        let contractManager = Mapper()
 
        self.defaultAccounts = {
          0x01: "Alice",
          0x02: "Bob",
          0x03: "Charlie",
          0x04: "Dave",
          0x05: "Eve"
        }
 
        self.accountManagerStorage = /storage/testSuiteAccountManager
        self.contractManagerStorage = /storage/testSuiteContractManager
 
        self.accountManagerPath = /public/testSuiteAccountManager
        self.contractManagerPath = /public/testSuiteContractManager
        
        // Destroy previously stored values
        self.account.load<Mapper>(from: self.accountManagerStorage)
        self.account.load<Mapper>(from: self.contractManagerStorage)
 
        self.account.save(accountManager, to: self.accountManagerStorage)
        self.account.save(contractManager, to: self.contractManagerStorage)
 
        self.account.link<&Mapper>(self.accountManagerPath, target: self.accountManagerStorage)
        self.account.link<&Mapper>(self.contractManagerPath, target: self.contractManagerStorage)
    }
}
 
`;
 
/**
* Method to generate cadence code for FlowManager contract
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
export const FlowManagerTemplate = 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, `FlowManager =>`)
 
  return replaceImportAddresses(CODE, fullMap);
};
 
 
/**
* Deploys FlowManager transaction to the network
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
* @param Array<*> args - list of arguments
* param Array<string> - list of signers
*/
export const  deployFlowManager = async (props) => {
  const { addressMap = {} } = props;
  const code = await FlowManagerTemplate(addressMap);
  const name = "FlowManager"
 
  return deployContract({ code, name, processed: true, ...props })
}