solana/web3.js/examples/budget-timestamp.js

78 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-11-11 21:28:05 -08:00
/* eslint-disable import/no-commonjs */
2018-09-19 17:35:16 -07:00
/*
Example of using the Budget program to perform a time-lock payment of 50
2019-03-05 17:52:13 -08:00
lamports from account1 to account2.
2018-09-19 17:35:16 -07:00
*/
2019-11-11 21:28:05 -08:00
const common = require('./budget-common');
2018-09-19 17:35:16 -07:00
const solanaWeb3 = require('..');
//const solanaWeb3 = require('@solana/web3.js');
const account1 = new solanaWeb3.Account();
const account2 = new solanaWeb3.Account();
const contractState = new solanaWeb3.Account();
let url;
url = 'http://localhost:8899';
const connection = new solanaWeb3.Connection(url, 'recent');
2019-11-11 21:28:05 -08:00
const getTransactionFee = () => common.getTransactionFee(connection);
const showBalance = () =>
common.showBalance(connection, account1, account2, contractState);
const confirmTransaction = signature =>
common.confirmTransaction(connection, signature);
const airDrop = feeCalculator =>
common.airDrop(connection, account1, feeCalculator);
2018-09-19 17:35:16 -07:00
2019-11-11 21:28:05 -08:00
getTransactionFee().then(feeCalculator => {
airDrop(feeCalculator)
.then(showBalance)
.then(() => {
console.log(`\n== Initializing contract`);
const transaction = solanaWeb3.BudgetProgram.pay(
account1.publicKey,
contractState.publicKey,
account2.publicKey,
50,
solanaWeb3.BudgetProgram.timestampCondition(
account1.publicKey,
new Date('2050'),
),
2019-05-24 15:07:16 -07:00
);
2019-11-11 21:28:05 -08:00
return solanaWeb3.sendAndConfirmTransaction(
connection,
transaction,
account1,
contractState,
);
})
.then(confirmTransaction)
.then(showBalance)
.then(() => {
console.log(`\n== Witness contract`);
const transaction = solanaWeb3.BudgetProgram.applyTimestamp(
2018-11-04 11:41:21 -08:00
account1.publicKey,
2019-11-11 21:28:05 -08:00
contractState.publicKey,
account2.publicKey,
2018-11-04 11:41:21 -08:00
new Date('2050'),
2019-11-11 21:28:05 -08:00
);
return solanaWeb3.sendAndConfirmTransaction(
connection,
transaction,
account1,
contractState,
);
})
.then(confirmTransaction)
.then(showBalance)
2018-09-19 17:35:16 -07:00
2019-11-11 21:28:05 -08:00
.then(() => {
console.log('\nDone');
})
2018-09-19 17:35:16 -07:00
2019-11-11 21:28:05 -08:00
.catch(err => {
console.log(err);
});
});