-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created deployment, deposit and withdraw scripts
- Loading branch information
Showing
7 changed files
with
2,593 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
WALLET_PRIVATE_KEY="" | ||
|
||
JSON_RPC_URL_PUBLIC=https://test.massa.net/api/v2:33035 | ||
JSON_RPC_URL_PUBLIC=https://buildnet.massa.net/api/v2 | ||
|
||
WMAS_ADRS="" |
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
import { readFileSync } from 'fs'; | ||
import path from 'path'; | ||
import { deploySC } from '@massalabs/massa-sc-deployer'; | ||
import { Args, fromMAS } from '@massalabs/web3-utils'; | ||
import { getClient } from './utils'; | ||
import { fileURLToPath } from 'url'; | ||
import { config } from 'dotenv'; | ||
config(); | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(path.dirname(__filename)); | ||
|
||
const { account, chainId } = await getClient(); | ||
|
||
if (!process.env.JSON_RPC_URL_PUBLIC) { | ||
throw new Error('JSON_RPC_URL_PUBLIC env variable is not set'); | ||
} | ||
await deploySC( | ||
process.env.JSON_RPC_URL_PUBLIC, | ||
account, | ||
[ | ||
{ | ||
data: readFileSync(path.join(__dirname, '..', 'smart-contracts/build', 'WMAS.wasm')), | ||
coins: fromMAS(0.0255), | ||
args: new Args() | ||
.addString('Wrapped Massa') | ||
.addString('WMAS') | ||
.addU8(9) | ||
.addU256(0n), | ||
}, | ||
], | ||
chainId, | ||
0n, | ||
1_000_000_000n, | ||
true, | ||
); |
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,24 @@ | ||
import { getClient, waitFinalOperation } from './utils'; | ||
import { config } from 'dotenv'; | ||
config(); | ||
|
||
export async function deposit(fee:bigint, amount:bigint): Promise<void> { | ||
if(!process.env.WMAS_ADRS) { | ||
throw new Error('WMAS_ADRS env variable is not set'); | ||
} | ||
const { client, baseAccount } = await getClient(); | ||
let operationId = await client.smartContracts().callSmartContract( | ||
{ | ||
fee: fee, | ||
maxGas: 2_100_000n, | ||
coins: amount, | ||
targetAddress: process.env.WMAS_ADRS, | ||
targetFunction: 'deposit', | ||
parameter: [], | ||
}, | ||
baseAccount, | ||
); | ||
console.log(`deposit operationId: ${operationId}`); | ||
waitFinalOperation(client, operationId); | ||
|
||
} |
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,103 @@ | ||
import { | ||
Client, | ||
WalletClient, | ||
IAccount, | ||
IBaseAccount, | ||
PublicApiClient, | ||
ProviderType, | ||
Web3Account, | ||
IEvent, | ||
EOperationStatus, | ||
} from '@massalabs/massa-web3'; | ||
import { scheduler } from 'timers/promises'; | ||
|
||
export const WAIT_STATUS_TIMEOUT = 300000; | ||
export const STATUS_POLL_INTERVAL_MS = 1000; | ||
|
||
export const getClient = async (): Promise<{ | ||
client: Client; | ||
account: IAccount; | ||
baseAccount: IBaseAccount; | ||
chainId: bigint; | ||
}> => { | ||
if (!process.env.WALLET_PRIVATE_KEY) { | ||
throw new Error('WALLET_PRIVATE_KEY env variable is not set'); | ||
} | ||
if (!process.env.JSON_RPC_URL_PUBLIC) { | ||
throw new Error('JSON_RPC_URL_PUBLIC env variable is not set'); | ||
} | ||
const account = await WalletClient.getAccountFromSecretKey( | ||
process.env.WALLET_PRIVATE_KEY, | ||
); | ||
|
||
const clientConfig = { | ||
retryStrategyOn: true, | ||
providers: [ | ||
{ url: process.env.JSON_RPC_URL_PUBLIC, type: ProviderType.PUBLIC }, | ||
], | ||
periodOffset: 9, | ||
}; | ||
|
||
const publicApi = new PublicApiClient(clientConfig); | ||
const status = await publicApi.getNodeStatus(); | ||
|
||
const web3account = new Web3Account(account, publicApi, status.chain_id); | ||
const client = new Client(clientConfig, web3account, publicApi); | ||
return { | ||
client, | ||
account, | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
baseAccount: client.wallet().getBaseAccount()!, | ||
chainId: status.chain_id, | ||
}; | ||
}; | ||
|
||
|
||
export const waitFinalOperation = async ( | ||
client: Client, | ||
opId: string, | ||
): Promise<IEvent[]> => { | ||
const start = Date.now(); | ||
let counterMs = 0; | ||
while (counterMs < WAIT_STATUS_TIMEOUT) { | ||
const status = await client.smartContracts().getOperationStatus(opId); | ||
if (status === EOperationStatus.FINAL_SUCCESS) { | ||
console.log(`Operation ${opId} finished successfully!`); | ||
return getOperationEvents(client, opId); | ||
} | ||
if ( | ||
status === EOperationStatus.FINAL_ERROR || | ||
status === EOperationStatus.SPECULATIVE_ERROR | ||
) { | ||
const events = await getOperationEvents(client, opId); | ||
if (!events.length) { | ||
console.log( | ||
`Operation ${opId} failed with no events! Try to increase maxGas`, | ||
); | ||
} | ||
events.map((l) => console.log(`>>>> New event: ${l.data}`)); | ||
throw new Error(`Operation ended with errors...`); | ||
} | ||
|
||
await scheduler.wait(STATUS_POLL_INTERVAL_MS); | ||
counterMs = Date.now() - start; | ||
} | ||
const status = await client.smartContracts().getOperationStatus(opId); | ||
throw new Error( | ||
`Wait operation timeout... status=${EOperationStatus[status]}`, | ||
); | ||
}; | ||
|
||
export async function getOperationEvents( | ||
client: Client, | ||
opId: string, | ||
): Promise<IEvent[]> { | ||
return client.smartContracts().getFilteredScOutputEvents({ | ||
start: null, | ||
end: null, | ||
original_caller_address: null, | ||
original_operation_id: opId, | ||
emitter_address: null, | ||
is_final: null, | ||
}); | ||
} |
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,27 @@ | ||
import { Args } from '@massalabs/massa-web3'; | ||
import { getClient, waitFinalOperation } from './utils'; | ||
import { config } from 'dotenv'; | ||
config(); | ||
|
||
export async function withdraw(fee: bigint, amount:bigint, withdrawer: string): Promise<void> { | ||
if(!process.env.WMAS_ADRS) { | ||
throw new Error('WMAS_ADRS env variable is not set'); | ||
} | ||
const { client, baseAccount } = await getClient(); | ||
let operationId = await client.smartContracts().callSmartContract( | ||
{ | ||
fee: fee, | ||
maxGas: 2_100_000n, | ||
coins: 0n, | ||
targetAddress: process.env.WMAS_ADRS, | ||
targetFunction: 'withdraw', | ||
parameter: new Args() | ||
.addU64(amount) | ||
.addString(withdrawer) | ||
.serialize(), | ||
}, | ||
baseAccount, | ||
); | ||
console.log(`withdraw operationId: ${operationId}`); | ||
waitFinalOperation(client, operationId); | ||
} |