-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { transaferCoinTransaction } 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 transaferCoinTransaction(args: { | ||
sender: Account; | ||
recipient: HexInput; | ||
amount: AnyNumber; | ||
coinType?: MoveResourceType; | ||
options?: GenerateTransactionOptions; | ||
}): Promise<SingleSignerTransaction> { | ||
const response = await transaferCoinTransaction({ aptosConfig: this.config, ...args }); | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 transaferCoinTransaction(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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
|
||
const transaction = await aptos.transaferCoinTransaction({ | ||
sender, | ||
recipient: recipient.accountAddress.toString(), | ||
amount: 10, | ||
}); | ||
|
||
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("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.transaferCoinTransaction({ | ||
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.transaferCoinTransaction({ | ||
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() }); | ||
|
||
expect(recipientCoins[0].amount).toBe(10); | ||
expect(recipientCoins[0].asset_type).toBe("0x1::aptos_coin::AptosCoin"); | ||
expect(senderCoinsAfter[0].amount).toBeLessThan(senderCoinsBefore[0].amount); | ||
}); | ||
}); |