-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add MockVm tests and integration tests
Signed-off-by: salaheldinsoliman <salaheldin_sameh@aucegypt.edu>
- Loading branch information
1 parent
320832c
commit 88ff09b
Showing
9 changed files
with
300 additions
and
26 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
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as StellarSdk from '@stellar/stellar-sdk'; | ||
import { readFileSync } from 'fs'; | ||
import { expect } from 'chai'; | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
import { call_contract_function } from './test_helpers.js'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const dirname = path.dirname(__filename); | ||
|
||
describe('Counter', () => { | ||
let keypair; | ||
const server = new StellarSdk.SorobanRpc.Server( | ||
"https://soroban-testnet.stellar.org:443", | ||
); | ||
|
||
let contractAddr; | ||
let contract; | ||
before(async () => { | ||
|
||
console.log('Setting up counter contract tests...'); | ||
|
||
// read secret from file | ||
const secret = readFileSync('alice.txt', 'utf8').trim(); | ||
keypair = StellarSdk.Keypair.fromSecret(secret); | ||
|
||
let contractIdFile = path.join(dirname, '.soroban', 'contract-ids', 'counter.txt'); | ||
// read contract address from file | ||
contractAddr = readFileSync(contractIdFile, 'utf8').trim().toString(); | ||
|
||
// load contract | ||
contract = new StellarSdk.Contract(contractAddr); | ||
|
||
// initialize the contract | ||
await call_contract_function("init", server, keypair, contract); | ||
|
||
}); | ||
|
||
it('get correct initial counter', async () => { | ||
// get the count | ||
let count = await call_contract_function("count", server, keypair, contract); | ||
expect(count.toString()).eq("10"); | ||
}); | ||
|
||
it('increment counter', async () => { | ||
// increment the counter | ||
await call_contract_function("increment", server, keypair, contract); | ||
|
||
// get the count | ||
let count = await call_contract_function("count", server, keypair, contract); | ||
expect(count.toString()).eq("11"); | ||
}); | ||
}); | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"type": "module", | ||
"dependencies": { | ||
"@stellar/stellar-sdk": "^12.0.1", | ||
"chai": "^5.1.1", | ||
"dotenv": "^16.4.5", | ||
"mocha": "^10.4.0" | ||
}, | ||
"scripts": { | ||
"build": "solang compile *.sol --target soroban", | ||
"setup": "node setup.js", | ||
"test": "mocha *.spec.js --timeout 20000" | ||
}, | ||
"devDependencies": { | ||
"@eslint/js": "^9.4.0", | ||
"@types/mocha": "^10.0.6", | ||
"eslint": "^9.4.0", | ||
"expect": "^29.7.0", | ||
"globals": "^15.4.0", | ||
"typescript": "^5.4.5" | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
import 'dotenv/config'; | ||
import { mkdirSync, readdirSync} from 'fs'; | ||
import { execSync } from 'child_process'; | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
console.log("###################### Initializing ########################"); | ||
|
||
// Get dirname (equivalent to the Bash version) | ||
const __filename = fileURLToPath(import.meta.url); | ||
const dirname = path.dirname(__filename); | ||
|
||
// variable for later setting pinned version of soroban in "$(dirname/target/bin/soroban)" | ||
const soroban = "soroban" | ||
|
||
// Function to execute and log shell commands | ||
function exe(command) { | ||
console.log(command); | ||
execSync(command, { stdio: 'inherit' }); | ||
} | ||
|
||
function generate_alice() { | ||
exe(`${soroban} keys generate alice --network testnet`); | ||
|
||
// get the secret key of alice and put it in alice.txt | ||
exe(`${soroban} keys show alice > alice.txt`); | ||
} | ||
|
||
|
||
function filenameNoExtension(filename) { | ||
return path.basename(filename, path.extname(filename)); | ||
} | ||
|
||
function deploy(wasm) { | ||
|
||
let contractId = path.join(dirname, '.soroban', 'contract-ids', filenameNoExtension(wasm) + '.txt'); | ||
|
||
exe(`(${soroban} contract deploy --wasm ${wasm} --ignore-checks --source-account alice --network testnet) > ${contractId}`); | ||
} | ||
|
||
function deploy_all() { | ||
const contractsDir = path.join(dirname, '.soroban', 'contract-ids'); | ||
mkdirSync(contractsDir, { recursive: true }); | ||
|
||
const wasmFiles = readdirSync(`${dirname}`).filter(file => file.endsWith('.wasm')); | ||
|
||
wasmFiles.forEach(wasmFile => { | ||
deploy(path.join(dirname, wasmFile)); | ||
}); | ||
} | ||
|
||
function add_testnet() { | ||
|
||
exe(`${soroban} network add \ | ||
--global testnet \ | ||
--rpc-url https://soroban-testnet.stellar.org:443 \ | ||
--network-passphrase "Test SDF Network ; September 2015"`); | ||
} | ||
|
||
add_testnet(); | ||
generate_alice(); | ||
deploy_all(); |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as StellarSdk from '@stellar/stellar-sdk'; | ||
|
||
|
||
|
||
async function call_contract_function(method, server, keypair, contract) { | ||
|
||
let res; | ||
let builtTransaction = new StellarSdk.TransactionBuilder(await server.getAccount(keypair.publicKey()), { | ||
fee: StellarSdk.BASE_FEE, | ||
networkPassphrase: StellarSdk.Networks.TESTNET, | ||
}).addOperation(contract.call(method)).setTimeout(30).build(); | ||
|
||
let preparedTransaction = await server.prepareTransaction(builtTransaction); | ||
|
||
// Sign the transaction with the source account's keypair. | ||
preparedTransaction.sign(keypair); | ||
|
||
try { | ||
let sendResponse = await server.sendTransaction(preparedTransaction); | ||
if (sendResponse.status === "PENDING") { | ||
let getResponse = await server.getTransaction(sendResponse.hash); | ||
// Poll `getTransaction` until the status is not "NOT_FOUND" | ||
while (getResponse.status === "NOT_FOUND") { | ||
console.log("Waiting for transaction confirmation..."); | ||
// See if the transaction is complete | ||
getResponse = await server.getTransaction(sendResponse.hash); | ||
// Wait one second | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
} | ||
|
||
if (getResponse.status === "SUCCESS") { | ||
// Make sure the transaction's resultMetaXDR is not empty | ||
if (!getResponse.resultMetaXdr) { | ||
throw "Empty resultMetaXDR in getTransaction response"; | ||
} | ||
// Find the return value from the contract and return it | ||
let transactionMeta = getResponse.resultMetaXdr; | ||
let returnValue = transactionMeta.v3().sorobanMeta().returnValue(); | ||
console.log(`Transaction result: ${returnValue.value()}`); | ||
res = returnValue.value(); | ||
} else { | ||
throw `Transaction failed: ${getResponse.resultXdr}`; | ||
} | ||
} else { | ||
throw sendResponse.errorResultXdr; | ||
} | ||
} catch (err) { | ||
// Catch and report any errors we've thrown | ||
console.log("Sending transaction failed"); | ||
console.log(err); | ||
} | ||
return res; | ||
} | ||
|
||
export { call_contract_function }; |
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
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
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,2 +1,3 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
mod math; | ||
mod storage; |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::build_solidity; | ||
use soroban_sdk::{IntoVal, Val}; | ||
|
||
#[test] | ||
fn counter() { | ||
let src = build_solidity( | ||
r#"contract counter { | ||
uint64 public count = 10; | ||
function increment() public returns (uint64){ | ||
count += 1; | ||
return count; | ||
} | ||
function decrement() public returns (uint64){ | ||
count -= 1; | ||
return count; | ||
} | ||
}"#, | ||
); | ||
|
||
let addr = src.contracts.last().unwrap(); | ||
|
||
let _res = src.invoke_contract(addr, "init", vec![]); | ||
|
||
let res = src.invoke_contract(addr, "count", vec![]); | ||
let expected: Val = 10_u64.into_val(&src.env); | ||
assert!(expected.shallow_eq(&res)); | ||
|
||
src.invoke_contract(addr, "increment", vec![]); | ||
let res = src.invoke_contract(addr, "count", vec![]); | ||
let expected: Val = 11_u64.into_val(&src.env); | ||
assert!(expected.shallow_eq(&res)); | ||
|
||
src.invoke_contract(addr, "decrement", vec![]); | ||
let res = src.invoke_contract(addr, "count", vec![]); | ||
let expected: Val = 10_u64.into_val(&src.env); | ||
assert!(expected.shallow_eq(&res)); | ||
} |