calls.spec.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: Apache-2.0
  2. import expect from 'expect';
  3. import { loadContractAndCallConstructor, loadContractWithProvider } from './setup';
  4. import { BN } from '@coral-xyz/anchor';
  5. describe('Testing calls', function () {
  6. this.timeout(100000);
  7. it('external_call', async function () {
  8. let caller = await loadContractAndCallConstructor('caller');
  9. const provider = caller.provider;
  10. const callee = await loadContractWithProvider(provider, 'callee');
  11. const callee2 = await loadContractWithProvider(provider, 'callee2');
  12. await callee.program.methods.setX(new BN(102))
  13. .accounts({ dataAccount: callee.storage.publicKey })
  14. .rpc();
  15. let res = await callee.program.methods.getX()
  16. .accounts({ dataAccount: callee.storage.publicKey })
  17. .view();
  18. expect(res).toEqual(new BN(102));
  19. res = await caller.program.methods.whoAmI()
  20. .view();
  21. expect(res).toStrictEqual(caller.program_key);
  22. await caller.program.methods.doCall(callee.program_key, new BN(13123))
  23. .accounts({
  24. callee_dataAccount: callee.storage.publicKey,
  25. callee_programId: callee.program_key,
  26. })
  27. .rpc();
  28. res = await callee.program.methods.getX()
  29. .accounts({ dataAccount: callee.storage.publicKey })
  30. .view();
  31. expect(res).toEqual(new BN(13123));
  32. res = await caller.program.methods.doCall2(callee.program_key, new BN(20000))
  33. .accounts({
  34. callee_dataAccount: callee.storage.publicKey,
  35. callee_programId: callee.program_key,
  36. })
  37. .view();
  38. expect(res).toEqual(new BN(33123));
  39. res = await caller.program.methods.doCall3(callee.program_key, callee2.program_key, [new BN(3), new BN(5), new BN(7), new BN(9)], "yo")
  40. .accounts({
  41. callee2_programId: callee2.program_key,
  42. callee_programId: callee.program_key,
  43. })
  44. .view();
  45. expect(res.return0).toEqual(new BN(24));
  46. expect(res.return1).toBe("my name is callee");
  47. res = await caller.program.methods.doCall4(callee.program_key, callee2.program_key, [new BN(1), new BN(2), new BN(3), new BN(4)], "asda")
  48. .accounts({
  49. callee2_programId: callee2.program_key,
  50. callee_programId: callee.program_key,
  51. })
  52. .view();
  53. expect(res.return0).toEqual(new BN(10));
  54. expect(res.return1).toBe("x:asda");
  55. });
  56. });