Skip to content

Commit e77b1dc

Browse files
authored
Merge pull request #53 from LucasSte/solang-release
Update examples according to use the annotation syntax
2 parents bbe6a8d + 3a4941b commit e77b1dc

File tree

37 files changed

+537
-755
lines changed

37 files changed

+537
-755
lines changed

basics/checking-accounts/solang/solidity/checking-accounts.sol

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,15 @@ contract checking_accounts {
88
@payer(payer) // "payer" is the account that pays to create the dataAccount
99
constructor() {}
1010

11-
function checkAccounts(address accountToChange, address accountToCreate) public view {
11+
@account(accountToChange)
12+
@mutableSigner(accountToCreate)
13+
function checkAccounts(address accountToChange, address accountToCreate) external view {
1214
print("Number of Accounts Provided: {:}".format(tx.accounts.length));
1315

14-
// Find the accounts we are looking for and perform checks on them
15-
for (uint64 i = 0; i < tx.accounts.length; i++) {
16-
if (tx.accounts[i].key == accountToChange) {
17-
print("Found Account To Change");
18-
programOwnerCheck(tx.accounts[i]);
19-
}
20-
if (tx.accounts[i].key == accountToCreate) {
21-
print("Found Account To Create");
22-
notInitializedCheck(tx.accounts[i]);
23-
signerCheck(tx.accounts[i]);
24-
}
25-
}
16+
// Perform checks on the account
17+
programOwnerCheck(tx.accounts.accountToChange);
18+
notInitializedCheck(tx.accounts.accountToCreate);
19+
signerCheck(tx.accounts.accountToCreate);
2620

2721
// (Create account...) (unimplemented)
2822
// (Change account...) (unimplemented)

basics/checking-accounts/solang/tests/checking-accounts.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,10 @@ describe("checking-accounts", () => {
5555
// Invoke the checkAccounts instruction on our program, passing in the account we want to "check"
5656
const tx = await program.methods
5757
.checkAccounts(accountToChange.publicKey, accountToCreate.publicKey)
58-
.accounts({ dataAccount: dataAccount.publicKey })
59-
.remainingAccounts([
60-
{
61-
pubkey: accountToChange.publicKey,
62-
isWritable: true,
63-
isSigner: false,
64-
},
65-
{
66-
pubkey: accountToCreate.publicKey,
67-
isWritable: true,
68-
isSigner: true,
69-
},
70-
])
58+
.accounts({
59+
accountToCreate: accountToCreate.publicKey,
60+
accountToChange: accountToChange.publicKey,
61+
})
7162
.signers([accountToCreate])
7263
.rpc({ skipPreflight: true });
7364
console.log("Your transaction signature", tx);

basics/cross-program-invocation/solang/solidity/hand.sol

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import "solana";
33

44
// Interface to the lever program.
5-
leverInterface constant leverProgram = leverInterface(address'4wFN9As94uDgcBK9umEi6DNjRLi8gq7jaHwSw3829xq8');
5+
@program_id("4wFN9As94uDgcBK9umEi6DNjRLi8gq7jaHwSw3829xq8")
66
interface leverInterface {
77
function switchPower(string name) external;
88
}
@@ -16,17 +16,18 @@ contract hand {
1616
constructor() {}
1717

1818
// "Pull the lever" by calling the switchPower instruction on the lever program via a Cross Program Invocation.
19-
function pullLever(address dataAccount, string name) public {
19+
@mutableAccount(leverData)
20+
function pullLever(string name) external {
2021
// The account required by the switchPower instruction.
2122
// This is the data account created by the lever program (not this program), which stores the state of the switch.
2223
AccountMeta[1] metas = [
23-
AccountMeta({pubkey: dataAccount, is_writable: true, is_signer: false})
24+
AccountMeta({pubkey: tx.accounts.leverData.key, is_writable: true, is_signer: false})
2425
];
2526

2627
// The data required by the switchPower instruction.
2728
string instructionData = name;
2829

2930
// Invoke the switchPower instruction on the lever program.
30-
leverProgram.switchPower{accounts: metas}(instructionData);
31+
leverInterface.switchPower{accounts: metas}(instructionData);
3132
}
3233
}

basics/cross-program-invocation/solang/tests/test.ts

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,11 @@ describe("cross-program-invocation", () => {
4747

4848
// Call the pullLever instruction on the hand program, which invokes the lever program via CPI
4949
const tx2 = await handProgram.methods
50-
.pullLever(dataAccountLever.publicKey, "Chris")
51-
.accounts({ dataAccount: dataAccountHand.publicKey })
52-
.remainingAccounts([
53-
{
54-
pubkey: dataAccountLever.publicKey, // The lever program's data account, which stores the state
55-
isWritable: true,
56-
isSigner: false,
57-
},
58-
{
59-
pubkey: leverProgram.programId, // The lever program's program ID
60-
isWritable: false,
61-
isSigner: false,
62-
},
63-
])
50+
.pullLever("Chris")
51+
.accounts({
52+
leverData: dataAccountLever.publicKey, // The lever program's data account, which stores the state
53+
leverInterface_programId: leverProgram.programId // The lever program's program ID
54+
})
6455
.rpc({ skipPreflight: true });
6556
console.log("Your transaction signature", tx2);
6657

@@ -76,20 +67,11 @@ describe("cross-program-invocation", () => {
7667
it("Pull it again!", async () => {
7768
// Call the pullLever instruction on the hand program, which invokes the lever program via CPI
7869
const tx = await handProgram.methods
79-
.pullLever(dataAccountLever.publicKey, "Ashley")
80-
.accounts({ dataAccount: dataAccountHand.publicKey })
81-
.remainingAccounts([
82-
{
83-
pubkey: dataAccountLever.publicKey, // The lever program's data account, which stores the state
84-
isWritable: true,
85-
isSigner: false,
86-
},
87-
{
88-
pubkey: leverProgram.programId, // The lever program's program ID
89-
isWritable: false,
90-
isSigner: false,
91-
},
92-
])
70+
.pullLever("Ashley")
71+
.accounts({
72+
leverData: dataAccountLever.publicKey, // The lever program's data account, which stores the state
73+
leverInterface_programId: leverProgram.programId, // The lever program's program ID
74+
})
9375
.rpc({ skipPreflight: true });
9476

9577
console.log("Your transaction signature", tx);

basics/hello-solana/solang/solidity/hello-solana.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ contract hello_solana {
55
// Here we create a new account that stores no data and only prints messages to the program logs when the constructor is called.
66
@payer(payer) // The "payer" pays for the account creation
77
constructor() {
8-
// We get the program ID by calling 'type(hello_solana).program_id', where "hello_solana" is the name of the contract.
9-
address programId = type(hello_solana).program_id;
8+
// We get the program ID by calling 'this';
9+
address programId = address(this);
1010

1111
// Print messages to the program logs
1212
print("Hello, Solana!");

basics/pda-rent-payer/solang/solidity/pda-rent-payer.sol

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ contract pda_rent_payer {
88
@seed("rent_vault") // hardcoded seed
99
constructor(@bump bytes1 bump, uint64 fundLamports) {
1010
// Independently derive the PDA address from the seeds, bump, and programId
11-
(address pda, bytes1 _bump) = try_find_program_address(["rent_vault"], type(pda_rent_payer).program_id);
11+
(address pda, bytes1 _bump) = try_find_program_address(["rent_vault"], address(this));
1212

1313
// Verify that the bump passed to the constructor matches the bump derived from the seeds and programId
1414
// This ensures that only the canonical pda address can be used to create the account (first bump that generates a valid pda address)
@@ -17,14 +17,16 @@ contract pda_rent_payer {
1717
// Fund the pda account with additional lamports
1818
SystemInstruction.transfer(
1919
tx.accounts.payer.key, // from
20-
address(this), // to (the address of the account being created)
20+
tx.accounts.dataAccount.key, // to (the address of the account being created)
2121
fundLamports // amount of lamports to transfer
2222
);
2323
}
2424

25-
function createNewAccount(uint64 lamports) public {
26-
AccountInfo from = tx.accounts[0]; // first account must be an account owned by the program
27-
AccountInfo to = tx.accounts[1]; // second account must be the intended recipient
25+
@mutableAccount(ownedByProgram)
26+
@mutableAccount(intendedRecipient)
27+
function createNewAccount(uint64 lamports) external {
28+
AccountInfo from = tx.accounts.ownedByProgram; // an account owned by the program
29+
AccountInfo to = tx.accounts.intendedRecipient; // second account must be the intended recipient
2830

2931
print("From: {:}".format(from.key));
3032
print("To: {:}".format(to.key));

basics/pda-rent-payer/solang/tests/pda-rent-payer.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,10 @@ describe("pda-rent-payer", () => {
4444
// Invoke the createNewAccount instruction on our program
4545
const tx = await program.methods
4646
.createNewAccount(new anchor.BN(lamports))
47-
.accounts({ dataAccount: dataAccountPDA })
48-
.remainingAccounts([
49-
{
50-
pubkey: newAccount.publicKey, // account to create by directly transferring lamports
51-
isWritable: true,
52-
isSigner: false,
53-
},
54-
])
47+
.accounts({
48+
ownedByProgram: dataAccountPDA,
49+
intendedRecipient: newAccount.publicKey, // account to create by directly transferring lamports
50+
})
5551
.rpc();
5652
console.log("Your transaction signature", tx);
5753

basics/rent/solang/solidity/rent.sol

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ contract rent {
88
@payer(payer) // The "payer" pays for the data account creation
99
constructor() {}
1010

11-
function createSystemAccount(address payer, address newAccount, uint64 space) public {
11+
@mutableSigner(payer)
12+
@mutableSigner(newAccount)
13+
function createSystemAccount(uint64 space) external {
1214
// The minimum lamports required for the amount of space allocated to the account
1315
uint64 lamports = minimum_balance(space);
1416

1517
SystemInstruction.create_account(
16-
payer, // lamports sent from this account (payer)
17-
newAccount, // lamports sent to this account (account to be created)
18+
tx.accounts.payer.key, // lamports sent from this account (payer)
19+
tx.accounts.newAccount.key, // lamports sent to this account (account to be created)
1820
lamports, // lamport amount (minimum lamports required)
1921
space, // space required for the account
2022
SystemInstruction.systemAddress // program owner (system program)

basics/rent/solang/tests/rent.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,13 @@ describe("rent", () => {
3333
// Create a new account via a Cross Program Invocation to the system program
3434
const tx = await program.methods
3535
.createSystemAccount(
36-
wallet.publicKey, // payer
37-
newAccount.publicKey, // new account
3836
new anchor.BN(space) // space
3937
)
40-
.accounts({ dataAccount: dataAccount.publicKey })
38+
.accounts({
39+
payer: wallet.publicKey,
40+
newAccount: newAccount.publicKey,
41+
})
4142
.signers([newAccount]) // new account keypair required as signer
42-
.remainingAccounts([
43-
{
44-
pubkey: wallet.publicKey,
45-
isWritable: true,
46-
isSigner: true,
47-
},
48-
{
49-
pubkey: newAccount.publicKey, // new account
50-
isWritable: true,
51-
isSigner: true,
52-
},
53-
])
5443
.rpc();
5544
console.log("Your transaction signature", tx);
5645
});

basics/transfer-sol/solang/solidity/transfer-sol.sol

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ contract transfer_sol {
1010
}
1111

1212
// Transfer SOL from one account to another using CPI (Cross Program Invocation) to the System program
13-
function transferSolWithCpi(address from, address to, uint64 lamports) public {
13+
@mutableSigner(sender)
14+
@mutableAccount(recipient)
15+
function transferSolWithCpi(uint64 lamports) external {
1416
// CPI to transfer SOL using "system_instruction" library
15-
SystemInstruction.transfer(from, to, lamports);
17+
SystemInstruction.transfer(tx.accounts.sender.key, tx.accounts.recipient.key, lamports);
1618
}
1719

1820
// Transfer SOL from program owned account to another address by directly modifying the account data lamports
1921
// This approach only works for accounts owned by the program itself (ex. the dataAccount created in the constructor)
20-
function transferSolWithProgram(uint64 lamports) public {
21-
AccountInfo from = tx.accounts[0]; // first account must be an account owned by the program
22-
AccountInfo to = tx.accounts[1]; // second account must be the intended recipient
22+
@mutableAccount(sender)
23+
@mutableAccount(recipient)
24+
function transferSolWithProgram(uint64 lamports) external {
25+
AccountInfo from = tx.accounts.sender; // first account must be an account owned by the program
26+
AccountInfo to = tx.accounts.recipient; // second account must be the intended recipient
2327

2428
print("From: {:}".format(from.key));
2529
print("To: {:}".format(to.key));

0 commit comments

Comments
 (0)