Skip to content

Commit

Permalink
Merge pull request #1208 from kadena-community/feat/graph/enable-othe…
Browse files Browse the repository at this point in the history
…r-accounts-to-pay-gas

[@kadena/graph] Enable other accounts to act as gas payers in the simulation (Feat)
  • Loading branch information
nil-amrutlal authored Nov 10, 2023
2 parents 6653feb + 9aacf24 commit 81df74c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-phones-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kadena/graph': patch
---

Enabled option for different gas payers in simulation
11 changes: 6 additions & 5 deletions packages/apps/graph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ pnpm build --filter @kadena/graph...

2. Start devnet:

> **NOTE:** This project has a built-in command to create and start devnet. For the full guide visit the quickstart page on the documentation website [here](https://docs.kadena.io/build/quickstart).
> **NOTE:** This project has a built-in command to create and start devnet.
> For the full guide visit the quickstart page on the documentation website
> [here](https://docs.kadena.io/build/quickstart).
```sh
pnpm run devnet
Expand All @@ -51,8 +53,7 @@ pnpm build --filter @kadena/graph...
the image by adding `--pull=always` after `docker run` in the `devnet`
script.

If something goes wrong, you can delete the volume, and try to
start again:
If something goes wrong, you can delete the volume, and try to start again:

```sh
docker volume rm kadena_devnet
Expand Down Expand Up @@ -120,10 +121,10 @@ transactions are different, with different amounts and to and from different
chains. The new number is generated using the previous one as seed.

```sh
npm run simulate -- -a <numberOfAccounts> -i <timeInterval> -t <maxAmount> -tp <tokenPool> -s <seed>
npm run simulate -a <numberOfAccounts> -i <timeInterval> -t <maxAmount> -tp <tokenPool> -s <seed>
```

- accounts - number of accounts to be created in the devnet (default: 5)
- numberOfAccounts - number of accounts to be created in the devnet (default: 6)
- timeInterval - frequency of transactions in miliseconds (default: 100)
- maxAmount - maximum amount for a single transaction (default: 25)
- tokenPool - amount of circulating tokens (default: 1000000)
Expand Down
22 changes: 20 additions & 2 deletions packages/apps/graph/src/devnet/crosschain-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,30 @@ export async function crossChainTransfer({
from,
to,
amount,
gasPayer = sender00,
}: {
from: IAccount;
to: IAccount;
amount: number;
gasPayer?: IAccount;
}): Promise<ICommandResult> {
// Gas Payer validations
if (gasPayer.chainId !== to.chainId) {
logger.info(
`Gas payer ${gasPayer.account} does not for sure have an account on the receiver chain; using sender00 as gas payer`,
);
gasPayer = sender00;
}

if (!gasPayer.secretKey) {
logger.info(
`Gas payer ${gasPayer.account} does not have a secret key; using sender00 as gas payer`,
);
gasPayer = sender00;
}

logger.info(
`Crosschain Transfer from ${from.account}, chain ${from.chainId}\nTo ${to.account}, chain ${to.chainId}\nAmount: ${amount}`,
`Crosschain Transfer from ${from.account}, chain ${from.chainId}\nTo ${to.account}, chain ${to.chainId}\nAmount: ${amount}\nGas Payer: ${gasPayer.account}`,
);

const pactAmount = new PactNumber(amount).toPactDecimal();
Expand Down Expand Up @@ -113,9 +130,10 @@ export async function crossChainTransfer({
const unsignedTx2 = finishInTheTargetChain(
continuation,
to.chainId || devnetConfig.CHAIN_ID,
gasPayer,
);

const signedTx2 = signAndAssertTransaction([sender00])(unsignedTx2);
const signedTx2 = signAndAssertTransaction([gasPayer])(unsignedTx2);
const submittedTx2 = await submit(signedTx2);
inspect('Transfer Submited')(submittedTx2);
const status2 = await listen(submittedTx2);
Expand Down
13 changes: 10 additions & 3 deletions packages/apps/graph/src/devnet/simulation/simulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
isEqualChainAccounts,
logger,
seedRandom,
sender00,
} from '../helper';
import { safeTransfer } from '../safe-transfer';
import { transfer } from '../transfer';
Expand All @@ -23,8 +24,8 @@ const simualtionTransferOptions: TransferType[] = [
];

export async function simulate({
numberOfAccounts = 2,
transferInterval = 1000,
numberOfAccounts = 6,
transferInterval = 100,
maxAmount = 25,
tokenPool = 1000000,
seed = Date.now().toString(),
Expand Down Expand Up @@ -134,11 +135,17 @@ export async function simulate({
continue;
}

logger.info('Cross chain transfer', account, nextAccount);
// Get a random account to potentially pay for the gas
const possibleGasPayer = getRandomOption(seededRandomNo, accounts);

result = await crossChainTransfer({
from: account,
to: nextAccount,
amount,
gasPayer:
possibleGasPayer.chainId === nextAccount.chainId
? possibleGasPayer
: sender00,
});
} else {
// Make sure the chain id is the same if the transfer type is transfer or safe-transfer
Expand Down

0 comments on commit 81df74c

Please sign in to comment.