-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transfer token and send uluna commands
- Loading branch information
Showing
10 changed files
with
200 additions
and
66 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
83 changes: 47 additions & 36 deletions
83
packages-ts/gauntlet-terra-contracts/src/commands/contracts/link/transfer.ts
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 |
---|---|---|
@@ -1,46 +1,57 @@ | ||
import { TerraCommand, TransactionResponse } from '@chainlink/gauntlet-terra' | ||
import { Result } from '@chainlink/gauntlet-core' | ||
import { BN, logger, prompt } from '@chainlink/gauntlet-core/dist/utils' | ||
import { CATEGORIES, TOKEN_DECIMALS } from '../../../lib/constants' | ||
import { AbstractInstruction, BeforeExecutionContext, instructionToCommand } from '../../abstract/executionWrapper' | ||
import { AccAddress } from '@terra-money/terra.js' | ||
|
||
export default class TransferLink extends TerraCommand { | ||
static description = 'Transfer LINK' | ||
static examples = [ | ||
`yarn gauntlet token:transfer --network=bombay-testnet --to=[RECEIVER] --amount=[AMOUNT_IN_TOKEN_UNITS]`, | ||
`yarn gauntlet token:transfer --network=bombay-testnet --to=[RECEIVER] --amount=[AMOUNT_IN_TOKEN_UNITS] --link=[TOKEN_ADDRESS] --decimals=[TOKEN_DECIMALS]`, | ||
] | ||
type CommandInput = { | ||
to: string | ||
// Units in LINK | ||
amount: string | ||
} | ||
|
||
static id = 'token:transfer' | ||
static category = CATEGORIES.LINK | ||
type ContractInput = { | ||
recipient: string | ||
amount: string | ||
} | ||
|
||
constructor(flags, args: string[]) { | ||
super(flags, args) | ||
const makeCommandInput = async (flags: any): Promise<CommandInput> => { | ||
if (flags.input) return flags.input as CommandInput | ||
return { | ||
to: flags.to, | ||
amount: flags.amount, | ||
} | ||
} | ||
|
||
makeRawTransaction = async () => { | ||
throw new Error('Transfer LINK command: makeRawTransaction method not implemented') | ||
} | ||
const validateInput = (input: CommandInput): boolean => { | ||
if (!AccAddress.validate(input.to)) throw new Error(`Invalid destination address`) | ||
return true | ||
} | ||
|
||
execute = async () => { | ||
const decimals = this.flags.decimals || TOKEN_DECIMALS | ||
const link = this.flags.link || process.env.LINK | ||
const amount = new BN(this.flags.amount).mul(new BN(10).pow(new BN(decimals))) | ||
|
||
await prompt(`Sending ${this.flags.amount} LINK (${amount.toString()}) to ${this.flags.to}. Continue?`) | ||
const tx = await this.call(link, { | ||
transfer: { | ||
recipient: this.flags.to, | ||
amount: amount.toString(), | ||
}, | ||
}) | ||
logger.success(`LINK transferred successfully to ${this.flags.to} (txhash: ${tx.hash})`) | ||
return { | ||
responses: [ | ||
{ | ||
tx, | ||
contract: link, | ||
}, | ||
], | ||
} as Result<TransactionResponse> | ||
const makeContractInput = async (input: CommandInput): Promise<ContractInput> => { | ||
const amount = new BN(input.amount).mul(new BN(10).pow(new BN(TOKEN_DECIMALS))) | ||
return { | ||
recipient: input.to, | ||
amount: amount.toString(), | ||
} | ||
} | ||
|
||
const beforeExecute = async (context: BeforeExecutionContext): Promise<void> => { | ||
logger.info( | ||
`Transferring ${context.contractInput.amount} (${context.input.amount}) Tokens to ${context.contractInput.recipient}`, | ||
) | ||
await prompt('Continue?') | ||
} | ||
|
||
const transferToken: AbstractInstruction<CommandInput, ContractInput> = { | ||
instruction: { | ||
category: CATEGORIES.LINK, | ||
contract: 'cw20_base', | ||
function: 'transfer', | ||
}, | ||
makeInput: makeCommandInput, | ||
validateInput: validateInput, | ||
makeContractInput: makeContractInput, | ||
beforeExecute, | ||
} | ||
|
||
export default instructionToCommand(transferToken) |
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
3 changes: 3 additions & 0 deletions
3
packages-ts/gauntlet-terra-contracts/src/commands/wallet/index.ts
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,3 @@ | ||
import Send from './send' | ||
|
||
export default [Send] |
60 changes: 60 additions & 0 deletions
60
packages-ts/gauntlet-terra-contracts/src/commands/wallet/send.ts
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,60 @@ | ||
import { BN, logger, prompt } from '@chainlink/gauntlet-core/dist/utils' | ||
import { defaultAfterExecute } from '../abstract/executionWrapper' | ||
import { AccAddress, MsgSend } from '@terra-money/terra.js' | ||
import { CATEGORIES, ULUNA_DECIMALS } from '../../lib/constants' | ||
import { TerraCommand, TransactionResponse } from '@chainlink/gauntlet-terra' | ||
import { Result } from '@chainlink/gauntlet-core' | ||
|
||
type CommandInput = { | ||
destination: string | ||
// Units in LUNA | ||
amount: string | ||
} | ||
|
||
export default class TransferLuna extends TerraCommand { | ||
static description = 'Transfer Luna' | ||
static examples = [`yarn gauntlet wallet:transfer --network=bombay-testnet`] | ||
|
||
static id = 'wallet:transfer' | ||
static category = CATEGORIES.WALLET | ||
|
||
input: CommandInput | ||
|
||
constructor(flags, args: string[]) { | ||
super(flags, args) | ||
} | ||
|
||
beforeExecute = async (input: CommandInput) => { | ||
await prompt(`Continue sending ${input.amount} uLUNA to ${input.destination}?`) | ||
} | ||
afterExecute = defaultAfterExecute | ||
|
||
makeInput = () => { | ||
return { | ||
destination: this.flags.to, | ||
amount: new BN(this.flags.amount).mul(new BN(10).pow(new BN(ULUNA_DECIMALS))), | ||
} | ||
} | ||
|
||
makeRawTransaction = async (signer: AccAddress) => { | ||
this.input = this.makeInput() | ||
if (!AccAddress.validate(this.input.destination)) throw new Error('Invalid destination address') | ||
return new MsgSend(signer, this.input.destination, `${this.input.amount.toString()}uluna`) | ||
} | ||
|
||
execute = async () => { | ||
const message = await this.makeRawTransaction(this.wallet.key.accAddress) | ||
await this.beforeExecute(this.input) | ||
const tx = await this.signAndSend([message]) | ||
const result = { | ||
responses: [ | ||
{ | ||
tx, | ||
contract: '', | ||
}, | ||
], | ||
} as Result<TransactionResponse> | ||
await this.afterExecute(result) | ||
return result | ||
} | ||
} |
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
Oops, something went wrong.