-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify backend environment for Unit & Integration tests
- Loading branch information
Showing
5 changed files
with
1,248 additions
and
870 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,76 @@ | ||
import Test | ||
|
||
pub struct BlockchainHelpers { | ||
pub let blockchain: Test.Blockchain | ||
/// Returns the current block height of the blockchain. | ||
/// | ||
access(all) | ||
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. | ||
/// | ||
access(all) | ||
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. | ||
/// | ||
access(all) | ||
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. | ||
/// | ||
access(all) | ||
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)) | ||
} |
Oops, something went wrong.