Handle a transaction with 2 signers and make one of them pay - blockchain

I have this account:
#[derive(Accounts)]
pub struct SignupEmployee<'info> {
#[account(init, payer = company_account, space = 8 + 32 + 32)]
pub employee_account: Account<'info, EmployeeState>,
#[account(mut)]
pub company_account: Signer<'info>,
#[account(mut)]
pub authority: Signer<'info>,
pub system_program: Program<'info, System>,
}
And this test function
const employeeAccount = anchor.web3.Keypair.generate();
await program.rpc.signupEmployee({
accounts: {
authority: provider.wallet.publicKey,
companyAccount: companyAccount.publicKey,
employeeAccount: employeeAccount.publicKey,
systemProgram: SystemProgram.programId,
},
signers: [employeeAccount, companyAccount],
});
What I want to achieve is to make the company pay for the employee signup.
Basically on my system I will have both the keys of the company and the keys of the employee.
The employee will signup with his key and the company would have to pay for the transaction.
I see that the signers is an array, so I imagine that I can enter both, the company and the employee accounts.
How can I achieve this?

The Transaction object in the solana web3 package has a feePayer field which you are supposed to set to the public key of the account you wish to pay for the transaction. Then that account needs to also sign the transaction.
Feel free to look into the documentation for the Transaction class here

in your instruction you already assigning company_account as payer for the new account initialisation, so i'm assuming that you are wondering how to pay gas fee with the same account. You could use program.instruction.signupEmployee which will output transaction instruction that you could add to the custom transaction where you can specify a gas fee payer. This way you would need to sign and send transaction using your custom code.

Related

Ethers.js token price chart

I'm using Ethers.js to get the token price from BSC using getReserves successfully.
const nodeRandom = !node ? wssNodes() : node;
const provider = new ethers.providers.WebSocketProvider(nodeRandom);
const pairAddress = await pancake.getPair(token0, token1);
if (pairAddress === "0x0000000000000000000000000000000000000000") {
return {
status: "Pair not found",
};
}
const pairContract = new ethers.Contract(pairAddress, pancakePair, provider);
const reserves = await pairContract.getReserves();
I want to create a price chart for that token, but I get into trouble when I don't know how to get the historical price data from BSC.
Does Ethers.js support getting the token price history, or should we store the price we get into our database? If then, is there any way we can build the price chart of a token from the very beginning of the first block when we don't have that in our DB?
Any idea?
You can use the blockTag field of the overrides object - docs. It queries the node to return the value from a specific block instead of the current.
const reserves = await pairContract.getReserves({
blockTag: <blockNumer>
});
Note that it depends on the node provider if they support these historical queries or not. Most providers support it only in higher tier plans or not at all.

Update Hyperledger ACL from transactions

I have User and Buyer participants on the network. Generally, the buyers cannot READ the user's data but I want to make GrantAccess and RevokeAccess transactions so the Users to have the option to grant and revoke the READ access from Buyers
I haven't been able to find anything on how to do this, would appreciate any help.
You would run a 'tx_GrantAccess' transaction that firstly, updates a particular BUYER's record (eg. id buyer123 - a participant modeled with a field called access, which is set to true by this transaction).
I can use a condition match (as a boolean) on the target BUYER records (resources) and if the BUYER, say buyer123 (ie that's accessing the business network) has access=true then he can READ the USER records.
Transaction rule (needed by User to access the transaction classes)
rule rule_1 {
description: "grant access to User, for the 2 x Transactions themselves"
participant: "org.acme.example.User"
operation: CREATE
resource: "org.acme.example.tx_*"
action: ALLOW
}
User Access rule:
rule rule_2 {
description: "if granted access, allow READ of User by buyer"
participant(m): "org.acme.example.Buyer"
operation: READ
resource(v): "org.acme.example.User"
condition: (m.access)
action: ALLOW
}
where Buyer has a field (eg.
participant Buyer identified by id {
o String id
o Boolean access default=false
}
and your transaction tx_GrantAccess has a function that will set access to true on a particular Buyer's record and tx_RevokeAccess will set it to false etc.

Designing model file in hyperledger composer

Considering a bank scenario , where participants are customers and transaction is money transfer , it can be as
asset Account identified by accountId{
o String accountId
--> Customer owner
o Double balance
}
transaction AccountTransfer {
--> Account from
--> Account to
o Double amount
}
But what if there are different types of Participants who holds accounts. Like one can only transfer(sender) and while other can only receive(receiver)?How to solve this as account cant have two types of owners.
Can this be like this?
asset account identified by accountId{
o String accountId
o Double balance
}
Participant sender identified by sid{
--> Account account
o String sId
}
Participant receiver identified by rid{
--> Account account
o String rId
}
Transaction send {
-->Sender sender
-->Receiver receiver
}
Is it appropriate to design the model like above?
Yes, it should work.
To be sure that only the sender can execute a transfer, you can implement a logic in the transaction processor, which arise an exception if the participant that is invoking the transaction is not a "sender".
Model
asset account identified by accountId{
o String accountId
o Double balance
}
Participant sender identified by sid{
--> Account account
o String sId
}
Participant receiver identified by rid{
--> Account account
o String rId
}
Transaction send {
-->Sender sender
-->Receiver receiver
}
TP
async function send(tx) {
if (currentParticipant.getFullyQualifiedType() !== 'org.example.sender') {
// Throw an error as the current participant is not a sender.
throw new Error('Current participant is not a sender');
}
//Other business logics
}
Otherwise you can implement a rule in the access control file which allow to transfer money only if who is invoking the transaction is a sender
rule AllowSenderToTransferMoney {
description: "Sender can transfer money"
participant(m): "org.example.sender"
operation: ALL
resource(v): "org.example.account"
transaction(tx): "org.example.send"
action: ALLOW
}

Ethereum send transaction for only one account

I have Ethereum account (#/Eth).
I use TestRPC and Truffle
0xb1c38c605b8468cea3fc51e204098659c666b8b9/41.86142259999994
0x7c6b249c8b721845df50d60c3a6eda75db36212a/41.86142259999994
0x2d8e958af078aba822a73d6bac8b1d116d5769fa/41.86142259999994
0x24c236cb31704b5a20c39ff165543ce9a058924a/41.86142259999994
I want transfert Ether from 1° account to the 2° account.
I try :
web3.eth.sendTransaction({from: '0xb1c38c605b8468cea3fc51e204098659c666b8b9', to: '0x7c6b249c8b721845df50d60c3a6eda75db36212a', value: web3.toWei(3, "ether")})
But all account are debited :'( Why ?
0xb1c38c605b8468cea3fc51e204098659c666b8b9 38.86142259999992
0x7c6b249c8b721845df50d60c3a6eda75db36212a 38.86142259999992
0x2d8e958af078aba822a73d6bac8b1d116d5769fa 38.86142259999992
0x24c236cb31704b5a20c39ff165543ce9a058924a 38.86142259999992

djStripe - zero dollar plan - do not ask for credit card, but create customer

I am using Django app djStripe to work integrate Stripe into my Django app to allow users to subscribe to plans and pay using Stripe.
I want to have a zero dollar plan but create a Stripe customer account so in future using can just change subscription from zero to a paid plan, and then they will be asked for their credit card info.
This is acceptable in Stripe and according to Stripe a zero dollar subscription does not ask for credit card though it does create customer. However, djStripe does ask for credit card with a zero dollar plan.
djStripe readthedocs does hint at custom plans being the solution but I am need help to determine if
a) that is indeed the way and
b) if yes to a), then how to implement.
I've setup the plan in my app's Settings.py as follows:
DJSTRIPE_PLANS = {
"starter": {
"stripe_plan_id": "starter",
"name": "Starter",
"description": "Starter subscription.",
"statement_descriptor": "Starter co",
"price": 0, # $0
"currency": "usd",
"interval": "year",
"trial_period_days": 0,
"team_size": 2,
"image_count": 1000
}
}
I haven't customized any of the standard djStripe subscription process.
First of all add trail period in the plan, because without a trial period stripe tries to charge the customer for that it requires a credit card info. Subscribe to customer.subscription.trial_will_end stripe will send this webhook three days before the trial expires and on that event update trail for customer.