-
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.
- Loading branch information
Showing
9 changed files
with
195 additions
and
6 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,54 @@ | ||
import { Mas } from '@massalabs/massa-web3'; | ||
import { | ||
SnapComponent, | ||
Container, | ||
Box, | ||
Heading, | ||
Text, | ||
Section, | ||
Bold, | ||
} from '@metamask/snaps-sdk/jsx'; | ||
|
||
type DeployScProps = { | ||
fee: string; | ||
args: number[]; | ||
coins: string; | ||
maxCoins: string; | ||
}; | ||
|
||
export const DeploySc: SnapComponent<DeployScProps> = ({ | ||
fee, | ||
args, | ||
coins, | ||
maxCoins, | ||
}) => { | ||
return ( | ||
<Container> | ||
<Box> | ||
<Heading>Deploying Smart contract</Heading> | ||
<Section> | ||
<Text> | ||
<Bold>Coins: </Bold> | ||
{Mas.toString(BigInt(coins))} MAS | ||
</Text> | ||
<Text> | ||
<Bold>Max coins: </Bold> | ||
{maxCoins !== 'none' | ||
? `${Mas.toString(BigInt(maxCoins))} MAS` | ||
: 'none'} | ||
</Text> | ||
<Text> | ||
<Bold>Fee: </Bold> | ||
{Mas.toString(BigInt(fee))} MAS | ||
</Text> | ||
{args.length ? ( | ||
<Text> | ||
<Bold>Constructor arguments: </Bold> | ||
{args.toString()} | ||
</Text> | ||
) : null} | ||
</Section> | ||
</Box> | ||
</Container> | ||
); | ||
}; |
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,96 @@ | ||
import { getProvider } from '../accounts/provider'; | ||
import { addAccountOperation } from '../operations'; | ||
import type { Handler } from './handler'; | ||
import { DeploySc } from '../components/DeploySc'; | ||
import { DeploySCParams } from '@massalabs/massa-web3'; | ||
import { getActiveNetwork } from '../active-chain'; | ||
|
||
export type DeploySCParameters = { | ||
bytecode: number[]; | ||
fee?: string; | ||
args?: number[]; | ||
coins?: string; | ||
maxCoins?: string; | ||
maxGas?: string; | ||
}; | ||
|
||
export type DeploySCResponse = { | ||
operationId: string; | ||
}; | ||
|
||
const validate = (params: DeploySCParameters) => { | ||
// mandatory parameters | ||
if (!params.bytecode || !Array.isArray(params.bytecode)) { | ||
throw new Error('Invalid params: bytecode must be an array'); | ||
} | ||
|
||
// optionnal parameters | ||
if (params.args && !Array.isArray(params.args)) { | ||
throw new Error('Invalid params: args must be an array'); | ||
} | ||
if (params.fee && typeof params.fee !== 'string') { | ||
throw new Error('Invalid params: fee must be a string'); | ||
} | ||
if (params?.coins && typeof params.coins !== 'string') { | ||
throw new Error('Invalid params: coins must be a string'); | ||
} | ||
if (params?.maxGas && typeof params.maxGas !== 'string') { | ||
throw new Error('Invalid params: maxGas must be a string'); | ||
} | ||
if (params?.maxCoins && typeof params.maxCoins !== 'string') { | ||
throw new Error('Invalid params: maxCoins must be a string'); | ||
} | ||
}; | ||
/** | ||
* @description Deploy a smart contract | ||
* @param params - DeploySCParameters | ||
* @returns The operation id | ||
* @throws If the user denies the transaction | ||
*/ | ||
export const deployContract: Handler< | ||
DeploySCParameters, | ||
DeploySCResponse | ||
> = async (params) => { | ||
validate(params); | ||
const provider = await getProvider(); | ||
|
||
const networkInfos = await getActiveNetwork(); | ||
const fee = params.fee ? params.fee : networkInfos.minimalFees; | ||
|
||
const confirm = await snap.request({ | ||
method: 'snap_dialog', | ||
params: { | ||
type: 'confirmation', | ||
content: ( | ||
<DeploySc | ||
fee={fee} | ||
args={params.args ?? []} | ||
coins={params.coins ?? '0'} | ||
maxCoins={params.maxCoins ?? 'none'} | ||
/> | ||
), | ||
}, | ||
}); | ||
|
||
if (!confirm) { | ||
throw new Error('User denied calling smart contract'); | ||
} | ||
const deploySCParams: DeploySCParams = { | ||
parameter: Uint8Array.from(params.args ?? []), | ||
byteCode: Uint8Array.from(params.bytecode), | ||
fee: BigInt(fee), | ||
}; | ||
|
||
if (params.coins) { | ||
deploySCParams.coins = BigInt(params.coins); | ||
} | ||
if (params.maxGas) { | ||
deploySCParams.maxGas = BigInt(params.maxGas); | ||
} | ||
// bypass protected attribute of deploy function | ||
const operationId: string = await (provider as any).deploy(deploySCParams); | ||
await addAccountOperation(provider.address, operationId); | ||
return { | ||
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
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,35 @@ | ||
import { expect } from '@jest/globals'; | ||
import type { SnapConfirmationInterface } from '@metamask/snaps-jest'; | ||
import { installSnap } from '@metamask/snaps-jest'; | ||
import { DeploySc } from '../src/components/DeploySc'; | ||
|
||
const baseParams = { | ||
bytecode: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], | ||
fee: '1000000000000000', | ||
}; | ||
|
||
describe('deploy-sc', () => { | ||
const origin = 'Jest'; | ||
|
||
it('should render ui', async () => { | ||
const { request } = await installSnap(); | ||
|
||
const response = request({ | ||
method: 'account.deploySC', | ||
origin, | ||
params: baseParams, | ||
}); | ||
|
||
const ui = (await response.getInterface()) as SnapConfirmationInterface; | ||
|
||
expect(ui.type).toBe('confirmation'); | ||
expect(ui).toRender( | ||
<DeploySc | ||
fee={'1000000000000000'} | ||
maxCoins={'none'} | ||
args={[]} | ||
coins={'0'} | ||
/>, | ||
); | ||
}); | ||
}); |