Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Change flow-cadut to @onflow/cadut & version bump #138

Merged
merged 4 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/giant-dots-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/flow-js-testing": patch
---

Version bump @onflow/flow-cadut (fixes "transaction" keyword not allowed in templates & adds PublicPath/PrivatePath/StoragePath/CapabilityPath argument compatibility)
443 changes: 223 additions & 220 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
"@onflow/config": "^0.0.2",
"@onflow/fcl": "^0.0.78",
"@onflow/fcl-config": "^0.0.1",
"@onflow/flow-cadut": "^0.2.0-alpha.7",
"@onflow/types": "^0.0.6",
"elliptic": "^6.5.4",
"esm": "^3.2.25",
"flow-cadut": "^0.1.15",
"jest-environment-uint8array": "^1.0.0",
"rimraf": "^3.0.2",
"rlp": "^2.2.6",
Expand Down
6 changes: 5 additions & 1 deletion src/deploy-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import {defaultsByName, getContractCode} from "./file"

import txRegistry from "./generated/transactions"
import {isObject} from "./utils"
import {extractContractParameters, generateSchema, splitArgs} from "flow-cadut"
import {
extractContractParameters,
generateSchema,
splitArgs,
} from "@onflow/flow-cadut"
import {replaceImportAddresses, resolveImports} from "./imports"

const {updateContractTemplate, deployContractTemplate} = txRegistry
Expand Down
312 changes: 169 additions & 143 deletions src/generated/contracts/FlowManager.js
Original file line number Diff line number Diff line change
@@ -1,143 +1,169 @@
/** pragma type contract **/

import {
getEnvironment,
replaceImportAddresses,
reportMissingImports,
deployContract,
} from '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/testSuitAccountManager
self.contractManagerStorage = /storage/testSuitContractManager
self.accountManagerPath = /public/testSuitAccountManager
self.contractManagerPath = /public/testSuitContractManager

// 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 })
}

/** 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 })
}
2 changes: 1 addition & 1 deletion src/generated/scripts/checkManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from 'flow-cadut'
} from '@onflow/flow-cadut'

export const CODE = `
import FlowManager from 0x01
Expand Down
2 changes: 1 addition & 1 deletion src/generated/scripts/getAccountAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from 'flow-cadut'
} from '@onflow/flow-cadut'

export const CODE = `
import FlowManager from 0x01
Expand Down
2 changes: 1 addition & 1 deletion src/generated/scripts/getBalance.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from 'flow-cadut'
} from '@onflow/flow-cadut'

export const CODE = `
// This script reads the balance field of an account's FlowToken Balance
Expand Down
2 changes: 1 addition & 1 deletion src/generated/scripts/getBlockOffset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from 'flow-cadut'
} from '@onflow/flow-cadut'

export const CODE = `
import FlowManager from 0x01
Expand Down
2 changes: 1 addition & 1 deletion src/generated/scripts/getContractAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from 'flow-cadut'
} from '@onflow/flow-cadut'

export const CODE = `
import FlowManager from 0x01
Expand Down
2 changes: 1 addition & 1 deletion src/generated/scripts/getManagerAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
reportMissingImports,
reportMissing,
executeScript
} from 'flow-cadut'
} from '@onflow/flow-cadut'

export const CODE = `
pub fun main(serviceAddress: Address): Address? {
Expand Down
Loading