All files / src/generated/scripts getBalance.js

63.64% Statements 7/11
0% Branches 0/4
50% Functions 1/2
63.64% Lines 7/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                    12x                                     12x 1x 1x           1x   1x     12x              
/** pragma type script **/
 
import {
  getEnvironment,
  replaceImportAddresses,
  reportMissingImports,
  reportMissing,
  executeScript
} from '@onflow/flow-cadut'
 
export const CODE = `
// This script reads the balance field of an account's FlowToken Balance
 
import FungibleToken from 0xFUNGIBLETOKENADDRESS
import ExampleToken from 0xTOKENADDRESS
 
pub fun main(account: Address): UFix64 {
    let acct = getAccount(account)
    let vaultRef = acct.getCapability(/public/exampleTokenBalance)!.borrow<&ExampleToken.Vault{FungibleToken.Balance}>()
        ?? panic("Could not borrow Balance reference to the Vault")
 
    return vaultRef.balance
}
`;
 
/**
* Method to generate cadence code for getBalance script
* @param {Object.<string, string>} addressMap - contract name as a key and address where it's deployed as value
*/
export const getBalanceTemplate = 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, `getBalance =>`)
 
  return replaceImportAddresses(CODE, fullMap);
};
 
export const getBalance = async (props = {}) => {
  const { addressMap = {}, args = [] } = props
  const code = await getBalanceTemplate(addressMap);
 
  reportMissing("arguments", args.length, 1, `getBalance =>`);
 
  return executeScript({code, processed: true, ...props})
}