瀏覽代碼

lang: Use Expr for init payer. (#772)

Alan O'Donnell 4 年之前
父節點
當前提交
5bd0309f66
共有 4 個文件被更改,包括 42 次插入2 次删除
  1. 2 2
      lang/syn/src/lib.rs
  2. 8 0
      tests/misc/programs/misc/src/context.rs
  3. 7 0
      tests/misc/programs/misc/src/lib.rs
  4. 25 0
      tests/misc/tests/misc.js

+ 2 - 2
lang/syn/src/lib.rs

@@ -611,7 +611,7 @@ pub enum ConstraintRentExempt {
 #[derive(Debug, Clone)]
 pub struct ConstraintInitGroup {
     pub seeds: Option<ConstraintSeedsGroup>,
-    pub payer: Option<Ident>,
+    pub payer: Option<Expr>,
     pub space: Option<Expr>,
     pub kind: InitKind,
 }
@@ -638,7 +638,7 @@ pub struct ConstraintState {
 
 #[derive(Debug, Clone)]
 pub struct ConstraintPayer {
-    pub target: Ident,
+    pub target: Expr,
 }
 
 #[derive(Debug, Clone)]

+ 8 - 0
tests/misc/programs/misc/src/context.rs

@@ -185,3 +185,11 @@ pub struct TestInitToken<'info> {
     pub system_program: AccountInfo<'info>,
     pub token_program: AccountInfo<'info>,
 }
+
+#[derive(Accounts)]
+pub struct TestCompositePayer<'info> {
+    pub composite: TestInit<'info>,
+    #[account(init, payer = composite.payer, space = 8 + size_of::<Data>())]
+    pub data: Account<'info, Data>,
+    pub system_program: Program<'info, System>,
+}

+ 7 - 0
tests/misc/programs/misc/src/lib.rs

@@ -152,4 +152,11 @@ pub mod misc {
         assert!(ctx.accounts.token.mint == ctx.accounts.mint.key());
         Ok(())
     }
+
+    pub fn test_composite_payer(ctx: Context<TestCompositePayer>) -> ProgramResult {
+        ctx.accounts.composite.data.data = 1;
+        ctx.accounts.data.udata = 2;
+        ctx.accounts.data.idata = 3;
+        Ok(())
+    }
 }

+ 25 - 0
tests/misc/tests/misc.js

@@ -578,4 +578,29 @@ describe("misc", () => {
     assert.ok(account.owner.equals(program.provider.wallet.publicKey));
     assert.ok(account.mint.equals(mint.publicKey));
   });
+
+  it("Can initialize multiple accounts via a composite payer", async () => {
+    const data1 = anchor.web3.Keypair.generate();
+    const data2 = anchor.web3.Keypair.generate();
+
+    const tx = await program.rpc.testCompositePayer({
+      accounts: {
+        composite: {
+          data: data1.publicKey,
+          payer: program.provider.wallet.publicKey,
+          systemProgram: anchor.web3.SystemProgram.programId,
+        },
+        data: data2.publicKey,
+        systemProgram: anchor.web3.SystemProgram.programId,
+      },
+      signers: [data1, data2]
+    });
+
+    const account1 = await program.account.dataI8.fetch(data1.publicKey);
+    assert.equal(account1.data, 1);
+
+    const account2 = await program.account.data.fetch(data2.publicKey);
+    assert.equal(account2.udata, 2);
+    assert.equal(account2.idata, 3);
+  });
 });