|
| 1 | +import * as StellarSdk from '@stellar/stellar-sdk'; |
| 2 | +import { readFileSync } from 'fs'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import path from 'path'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | +import { call_contract_function, extractLogEvent } from './test_helpers.js'; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const dirname = path.dirname(__filename); |
| 10 | +const server = new StellarSdk.SorobanRpc.Server("https://soroban-testnet.stellar.org:443"); |
| 11 | + |
| 12 | +function readContractAddress(filename) { |
| 13 | + return readFileSync(path.join(dirname, '.soroban', 'contract-ids', filename), 'utf8').trim(); |
| 14 | +} |
| 15 | + |
| 16 | +describe('Cross Contract Calls', () => { |
| 17 | + let keypair, caller, callee, calleeRust; |
| 18 | + |
| 19 | + before(async () => { |
| 20 | + console.log('Setting up cross contract tests...'); |
| 21 | + |
| 22 | + keypair = StellarSdk.Keypair.fromSecret(readFileSync('alice.txt', 'utf8').trim()); |
| 23 | + caller = new StellarSdk.Contract(readContractAddress('caller.txt')); |
| 24 | + callee = new StellarSdk.Contract(readContractAddress('callee.txt')); |
| 25 | + calleeRust = new StellarSdk.Contract(readContractAddress('hello_world.txt')); |
| 26 | + }); |
| 27 | + |
| 28 | + it('calls Rust contract', async () => { |
| 29 | + let addr = calleeRust.address().toScVal(); |
| 30 | + let values = [ |
| 31 | + new StellarSdk.xdr.Uint64(BigInt(1)), |
| 32 | + new StellarSdk.xdr.Uint64(BigInt(2)), |
| 33 | + new StellarSdk.xdr.Uint64(BigInt(0)) |
| 34 | + ].map(StellarSdk.xdr.ScVal.scvU64); |
| 35 | + |
| 36 | + let res = await call_contract_function("add", server, keypair, caller, addr, ...values); |
| 37 | + let returnValue = res.returnValue().value().toString(); |
| 38 | + |
| 39 | + console.log(returnValue); |
| 40 | + expect(returnValue).to.equal("3"); |
| 41 | + |
| 42 | + let logMessages = extractLogEvent(res.diagnosticEvents()).logMessages; |
| 43 | + console.log(logMessages); |
| 44 | + expect(logMessages[0]).to.contain('Soroban SDK add function called!'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('calls Solidity contract', async () => { |
| 48 | + let addr = callee.address().toScVal(); |
| 49 | + let values = [ |
| 50 | + new StellarSdk.xdr.Uint64(BigInt(1)), |
| 51 | + new StellarSdk.xdr.Uint64(BigInt(2)), |
| 52 | + new StellarSdk.xdr.Uint64(BigInt(0)) |
| 53 | + ].map(StellarSdk.xdr.ScVal.scvU64); |
| 54 | + |
| 55 | + let res = await call_contract_function("add", server, keypair, caller, addr, ...values); |
| 56 | + let returnValue = res.returnValue().value().toString(); |
| 57 | + |
| 58 | + console.log(returnValue); |
| 59 | + expect(returnValue).to.equal("3"); |
| 60 | + |
| 61 | + let logMessages = extractLogEvent(res.diagnosticEvents()).logMessages; |
| 62 | + console.log(logMessages); |
| 63 | + expect(logMessages[0]).to.contain('add called in Solidity'); |
| 64 | + }); |
| 65 | +}); |
0 commit comments