-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NayNay] Transfer updates + unit testing (#140)
* [NayNay] Transfer updates + unit testing closes #124 * updated transfer pure function and added tests * Update transfer.test.ts Co-authored-by: mix irving <mix@protozoa.nz> * updated based on PR suggestions * updated progress bar use to be more universal for the cli; based on pr suggestion * updating comment --------- Co-authored-by: Nayyir Jutha <nayyir@entropy.xyz> Co-authored-by: mixmix <mix@protozoa.nz>
- Loading branch information
1 parent
fb1f6ee
commit a9b4b0b
Showing
12 changed files
with
224 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import cliProgress from 'cli-progress' | ||
import colors from 'ansi-colors' | ||
|
||
export function setupProgress (label: string): { start: () => void; stop: () => void } { | ||
let interval: NodeJS.Timeout | ||
const b1 = new cliProgress.SingleBar({ | ||
format: `${label} |` + colors.cyan('{bar}') + '| {percentage}%', | ||
barCompleteChar: '\u2588', | ||
barIncompleteChar: '\u2591', | ||
hideCursor: true | ||
}) | ||
|
||
const start = () => { | ||
// 160 was found through trial and error, don't believe there is a formula to | ||
// determine the exact time it takes for the transaction to be processed and finalized | ||
// TO-DO: Change progress bar to loading animation? | ||
b1.start(160, 0, { | ||
speed: "N/A" | ||
}) | ||
// update values | ||
interval = setInterval(() => { | ||
b1.increment() | ||
}, 100) | ||
} | ||
|
||
const stop = () => { | ||
b1.stop() | ||
clearInterval(interval) | ||
} | ||
|
||
return { start, stop } | ||
} |
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,36 @@ | ||
export interface EntropyConfig { | ||
accounts: EntropyAccountConfig[] | ||
endpoints: { dev: string; 'test-net': string } | ||
'migration-version': string | ||
} | ||
|
||
export interface EntropyAccountConfig { | ||
name: string | ||
address: string | ||
data: EntropyAccountData | ||
} | ||
|
||
export interface EntropyAccountData { | ||
debug?: boolean | ||
seed: string | ||
admin?: EntropyAccount | ||
registration?: EntropyAccount | ||
deviceKey?: EntropyAccount | ||
programDev?: EntropyAccount | ||
} | ||
|
||
export interface EntropyAccount { | ||
seed: string | ||
path: string | ||
address: string | ||
verifyingKeys?: string[] | ||
userContext?: EntropyAccountContextType | ||
used?: boolean | ||
} | ||
|
||
export enum EntropyAccountContextType { | ||
programDev = 'PROGRAM_DEV_KEY', | ||
registration = 'ADMIN_KEY', | ||
deviceKey = 'CONSUMER_KEY', | ||
undefined = 'MIXED_KEY', | ||
} |
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,34 @@ | ||
import Entropy from "@entropyxyz/sdk"; | ||
import { TransferOptions } from "./types"; | ||
|
||
export async function transfer (entropy: Entropy, payload: TransferOptions): Promise<any> { | ||
const { from, to, amount } = payload | ||
|
||
return new Promise((resolve, reject) => { | ||
// WARN: await signAndSend is dangerous as it does not resolve | ||
// after transaction is complete :melt: | ||
entropy.substrate.tx.balances | ||
.transferAllowDeath(to, amount) | ||
// @ts-ignore | ||
.signAndSend(from, ({ status, dispatchError }) => { | ||
if (dispatchError) { | ||
let msg: string | ||
if (dispatchError.isModule) { | ||
// for module errors, we have the section indexed, lookup | ||
const decoded = entropy.substrate.registry.findMetaError( | ||
dispatchError.asModule | ||
) | ||
const { docs, name, section } = decoded | ||
|
||
msg = `${section}.${name}: ${docs.join(' ')}` | ||
} else { | ||
// Other, CannotLookup, BadOrigin, no extra info | ||
msg = dispatchError.toString() | ||
} | ||
return reject(Error(msg)) | ||
} | ||
|
||
if (status.isFinalized) resolve(status) | ||
}) | ||
}) | ||
} |
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,7 @@ | ||
// @ts-ignore | ||
import { Pair } from '@entropyxyz/sdk/keys' | ||
export interface TransferOptions { | ||
from: Pair | ||
to: string | ||
amount: bigint | ||
} |
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
Oops, something went wrong.