2019-11-12 00:28:05 -05:00
|
|
|
/* eslint-disable import/no-commonjs */
|
|
|
|
|
2018-09-19 17:35:16 -07:00
|
|
|
/*
|
2018-09-26 09:49:59 -07:00
|
|
|
Example of using the Budget program to perform a payment authorized by two parties
|
2018-09-19 17:35:16 -07:00
|
|
|
*/
|
|
|
|
|
2019-11-12 00:28:05 -05: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();
|
|
|
|
|
2018-09-26 09:49:59 -07:00
|
|
|
const approver1 = new solanaWeb3.Account();
|
|
|
|
const approver2 = new solanaWeb3.Account();
|
|
|
|
|
2018-09-19 17:35:16 -07:00
|
|
|
let url;
|
|
|
|
url = 'http://localhost:8899';
|
2020-03-10 21:13:31 -07:00
|
|
|
//url = 'http://devnet.solana.com';
|
2019-11-11 13:01:10 -05:00
|
|
|
const connection = new solanaWeb3.Connection(url, 'recent');
|
2019-11-12 00:28:05 -05: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-07-18 15:35:01 -06:00
|
|
|
getTransactionFee().then(feeCalculator => {
|
2019-11-12 00:28:05 -05:00
|
|
|
airDrop(feeCalculator)
|
2019-07-18 15:35:01 -06:00
|
|
|
.then(() => {
|
|
|
|
console.log(`\n== Move 1 lamport to approver1`);
|
|
|
|
const transaction = solanaWeb3.SystemProgram.transfer(
|
|
|
|
account1.publicKey,
|
|
|
|
approver1.publicKey,
|
|
|
|
1 + feeCalculator.lamportsPerSignature,
|
|
|
|
);
|
|
|
|
return solanaWeb3.sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
transaction,
|
|
|
|
account1,
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(confirmTransaction)
|
|
|
|
.then(getTransactionFee)
|
|
|
|
.then(() => {
|
|
|
|
console.log(`\n== Move 1 lamport to approver2`);
|
|
|
|
const transaction = solanaWeb3.SystemProgram.transfer(
|
|
|
|
account1.publicKey,
|
|
|
|
approver2.publicKey,
|
|
|
|
1 + feeCalculator.lamportsPerSignature,
|
|
|
|
);
|
|
|
|
return solanaWeb3.sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
transaction,
|
|
|
|
account1,
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(confirmTransaction)
|
|
|
|
.then(showBalance)
|
|
|
|
.then(() => {
|
|
|
|
console.log(`\n== Initializing contract`);
|
|
|
|
const transaction = solanaWeb3.BudgetProgram.payOnBoth(
|
|
|
|
account1.publicKey,
|
|
|
|
contractState.publicKey,
|
|
|
|
account2.publicKey,
|
|
|
|
50,
|
|
|
|
solanaWeb3.BudgetProgram.signatureCondition(approver1.publicKey),
|
|
|
|
solanaWeb3.BudgetProgram.signatureCondition(approver2.publicKey),
|
|
|
|
);
|
|
|
|
return solanaWeb3.sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
transaction,
|
|
|
|
account1,
|
2019-11-12 00:50:58 +05:30
|
|
|
contractState,
|
2019-07-18 15:35:01 -06:00
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(confirmTransaction)
|
|
|
|
.then(showBalance)
|
|
|
|
.then(() => {
|
|
|
|
console.log(`\n== Apply approver 1`);
|
|
|
|
const transaction = solanaWeb3.BudgetProgram.applySignature(
|
|
|
|
approver1.publicKey,
|
|
|
|
contractState.publicKey,
|
|
|
|
account2.publicKey,
|
|
|
|
);
|
|
|
|
return solanaWeb3.sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
transaction,
|
|
|
|
approver1,
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(confirmTransaction)
|
|
|
|
.then(showBalance)
|
|
|
|
.then(() => {
|
|
|
|
console.log(`\n== Apply approver 2`);
|
|
|
|
const transaction = solanaWeb3.BudgetProgram.applySignature(
|
|
|
|
approver2.publicKey,
|
|
|
|
contractState.publicKey,
|
|
|
|
account2.publicKey,
|
|
|
|
);
|
|
|
|
return solanaWeb3.sendAndConfirmTransaction(
|
|
|
|
connection,
|
|
|
|
transaction,
|
|
|
|
approver2,
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.then(confirmTransaction)
|
|
|
|
.then(showBalance)
|
2018-09-19 17:35:16 -07:00
|
|
|
|
2019-07-18 15:35:01 -06:00
|
|
|
.then(() => {
|
|
|
|
console.log('\nDone');
|
|
|
|
})
|
2018-09-19 17:35:16 -07:00
|
|
|
|
2019-07-18 15:35:01 -06:00
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
});
|