Created
September 20, 2018 00:22
-
-
Save mvines/1338f4468a6167c655bbc3f7c438ceca to your computer and use it in GitHub Desktop.
Preliminary JS example of sending a witness-based Budget transaction
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
mvines@sol:~/ws/solana-web3.js/examples$ node pay-after-signature.js | |
== Account State | |
Account1: 9cyS7zidau1ZzJxK7iDcYRuRwkDVVSv4NQdGW9oWTnRr has a balance of 0 | |
Account2: H8csfHmybstxv5RjGdBCHNugVkz9aaSPry3pn4zdr3jW has a balance of 0 | |
TempContract: 7eWzYdZTfQyvs6aSMUMU4DHpGUhe9umom1qMmtbXwCWn has a balance of 0 | |
Contract: 1Tdg7BoqsWJFaFjucA5fsj18esh7kSREv4odmdUxN7E has a balance of 0 | |
== Requesting airdrop of 100 to 9cyS7zidau1ZzJxK7iDcYRuRwkDVVSv4NQdGW9oWTnRr | |
Confirming transaction: FksKGyryYAhUsFd8R5868xRgAKHMgVnHUGXbJMb5tLic6WRpQmn6JYAWYL2k9vUy68ut3NsxyHBgeZazU6sqFqt | |
Transaction confirmed | |
== Account State | |
Account1: 9cyS7zidau1ZzJxK7iDcYRuRwkDVVSv4NQdGW9oWTnRr has a balance of 100 | |
Account2: H8csfHmybstxv5RjGdBCHNugVkz9aaSPry3pn4zdr3jW has a balance of 0 | |
TempContract: 7eWzYdZTfQyvs6aSMUMU4DHpGUhe9umom1qMmtbXwCWn has a balance of 0 | |
Contract: 1Tdg7BoqsWJFaFjucA5fsj18esh7kSREv4odmdUxN7E has a balance of 0 | |
== Moving 100 to H8csfHmybstxv5RjGdBCHNugVkz9aaSPry3pn4zdr3jW | |
Confirming transaction: 2DUsDAsZE1syFLHm77ce2T9NdaxgJXuR81brJGYugpR1JsETCXsLTqzzjkCYToDEzbrKViLZbXLyTaC3VpofTW9y | |
Transaction confirmed | |
== Account State | |
Account1: 9cyS7zidau1ZzJxK7iDcYRuRwkDVVSv4NQdGW9oWTnRr has a balance of 50 | |
Account2: H8csfHmybstxv5RjGdBCHNugVkz9aaSPry3pn4zdr3jW has a balance of 0 | |
TempContract: 7eWzYdZTfQyvs6aSMUMU4DHpGUhe9umom1qMmtbXwCWn has a balance of 50 | |
Contract: 1Tdg7BoqsWJFaFjucA5fsj18esh7kSREv4odmdUxN7E has a balance of 0 | |
== Assigning 7eWzYdZTfQyvs6aSMUMU4DHpGUhe9umom1qMmtbXwCWn to BudgetContract | |
Confirming transaction: 3Ta2pwsp87gmLh3bRSrMyxSL9fV2Fpu7SUNXMxGaRGQhWEFnvxs4GQfZj6XqC4mMRC8CiP3HTeQB8rbzmbgjbQgc | |
Transaction confirmed | |
== Account State | |
Account1: 9cyS7zidau1ZzJxK7iDcYRuRwkDVVSv4NQdGW9oWTnRr has a balance of 50 | |
Account2: H8csfHmybstxv5RjGdBCHNugVkz9aaSPry3pn4zdr3jW has a balance of 0 | |
TempContract: 7eWzYdZTfQyvs6aSMUMU4DHpGUhe9umom1qMmtbXwCWn has a balance of 50 | |
Contract: 1Tdg7BoqsWJFaFjucA5fsj18esh7kSREv4odmdUxN7E has a balance of 0 | |
== Creating contract | |
Confirming transaction: 3JZmjtn7cM84pe9Az43yirt4DEPGGwhEarkJgiBpt4tbps1umWSjpYKq48BuTD6DFM1PwcfKbV6HvHJw7xzXQEiJ | |
Transaction confirmed | |
== Account State | |
Account1: 9cyS7zidau1ZzJxK7iDcYRuRwkDVVSv4NQdGW9oWTnRr has a balance of 49 | |
Account2: H8csfHmybstxv5RjGdBCHNugVkz9aaSPry3pn4zdr3jW has a balance of 0 | |
TempContract: 7eWzYdZTfQyvs6aSMUMU4DHpGUhe9umom1qMmtbXwCWn has a balance of 50 | |
Contract: 1Tdg7BoqsWJFaFjucA5fsj18esh7kSREv4odmdUxN7E has a balance of 1 | |
== Initializing contract | |
Confirming transaction: 2dWFy9HN8ZfqucDuUXVqK5817DMMPpDgNX6po2Mh8zyeCt5qw3C82nmCYjGEERmCKKqjioyUZAXepRq5GuGxeQfH | |
Transaction confirmed | |
== Account State | |
Account1: 9cyS7zidau1ZzJxK7iDcYRuRwkDVVSv4NQdGW9oWTnRr has a balance of 49 | |
Account2: H8csfHmybstxv5RjGdBCHNugVkz9aaSPry3pn4zdr3jW has a balance of 0 | |
TempContract: 7eWzYdZTfQyvs6aSMUMU4DHpGUhe9umom1qMmtbXwCWn has a balance of 0 | |
Contract: 1Tdg7BoqsWJFaFjucA5fsj18esh7kSREv4odmdUxN7E has a balance of 51 | |
== Witness contract | |
Confirming transaction: 25GcVPNFJ8X7bwCTuS6PkNm1PEYGyvjeQUaFZxJyuCzEYK2zcT9MYJLachqswgQ4iBds1XQRJ2ZLXZrTwWjXo3mz | |
Transaction confirmed | |
== Account State | |
Account1: 9cyS7zidau1ZzJxK7iDcYRuRwkDVVSv4NQdGW9oWTnRr has a balance of 49 | |
Account2: H8csfHmybstxv5RjGdBCHNugVkz9aaSPry3pn4zdr3jW has a balance of 50 | |
TempContract: 7eWzYdZTfQyvs6aSMUMU4DHpGUhe9umom1qMmtbXwCWn has a balance of 0 | |
Contract: 1Tdg7BoqsWJFaFjucA5fsj18esh7kSREv4odmdUxN7E has a balance of 1 | |
Done |
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
//eslint-disable-next-line import/no-commonjs | |
const solanaWeb3 = require('..'); | |
//const solanaWeb3 = require('@solana/web3.js'); | |
const account1 = new solanaWeb3.Account(); | |
const account2 = new solanaWeb3.Account(); | |
const tempcontract = new solanaWeb3.Account(); | |
const contract = new solanaWeb3.Account(); | |
const amount = 100; | |
const connection = new solanaWeb3.Connection('http://localhost:8899'); | |
function showBalance() { | |
console.log(`\n== Account State`); | |
return Promise.all([ | |
connection.getBalance(account1.publicKey), | |
connection.getBalance(account2.publicKey), | |
connection.getBalance(tempcontract.publicKey), | |
connection.getBalance(contract.publicKey), | |
]).then(([fromBalance, toBalance, tempcontractBalance, contractBalance]) => { | |
console.log(`Account1: ${account1.publicKey} has a balance of ${fromBalance}`); | |
console.log(`Account2: ${account2.publicKey} has a balance of ${toBalance}`); | |
console.log(`TempContract: ${tempcontract.publicKey} has a balance of ${tempcontractBalance}`); | |
console.log(`Contract: ${contract.publicKey} has a balance of ${contractBalance}`); | |
}); | |
} | |
function confirmTransaction(signature) { | |
console.log('Confirming transaction:', signature); | |
return connection.confirmTransaction(signature) | |
.then((confirmation) => { | |
if (!confirmation) { | |
throw new Error('Transaction was not confirmed'); | |
} | |
console.log('Transaction confirmed'); | |
}); | |
} | |
function airDrop() { | |
console.log(`\n== Requesting airdrop of ${amount} to ${account1.publicKey}`); | |
return connection.requestAirdrop(account1.publicKey, amount) | |
.then(confirmTransaction); | |
} | |
showBalance() | |
.then(airDrop) | |
.then(showBalance) | |
.then(() => { | |
console.log(`\n== Moving ${amount} to ${account2.publicKey}`); | |
const transaction = solanaWeb3.SystemContract.move( | |
account1.publicKey, | |
tempcontract.publicKey, | |
amount / 2, | |
); | |
return connection.sendTransaction(account1, transaction); | |
}) | |
.then(confirmTransaction) | |
.then(showBalance) | |
.then(() => { | |
console.log(`\n== Assigning ${tempcontract.publicKey} to BudgetContract`); | |
const transaction = solanaWeb3.SystemContract.assign( | |
tempcontract.publicKey, | |
solanaWeb3.BudgetContract.contractId, | |
); | |
return connection.sendTransaction(tempcontract, transaction); | |
}) | |
.then(confirmTransaction) | |
.then(showBalance) | |
.then(() => { | |
console.log(`\n== Creating contract`); | |
const transaction = solanaWeb3.SystemContract.createAccount( | |
account1.publicKey, | |
contract.publicKey, | |
1, | |
solanaWeb3.BudgetContract.space, | |
solanaWeb3.BudgetContract.contractId, | |
); | |
return connection.sendTransaction(account1, transaction); | |
}) | |
.then(confirmTransaction) | |
.then(showBalance) | |
.then(() => { | |
console.log(`\n== Initializing contract`); | |
const transaction = solanaWeb3.BudgetContract.pay( | |
tempcontract.publicKey, | |
contract.publicKey, | |
account2.publicKey, | |
amount / 2, | |
solanaWeb3.BudgetContract.signatureCondition(account1.publicKey), | |
); | |
return connection.sendTransaction(tempcontract, transaction); | |
}) | |
.then(confirmTransaction) | |
.then(showBalance) | |
.then(() => { | |
console.log(`\n== Witness contract`); | |
const transaction = solanaWeb3.BudgetContract.applySignature( | |
account1.publicKey, | |
contract.publicKey, | |
account2.publicKey, | |
); | |
return connection.sendTransaction(account1, transaction); | |
}) | |
.then(confirmTransaction) | |
.then(showBalance) | |
.then(() => { | |
console.log('\nDone'); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment