account_info.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: Apache-2.0
  2. use crate::{build_solidity, BorshToken};
  3. use num_bigint::BigInt;
  4. #[test]
  5. fn lamports() {
  6. let mut vm = build_solidity(
  7. r#"
  8. import 'solana';
  9. contract c {
  10. function test(address needle) public payable returns (uint64) {
  11. for (uint32 i = 0; i < tx.accounts.length; i++) {
  12. AccountInfo ai = tx.accounts[i];
  13. assert(ai.is_writable);
  14. assert(!ai.is_signer);
  15. assert(ai.executable);
  16. if (ai.key == needle) {
  17. return ai.lamports;
  18. }
  19. }
  20. revert("account not found");
  21. }
  22. }"#,
  23. );
  24. vm.constructor(&[]);
  25. vm.account_data.get_mut(&vm.origin).unwrap().lamports = 17672630920854456917u64;
  26. let returns = vm
  27. .function("test", &[BorshToken::Address(vm.origin)])
  28. .unwrap();
  29. assert_eq!(
  30. returns,
  31. BorshToken::Uint {
  32. width: 64,
  33. value: BigInt::from(17672630920854456917u64),
  34. }
  35. );
  36. }
  37. #[test]
  38. fn owner() {
  39. let mut vm = build_solidity(
  40. r#"
  41. import 'solana';
  42. contract c {
  43. function test() public payable returns (address) {
  44. for (uint32 i = 0; i < tx.accounts.length; i++) {
  45. AccountInfo ai = tx.accounts[i];
  46. if (ai.key == address(this)) {
  47. return ai.owner;
  48. }
  49. }
  50. revert("account not found");
  51. }
  52. }"#,
  53. );
  54. vm.constructor(&[]);
  55. let returns = vm.function("test", &[]).unwrap();
  56. let owner = vm.stack[0].program;
  57. assert_eq!(returns, BorshToken::Address(owner));
  58. }
  59. #[test]
  60. fn data() {
  61. let mut vm = build_solidity(
  62. r#"
  63. import 'solana';
  64. contract c {
  65. function test(uint32 index) public payable returns (uint8) {
  66. for (uint32 i = 0; i < tx.accounts.length; i++) {
  67. AccountInfo ai = tx.accounts[i];
  68. if (ai.key == address(this)) {
  69. return ai.data[index];
  70. }
  71. }
  72. revert("account not found");
  73. }
  74. function test2() public payable returns (uint32) {
  75. for (uint32 i = 0; i < tx.accounts.length; i++) {
  76. AccountInfo ai = tx.accounts[i];
  77. if (ai.key == address(this)) {
  78. return ai.data.readUint32LE(1);
  79. }
  80. }
  81. revert("account not found");
  82. }
  83. }"#,
  84. );
  85. vm.constructor(&[]);
  86. for i in 0..10 {
  87. let returns = vm
  88. .function(
  89. "test",
  90. &[BorshToken::Uint {
  91. width: 32,
  92. value: BigInt::from(i),
  93. }],
  94. )
  95. .unwrap();
  96. let this = &vm.stack[0].data;
  97. let val = vm.account_data[this].data[i];
  98. assert_eq!(
  99. returns,
  100. BorshToken::Uint {
  101. width: 8,
  102. value: BigInt::from(val),
  103. }
  104. );
  105. }
  106. let returns = vm.function("test2", &[]).unwrap();
  107. let this = &vm.stack[0].data;
  108. let val = u32::from_le_bytes(vm.account_data[this].data[1..5].try_into().unwrap());
  109. assert_eq!(
  110. returns,
  111. BorshToken::Uint {
  112. width: 32,
  113. value: BigInt::from(val),
  114. }
  115. );
  116. }