Skip to content

Commit

Permalink
Merge 2c0dd7e into 5783892
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyar committed Jun 17, 2021
2 parents 5783892 + 2c0dd7e commit 55eb92e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@4ire-labs/near-sdk",
"version": "1.0.0-beta.7",
"version": "1.0.0-beta.8",
"license": "UNLICENSED",
"description": "SDK for NEAR Protocol",
"keywords": [
Expand Down
17 changes: 9 additions & 8 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as api from 'near-api-js'
import * as config from './config'
import * as util from './util'
import {
toGas,
toYocto,
} from './util'
import * as provider from 'near-api-js/lib/providers/provider'
Expand Down Expand Up @@ -30,7 +31,7 @@ import {
FunctionCallOptions,
} from 'near-api-js/lib/account'

const DEFAULT_FUNC_CALL_GAS = new BN('30000000000000')
const DEFAULT_FUNC_CALL_GAS = new BN('300000000000000')

export async function fetchContract(accountId: string, networkId?: string): Promise<Uint8Array> {
const rpc = new api.providers.JsonRpcProvider(config.environment(networkId).nodeUrl)
Expand Down Expand Up @@ -61,8 +62,8 @@ export interface ChangeMethod {
methodName: string;
/** named arguments to pass the method `{ field: any }` */
args: Record<string, unknown>;
/** max amount of gas that method call can use */
gas?: string;
/** max amount of Tera Gas that method call can use */
teraGas?: string;
/** amount of NEAR to send together with the call */
attachedDeposit?: string;
/** Metadata to send the NEAR Wallet if using it to sign transactions */
Expand Down Expand Up @@ -110,7 +111,7 @@ export async function deployContract<Type>(
actionList.push(functionCallAction(
props.init.methodName,
props.init.args || {},
parseGas(props.init.gas),
parseGas(props.init.teraGas),
parseDeposit(props.init.attachedDeposit),
))
}
Expand All @@ -119,8 +120,8 @@ export async function deployContract<Type>(
return result
}

function parseGas(gas: string): BN {
return (gas ? toYocto(gas) : DEFAULT_FUNC_CALL_GAS)
function parseGas(terraGas: string): BN {
return (terraGas ? new BN(toGas(terraGas)) : DEFAULT_FUNC_CALL_GAS)
}

function parseDeposit(deposit: string): BN {
Expand All @@ -131,7 +132,7 @@ function createFunctionCallOptions(contractId: string, method: ChangeMethod): Fu
const {
methodName,
args,
gas,
teraGas,
attachedDeposit,
walletMeta,
walletCallbackUrl,
Expand All @@ -140,7 +141,7 @@ function createFunctionCallOptions(contractId: string, method: ChangeMethod): Fu
contractId,
methodName,
args,
gas: parseGas(gas),
gas: parseGas(teraGas),
attachedDeposit: parseDeposit(attachedDeposit),
walletMeta,
walletCallbackUrl,
Expand Down
22 changes: 22 additions & 0 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,25 @@ test('toYocto', async () => {
const yocto = near.toYocto('0.000000000000000000000001')
expect(yocto.toString()).toBe('1')
})

test('toTerraGas', async () => {
expect(near.toTerraGas('299899999999910')).toBe(299.89999999991)
expect(near.toTerraGas('299899999910')).toBe(0.29989999991)
})

test('toGas', async () => {
expect(near.toGas(30)).toBe('30000000000000')
expect(near.toGas('299.89999999991')).toBe('299899999999910')
expect(near.toGas('0.29989999991')).toBe('299899999910')
expect(near.toGas('0.000000000001')).toBe('1')
})

test('toGas fail', async () => {
process.env.NEAR_SENDER_ID = ''
try {
near.toGas('0.0000000000001')
expect(true).toBe(false)
} catch (e) {
expect(e.message).toBe('Cannot parse \'0.0000000000001\' as Gas amount')
}
})
20 changes: 20 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
} from 'near-api-js/lib/utils/format'
import BN from 'bn.js'

const TERA_EXP = 12

/**
* Convert human readable NEAR amount to internal indivisible units
*
Expand All @@ -18,6 +20,24 @@ export function toYocto(near: string): BN {
return new BN(deposit)
}

export function toGas(terraGas: string | number): string {
const part = terraGas.toString().split('.')
const number = part[0]
const mantissa = part[1] || ''
if (part.length > 2 || mantissa.length > TERA_EXP) {
throw new Error(`Cannot parse '${terraGas}' as Gas amount`)
}
const value = number + mantissa.padEnd(TERA_EXP, '0')
return value.replace(/^0+/, '')
}

export function toTerraGas(gas: string): number {
const number = gas.substring(0, gas.length - TERA_EXP) || '0'
const mantissa = gas.substring(gas.length - TERA_EXP).padStart(TERA_EXP, '0').substring(0, TERA_EXP)
const value = `${number}.${mantissa}`
return +value.replace(/\.?0*$/, '')
}

export function toNear(yocto: string): number {
return +formatNearAmount(yocto)
}

0 comments on commit 55eb92e

Please sign in to comment.