From 864940f63baa7280233dd415538b603b547f8847 Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Wed, 19 Sep 2018 17:35:16 -0700 Subject: [PATCH] Add budget example --- web3.js/examples/budget.js | 113 +++++++++++++++++++++++++++++++++ web3.js/src/budget-contract.js | 12 +++- web3.js/src/index.js | 2 + 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 web3.js/examples/budget.js diff --git a/web3.js/examples/budget.js b/web3.js/examples/budget.js new file mode 100644 index 0000000000..25cf71fc10 --- /dev/null +++ b/web3.js/examples/budget.js @@ -0,0 +1,113 @@ +/* + Example of using the Budget program to perform a time-lock payment of 50 + tokens from account1 to account2. +*/ + +//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 contractFunds = new solanaWeb3.Account(); +const contractState = new solanaWeb3.Account(); + +let url; +url = 'http://localhost:8899'; +//url = 'http://testnet.solana.com:8899'; +const connection = new solanaWeb3.Connection(url); + +function showBalance() { + console.log(`\n== Account State`); + return Promise.all([ + connection.getBalance(account1.publicKey), + connection.getBalance(account2.publicKey), + connection.getBalance(contractFunds.publicKey), + connection.getBalance(contractState.publicKey), + ]).then(([fromBalance, toBalance, contractFundsBalance, contractStateBalance]) => { + console.log(`Account1: ${account1.publicKey} has a balance of ${fromBalance}`); + console.log(`Account2: ${account2.publicKey} has a balance of ${toBalance}`); + console.log(`Contract Funds: ${contractFunds.publicKey} has a balance of ${contractFundsBalance}`); + console.log(`Contract State: ${contractState.publicKey} has a balance of ${contractStateBalance}`); + }); +} + +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 100 to ${account1.publicKey}`); + return connection.requestAirdrop(account1.publicKey, 100) + .then(confirmTransaction); +} + +showBalance() +.then(airDrop) +.then(showBalance) +.then(() => { + console.log(`\n== Creating account for the contract funds`); + const transaction = solanaWeb3.SystemContract.createAccount( + account1.publicKey, + contractFunds.publicKey, + 50, // number of tokens to transfer + 0, + solanaWeb3.BudgetContract.contractId, + ); + return connection.sendTransaction(account1, transaction); +}) +.then(confirmTransaction) +.then(showBalance) +.then(() => { + console.log(`\n== Creating account for the contract state`); + const transaction = solanaWeb3.SystemContract.createAccount( + account1.publicKey, + contractState.publicKey, + 1, // sender pays 1 token to hold the contract state + 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( + contractFunds.publicKey, + contractState.publicKey, + account2.publicKey, + 50, + solanaWeb3.BudgetContract.timestampCondition(account1.publicKey, new Date("2050")), + ); + return connection.sendTransaction(contractFunds, transaction); +}) +.then(confirmTransaction) +.then(showBalance) +.then(() => { + console.log(`\n== Witness contract`); + const transaction = solanaWeb3.BudgetContract.applyTimestamp( + account1.publicKey, + contractState.publicKey, + account2.publicKey, + new Date("2050"), + ); + return connection.sendTransaction(account1, transaction); +}) +.then(confirmTransaction) +.then(showBalance) + +.then(() => { + console.log('\nDone'); +}) + +.catch((err) => { + console.log(err); +}); diff --git a/web3.js/src/budget-contract.js b/web3.js/src/budget-contract.js index 0e9c5042b9..08fa8357c1 100644 --- a/web3.js/src/budget-contract.js +++ b/web3.js/src/budget-contract.js @@ -118,6 +118,14 @@ export class BudgetContract { return '4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM'; } + /** + * The amount of space this contract requires + */ + static get space(): number { + return 128; + } + + /** * Creates a timestamp condition */ @@ -196,7 +204,7 @@ export class BudgetContract { return new Transaction({ fee: 0, - keys: [from, contract], + keys: [from, contract, to], contractId: this.contractId, userdata: userdata.slice(0, pos), }); @@ -217,7 +225,7 @@ export class BudgetContract { return new Transaction({ fee: 0, - keys: [from, to], + keys: [from, contract, to], contractId: this.contractId, userdata: userdata.slice(0, pos), }); diff --git a/web3.js/src/index.js b/web3.js/src/index.js index 31ad2648be..3bc0faff0d 100644 --- a/web3.js/src/index.js +++ b/web3.js/src/index.js @@ -2,3 +2,5 @@ export {Account} from './account'; export {Connection} from './connection'; export {Transaction} from './transaction'; +export {SystemContract} from './system-contract'; +export {BudgetContract} from './budget-contract';