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

Convenient Coin APIs #192

Merged
merged 4 commits into from
Mar 22, 2022
Merged

Convenient Coin APIs #192

merged 4 commits into from
Mar 22, 2022

Conversation

AlicanC
Copy link
Contributor

@AlicanC AlicanC commented Mar 19, 2022

feat!: add TransactionRequest classes

  • Replaces ScriptTransactionRequest/CreateTransactionRequest types with classes that have methods like addCoins, addContract, etc. to make things easier.

feat: add wallet coin APIs

  • Adds methods: getCoinsToSpend, getCoins, getBalances and transfer
  • Adds fund(tx) method which calculates the cost of the tx and adds coins from the wallet to the tx to cover it.

feat: add CoinQuantity

  • Adds CoinQuantity which is a combination of an amount and an AssetId. This replaces SpendQueryElement, and instead of just { amount, assetId } it also accepts [amount, assetId], allowing simpler APIs.

feat: add interfaces package

  • Adds @fuel-ts/interfaces, which is similar to fuel-core-interfaces. Currently contains AbstractWallet and AbstractContract. This allows working with Wallets and Contracts without importing their @fuel-ts/xyz packages and prevents circular deps.

A transfer (single-source/multi-destination/multi-asset) after these changes:

const coins = await sender.getCoinsToSpend([
  [amount.mul(2), assetIdA],
  [amount.mul(2), assetIdB],
]);

const request = new ScriptTransactionRequest({ gasLimit: 1000000 });
request.addCoins(coins);
request.addCoinOutputs(receiverA, [
  [amount, assetIdA],
  [amount, assetIdB],
]);
request.addCoinOutputs(receiverB, [
  [amount, assetIdA],
  [amount, assetIdB],
]);

const response = await provider.sendTransaction(request);

Before:

const coins = await provider.getCoinsToSpend(sender, [
      { assetId: assetIdA, amount: amount.mul(2) },
      { assetId: assetIdB, amount: amount.mul(2) },
    ]);

await provider.sendTransaction({
  type: TransactionType.Script,
  gasPrice: BigNumber.from(0),
  gasLimit: BigNumber.from(1000000),
  bytePrice: BigNumber.from(0),
  script: returnZeroScript.bytes,
  inputs: coins.map((coin) => ({
    type: InputType.Coin,
    ...coin,
    witnessIndex: 0,
  })),
  outputs: [
    {
      type: OutputType.Coin,
      to: receiverA.address,
      assetId: assetIdA,
      amount,
    },
    {
      type: OutputType.Coin,
      to: receiverA.address,
      assetId: assetIdB,
      amount,
    },
    {
      type: OutputType.Coin,
      to: receiverB.address,
      assetId: assetIdA,
      amount,
    },
    {
      type: OutputType.Coin,
      to: receiverB.address,
      assetId: assetIdB,
      amount,
    },
    { type: OutputType.Change, assetId: assetIdA, to: sender },
    { type: OutputType.Change, assetId: assetIdB, to: sender },
  ],
  witnesses: ['0x'],
});

@AlicanC AlicanC self-assigned this Mar 19, 2022
@AlicanC AlicanC added the feat Issue is a feature label Mar 19, 2022
@AlicanC AlicanC linked an issue Mar 19, 2022 that may be closed by this pull request
@AlicanC AlicanC changed the title Jc/coin apis Convenient Coin APIs Mar 19, 2022
@AlicanC AlicanC marked this pull request as ready for review March 20, 2022 03:10
@AlicanC
Copy link
Contributor Author

AlicanC commented Mar 20, 2022

Support submitting multiple transactions at once #194 was going to be resolved in this PR, but I've found a better way to do it which is blocked by a fuel-core issue so I left it to not block this.

Copy link
Member

@digorithm digorithm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Great work on this, the API looks pretty neat and clean!

@AlicanC AlicanC merged commit b19615b into master Mar 22, 2022
@AlicanC AlicanC deleted the jc/coin-apis branch March 22, 2022 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Convenient Coin APIs
3 participants