fix: Squash budget bugs
This commit is contained in:
committed by
Michael Vines
parent
c99d8dd15c
commit
87c1becbef
@ -9,7 +9,6 @@ const solanaWeb3 = require('..');
|
||||
|
||||
const account1 = new solanaWeb3.Account();
|
||||
const account2 = new solanaWeb3.Account();
|
||||
const contractFunds = new solanaWeb3.Account();
|
||||
const contractState = new solanaWeb3.Account();
|
||||
|
||||
let url;
|
||||
@ -23,28 +22,20 @@ function showBalance() {
|
||||
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}`,
|
||||
);
|
||||
},
|
||||
);
|
||||
]).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) {
|
||||
@ -52,6 +43,10 @@ function confirmTransaction(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})`);
|
||||
}
|
||||
@ -68,36 +63,10 @@ function airDrop() {
|
||||
showBalance()
|
||||
.then(airDrop)
|
||||
.then(showBalance)
|
||||
.then(() => {
|
||||
console.log(`\n== Creating account for the contract funds`);
|
||||
const transaction = solanaWeb3.SystemProgram.createAccount(
|
||||
account1.publicKey,
|
||||
contractFunds.publicKey,
|
||||
50, // number of lamports to transfer
|
||||
0,
|
||||
solanaWeb3.BudgetProgram.programId,
|
||||
);
|
||||
return connection.sendTransaction(transaction, account1);
|
||||
})
|
||||
.then(confirmTransaction)
|
||||
.then(showBalance)
|
||||
.then(() => {
|
||||
console.log(`\n== Creating account for the contract state`);
|
||||
const transaction = solanaWeb3.SystemProgram.createAccount(
|
||||
account1.publicKey,
|
||||
contractState.publicKey,
|
||||
1, // account1 pays 1 lamport to hold the contract state
|
||||
solanaWeb3.BudgetProgram.space,
|
||||
solanaWeb3.BudgetProgram.programId,
|
||||
);
|
||||
return connection.sendTransaction(transaction, account1);
|
||||
})
|
||||
.then(confirmTransaction)
|
||||
.then(showBalance)
|
||||
.then(() => {
|
||||
console.log(`\n== Initializing contract`);
|
||||
const transaction = solanaWeb3.BudgetProgram.pay(
|
||||
contractFunds.publicKey,
|
||||
account1.publicKey,
|
||||
contractState.publicKey,
|
||||
account2.publicKey,
|
||||
50,
|
||||
@ -106,7 +75,11 @@ showBalance()
|
||||
new Date('2050'),
|
||||
),
|
||||
);
|
||||
return connection.sendTransaction(transaction, contractFunds);
|
||||
return solanaWeb3.sendAndConfirmTransaction(
|
||||
connection,
|
||||
transaction,
|
||||
account1,
|
||||
);
|
||||
})
|
||||
.then(confirmTransaction)
|
||||
.then(showBalance)
|
||||
@ -118,7 +91,11 @@ showBalance()
|
||||
account2.publicKey,
|
||||
new Date('2050'),
|
||||
);
|
||||
return connection.sendTransaction(transaction, account1);
|
||||
return solanaWeb3.sendAndConfirmTransaction(
|
||||
connection,
|
||||
transaction,
|
||||
account1,
|
||||
);
|
||||
})
|
||||
.then(confirmTransaction)
|
||||
.then(showBalance)
|
||||
|
Reference in New Issue
Block a user