external_call.sol 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. contract caller {
  2. @account(callee_pid)
  3. function do_call(int64 v) external {
  4. callee.set_x{program_id: tx.accounts.callee_pid.key}(v);
  5. }
  6. @account(callee_pid)
  7. function do_call2(int64 v) view external returns (int64) {
  8. return v + callee.get_x{program_id: tx.accounts.callee_pid.key}();
  9. }
  10. // call two different functions
  11. @account(callee_pid)
  12. @account(callee2_pid)
  13. function do_call3(int64[4] memory x, string memory y) external returns (int64, string memory) {
  14. return (callee2.do_stuff{program_id: tx.accounts.callee2_pid.key}(x),
  15. callee.get_name{program_id: tx.accounts.callee_pid.key}());
  16. }
  17. // call two different functions
  18. @account(callee_pid)
  19. @account(callee2_pid)
  20. function do_call4(int64[4] memory x, string memory y) external returns (int64, string memory) {
  21. return (callee2.do_stuff{program_id: tx.accounts.callee2_pid.key}(x),
  22. callee.call2{program_id: tx.accounts.callee_pid.key}(y));
  23. }
  24. function who_am_i() public view returns (address) {
  25. return address(this);
  26. }
  27. }
  28. contract callee {
  29. int64 x;
  30. function set_x(int64 v) public {
  31. x = v;
  32. }
  33. function get_x() public view returns (int64) {
  34. return x;
  35. }
  36. @account(other_callee2)
  37. function call2(string s) external returns (string) {
  38. return callee2.do_stuff2{program_id: tx.accounts.other_callee2.key}(s);
  39. }
  40. function get_name() public pure returns (string) {
  41. return "my name is callee";
  42. }
  43. }
  44. contract callee2 {
  45. function do_stuff(int64[4] memory x) public pure returns (int64) {
  46. int64 total = 0;
  47. for (uint i=0; i< x.length; i++) {
  48. total += x[i];
  49. }
  50. return total;
  51. }
  52. function do_stuff2(string x) public pure returns (string) {
  53. return string.concat("x:", x);
  54. }
  55. }