Skip to content

Commit 79574af

Browse files
authored
Merge pull request solana-developers#36 from ZYJLiu/main
Update Solang examples to version 0.3.1 and add a cnft example
2 parents 0c8e9bb + 7b2d09d commit 79574af

File tree

105 files changed

+3871
-2360
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+3871
-2360
lines changed

basics/account-data/solang/solidity/account-data.sol

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ contract account_data {
1414
}
1515

1616
@payer(payer) // "payer" is the account that pays to create the dataAccount
17-
@space(space) // "space" allocated to the account (maximum 10240 bytes, maximum space that can be reallocate when creating account in program via a CPI)
18-
constructor(address payer, uint16 space, string _name, uint8 _houseNumber, string _street, string _city) {
17+
constructor(
18+
@space uint16 space, // "space" allocated to the account (maximum 10240 bytes, maximum space that can be reallocate when creating account in program via a CPI)
19+
string _name,
20+
uint8 _houseNumber,
21+
string _street,
22+
string _city
23+
) {
1924
// The AddressInfo instance is initialized with the data passed to the constructor
2025
addressInfo = AddressInfo(_name, _houseNumber, _street, _city);
2126
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
import * as anchor from "@coral-xyz/anchor"
2-
import { Program } from "@coral-xyz/anchor"
3-
import { AccountData } from "../target/types/account_data"
1+
import * as anchor from "@coral-xyz/anchor";
2+
import { Program } from "@coral-xyz/anchor";
3+
import { AccountData } from "../target/types/account_data";
44

55
describe("account-data", () => {
66
// Configure the client to use the local cluster.
7-
const provider = anchor.AnchorProvider.env()
8-
anchor.setProvider(provider)
7+
const provider = anchor.AnchorProvider.env();
8+
anchor.setProvider(provider);
99

1010
// Generate a new random keypair for the data account.
11-
const dataAccount = anchor.web3.Keypair.generate()
12-
const wallet = provider.wallet
13-
const program = anchor.workspace.AccountData as Program<AccountData>
11+
const dataAccount = anchor.web3.Keypair.generate();
12+
const wallet = provider.wallet;
13+
const program = anchor.workspace.AccountData as Program<AccountData>;
1414

1515
// Create the new account
1616
// Using 10240 bytes of space, because its unclear how to correctly calculate the minimum space needed for the account
1717
// Space calculation is different from regular Native/Anchor Solana programs
1818
it("Is initialized!", async () => {
1919
const tx = await program.methods
2020
.new(
21-
wallet.publicKey, // payer
2221
10240, // space (10240 bytes is the maximum space allowed when allocating space through a program)
2322
"Joe C", // name
2423
136, // house number
@@ -27,18 +26,18 @@ describe("account-data", () => {
2726
)
2827
.accounts({ dataAccount: dataAccount.publicKey })
2928
.signers([dataAccount])
30-
.rpc()
31-
console.log("Your transaction signature", tx)
32-
})
29+
.rpc();
30+
console.log("Your transaction signature", tx);
31+
});
3332

3433
// Get the account data
3534
it("Get AddressInfo Data", async () => {
3635
const val = await program.methods
3736
.get()
3837
.accounts({ dataAccount: dataAccount.publicKey })
39-
.view()
40-
console.log("State:", val)
41-
})
38+
.view();
39+
console.log("State:", val);
40+
});
4241

4342
// Get the account data size
4443
// Testing how much space is used to store the account data
@@ -47,7 +46,7 @@ describe("account-data", () => {
4746
const size = await program.methods
4847
.getAddressInfoSize()
4948
.accounts({ dataAccount: dataAccount.publicKey })
50-
.view()
51-
console.log("Size:", size.toNumber())
52-
})
53-
})
49+
.view();
50+
console.log("Size:", size.toNumber());
51+
});
52+
});
+10-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"compilerOptions": {
3-
"types": ["mocha", "chai"],
4-
"typeRoots": ["./node_modules/@types"],
5-
"lib": ["es2015"],
6-
"module": "commonjs",
7-
"target": "es6",
8-
"esModuleInterop": true
9-
}
10-
}
11-
2+
"compilerOptions": {
3+
"types": ["mocha", "chai"],
4+
"typeRoots": ["./node_modules/@types"],
5+
"lib": ["es2015"],
6+
"module": "commonjs",
7+
"target": "es6",
8+
"esModuleInterop": true
9+
}
10+
}
11+

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ contract checking_accounts {
66

77
// The dataAccount is unused in this example, but is a required account when using Solang
88
@payer(payer) // "payer" is the account that pays to create the dataAccount
9-
constructor(address payer) {}
9+
constructor() {}
1010

1111
function checkAccounts(address accountToChange, address accountToCreate) public view {
1212
print("Number of Accounts Provided: {:}".format(tx.accounts.length));
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
import * as anchor from "@coral-xyz/anchor"
2-
import { Program } from "@coral-xyz/anchor"
3-
import { CheckingAccounts } from "../target/types/checking_accounts"
1+
import * as anchor from "@coral-xyz/anchor";
2+
import { Program } from "@coral-xyz/anchor";
3+
import { CheckingAccounts } from "../target/types/checking_accounts";
44
import {
55
SystemProgram,
66
Transaction,
77
sendAndConfirmTransaction,
8-
} from "@solana/web3.js"
8+
} from "@solana/web3.js";
99

1010
describe("checking-accounts", () => {
1111
// Configure the client to use the local cluster.
12-
const provider = anchor.AnchorProvider.env()
13-
anchor.setProvider(provider)
12+
const provider = anchor.AnchorProvider.env();
13+
anchor.setProvider(provider);
1414

1515
// Generate a new random keypair for the data account.
16-
const dataAccount = anchor.web3.Keypair.generate()
16+
const dataAccount = anchor.web3.Keypair.generate();
1717

1818
// Generate a new keypair to represent the account we will change.
19-
const accountToChange = anchor.web3.Keypair.generate()
19+
const accountToChange = anchor.web3.Keypair.generate();
2020
// Generate a new keypair to represent the account we will create.
21-
const accountToCreate = anchor.web3.Keypair.generate()
22-
const wallet = provider.wallet as anchor.Wallet
23-
const connection = provider.connection
21+
const accountToCreate = anchor.web3.Keypair.generate();
22+
const wallet = provider.wallet as anchor.Wallet;
23+
const connection = provider.connection;
2424

25-
const program = anchor.workspace.CheckingAccounts as Program<CheckingAccounts>
25+
const program = anchor.workspace
26+
.CheckingAccounts as Program<CheckingAccounts>;
2627

2728
it("Is initialized!", async () => {
2829
// Create the new dataAccount, this is an account required by Solang even though we don't use it
2930
const tx = await program.methods
30-
.new(wallet.publicKey)
31+
.new()
3132
.accounts({ dataAccount: dataAccount.publicKey })
3233
.signers([dataAccount])
33-
.rpc({ skipPreflight: true })
34-
console.log("Your transaction signature", tx)
35-
})
34+
.rpc({ skipPreflight: true });
35+
console.log("Your transaction signature", tx);
36+
});
3637

3738
it("Create an account owned by our program", async () => {
3839
// Create the new account owned by our program by directly calling the system program
@@ -42,13 +43,13 @@ describe("checking-accounts", () => {
4243
lamports: await connection.getMinimumBalanceForRentExemption(0),
4344
space: 0,
4445
programId: program.programId, // Our program
45-
})
46+
});
4647

4748
await sendAndConfirmTransaction(connection, new Transaction().add(ix), [
4849
wallet.payer,
4950
accountToChange,
50-
])
51-
})
51+
]);
52+
});
5253

5354
it("Check Accounts", async () => {
5455
// Invoke the checkAccounts instruction on our program, passing in the account we want to "check"
@@ -68,7 +69,7 @@ describe("checking-accounts", () => {
6869
},
6970
])
7071
.signers([accountToCreate])
71-
.rpc({ skipPreflight: true })
72-
console.log("Your transaction signature", tx)
73-
})
74-
})
72+
.rpc({ skipPreflight: true });
73+
console.log("Your transaction signature", tx);
74+
});
75+
});
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"compilerOptions": {
3-
"types": ["mocha", "chai"],
4-
"typeRoots": ["./node_modules/@types"],
5-
"lib": ["es2015"],
6-
"module": "commonjs",
7-
"target": "es6",
8-
"esModuleInterop": true
9-
}
10-
}
2+
"compilerOptions": {
3+
"types": ["mocha", "chai"],
4+
"typeRoots": ["./node_modules/@types"],
5+
"lib": ["es2015"],
6+
"module": "commonjs",
7+
"target": "es6",
8+
"esModuleInterop": true
9+
}
10+
}
1111

basics/counter/solang/solidity/counter.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ contract counter {
66

77
// The constructor is used to create a new counter account
88
@payer(payer) // The "payer" pays for the counter account creation
9-
constructor(address payer) {
9+
constructor() {
1010
// Initialize the count to zero
1111
count = 0;
1212
}

basics/counter/solang/tests/counter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("counter", () => {
1717
it("Is initialized!", async () => {
1818
// Initialize new Counter account
1919
const tx = await program.methods
20-
.new(wallet.publicKey) // wallet.publicKey is the payer for the new account
20+
.new() // wallet.publicKey is the payer for the new account
2121
.accounts({ dataAccount: dataAccount.publicKey })
2222
.signers([dataAccount]) // dataAccount keypair is a required signer because we're using it to create a new account
2323
.rpc()

basics/counter/solang/tsconfig.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
2-
"compilerOptions": {
3-
"types": ["mocha", "chai"],
4-
"typeRoots": ["./node_modules/@types"],
5-
"lib": ["es2015"],
6-
"module": "commonjs",
7-
"target": "es6",
8-
"esModuleInterop": true
9-
}
10-
}
2+
"compilerOptions": {
3+
"types": ["mocha", "chai"],
4+
"typeRoots": ["./node_modules/@types"],
5+
"lib": ["es2015"],
6+
"module": "commonjs",
7+
"target": "es6",
8+
"esModuleInterop": true
9+
}
10+
}
1111

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ contract hand {
1313
// Creating a data account is required by Solang, but the account is not used in this example.
1414
// We only interact with the lever program.
1515
@payer(payer) // payer for the data account
16-
constructor(address payer) {}
16+
constructor() {}
1717

1818
// "Pull the lever" by calling the switchPower instruction on the lever program via a Cross Program Invocation.
1919
function pullLever(address dataAccount, string name) public {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ contract lever {
55
bool private isOn = true;
66

77
@payer(payer) // payer for the data account
8-
constructor(address payer) {}
8+
constructor() {}
99

1010
// Switch the power on or off
1111
function switchPower(string name) public {
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
1-
import * as anchor from "@coral-xyz/anchor"
2-
import { Program } from "@coral-xyz/anchor"
3-
import { Lever } from "../target/types/lever"
4-
import { Hand } from "../target/types/hand"
1+
import * as anchor from "@coral-xyz/anchor";
2+
import { Program } from "@coral-xyz/anchor";
3+
import { Lever } from "../target/types/lever";
4+
import { Hand } from "../target/types/hand";
55

66
describe("cross-program-invocation", () => {
77
// Configure the client to use the local cluster.
8-
const provider = anchor.AnchorProvider.env()
9-
anchor.setProvider(provider)
8+
const provider = anchor.AnchorProvider.env();
9+
anchor.setProvider(provider);
1010

1111
// Generate a new keypair for the data accounts for each program
12-
const dataAccountLever = anchor.web3.Keypair.generate()
13-
const dataAccountHand = anchor.web3.Keypair.generate()
14-
const wallet = provider.wallet
12+
const dataAccountLever = anchor.web3.Keypair.generate();
13+
const dataAccountHand = anchor.web3.Keypair.generate();
14+
const wallet = provider.wallet;
1515

1616
// The lever program and hand program
17-
const leverProgram = anchor.workspace.Lever as Program<Lever>
18-
const handProgram = anchor.workspace.Hand as Program<Hand>
17+
const leverProgram = anchor.workspace.Lever as Program<Lever>;
18+
const handProgram = anchor.workspace.Hand as Program<Hand>;
1919

2020
it("Initialize the lever!", async () => {
2121
// Initialize data account for the lever program
2222
const tx = await leverProgram.methods
23-
.new(wallet.publicKey)
23+
.new()
2424
.accounts({ dataAccount: dataAccountLever.publicKey })
2525
.signers([dataAccountLever])
26-
.rpc()
27-
console.log("Your transaction signature", tx)
26+
.rpc();
27+
console.log("Your transaction signature", tx);
2828

2929
// Fetch the state of the data account
3030
const val = await leverProgram.methods
3131
.get()
3232
.accounts({ dataAccount: dataAccountLever.publicKey })
33-
.view()
33+
.view();
3434

35-
console.log("State:", val)
36-
})
35+
console.log("State:", val);
36+
});
3737

3838
it("Pull the lever!", async () => {
3939
// Initialize data account for the hand program
4040
// This is required by Solang, but the account is not used
4141
const tx = await handProgram.methods
42-
.new(wallet.publicKey)
42+
.new()
4343
.accounts({ dataAccount: dataAccountHand.publicKey })
4444
.signers([dataAccountHand])
45-
.rpc()
46-
console.log("Your transaction signature", tx)
45+
.rpc();
46+
console.log("Your transaction signature", tx);
4747

4848
// Call the pullLever instruction on the hand program, which invokes the lever program via CPI
4949
const tx2 = await handProgram.methods
@@ -61,17 +61,17 @@ describe("cross-program-invocation", () => {
6161
isSigner: false,
6262
},
6363
])
64-
.rpc({ skipPreflight: true })
65-
console.log("Your transaction signature", tx2)
64+
.rpc({ skipPreflight: true });
65+
console.log("Your transaction signature", tx2);
6666

6767
// Fetch the state of the data account
6868
const val = await leverProgram.methods
6969
.get()
7070
.accounts({ dataAccount: dataAccountLever.publicKey })
71-
.view()
71+
.view();
7272

73-
console.log("State:", val)
74-
})
73+
console.log("State:", val);
74+
});
7575

7676
it("Pull it again!", async () => {
7777
// Call the pullLever instruction on the hand program, which invokes the lever program via CPI
@@ -90,16 +90,16 @@ describe("cross-program-invocation", () => {
9090
isSigner: false,
9191
},
9292
])
93-
.rpc({ skipPreflight: true })
93+
.rpc({ skipPreflight: true });
9494

95-
console.log("Your transaction signature", tx)
95+
console.log("Your transaction signature", tx);
9696

9797
// Fetch the state of the data account
9898
const val = await leverProgram.methods
9999
.get()
100100
.accounts({ dataAccount: dataAccountLever.publicKey })
101-
.view()
101+
.view();
102102

103-
console.log("State:", val)
104-
})
105-
})
103+
console.log("State:", val);
104+
});
105+
});

0 commit comments

Comments
 (0)