Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@kadena/graph] Enable other accounts to act as gas payers in the simulation (Feat) #1208

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading