-
Notifications
You must be signed in to change notification settings - Fork 44
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
Add built-in transferCoinTransaction()
function
#20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Account } from "../core"; | ||
import { transferCoinTransaction } from "../internal/coin"; | ||
import { SingleSignerTransaction, GenerateTransactionOptions } from "../transactions/types"; | ||
import { AnyNumber, HexInput, MoveResourceType } from "../types"; | ||
import { AptosConfig } from "./aptos_config"; | ||
|
||
/** | ||
* A class to handle all `Coin` operations | ||
*/ | ||
export class Coin { | ||
readonly config: AptosConfig; | ||
|
||
constructor(config: AptosConfig) { | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* Generate a transfer coin transaction that can be simulated and/or signed and submitted | ||
* | ||
* @param args.sender The sender account | ||
* @param args.recipient The recipient address | ||
* @param args.amount The amount to transfer | ||
* @param args.coinType optional. The coin struct type to transfer. Default to 0x1::aptos_coin::AptosCoin | ||
* | ||
* @returns SingleSignerTransaction | ||
*/ | ||
async transferCoinTransaction(args: { | ||
sender: Account; | ||
recipient: HexInput; | ||
amount: AnyNumber; | ||
coinType?: MoveResourceType; | ||
options?: GenerateTransactionOptions; | ||
}): Promise<SingleSignerTransaction> { | ||
const response = await transferCoinTransaction({ aptosConfig: this.config, ...args }); | ||
return response; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { AptosConfig } from "../api/aptos_config"; | ||
import { U64 } from "../bcs/serializable/move-primitives"; | ||
import { Account, AccountAddress } from "../core"; | ||
import { GenerateTransactionOptions, SingleSignerTransaction } from "../transactions/types"; | ||
import { StructTag, TypeTagStruct } from "../transactions/typeTag/typeTag"; | ||
import { HexInput, AnyNumber, MoveResourceType } from "../types"; | ||
import { APTOS_COIN } from "../utils/const"; | ||
import { generateTransaction } from "./transaction_submission"; | ||
|
||
export async function transferCoinTransaction(args: { | ||
aptosConfig: AptosConfig; | ||
sender: Account; | ||
recipient: HexInput; | ||
amount: AnyNumber; | ||
coinType?: MoveResourceType; | ||
options?: GenerateTransactionOptions; | ||
}): Promise<SingleSignerTransaction> { | ||
const { aptosConfig, sender, recipient, amount, coinType, options } = args; | ||
const coinStructType = coinType ?? APTOS_COIN; | ||
const transaction = await generateTransaction({ | ||
aptosConfig, | ||
sender: sender.accountAddress.toString(), | ||
data: { | ||
function: "0x1::aptos_account::transfer_coins", | ||
type_arguments: [new TypeTagStruct(StructTag.fromString(coinStructType))], | ||
arguments: [AccountAddress.fromHexInput({ input: recipient }), new U64(amount)], | ||
}, | ||
options, | ||
}); | ||
|
||
return transaction as SingleSignerTransaction; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { AptosConfig, Network, Aptos, Account, Deserializer } from "../../../src"; | ||
import { waitForTransaction } from "../../../src/internal/transaction"; | ||
import { RawTransaction, TransactionPayloadEntryFunction } from "../../../src/transactions/instances"; | ||
import { TypeTagStruct } from "../../../src/transactions/typeTag/typeTag"; | ||
import { SigningScheme } from "../../../src/types"; | ||
|
||
describe("coin", () => { | ||
test("it generates a transfer coin transaction with AptosCoin coin type", async () => { | ||
const config = new AptosConfig({ network: Network.DEVNET }); | ||
const aptos = new Aptos(config); | ||
const sender = Account.generate({ scheme: SigningScheme.Ed25519 }); | ||
const recipient = Account.generate({ scheme: SigningScheme.Ed25519 }); | ||
await aptos.fundAccount({ accountAddress: sender.accountAddress.toString(), amount: 100000000 }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest we make these numbers constants for the test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, we need a test framework. will work on it after we have full test cover to our code (not a P0 for alpha version) |
||
|
||
const transaction = await aptos.transferCoinTransaction({ | ||
sender, | ||
recipient: recipient.accountAddress.toString(), | ||
amount: 10, | ||
}); | ||
|
||
const txnDeserializer = new Deserializer(transaction.rawTransaction); | ||
const rawTransaction = RawTransaction.deserialize(txnDeserializer); | ||
Comment on lines
+21
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this is an interesting way of handling it, where you need to deserialize by passing the deserializer in. Might be interesting if we also just provide a way to deserialize the bytes directly (if you only have that). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe @xbtmatt has some thoughts about it |
||
const typeArgs = (rawTransaction.payload as TransactionPayloadEntryFunction).entryFunction.type_args; | ||
expect((typeArgs[0] as TypeTagStruct).value.address.toString()).toBe("0x1"); | ||
expect((typeArgs[0] as TypeTagStruct).value.module_name.identifier).toBe("aptos_coin"); | ||
expect((typeArgs[0] as TypeTagStruct).value.name.identifier).toBe("AptosCoin"); | ||
}); | ||
|
||
test("it generates a transfer coin transaction with a custom coin type", async () => { | ||
const config = new AptosConfig({ network: Network.DEVNET }); | ||
const aptos = new Aptos(config); | ||
const sender = Account.generate({ scheme: SigningScheme.Ed25519 }); | ||
const recipient = Account.generate({ scheme: SigningScheme.Ed25519 }); | ||
await aptos.fundAccount({ accountAddress: sender.accountAddress.toString(), amount: 100000000 }); | ||
|
||
const transaction = await aptos.transferCoinTransaction({ | ||
sender, | ||
recipient: recipient.accountAddress.toString(), | ||
amount: 10, | ||
coinType: "0x1::my_coin::type", | ||
}); | ||
|
||
const txnDeserializer = new Deserializer(transaction.rawTransaction); | ||
const rawTransaction = RawTransaction.deserialize(txnDeserializer); | ||
const typeArgs = (rawTransaction.payload as TransactionPayloadEntryFunction).entryFunction.type_args; | ||
expect((typeArgs[0] as TypeTagStruct).value.address.toString()).toBe("0x1"); | ||
expect((typeArgs[0] as TypeTagStruct).value.module_name.identifier).toBe("my_coin"); | ||
expect((typeArgs[0] as TypeTagStruct).value.name.identifier).toBe("type"); | ||
}); | ||
|
||
test("it transfers APT coin aomunt from sender to recipient", async () => { | ||
const config = new AptosConfig({ network: Network.DEVNET }); | ||
const aptos = new Aptos(config); | ||
const sender = Account.generate({ scheme: SigningScheme.Ed25519 }); | ||
const recipient = Account.generate({ scheme: SigningScheme.Ed25519 }); | ||
|
||
await aptos.fundAccount({ accountAddress: sender.accountAddress.toString(), amount: 100000000 }); | ||
const senderCoinsBefore = await aptos.getAccountCoinsData({ accountAddress: sender.accountAddress.toString() }); | ||
|
||
const transaction = await aptos.transferCoinTransaction({ | ||
sender, | ||
recipient: recipient.accountAddress.toString(), | ||
amount: 10, | ||
}); | ||
const response = await aptos.signAndSubmitTransaction({ signer: sender, transaction }); | ||
|
||
await waitForTransaction({ aptosConfig: config, txnHash: response.hash }); | ||
|
||
const recipientCoins = await aptos.getAccountCoinsData({ accountAddress: recipient.accountAddress.toString() }); | ||
const senderCoinsAfter = await aptos.getAccountCoinsData({ accountAddress: sender.accountAddress.toString() }); | ||
Comment on lines
+69
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to consider unioning The wallet adapter can be less stringent probably. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think most users will get this data from some servers, that will return the address as a string (or in some cases as Uint8Array) - that was the thought behind accepting HexInput as an input and not force them initialize |
||
|
||
expect(recipientCoins[0].amount).toBe(10); | ||
expect(recipientCoins[0].asset_type).toBe("0x1::aptos_coin::AptosCoin"); | ||
expect(senderCoinsAfter[0].amount).toBeLessThan(senderCoinsBefore[0].amount); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this query may be confusing. Is it supposed to be the number of coin types? Or combined between multiple coin types?
Let's be concrete
If I have 5 DogCoin and 7 CatCoin, would the number be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is an existing query in sdk v1. I believe it returns the former. can validate it in a second PR as it is not really relevant to this PR