Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Sep 15, 2023
1 parent 4bd67cc commit 9f32293
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 392 deletions.
126 changes: 59 additions & 67 deletions test/blockchain_helpers.cdc
Original file line number Diff line number Diff line change
@@ -1,80 +1,72 @@
import Test

pub struct BlockchainHelpers {
pub let blockchain: Test.Blockchain
/// Returns the current block height of the blockchain.
///
pub fun getCurrentBlockHeight(): UInt64 {
let script = readFile("get_current_block_height.cdc")
let scriptResult = Test.executeScript(script, [])

init(blockchain: Test.Blockchain) {
self.blockchain = blockchain
}

/// Returns the current block height of the blockchain.
///
pub fun getCurrentBlockHeight(): UInt64 {
let script = self.readFile("get_current_block_height.cdc")
let scriptResult = self.blockchain.executeScript(script, [])

if scriptResult.status == Test.ResultStatus.failed {
panic(scriptResult.error!.message)
}
return scriptResult.returnValue! as! UInt64
if scriptResult.status == Test.ResultStatus.failed {
panic(scriptResult.error!.message)
}
return scriptResult.returnValue! as! UInt64
}

/// Returns the Flow token balance for the given account.
///
pub fun getFlowBalance(for account: Test.Account): UFix64 {
let script = self.readFile("get_flow_balance.cdc")
let scriptResult = self.blockchain.executeScript(script, [account.address])
/// Returns the Flow token balance for the given account.
///
pub fun getFlowBalance(for account: Test.Account): UFix64 {
let script = readFile("get_flow_balance.cdc")
let scriptResult = Test.executeScript(script, [account.address])

if scriptResult.status == Test.ResultStatus.failed {
panic(scriptResult.error!.message)
}
return scriptResult.returnValue! as! UFix64
if scriptResult.status == Test.ResultStatus.failed {
panic(scriptResult.error!.message)
}
return scriptResult.returnValue! as! UFix64
}

/// Mints the given amount of Flow tokens to a specified test account.
/// The transaction is authorized and signed by the service account.
/// Returns the result of the transaction.
///
pub fun mintFlow(
to receiver: Test.Account,
amount: UFix64
): Test.TransactionResult {
let code = self.readFile("mint_flow.cdc")
let tx = Test.Transaction(
code: code,
authorizers: [self.blockchain.serviceAccount().address],
signers: [],
arguments: [receiver.address, amount]
)
/// Mints the given amount of Flow tokens to a specified test account.
/// The transaction is authorized and signed by the service account.
/// Returns the result of the transaction.
///
pub fun mintFlow(
to receiver: Test.Account,
amount: UFix64
): Test.TransactionResult {
let code = readFile("mint_flow.cdc")
let tx = Test.Transaction(
code: code,
authorizers: [Test.serviceAccount().address],
signers: [],
arguments: [receiver.address, amount]
)

return self.blockchain.executeTransaction(tx)
}
return Test.executeTransaction(tx)
}

/// Burns the specified amount of Flow tokens for the given
/// test account. Returns the result of the transaction.
///
pub fun burnFlow(
from account: Test.Account,
amount: UFix64
): Test.TransactionResult {
let code = self.readFile("burn_flow.cdc")
let tx = Test.Transaction(
code: code,
authorizers: [account.address],
signers: [account],
arguments: [amount]
)
/// Burns the specified amount of Flow tokens for the given
/// test account. Returns the result of the transaction.
///
pub fun burnFlow(
from account: Test.Account,
amount: UFix64
): Test.TransactionResult {
let code = readFile("burn_flow.cdc")
let tx = Test.Transaction(
code: code,
authorizers: [account.address],
signers: [account],
arguments: [amount]
)

return self.blockchain.executeTransaction(tx)
}
return Test.executeTransaction(tx)
}

/// Reads the code for the script/transaction with the given
/// file name and returns its content as a String.
///
access(self)
fun readFile(_ name: String): String {
// The "\u{0}helper/" prefix is used in order to prevent
// conflicts with user-defined scripts/transactions.
return Test.readFile("\u{0}helper/".concat(name))
}
/// Reads the code for the script/transaction with the given
/// file name and returns its content as a String.
///
access(self)
fun readFile(_ name: String): String {
// The "\u{0}helper/" prefix is used in order to prevent
// conflicts with user-defined scripts/transactions.
return Test.readFile("\u{0}helper/".concat(name))
}
17 changes: 11 additions & 6 deletions test/emulator_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,18 @@ func (e *EmulatorBackend) replaceImports(code string) string {

address, ok := e.configuration.Addresses[location.String()]
if !ok {
contractAddress, ok := e.contracts[importDeclaration.Identifiers[0].Identifier]
if ok {
address, _ = common.HexToAddress(contractAddress)
if len(importDeclaration.Identifiers) > 0 {
contractAddress, ok := e.contracts[importDeclaration.Identifiers[0].Identifier]
if ok {
address, _ = common.HexToAddress(contractAddress)
} else {
// keep import statement it as-is
sb.WriteString(code[prevImportDeclEnd:importDeclEnd])
continue
}
} else {
// keep import statement it as-is
sb.WriteString(code[prevImportDeclEnd:importDeclEnd])
continue
contractAddress := e.contracts[location.String()]
address, _ = common.HexToAddress(contractAddress)
}
}

Expand Down
Loading

0 comments on commit 9f32293

Please sign in to comment.