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

108 lines
2.9 KiB
JavaScript
Raw Normal View History

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
*/
//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 contractState = new solanaWeb3.Account();
let url;
url = 'http://localhost:8899';
//url = 'https://api.testnet.solana.com/master';
//url = 'https://api.testnet.solana.com';
2018-09-19 17:35:16 -07:00
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(contractState.publicKey),
2019-05-24 15:07:16 -07:00
]).then(([fromBalance, toBalance, contractStateBalance]) => {
console.log(
`Account1: ${account1.publicKey} has a balance of ${fromBalance}`,
);
console.log(
`Account2: ${account2.publicKey} has a balance of ${toBalance}`,
);
console.log(
2019-06-26 09:31:04 -07:00
`Contract State: ${contractState.publicKey} has a balance of ${contractStateBalance}`,
2019-05-24 15:07:16 -07:00
);
});
2018-09-19 17:35:16 -07:00
}
function confirmTransaction(signature) {
console.log('Confirming transaction:', signature);
2018-11-04 11:41:21 -08:00
return connection.getSignatureStatus(signature).then(confirmation => {
2019-04-12 11:41:30 -07:00
if (confirmation && 'Ok' in confirmation) {
2019-05-23 08:51:40 -07:00
console.log('Transaction confirmed');
2019-05-24 15:07:16 -07:00
} else if (confirmation) {
throw new Error(
`Transaction was not confirmed (${JSON.stringify(confirmation.Err)})`,
);
2019-05-23 08:51:40 -07:00
} else {
2018-09-26 19:16:17 -07:00
throw new Error(`Transaction was not confirmed (${confirmation})`);
2018-09-19 17:35:16 -07:00
}
});
}
function airDrop() {
console.log(`\n== Requesting airdrop of 100 to ${account1.publicKey}`);
2018-11-04 11:41:21 -08:00
return connection
.requestAirdrop(account1.publicKey, 100)
.then(confirmTransaction);
2018-09-19 17:35:16 -07:00
}
showBalance()
2018-11-04 11:41:21 -08:00
.then(airDrop)
.then(showBalance)
.then(() => {
console.log(`\n== Initializing contract`);
const transaction = solanaWeb3.BudgetProgram.pay(
2019-05-24 15:07:16 -07:00
account1.publicKey,
2018-11-04 11:41:21 -08:00
contractState.publicKey,
account2.publicKey,
50,
solanaWeb3.BudgetProgram.timestampCondition(
account1.publicKey,
new Date('2050'),
),
);
2019-05-24 15:07:16 -07:00
return solanaWeb3.sendAndConfirmTransaction(
connection,
transaction,
account1,
);
2018-11-04 11:41:21 -08:00
})
.then(confirmTransaction)
.then(showBalance)
.then(() => {
console.log(`\n== Witness contract`);
const transaction = solanaWeb3.BudgetProgram.applyTimestamp(
account1.publicKey,
contractState.publicKey,
account2.publicKey,
new Date('2050'),
);
2019-05-24 15:07:16 -07:00
return solanaWeb3.sendAndConfirmTransaction(
connection,
transaction,
account1,
);
2018-11-04 11:41:21 -08:00
})
.then(confirmTransaction)
.then(showBalance)
2018-09-19 17:35:16 -07:00
2018-11-04 11:41:21 -08:00
.then(() => {
console.log('\nDone');
})
2018-09-19 17:35:16 -07:00
2018-11-04 11:41:21 -08:00
.catch(err => {
console.log(err);
});