create_contract.sol 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import 'solana';
  2. contract creator {
  3. function create_child() external {
  4. print("Going to create child");
  5. Child.new();
  6. Child.say_hello();
  7. }
  8. function create_seed1(bytes seed, bytes1 bump, uint64 space) external {
  9. print("Going to create Seed1");
  10. Seed1.new(seed, bump, space);
  11. Seed1.say_hello();
  12. }
  13. function create_seed2(bytes seed, uint32 space) external {
  14. print("Going to create Seed2");
  15. Seed2.new(seed, space);
  16. }
  17. @mutableSigner(child)
  18. @mutableSigner(payer)
  19. function create_child_with_metas() external {
  20. print("Going to create child with metas");
  21. AccountMeta[3] metas = [
  22. AccountMeta({pubkey: tx.accounts.child.key, is_signer: true, is_writable: true}),
  23. AccountMeta({pubkey: tx.accounts.payer.key, is_signer: true, is_writable: true}),
  24. AccountMeta({pubkey: address"11111111111111111111111111111111", is_writable: false, is_signer: false})
  25. ];
  26. Child.new{accounts: metas}();
  27. Child.use_metas();
  28. }
  29. function create_without_annotation() external {
  30. MyCreature.new();
  31. MyCreature.say_my_name{accounts: []}();
  32. }
  33. @signer(my_signer)
  34. function call_with_signer() view external {
  35. require(tx.accounts.my_signer.is_signer, "the signer must sign the transaction");
  36. print("Signer found");
  37. }
  38. }
  39. @program_id("Chi1d5XD6nTAp2EyaNGqMxZzUjh6NvhXRxbGHP3D1RaT")
  40. contract Child {
  41. @payer(payer)
  42. @space(511 + 7)
  43. constructor() {
  44. assert(tx.accounts.payer.is_signer);
  45. assert(tx.accounts.payer.is_writable);
  46. print("In child constructor");
  47. }
  48. function say_hello() pure public {
  49. print("Hello there");
  50. }
  51. function use_metas() pure public {
  52. print("I am using metas");
  53. }
  54. }
  55. @program_id("SeedHw4CsFsDEGu2AVwFM1toGXsbAJSKnb7kS8TrLxu")
  56. contract Seed1 {
  57. bytes saved_seed;
  58. bytes1 saved_bump;
  59. @payer(payer)
  60. constructor(@seed bytes seed, @bump bytes1 bump, @space uint64 space) {
  61. print("In Seed1 constructor");
  62. saved_seed = seed;
  63. saved_bump = bump;
  64. }
  65. function say_hello() pure public {
  66. print("Hello from Seed1");
  67. }
  68. @account(creator_program_id)
  69. function sign() view external {
  70. AccountMeta[1] metas = [
  71. AccountMeta({pubkey: tx.accounts.dataAccount.key, is_signer: true, is_writable: false})
  72. ];
  73. creator.call_with_signer{seeds: [ [ saved_seed, saved_bump ] ],
  74. accounts: metas,
  75. program_id: tx.accounts.creator_program_id.key}();
  76. }
  77. }
  78. @program_id("Seed23VDZ9HFCfKvFwmemB6dpi25n5XjZdP52B2RUmh")
  79. contract Seed2 {
  80. bytes my_seed;
  81. @payer(payer)
  82. @seed("sunflower")
  83. constructor(@seed bytes seed, @space uint64 space) {
  84. my_seed = seed;
  85. print("In Seed2 constructor");
  86. }
  87. function check() public view {
  88. address pda = create_program_address([ "sunflower", my_seed ], address(this));
  89. if (pda == tx.accounts.dataAccount.key) {
  90. print("I am PDA.");
  91. }
  92. }
  93. @account(creator_program_id)
  94. function sign() view external {
  95. bytes[2][1] seeds = [ [ "sunflower", my_seed ] ];
  96. sign2(seeds, tx.accounts.dataAccount.key, tx.accounts.creator_program_id.key);
  97. }
  98. function sign2(bytes[2][1] seeds, address child, address creator_program_id) view internal {
  99. AccountMeta[1] metas = [
  100. AccountMeta({pubkey: child, is_signer: true, is_writable: false})
  101. ];
  102. creator.call_with_signer{seeds: seeds, accounts: metas, program_id: creator_program_id}();
  103. }
  104. @account(pdaSigner)
  105. @account(creatorId)
  106. function pda_sign(uint8 bump) external view {
  107. (address pda, bytes1 _bump) = try_find_program_address(["mint_authority"], address(this));
  108. assert(bump == _bump);
  109. assert(pda == tx.accounts.pdaSigner.key);
  110. AccountMeta[1] metas = [
  111. AccountMeta({pubkey: tx.accounts.pdaSigner.key, is_signer: true, is_writable: false})
  112. ];
  113. creator.call_with_signer{accounts: metas, seeds: [["mint_authority", abi.encode(_bump)]], program_id: tx.accounts.creatorId.key}();
  114. }
  115. }
  116. @program_id("8gTkAidfM82u3DGbKcZpHwL5p47KQA16MDb4WmrHdmF6")
  117. contract MyCreature {
  118. constructor() {
  119. print("In child constructor");
  120. }
  121. function say_my_name() public pure {
  122. print("say_my_name");
  123. }
  124. }