fix: example budget programs
This commit is contained in:
parent
086eb6f8b8
commit
979a707c94
67
web3.js/examples/budget-common.js
Normal file
67
web3.js/examples/budget-common.js
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
/* eslint-disable import/no-commonjs */
|
||||||
|
|
||||||
|
/*
|
||||||
|
Common code for the budget program examples
|
||||||
|
*/
|
||||||
|
|
||||||
|
function getTransactionFee(connection) {
|
||||||
|
return connection.getRecentBlockhash().then(response => {
|
||||||
|
return response[1];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function showBalance(connection, account1, account2, contractState) {
|
||||||
|
console.log(`\n== Account State`);
|
||||||
|
return Promise.all([
|
||||||
|
connection.getBalance(account1.publicKey),
|
||||||
|
connection.getBalance(account2.publicKey),
|
||||||
|
connection.getBalance(contractState.publicKey),
|
||||||
|
]).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(
|
||||||
|
`Contract State: ${contractState.publicKey} has a balance of ${contractStateBalance}`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function confirmTransaction(connection, signature) {
|
||||||
|
console.log('Confirming transaction:', signature);
|
||||||
|
return connection.getSignatureStatus(signature).then(confirmation => {
|
||||||
|
if (confirmation && 'Ok' in confirmation) {
|
||||||
|
console.log('Transaction confirmed');
|
||||||
|
} else if (confirmation) {
|
||||||
|
throw new Error(
|
||||||
|
`Transaction was not confirmed (${JSON.stringify(confirmation.Err)})`,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw new Error(`Transaction was not confirmed (${confirmation})`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function airDrop(connection, account, feeCalculator) {
|
||||||
|
const airdrop = 100 + 5 * feeCalculator.targetLamportsPerSignature;
|
||||||
|
console.log(`\n== Requesting airdrop of ${airdrop} to ${account.publicKey}`);
|
||||||
|
return connection
|
||||||
|
.requestAirdrop(account.publicKey, airdrop)
|
||||||
|
.then(signature => confirmTransaction(connection, signature));
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(millis) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
setTimeout(resolve, millis);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
airDrop,
|
||||||
|
confirmTransaction,
|
||||||
|
getTransactionFee,
|
||||||
|
showBalance,
|
||||||
|
sleep,
|
||||||
|
};
|
@ -1,9 +1,12 @@
|
|||||||
|
/* eslint-disable import/no-commonjs */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Example of using the Budget program to perform a time-lock payment of 50
|
Example of using the Budget program to perform a time-lock payment of 50
|
||||||
lamports from account1 to account2.
|
lamports from account1 to account2.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//eslint-disable-next-line import/no-commonjs
|
const common = require('./budget-common');
|
||||||
|
|
||||||
const solanaWeb3 = require('..');
|
const solanaWeb3 = require('..');
|
||||||
//const solanaWeb3 = require('@solana/web3.js');
|
//const solanaWeb3 = require('@solana/web3.js');
|
||||||
|
|
||||||
@ -14,93 +17,61 @@ const contractState = new solanaWeb3.Account();
|
|||||||
let url;
|
let url;
|
||||||
url = 'http://localhost:8899';
|
url = 'http://localhost:8899';
|
||||||
const connection = new solanaWeb3.Connection(url, 'recent');
|
const connection = new solanaWeb3.Connection(url, 'recent');
|
||||||
|
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);
|
||||||
|
|
||||||
function showBalance() {
|
getTransactionFee().then(feeCalculator => {
|
||||||
console.log(`\n== Account State`);
|
airDrop(feeCalculator)
|
||||||
return Promise.all([
|
.then(showBalance)
|
||||||
connection.getBalance(account1.publicKey),
|
.then(() => {
|
||||||
connection.getBalance(account2.publicKey),
|
console.log(`\n== Initializing contract`);
|
||||||
connection.getBalance(contractState.publicKey),
|
const transaction = solanaWeb3.BudgetProgram.pay(
|
||||||
]).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(
|
|
||||||
`Contract State: ${contractState.publicKey} has a balance of ${contractStateBalance}`,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function confirmTransaction(signature) {
|
|
||||||
console.log('Confirming transaction:', signature);
|
|
||||||
return connection.getSignatureStatus(signature).then(confirmation => {
|
|
||||||
if (confirmation && 'Ok' in confirmation) {
|
|
||||||
console.log('Transaction confirmed');
|
|
||||||
} else if (confirmation) {
|
|
||||||
throw new Error(
|
|
||||||
`Transaction was not confirmed (${JSON.stringify(confirmation.Err)})`,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
throw new Error(`Transaction was not confirmed (${confirmation})`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function airDrop() {
|
|
||||||
console.log(`\n== Requesting airdrop of 100000 to ${account1.publicKey}`);
|
|
||||||
return connection
|
|
||||||
.requestAirdrop(account1.publicKey, 100000)
|
|
||||||
.then(confirmTransaction);
|
|
||||||
}
|
|
||||||
|
|
||||||
showBalance()
|
|
||||||
.then(airDrop)
|
|
||||||
.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,
|
account1.publicKey,
|
||||||
|
contractState.publicKey,
|
||||||
|
account2.publicKey,
|
||||||
|
50,
|
||||||
|
solanaWeb3.BudgetProgram.timestampCondition(
|
||||||
|
account1.publicKey,
|
||||||
|
new Date('2050'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return solanaWeb3.sendAndConfirmTransaction(
|
||||||
|
connection,
|
||||||
|
transaction,
|
||||||
|
account1,
|
||||||
|
contractState,
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.then(confirmTransaction)
|
||||||
|
.then(showBalance)
|
||||||
|
.then(() => {
|
||||||
|
console.log(`\n== Witness contract`);
|
||||||
|
const transaction = solanaWeb3.BudgetProgram.applyTimestamp(
|
||||||
|
account1.publicKey,
|
||||||
|
contractState.publicKey,
|
||||||
|
account2.publicKey,
|
||||||
new Date('2050'),
|
new Date('2050'),
|
||||||
),
|
);
|
||||||
);
|
return solanaWeb3.sendAndConfirmTransaction(
|
||||||
return solanaWeb3.sendAndConfirmTransaction(
|
connection,
|
||||||
connection,
|
transaction,
|
||||||
transaction,
|
account1,
|
||||||
account1,
|
contractState,
|
||||||
contractState,
|
);
|
||||||
);
|
})
|
||||||
})
|
.then(confirmTransaction)
|
||||||
.then(confirmTransaction)
|
.then(showBalance)
|
||||||
.then(showBalance)
|
|
||||||
.then(() => {
|
|
||||||
console.log(`\n== Witness contract`);
|
|
||||||
const transaction = solanaWeb3.BudgetProgram.applyTimestamp(
|
|
||||||
account1.publicKey,
|
|
||||||
contractState.publicKey,
|
|
||||||
account2.publicKey,
|
|
||||||
new Date('2050'),
|
|
||||||
);
|
|
||||||
return solanaWeb3.sendAndConfirmTransaction(
|
|
||||||
connection,
|
|
||||||
transaction,
|
|
||||||
account1,
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.then(confirmTransaction)
|
|
||||||
.then(showBalance)
|
|
||||||
|
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log('\nDone');
|
console.log('\nDone');
|
||||||
})
|
})
|
||||||
|
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
|
/* eslint-disable import/no-commonjs */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Example of using the Budget program to perform a payment authorized by two parties
|
Example of using the Budget program to perform a payment authorized by two parties
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//eslint-disable-next-line import/no-commonjs
|
const common = require('./budget-common');
|
||||||
|
|
||||||
const solanaWeb3 = require('..');
|
const solanaWeb3 = require('..');
|
||||||
//const solanaWeb3 = require('@solana/web3.js');
|
//const solanaWeb3 = require('@solana/web3.js');
|
||||||
|
|
||||||
@ -17,58 +20,16 @@ let url;
|
|||||||
url = 'http://localhost:8899';
|
url = 'http://localhost:8899';
|
||||||
//url = 'http://testnet.solana.com:8899';
|
//url = 'http://testnet.solana.com:8899';
|
||||||
const connection = new solanaWeb3.Connection(url, 'recent');
|
const connection = new solanaWeb3.Connection(url, 'recent');
|
||||||
|
const getTransactionFee = () => common.getTransactionFee(connection);
|
||||||
function getTransactionFee() {
|
const showBalance = () =>
|
||||||
return connection.getRecentBlockhash().then(response => {
|
common.showBalance(connection, account1, account2, contractState);
|
||||||
return response[1];
|
const confirmTransaction = signature =>
|
||||||
});
|
common.confirmTransaction(connection, signature);
|
||||||
}
|
const airDrop = feeCalculator =>
|
||||||
|
common.airDrop(connection, account1, feeCalculator);
|
||||||
function showBalance() {
|
|
||||||
console.log(`\n== Account State`);
|
|
||||||
return Promise.all([
|
|
||||||
connection.getBalance(account1.publicKey),
|
|
||||||
connection.getBalance(account2.publicKey),
|
|
||||||
connection.getBalance(contractState.publicKey),
|
|
||||||
]).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(
|
|
||||||
`Contract State: ${contractState.publicKey} has a balance of ${contractStateBalance}`,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function confirmTransaction(signature) {
|
|
||||||
console.log('Confirming transaction:', signature);
|
|
||||||
return connection.getSignatureStatus(signature).then(confirmation => {
|
|
||||||
if (confirmation && 'Ok' in confirmation) {
|
|
||||||
console.log('Transaction confirmed');
|
|
||||||
} else if (confirmation) {
|
|
||||||
throw new Error(
|
|
||||||
`Transaction was not confirmed (${JSON.stringify(confirmation.Err)})`,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
throw new Error(`Transaction was not confirmed (${confirmation})`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function airDrop(feeCalculator) {
|
|
||||||
const airdrop = 100 + 5 * feeCalculator.targetLamportsPerSignature;
|
|
||||||
console.log(`\n== Requesting airdrop of ${airdrop} to ${account1.publicKey}`);
|
|
||||||
return connection
|
|
||||||
.requestAirdrop(account1.publicKey, airdrop)
|
|
||||||
.then(confirmTransaction);
|
|
||||||
}
|
|
||||||
|
|
||||||
getTransactionFee().then(feeCalculator => {
|
getTransactionFee().then(feeCalculator => {
|
||||||
showBalance()
|
airDrop(feeCalculator)
|
||||||
.then(airDrop(feeCalculator))
|
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log(`\n== Move 1 lamport to approver1`);
|
console.log(`\n== Move 1 lamport to approver1`);
|
||||||
const transaction = solanaWeb3.SystemProgram.transfer(
|
const transaction = solanaWeb3.SystemProgram.transfer(
|
||||||
|
@ -351,7 +351,7 @@ export class BudgetProgram {
|
|||||||
keys: [
|
keys: [
|
||||||
{pubkey: from, isSigner: true, isWritable: true},
|
{pubkey: from, isSigner: true, isWritable: true},
|
||||||
{pubkey: program, isSigner: false, isWritable: true},
|
{pubkey: program, isSigner: false, isWritable: true},
|
||||||
{pubkey: to, isSigner: false, isWritable: false},
|
{pubkey: to, isSigner: false, isWritable: true},
|
||||||
],
|
],
|
||||||
programId: this.programId,
|
programId: this.programId,
|
||||||
data,
|
data,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user