2018-09-19 17:35:16 -07:00
|
|
|
/*
|
|
|
|
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';
|
2018-09-26 14:21:48 -07:00
|
|
|
//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(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`);
|
2018-09-20 10:10:46 -07:00
|
|
|
const transaction = solanaWeb3.SystemProgram.createAccount(
|
2018-09-19 17:35:16 -07:00
|
|
|
account1.publicKey,
|
|
|
|
contractFunds.publicKey,
|
|
|
|
50, // number of tokens to transfer
|
|
|
|
0,
|
2018-09-20 10:10:46 -07:00
|
|
|
solanaWeb3.BudgetProgram.programId,
|
2018-09-19 17:35:16 -07:00
|
|
|
);
|
|
|
|
return connection.sendTransaction(account1, transaction);
|
|
|
|
})
|
|
|
|
.then(confirmTransaction)
|
|
|
|
.then(showBalance)
|
|
|
|
.then(() => {
|
|
|
|
console.log(`\n== Creating account for the contract state`);
|
2018-09-20 10:10:46 -07:00
|
|
|
const transaction = solanaWeb3.SystemProgram.createAccount(
|
2018-09-19 17:35:16 -07:00
|
|
|
account1.publicKey,
|
|
|
|
contractState.publicKey,
|
2018-09-20 10:10:46 -07:00
|
|
|
1, // account1 pays 1 token to hold the contract state
|
|
|
|
solanaWeb3.BudgetProgram.space,
|
|
|
|
solanaWeb3.BudgetProgram.programId,
|
2018-09-19 17:35:16 -07:00
|
|
|
);
|
|
|
|
return connection.sendTransaction(account1, transaction);
|
|
|
|
})
|
|
|
|
.then(confirmTransaction)
|
|
|
|
.then(showBalance)
|
|
|
|
.then(() => {
|
|
|
|
console.log(`\n== Initializing contract`);
|
2018-09-20 10:10:46 -07:00
|
|
|
const transaction = solanaWeb3.BudgetProgram.pay(
|
2018-09-19 17:35:16 -07:00
|
|
|
contractFunds.publicKey,
|
|
|
|
contractState.publicKey,
|
|
|
|
account2.publicKey,
|
|
|
|
50,
|
2018-09-20 10:10:46 -07:00
|
|
|
solanaWeb3.BudgetProgram.timestampCondition(account1.publicKey, new Date('2050')),
|
2018-09-19 17:35:16 -07:00
|
|
|
);
|
|
|
|
return connection.sendTransaction(contractFunds, transaction);
|
|
|
|
})
|
|
|
|
.then(confirmTransaction)
|
|
|
|
.then(showBalance)
|
|
|
|
.then(() => {
|
|
|
|
console.log(`\n== Witness contract`);
|
2018-09-20 10:10:46 -07:00
|
|
|
const transaction = solanaWeb3.BudgetProgram.applyTimestamp(
|
2018-09-19 17:35:16 -07:00
|
|
|
account1.publicKey,
|
|
|
|
contractState.publicKey,
|
|
|
|
account2.publicKey,
|
2018-09-20 10:10:46 -07:00
|
|
|
new Date('2050'),
|
2018-09-19 17:35:16 -07:00
|
|
|
);
|
|
|
|
return connection.sendTransaction(account1, transaction);
|
|
|
|
})
|
|
|
|
.then(confirmTransaction)
|
|
|
|
.then(showBalance)
|
|
|
|
|
|
|
|
.then(() => {
|
|
|
|
console.log('\nDone');
|
|
|
|
})
|
|
|
|
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|