Skip to content

Commit

Permalink
and moved all the existing token buttons to using the wallet-adapter
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
  • Loading branch information
SvenDowideit committed Jun 11, 2022
1 parent 8652ecb commit 7a4b73a
Show file tree
Hide file tree
Showing 2 changed files with 252 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/renderer/nav/TokenPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as sol from '@solana/web3.js';
import * as metaplex from '@metaplex/js';
import { useConnection, useWallet } from '@solana/wallet-adapter-react';

import { mintTo, setAuthority, transfer } from '@solana/spl-token';
import { setAuthority } from '@solana/spl-token';
import { toast } from 'react-toastify';
import createNewAccount from 'renderer/data/accounts/account';
import * as walletWeb3 from '../wallet-adapter/web3';
Expand Down Expand Up @@ -160,7 +160,7 @@ function TokenPage() {
connection,
fromKey,
mintKey.publicKey,
myWallet
toWallet.publicKey
);
updateAtaReceiver(toTokenAccount.address);
} catch (e) {
Expand All @@ -182,10 +182,10 @@ function TokenPage() {
}

// Minting 1 new token to the "fromTokenAccount" account we just returned/created.
const signature = await mintTo(
const signature = await walletWeb3.mintTo(
connection,
myWallet, // Payer of the transaction fees
mintKey, // Mint for the account
fromKey, // Payer of the transaction fees
mintKey.publicKey, // Mint for the account
tokenSender, // Address of the account to mint to
myWallet, // Minting authority
1 // Amount to mint
Expand All @@ -202,12 +202,12 @@ function TokenPage() {
return;
}

await setAuthority(
await walletWeb3.setAuthority(
connection,
myWallet, // Payer of the transaction fees
mintKey, // Account
myWallet.publicKey, // Current authority
0, // Authority type: "0" represents Mint Tokens
fromKey, // Payer of the transaction fees
mintKey.publicKey, // Account
myWallet, // Current authority
'MintTokens', // Authority type: "0" represents Mint Tokens
null // Setting the new Authority to null
);
}
Expand All @@ -224,12 +224,12 @@ function TokenPage() {
logger.info('no ataReceiver', ataReceiver);
return;
}
const signature = await transfer(
const signature = await walletWeb3.transfer(
connection,
myWallet, // Payer of the transaction fees
fromKey, // Payer of the transaction fees
tokenSender, // Source account
ataReceiver, // Destination account
myWallet.publicKey, // Owner of the source account
myWallet, // Owner of the source account
1 // Number of tokens to transfer
);

Expand Down Expand Up @@ -322,9 +322,9 @@ function TokenPage() {
}
onClick={() => {
toast.promise(mintToken(), {
pending: `Mint To ${myWallet?.publicKey.toString()} submitted`,
success: `Mint To ${myWallet?.publicKey.toString()} succeeded 👌`,
error: `Mint To ${myWallet?.publicKey.toString()} failed 🤯`,
pending: `Mint To ${myWallet?.toString()} submitted`,
success: `Mint To ${myWallet?.toString()} succeeded 👌`,
error: `Mint To ${myWallet?.toString()} failed 🤯`,
});
}}
>
Expand Down
236 changes: 236 additions & 0 deletions src/renderer/wallet-adapter/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,240 @@ export async function getOrCreateAssociatedTokenAccount(
return account;
}

// ARGH!
// https://github.com/solana-labs/solana-program-library/blob/master/token/js/src/actions/internal.ts
export function getSigners(
signerOrMultisig: sol.Signer | sol.PublicKey,
multiSigners: sol.Signer[]
): [sol.PublicKey, sol.Signer[]] {
return signerOrMultisig instanceof sol.PublicKey
? [signerOrMultisig, multiSigners]
: [signerOrMultisig.publicKey, [signerOrMultisig]];
}

// https://github.com/solana-labs/solana-program-library/blob/master/token/js/src/actions/mintTo.ts
/**
* Mint tokens to an account
*
* @param connection Connection to use
* @param payer Payer of the transaction fees
* @param mint Mint for the account
* @param destination Address of the account to mint to
* @param authority Minting authority
* @param amount Amount to mint
* @param multiSigners Signing accounts if `authority` is a multisig
* @param confirmOptions Options for confirming the transaction
* @param programId SPL Token program account
*
* @return Signature of the confirmed transaction
*/
export async function mintTo(
connection: sol.Connection,
payer: sol.Signer | WalletContextState,
mint: sol.PublicKey,
destination: sol.PublicKey,
authority: sol.Signer | sol.PublicKey,
amount: number | bigint,
multiSigners: sol.Signer[] = [],
confirmOptions?: sol.ConfirmOptions,
programId = splToken.TOKEN_PROGRAM_ID
): Promise<sol.TransactionSignature> {
const [authorityPublicKey, signers] = getSigners(authority, multiSigners);

const transaction = new sol.Transaction().add(
splToken.createMintToInstruction(
mint,
destination,
authorityPublicKey,
amount,
multiSigners,
programId
)
);

if ('privateKey' in payer) {
// payer is a sol.Signer
return sol.sendAndConfirmTransaction(
connection,
transaction,
[payer, ...signers],
confirmOptions
);
}
if ('sendTransaction' in payer) {
// payer is a WalletContextState
const options: SendTransactionOptions = { signers: [...signers] };
const signature = await payer.sendTransaction(
transaction,
connection,
options
);

// await connection.confirmTransaction(signature, confirmOptions?.commitment);
const latestBlockHash = await connection.getLatestBlockhash();

await connection.confirmTransaction(
{
blockhash: latestBlockHash.blockhash,
lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
signature,
},
confirmOptions?.commitment
);
} else {
throw Error('payer not a Keypair or Wallet.');
}
}

// https://github.com/solana-labs/solana-program-library/blob/master/token/js/src/actions/transfer.ts
/**
* Transfer tokens from one account to another
*
* @param connection Connection to use
* @param payer Payer of the transaction fees
* @param source Source account
* @param destination Destination account
* @param owner Owner of the source account
* @param amount Number of tokens to transfer
* @param multiSigners Signing accounts if `owner` is a multisig
* @param confirmOptions Options for confirming the transaction
* @param programId SPL Token program account
*
* @return Signature of the confirmed transaction
*/
export async function transfer(
connection: sol.Connection,
payer: sol.Signer | WalletContextState,
source: sol.PublicKey,
destination: sol.PublicKey,
owner: sol.Signer | sol.PublicKey,
amount: number | bigint,
multiSigners: sol.Signer[] = [],
confirmOptions?: sol.ConfirmOptions,
programId = splToken.TOKEN_PROGRAM_ID
): Promise<sol.TransactionSignature> {
const [ownerPublicKey, signers] = getSigners(owner, multiSigners);

const transaction = new sol.Transaction().add(
splToken.createTransferInstruction(
source,
destination,
ownerPublicKey,
amount,
multiSigners,
programId
)
);

if ('privateKey' in payer) {
// payer is a sol.Signer
return sol.sendAndConfirmTransaction(
connection,
transaction,
[payer, ...signers],
confirmOptions
);
}
if ('sendTransaction' in payer) {
// payer is a WalletContextState
const options: SendTransactionOptions = { signers: [...signers] };
const signature = await payer.sendTransaction(
transaction,
connection,
options
);

// await connection.confirmTransaction(signature, confirmOptions?.commitment);
const latestBlockHash = await connection.getLatestBlockhash();

await connection.confirmTransaction(
{
blockhash: latestBlockHash.blockhash,
lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
signature,
},
confirmOptions?.commitment
);
} else {
throw Error('payer not a Keypair or Wallet.');
}
}

// https://github.com/solana-labs/solana-program-library/blob/master/token/js/src/actions/setAuthority.ts
/**
* Assign a new authority to the account
*
* @param connection Connection to use
* @param payer Payer of the transaction fees
* @param account Address of the account
* @param currentAuthority Current authority of the specified type
* @param authorityType Type of authority to set
* @param newAuthority New authority of the account
* @param multiSigners Signing accounts if `currentAuthority` is a multisig
* @param confirmOptions Options for confirming the transaction
* @param programId SPL Token program account
*
* @return Signature of the confirmed transaction
*/
export async function setAuthority(
connection: sol.Connection,
payer: sol.Signer | WalletContextState,
account: sol.PublicKey,
currentAuthority: sol.Signer | sol.PublicKey,
authorityType: splToken.AuthorityType,
newAuthority: sol.PublicKey | null,
multiSigners: sol.Signer[] = [],
confirmOptions?: sol.ConfirmOptions,
programId = splToken.TOKEN_PROGRAM_ID
): Promise<sol.TransactionSignature> {
const [currentAuthorityPublicKey, signers] = getSigners(
currentAuthority,
multiSigners
);

const transaction = new sol.Transaction().add(
splToken.createSetAuthorityInstruction(
account,
currentAuthorityPublicKey,
authorityType,
newAuthority,
multiSigners,
programId
)
);

if ('privateKey' in payer) {
// payer is a sol.Signer
return sol.sendAndConfirmTransaction(
connection,
transaction,
[payer, ...signers],
confirmOptions
);
}
if ('sendTransaction' in payer) {
// payer is a WalletContextState
const options: SendTransactionOptions = { signers: [...signers] };
const signature = await payer.sendTransaction(
transaction,
connection,
options
);

// await connection.confirmTransaction(signature, confirmOptions?.commitment);
const latestBlockHash = await connection.getLatestBlockhash();

await connection.confirmTransaction(
{
blockhash: latestBlockHash.blockhash,
lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
signature,
},
confirmOptions?.commitment
);
} else {
throw Error('payer not a Keypair or Wallet.');
}
}

export default {};

0 comments on commit 7a4b73a

Please sign in to comment.