@agreewe/solana-blockchain-contracts
v0.1.5
Published
Interface to the Fanout wallet Solana Blockchain contract
Downloads
69
Readme
Introduction
This package is derived from the Rust program found in programs/SolSplitter via a script using Solita.
How to use
The interface for functions from the rust programs are found in /packages/sdk/generated. These files should NOT have to be touched unless necessary. The interactions with these functions can be found in packages/sdk/index.ts.
The general idea of the functions found in index.ts is as follows:
- Call functions to generated instructions
- Signing of instructions/transaction
- Send to Solana blockchain
Initializing a Fanout Wallet
const connection = new Connection("devnet", "confirmed");
let fanoutSdk: FanoutClient;
authorityWallet = useWallet() // wallet instance
fanoutSdk = new FanoutClient(
connection,
new NodeWallet(new Account(authorityWallet.secretKey))
);
const init = await fanoutSdk.initializeFanout({
totalShares: 100,
name: `Test${Date.now()}`,
membershipModel: MembershipModel.Wallet,
phaseLimits: []
});
Adding Members
For adding members, a sample usecase is as follows. Input parameters for function can be found in index.ts
const member = new Keypair()
const {membershipAccount} = await fanoutSdk.addMemberWallet({
fanout: init.fanout,
fanoutNativeAccount: init.nativeAccount,
membershipKey: member.publicKey,
shares: [50]
});
Distribution
To distribute all, call the distributeAll function as shown below.
await fanoutSdk.distributeAll(
{
fanout: init.fanout,
payer: signer
}
)
Transfer Shares
To transfer shares from one member wallet to another, call the transferShares function as shown below.
await fanoutSdk.transferShares({
fromMember: member0Wallet.publicKey,
toMember: member1Wallet.publicKey,
fanout: builtFanout.fanout,
shares: [40,40,30]
});
Quick start
Install the package from npm:
yarn add @agreewe/solana-blockchain-contracts
This is how you'd setup
import { Keypair, LAMPORTS_PER_SOL} from "@solana/web3.js";
import * as anchor from "@project-serum/anchor"
import {NodeWallet} from "@project-serum/common";
import {
Fanout,
FanoutClient,
FanoutMembershipVoucher,
} from "@agreewe/solana-blockchain-contracts";
const connection = new anchor.web3.Connection("devnet", "confirmed");
const authorityWallet = Keypair.generate();
await connection.requestAirdrop(authorityWallet.publicKey, LAMPORTS_PER_SOL * 2);
const fanoutSdk = new FanoutClient(
connection,
new NodeWallet(new Account(authorityWallet.secretKey))
);
const phaseLimits = [100, 200];
// Initialize the Wallet
const {fanout} = await fanoutSdk.initializeFanout({
totalShares: 100,
name: `Your Globally Unique Wallet Name`,
membershipModel: MembershipModel.Wallet,
phaseLimits: phaseLimits,
});
// fanout is your fanout config address
// nativeAccount is your account address
// Retrieve the On-chain Wallet
const fanoutAccount = await fanoutSdk.fetch<Fanout>(
fanout,
Fanout
);
console.log(fanoutAccount); // Shows you all the parameters in your wallet
// This is your Wallet Address
let AccountKey = fanoutAccount.accountKey // this is the same thing as nativeAccount above
// Add members
const member1 = new Keypair();
const {membershipAccount1} = await fanoutSdk.addMemberWallet({
fanout: init.fanout,
fanoutNativeAccount: init.nativeAccount,
membershipKey: member1.publicKey,
shares: 10
});
//Repeat for all members until sum(shares) == totalShares from initialization
...
// Send some Sol to the Wallet so you can distribute
await connection.requestAirdrop(AccountKey, 2);
// Generate the distribution instructions
let distMember1 = await fanoutSdk.distributeWalletMemberInstructions(
{
distributeForMint: false,
member: member1.wallet.publicKey,
fanout: fanout,
payer: authorityWallet.publicKey, // This can be changed to whoever sends the tx
},
);
// Send the distribution instructions
const tx = await fanoutSdk.sendInstructions(
[...distMember1.instructions],
[authorityWallet],
authorityWallet.publicKey
);
if (!!tx.RpcResponseAndContext.value.err) {
const txdetails = await connection.getConfirmedTransaction(tx.TransactionSignature);
console.log(txdetails, tx.RpcResponseAndContext.value.err);
}
// Member1 Should have 0.2 more sol in their wallet