forked from solana-developers/web3-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
951a345
commit b7fae5c
Showing
77 changed files
with
2,617 additions
and
7,038 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ test-ledger/ | |
*/**/test-ledger/ | ||
*/**/node_modules/ | ||
*/**/yarn.lock | ||
*/**/package-lock.json | ||
*/**/yarn-error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"trailingComma": "all", | ||
"tabWidth": 2, | ||
"useTabs": true, | ||
"semi": true, | ||
"singleQuote": false, | ||
"proseWrap": "always", | ||
"printWidth": 80 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
# Web3 Examples | ||
|
||
### :space_invader: Welcome, Solana Developer. :space_invader: | ||
|
||
Are you just starting out on Solana? Stuck on something? | ||
|
||
Or maybe you're in a hackathon right now, building an absolutely *epic* dApp. | ||
|
||
Take a look at this awesome collection of examples in Typescript using Solana's `web3.js` (and Anchor); for your cloning & running pleasure. | ||
|
||
### :large_blue_diamond: TypeScript. :large_orange_diamond: JavaScript. :cyclone: Web3. | ||
### :space_invader: Welcome, Solana Developer. :space_invader: | ||
|
||
Are you just starting out on Solana? Stuck on something? | ||
|
||
Or maybe you're in a hackathon right now, building an absolutely _epic_ dApp. | ||
|
||
Take a look at this awesome collection of examples in Typescript using Solana's | ||
`web3.js` (and Anchor); for your cloning & running pleasure. | ||
|
||
### :large_blue_diamond: TypeScript. :large_orange_diamond: JavaScript. :cyclone: Web3. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
{ | ||
"scripts": { | ||
"test": "yarn run ts-mocha -p ./tests/tsconfig.json -t 1000000 ./tests/test.ts" | ||
}, | ||
"dependencies": { | ||
"@solana/web3.js": "1.63.1" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.3.3", | ||
"@types/mocha": "^9.1.1", | ||
"chai": "^4.3.6", | ||
"mocha": "^10.0.0", | ||
"ts-mocha": "^10.0.0", | ||
"typescript": "^4.8.3" | ||
} | ||
"scripts": { | ||
"test": "yarn run ts-mocha -p ./tests/tsconfig.json -t 1000000 ./tests/test.ts" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"@solana/web3.js": "1.63.1" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.3.3", | ||
"@types/mocha": "^9.1.1", | ||
"chai": "^4.3.6", | ||
"mocha": "^10.0.0", | ||
"ts-mocha": "^10.0.0", | ||
"typescript": "^4.8.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,128 @@ | ||
import { | ||
AddressLookupTableProgram, | ||
Connection, | ||
Keypair, | ||
PublicKey, | ||
SystemProgram, | ||
TransactionInstruction, | ||
} from '@solana/web3.js'; | ||
AddressLookupTableProgram, | ||
Connection, | ||
Keypair, | ||
PublicKey, | ||
SystemProgram, | ||
TransactionInstruction, | ||
} from "@solana/web3.js"; | ||
import { | ||
createKeypairFromFile, | ||
printAddressLookupTable, | ||
printBalances, | ||
sendTransactionV0, | ||
sendTransactionV0WithLookupTable, | ||
} from './util'; | ||
|
||
createKeypairFromFile, | ||
printAddressLookupTable, | ||
printBalances, | ||
sendTransactionV0, | ||
sendTransactionV0WithLookupTable, | ||
} from "./util"; | ||
|
||
describe("Address Lookup Tables!", () => { | ||
|
||
const connection = new Connection( | ||
`https://api.devnet.solana.com`, | ||
'confirmed' | ||
); | ||
const payer = createKeypairFromFile( | ||
require('os').homedir() + '/.config/solana/id.json' | ||
); | ||
|
||
let lookupTablePubkey: PublicKey; | ||
const testAccountOne = Keypair.generate(); | ||
const testAccountTwo = Keypair.generate(); | ||
|
||
it("Create an Address Lookup Table", async () => { | ||
|
||
let ix: TransactionInstruction; | ||
[ix, lookupTablePubkey] = AddressLookupTableProgram.createLookupTable({ | ||
authority: payer.publicKey, | ||
payer: payer.publicKey, | ||
recentSlot: await connection.getSlot(), | ||
}); | ||
|
||
await sendTransactionV0(connection, [ix], payer); | ||
|
||
console.log("Pubkeys from generated keypairs:") | ||
console.log(` Test Account #1: ${testAccountOne.publicKey}`); | ||
console.log(` Test Account #2: ${testAccountTwo.publicKey}`); | ||
await printAddressLookupTable(connection, lookupTablePubkey); | ||
}); | ||
|
||
it("Add some addresses to the ALT", async () => { | ||
|
||
const ix = AddressLookupTableProgram.extendLookupTable({ | ||
addresses: [ | ||
testAccountOne.publicKey, | ||
testAccountTwo.publicKey | ||
], | ||
authority: payer.publicKey, | ||
lookupTable: lookupTablePubkey, | ||
payer: payer.publicKey, | ||
}); | ||
|
||
await sendTransactionV0(connection, [ix], payer); | ||
|
||
await printAddressLookupTable(connection, lookupTablePubkey); | ||
}); | ||
|
||
it("Fund the first test account", async () => { | ||
|
||
const ix = SystemProgram.transfer({ | ||
fromPubkey: payer.publicKey, | ||
toPubkey: testAccountOne.publicKey, | ||
lamports: 100000000, | ||
}); | ||
|
||
await sendTransactionV0(connection, [ix], payer); | ||
|
||
await printBalances( | ||
connection, "After", testAccountOne.publicKey, testAccountTwo.publicKey | ||
); | ||
}); | ||
|
||
it("Send a transaction WITHOUT using the ALT", async () => { | ||
|
||
await printBalances( | ||
connection, "Before", testAccountOne.publicKey, testAccountTwo.publicKey | ||
); | ||
|
||
const ix = SystemProgram.transfer({ | ||
fromPubkey: testAccountOne.publicKey, | ||
toPubkey: testAccountTwo.publicKey, | ||
lamports: 20000000, | ||
}); | ||
|
||
await sendTransactionV0(connection, [ix], testAccountOne); | ||
|
||
await printBalances( | ||
connection, "After", testAccountOne.publicKey, testAccountTwo.publicKey | ||
); | ||
}); | ||
|
||
it("Now send that same transaction using the ALT", async () => { | ||
|
||
await printBalances( | ||
connection, "Before", payer.publicKey, testAccountOne.publicKey | ||
); | ||
|
||
const ix = SystemProgram.transfer({ | ||
fromPubkey: testAccountOne.publicKey, | ||
toPubkey: testAccountTwo.publicKey, | ||
lamports: 20000000, | ||
}); | ||
|
||
await sendTransactionV0WithLookupTable( | ||
connection, [ix], testAccountOne, lookupTablePubkey | ||
); | ||
|
||
await printBalances( | ||
connection, "After", testAccountOne.publicKey, testAccountTwo.publicKey | ||
); | ||
}); | ||
}); | ||
|
||
const connection = new Connection( | ||
`https://api.devnet.solana.com`, | ||
"confirmed", | ||
); | ||
const payer = createKeypairFromFile( | ||
require("os").homedir() + "/.config/solana/id.json", | ||
); | ||
|
||
let lookupTablePubkey: PublicKey; | ||
const testAccountOne = Keypair.generate(); | ||
const testAccountTwo = Keypair.generate(); | ||
|
||
it("Create an Address Lookup Table", async () => { | ||
let ix: TransactionInstruction; | ||
[ix, lookupTablePubkey] = AddressLookupTableProgram.createLookupTable({ | ||
authority: payer.publicKey, | ||
payer: payer.publicKey, | ||
recentSlot: await connection.getSlot(), | ||
}); | ||
|
||
await sendTransactionV0(connection, [ix], payer); | ||
|
||
console.log("Pubkeys from generated keypairs:"); | ||
console.log(` Test Account #1: ${testAccountOne.publicKey}`); | ||
console.log(` Test Account #2: ${testAccountTwo.publicKey}`); | ||
await printAddressLookupTable(connection, lookupTablePubkey); | ||
}); | ||
|
||
it("Add some addresses to the ALT", async () => { | ||
const ix = AddressLookupTableProgram.extendLookupTable({ | ||
addresses: [testAccountOne.publicKey, testAccountTwo.publicKey], | ||
authority: payer.publicKey, | ||
lookupTable: lookupTablePubkey, | ||
payer: payer.publicKey, | ||
}); | ||
|
||
await sendTransactionV0(connection, [ix], payer); | ||
|
||
await printAddressLookupTable(connection, lookupTablePubkey); | ||
}); | ||
|
||
it("Fund the first test account", async () => { | ||
const ix = SystemProgram.transfer({ | ||
fromPubkey: payer.publicKey, | ||
toPubkey: testAccountOne.publicKey, | ||
lamports: 100000000, | ||
}); | ||
|
||
await sendTransactionV0(connection, [ix], payer); | ||
|
||
await printBalances( | ||
connection, | ||
"After", | ||
testAccountOne.publicKey, | ||
testAccountTwo.publicKey, | ||
); | ||
}); | ||
|
||
it("Send a transaction WITHOUT using the ALT", async () => { | ||
await printBalances( | ||
connection, | ||
"Before", | ||
testAccountOne.publicKey, | ||
testAccountTwo.publicKey, | ||
); | ||
|
||
const ix = SystemProgram.transfer({ | ||
fromPubkey: testAccountOne.publicKey, | ||
toPubkey: testAccountTwo.publicKey, | ||
lamports: 20000000, | ||
}); | ||
|
||
await sendTransactionV0(connection, [ix], testAccountOne); | ||
|
||
await printBalances( | ||
connection, | ||
"After", | ||
testAccountOne.publicKey, | ||
testAccountTwo.publicKey, | ||
); | ||
}); | ||
|
||
it("Now send that same transaction using the ALT", async () => { | ||
await printBalances( | ||
connection, | ||
"Before", | ||
payer.publicKey, | ||
testAccountOne.publicKey, | ||
); | ||
|
||
const ix = SystemProgram.transfer({ | ||
fromPubkey: testAccountOne.publicKey, | ||
toPubkey: testAccountTwo.publicKey, | ||
lamports: 20000000, | ||
}); | ||
|
||
await sendTransactionV0WithLookupTable( | ||
connection, | ||
[ix], | ||
testAccountOne, | ||
lookupTablePubkey, | ||
); | ||
|
||
await printBalances( | ||
connection, | ||
"After", | ||
testAccountOne.publicKey, | ||
testAccountTwo.publicKey, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"types": ["mocha", "chai"], | ||
"typeRoots": ["./node_modules/@types"], | ||
"lib": ["es2015"], | ||
"module": "commonjs", | ||
"target": "es6", | ||
"esModuleInterop": true | ||
} | ||
"compilerOptions": { | ||
"lib": ["es2015"], | ||
"module": "commonjs", | ||
"target": "es6", | ||
"esModuleInterop": true | ||
} | ||
} |
Oops, something went wrong.